예제 #1
0
    def __init__(self, mesh_m):
        super().__init__()

        # Setup problem
        V = fd.FunctionSpace(mesh_m, "CG", 1)

        # Weak form of Poisson problem
        u = fda.Function(V, name="State")
        v = fd.TestFunction(V)
        f = fda.Constant(4.)
        F = (fd.inner(fd.grad(u), fd.grad(v)) - f * v) * fd.dx
        bcs = fda.DirichletBC(V, 0., "on_boundary")

        # PDE-solver parameters
        params = {
            "ksp_type": "cg",
            "mat_type": "aij",
            "pc_type": "hypre",
            "pc_factor_mat_solver_package": "boomerang",
            "ksp_rtol": 1e-11,
            "ksp_atol": 1e-11,
            "ksp_stol": 1e-15,
        }

        self.solution = u
        problem = fda.NonlinearVariationalProblem(F, self.solution, bcs=bcs)
        self.solver = fda.NonlinearVariationalSolver(problem,
                                                     solver_parameters=params)
예제 #2
0
 def update(self, x, flag, iteration):
     """Update domain and solution to state and adjoint equation."""
     if self.Q.update_domain(x):
         try:
             # We use pyadjoint to calculate adjoint and shape derivatives,
             # in order to do this we need to "record a tape of the forward
             # solve", pyadjoint will then figure out all necessary
             # adjoints.
             tape = fda.get_working_tape()
             tape.clear_tape()
             fda.continue_annotation()
             mesh_m = self.J.Q.mesh_m
             s = fda.Function(self.J.V_m)
             mesh_m.coordinates.assign(mesh_m.coordinates + s)
             self.s = s
             self.c = fda.Control(s)
             self.e.solve()
             Jpyadj = fda.assemble(self.J.value_form())
             self.Jred = fda.ReducedFunctional(Jpyadj, self.c)
             fda.pause_annotation()
         except fd.ConvergenceError:
             if self.cb is not None:
                 self.cb()
             raise
     if iteration >= 0 and self.cb is not None:
         self.cb()
예제 #3
0
    def __init__(self, mesh_m):
        super().__init__()
        self.mesh_m = mesh_m

        # Setup problem
        self.V = fd.FunctionSpace(self.mesh_m, "CG", 1)

        # Preallocate solution variables for state and adjoint equations
        self.solution = fda.Function(self.V, name="State")

        # Weak form of Poisson problem
        u = self.solution
        v = fd.TestFunction(self.V)
        self.f = fda.Constant(4.)
        self.F = (fd.inner(fd.grad(u), fd.grad(v)) - self.f * v) * fd.dx
        self.bcs = fda.DirichletBC(self.V, 0., "on_boundary")

        # PDE-solver parameters
        self.params = {
            "ksp_type": "cg",
            "mat_type": "aij",
            "pc_type": "hypre",
            "pc_factor_mat_solver_package": "boomerang",
            "ksp_rtol": 1e-11,
            "ksp_atol": 1e-11,
            "ksp_stol": 1e-15,
        }

        stateproblem = fda.NonlinearVariationalProblem(self.F,
                                                       self.solution,
                                                       bcs=self.bcs)
        self.solver = fda.NonlinearVariationalSolver(
            stateproblem, solver_parameters=self.params)
예제 #4
0
    def __init__(self,
                 mesh_m,
                 mini=False,
                 direct=True,
                 inflow_bids=[],
                 inflow_expr=None,
                 noslip_bids=[],
                 nu=1.0):
        """
        Instantiate a FluidSolver.

        Inputs:
            mesh_m: type fd.Mesh
            mini: type bool, set to true to use MINI elments
            direct: type bool, set to True to use direct solver
            inflow_bids: type list (of ints), list of inflow bdries
            inflow_expr: type ???UFL??, UFL formula for inflow bdry conditions
            noslip_bids: typ list (of ints), list of bdries with homogeneous
                         Dirichlet bdry condition for velocity
            nu: type float, viscosity
        """
        super().__init__()
        self.mesh_m = mesh_m
        self.mini = mini
        self.direct = direct
        self.inflow_bids = inflow_bids
        self.inflow_expr = inflow_expr
        self.noslip_bids = noslip_bids
        self.nu = fda.Constant(nu)

        # Setup problem
        self.V = self.get_functionspace()

        # Manage boundary conditions
        # this only works after a functionspace on the mesh has been declared..
        all_bids = list(self.mesh_m.topology.exterior_facets.unique_markers)
        for bid in inflow_bids + noslip_bids:
            all_bids.remove(bid)
        self.outflow_bids = all_bids

        # Preallocate solution variables for state and adjoint equations
        self.solution = fda.Function(self.V, name="State")

        self.F = self.get_weak_form()
        self.bcs = self.get_boundary_conditions()
        self.nsp = self.get_nullspace()
        self.params = self.get_parameters()
        problem = fda.NonlinearVariationalProblem(
            self.F,
            self.solution,
            bcs=self.bcs,
        )
        self.solver = fda.NonlinearVariationalSolver(
            problem, solver_parameters=self.params, nullspace=self.nsp)
예제 #5
0
    def __init__(self, mesh_r):
        # Create mesh_r and V_r
        self.mesh_r = mesh_r
        element = self.mesh_r.coordinates.function_space().ufl_element()
        self.V_r = fd.FunctionSpace(self.mesh_r, element)

        # Create self.id and self.T, self.mesh_m, and self.V_m.
        X = fd.SpatialCoordinate(self.mesh_r)
        self.id = fd.interpolate(X, self.V_r)
        self.T = fda.Function(self.V_r, name="T")
        self.T.assign(self.id)
        self.mesh_m = fda.Mesh(self.T)
        self.V_m = fd.FunctionSpace(self.mesh_m, element)