def __init__(self, truth_problem, expression_type, basis_generation): self.V = truth_problem.V1 (f0, _) = split(truth_problem._solution) # folder_prefix = os.path.join("test_eim_approximation_14_tempdir", expression_type, basis_generation) assert expression_type in ("Function", "Vector", "Matrix") if expression_type == "Function": # Call Parent constructor EIMApproximation.__init__(self, truth_problem, ParametrizedExpressionFactory(f0), folder_prefix, basis_generation) elif expression_type == "Vector": v = TestFunction(self.V) form = f0[0] * v * dx + f0[1] * v.dx(0) * dx # Call Parent constructor EIMApproximation.__init__(self, truth_problem, ParametrizedTensorFactory(form), folder_prefix, basis_generation) elif expression_type == "Matrix": u = TrialFunction(self.V) v = TestFunction(self.V) form = f0[0] * u * v * dx + f0[1] * u.dx(0) * v * dx # Call Parent constructor EIMApproximation.__init__(self, truth_problem, ParametrizedTensorFactory(form), folder_prefix, basis_generation) else: # impossible to arrive here anyway thanks to the assert raise AssertionError("Invalid expression_type")
def __init__(self, V, expression_type, basis_generation): self.V = V # Parametrized function to be interpolated mock_problem = MockProblem(V) f = project(Expression("(1-x[0])", element=V.ufl_element()), V) g = ParametrizedExpression(mock_problem, "cos(3*pi*mu[0]*(1+x[0]))*exp(-mu[0]*(1+x[0]))", mu=(1., ), element=V.ufl_element()) # folder_prefix = os.path.join("test_eim_approximation_23_tempdir", expression_type, basis_generation) assert expression_type in ("Function", "Vector", "Matrix") if expression_type == "Function": # Call Parent constructor EIMApproximation.__init__( self, mock_problem, ParametrizedExpressionFactory(f * g), folder_prefix, basis_generation) elif expression_type == "Vector": v = TestFunction(V) form = f * g * v * dx # Call Parent constructor EIMApproximation.__init__( self, mock_problem, ParametrizedTensorFactory(form), folder_prefix, basis_generation) elif expression_type == "Matrix": u = TrialFunction(V) v = TestFunction(V) form = f * g * u * v * dx # Call Parent constructor EIMApproximation.__init__( self, mock_problem, ParametrizedTensorFactory(form), folder_prefix, basis_generation) else: # impossible to arrive here anyway thanks to the assert raise AssertionError("Invalid expression_type")
def __init__(self, V, expression_type, basis_generation): self.V = V # Parametrized function to be interpolated mock_problem = MockProblem(V) f = ParametrizedExpression( mock_problem, "1/sqrt(pow(x[0]-mu[0], 2) + pow(x[1]-mu[1], 2) + 0.01)", mu=(-1., -1.), element=V.ufl_element()) # folder_prefix = os.path.join("test_eim_approximation_02_tempdir", expression_type, basis_generation) assert expression_type in ("Function", "Vector", "Matrix") if expression_type == "Function": # Call Parent constructor EIMApproximation.__init__(self, mock_problem, ParametrizedExpressionFactory(f), folder_prefix, basis_generation) elif expression_type == "Vector": v = TestFunction(V) form = f * v * dx # Call Parent constructor EIMApproximation.__init__(self, mock_problem, ParametrizedTensorFactory(form), folder_prefix, basis_generation) elif expression_type == "Matrix": u = TrialFunction(V) v = TestFunction(V) form = f * u * v * dx # Call Parent constructor EIMApproximation.__init__(self, mock_problem, ParametrizedTensorFactory(form), folder_prefix, basis_generation) else: # impossible to arrive here anyway thanks to the assert raise AssertionError("Invalid expression_type")
def __init__(self, mock_problem, expression_type, basis_generation): self.V = mock_problem.V # Parametrized function to be interpolated mu = SymbolicParameters(mock_problem, self.V, (1., )) x = SpatialCoordinate(self.V.mesh()) f = (1 - x[0]) * cos(3 * pi * mu[0] * (1 + x[0])) * exp(-mu[0] * (1 + x[0])) # folder_prefix = os.path.join("test_eim_approximation_09_tempdir", expression_type, basis_generation) assert expression_type in ("Function", "Vector", "Matrix") if expression_type == "Function": # Call Parent constructor EIMApproximation.__init__(self, mock_problem, ParametrizedExpressionFactory(f), folder_prefix, basis_generation) elif expression_type == "Vector": v = TestFunction(self.V) form = f * v * dx # Call Parent constructor EIMApproximation.__init__(self, mock_problem, ParametrizedTensorFactory(form), folder_prefix, basis_generation) elif expression_type == "Matrix": u = TrialFunction(self.V) v = TestFunction(self.V) form = f * u * v * dx # Call Parent constructor EIMApproximation.__init__(self, mock_problem, ParametrizedTensorFactory(form), folder_prefix, basis_generation) else: # impossible to arrive here anyway thanks to the assert raise AssertionError("Invalid expression_type")
def __init__(self, V, expression_type, basis_generation): self.V = V # Parametrized function to be interpolated mock_problem = MockProblem(V) f_expression = ( ("1/sqrt(pow(x[0]-mu[0], 2) + pow(x[1]-mu[1], 2) + 0.01)", "exp( - 2*pow(x[0]-mu[0], 2) - 2*pow(x[1]-mu[1], 2) )"), ("10.*(1-x[0])*cos(3*pi*(pi+mu[1])*(1+x[1]))*exp(-(pi+mu[0])*(1+x[0]))", "sqrt(pow(x[0]-mu[0], 2) + pow(x[1]-mu[1], 2) + 0.01)") ) tensor_element = TensorElement(V.sub(0).ufl_element()) f = ParametrizedExpression(mock_problem, f_expression, mu=(-1., -1.), element=tensor_element) # folder_prefix = os.path.join("test_eim_approximation_07_tempdir", expression_type, basis_generation) assert expression_type in ("Function", "Vector", "Matrix") if expression_type == "Function": # Call Parent constructor EIMApproximation.__init__( self, mock_problem, ParametrizedExpressionFactory(f), folder_prefix, basis_generation) elif expression_type == "Vector": v = TestFunction(V) form = inner(f, grad(v)) * dx # Call Parent constructor EIMApproximation.__init__( self, mock_problem, ParametrizedTensorFactory(form), folder_prefix, basis_generation) elif expression_type == "Matrix": u = TrialFunction(V) v = TestFunction(V) form = inner(grad(u) * f, grad(v)) * dx # Call Parent constructor EIMApproximation.__init__( self, mock_problem, ParametrizedTensorFactory(form), folder_prefix, basis_generation) else: # impossible to arrive here anyway thanks to the assert raise AssertionError("Invalid expression_type")
def _init_EIM_approximations(self): # Preprocess each term in the affine expansions. # Note that this cannot be done in __init__, because operators may depend on self.mu, # which is not defined at __init__ time. Moreover, it cannot be done either by init, # because the init method is called by offline stage of the reduction method instance, # but we need EIM approximations to be already set up at the time the reduction method # instance is built. Thus, we will call this method in the reduction method instance # constructor (having a safeguard in place to avoid repeated calls). assert ( (len(self.separated_forms) == 0) == (len(self.EIM_approximations) == 0) ) if len(self.EIM_approximations) == 0: # initialize EIM approximations only once # Temporarily replace float parameters with symbols, so that we can detect if operators # are parametrized self.attach_symbolic_parameters() # Loop over each term for term in self.terms: try: forms = self.assemble_operator(term) except ValueError: # possibily raised e.g. because output computation is optional pass else: Q = len(forms) self.separated_forms[term] = AffineExpansionSeparatedFormsStorage(Q) for q in range(Q): self.separated_forms[term][q] = SeparatedParametrizedForm(forms[q]) self.separated_forms[term][q].separate() # All parametrized coefficients should be approximated by EIM for (addend_index, addend) in enumerate(self.separated_forms[term][q].coefficients): for (factor, factor_name) in zip(addend, self.separated_forms[term][q].placeholders_names(addend_index)): if factor not in self.EIM_approximations: factory_factor = ParametrizedExpressionFactory(factor) if factory_factor.is_time_dependent(): EIMApproximationType = TimeDependentEIMApproximation else: EIMApproximationType = EIMApproximation self.EIM_approximations[factor] = EIMApproximationType(self, factory_factor, os.path.join(self.name(), "eim", factor_name), basis_generation) # Restore float parameters self.detach_symbolic_parameters()