def _setup_energy(self, rho, vel, gvec, x0): """ Calculate kinetic and potential energy """ x = df.SpatialCoordinate(self.mesh) self._form_E_k = Form(1 / 2 * rho * dot(vel, vel) * dx(domain=self.mesh)) self._form_E_p = Form(rho * dot(-gvec, x - x0) * dx(domain=self.mesh))
def __init__(self, simulation, every_timestep): """ Calculate the hydrostatic pressure """ self.simulation = simulation self.active = True self.every_timestep = every_timestep rho = simulation.data['rho'] g = simulation.data['g'] ph = simulation.data['p_hydrostatic'] # Define the weak form Vp = ph.function_space() p = TrialFunction(Vp) q = TestFunction(Vp) a = dot(grad(p), grad(q)) * dx L = rho * dot(g, grad(q)) * dx self.func = ph self.tensor_lhs = assemble(a) self.form_rhs = Form(L) self.null_space = None self.solver = linear_solver_from_input( simulation, 'solver/p_hydrostatic', default_parameters=DEFAULT_SOLVER_CONFIGURATION, )
def test_local_assembler_on_facet_integrals(): mesh = UnitSquareMesh(MPI.comm_world, 4, 4, 'right') Vdgt = FunctionSpace(mesh, 'DGT', 1) v = TestFunction(Vdgt) x = SpatialCoordinate(mesh) w = (1.0 + x[0] ** 2.2 + 1. / (0.1 + x[1] ** 3)) * 300 # Define form that tests that the correct + and - values are used L = w('-') * v('+') * dS # Compile form. This is collective L = Form(L) # Get global cell 10. This will return a cell only on one of the # processes c = get_cell_at(mesh, 5 / 12, 1 / 3, 0) if c: # Assemble locally on the selected cell b_e = assemble_local(L, c) # Compare to values from phonyx (fully independent # implementation) b_phonyx = numpy.array([266.55210302, 266.55210302, 365.49000122, 365.49000122, 0.0, 0.0]) error = sum((b_e - b_phonyx)**2)**0.5 error = float(error) # MPI.max does strange things to numpy.float64 else: error = 0.0 error = MPI.max(MPI.comm_world, float(error)) assert error < 1e-8
def __fem_forms(self, N_a, G_a, L_a, H_a, B_a, Q_a, R_a, S_a): # Turn into forms N_a = Form(N_a) G_a = Form(G_a) L_a = Form(L_a) H_a = Form(H_a) B_a = Form(B_a) Q_a = Form(Q_a) R_a = Form(R_a) S_a = Form(S_a) return {'N_a': N_a, 'G_a': G_a, 'L_a': L_a, 'H_a': H_a, 'B_a': B_a, 'Q_a': Q_a, 'R_a': R_a, 'S_a': S_a}
def test_mpi_dependent_jiting(): # FIXME: Not a proper unit test... from dolfin import (Expression, UnitSquareMesh, Function, TestFunction, Form, FunctionSpace, dx, CompiledSubDomain, SubSystemsManager) # Init petsc (needed to initalize petsc and slepc collectively on # all processes) SubSystemsManager.init_petsc() try: import mpi4py.MPI as mpi except ImportError: return try: import petsc4py.PETSc as petsc except ImportError: return # Set communicator and get process information comm = mpi.COMM_WORLD group = comm.Get_group() size = comm.Get_size() # Only consider parallel runs if size == 1: return rank = comm.Get_rank() group_comm_0 = petsc.Comm(comm.Create(group.Incl(range(1)))) group_comm_1 = petsc.Comm(comm.Create(group.Incl(range(1, 2)))) if size > 2: group_comm_2 = petsc.Comm(comm.Create(group.Incl(range(2, size)))) if rank == 0: e = Expression("4", mpi_comm=group_comm_0, degree=0) elif rank == 1: e = Expression("5", mpi_comm=group_comm_1, degree=0) assert (e) domain = CompiledSubDomain("on_boundary", mpi_comm=group_comm_1, degree=0) assert (domain) else: mesh = UnitSquareMesh(group_comm_2, 2, 2) V = FunctionSpace(mesh, "P", 1) u = Function(V) v = TestFunction(V) Form(u * v * dx)
def fem_forms(self, A_S, G_S, G_ST, B_S, Q_S, S_S): # Turn into forms A_S = Form(A_S) G_S = Form(G_S) G_ST = Form(G_ST) B_S = Form(B_S) Q_S = Form(Q_S) S_S = Form(S_S) return {"A_S": A_S, "G_S": G_S, "G_ST": G_ST, "B_S": B_S, "Q_S": Q_S, "S_S": S_S}
def _fem_forms(self, N_a, G_a, L_a, H_a, B_a, Q_a, R_a, S_a): # Turn into forms N_a = Form(N_a) G_a = Form(G_a) L_a = Form(L_a) H_a = Form(H_a) B_a = Form(B_a) Q_a = Form(Q_a) R_a = Form(R_a) S_a = Form(S_a) return { "N_a": N_a, "G_a": G_a, "L_a": L_a, "H_a": H_a, "B_a": B_a, "Q_a": Q_a, "R_a": R_a, "S_a": S_a, }
def __fem_forms(self, A_S, G_S, G_ST, B_S, Q_S, S_S): # Turn into forms A_S = Form(A_S) G_S = Form(G_S) G_ST = Form(G_ST) B_S = Form(B_S) Q_S = Form(Q_S) S_S = Form(S_S) return { 'A_S': A_S, 'G_S': G_S, 'G_ST': G_ST, 'B_S': B_S, 'Q_S': Q_S, 'S_S': S_S }
def test_local_assembler_on_facet_integrals2(): mesh = UnitSquareMesh(MPI.comm_world, 4, 4) Vu = VectorFunctionSpace(mesh, 'DG', 1) Vv = FunctionSpace(mesh, 'DGT', 1) u = TrialFunction(Vu) v = TestFunction(Vv) n = FacetNormal(mesh) # Define form a = dot(u, n) * v * ds for R in '+-': a += dot(u(R), n(R)) * v(R) * dS # Compile form. This is collective a = Form(a) # Get global cell 0. This will return a cell only on one of the # processes c = get_cell_at(mesh, 1 / 6, 1 / 12, 0) if c: A_e = assemble_local(a, c) A_correct = numpy.array([[0, 1 / 12, 1 / 24, 0, 0, 0], [0, 1 / 24, 1 / 12, 0, 0, 0], [-1 / 12, 0, -1 / 24, 1 / 12, 0, 1 / 24], [-1 / 24, 0, -1 / 12, 1 / 24, 0, 1 / 12], [0, 0, 0, -1 / 12, -1 / 24, 0], [0, 0, 0, -1 / 24, -1 / 12, 0]]) error = ((A_e - A_correct)**2).sum()**0.5 error = float(error) # MPI.max does strange things to numpy.float64 else: error = 0.0 error = MPI.max(MPI.comm_world, float(error)) assert error < 1e-16
def assemble_matrix(form, bcs=[]): """Assemble matrix using cache register. """ assert Form(form).rank() == 2 return A_cache[(form, tuple(bcs))]
def init_jacobian(self): A = PETScMatrix(self.comm) self.ass.init_global_tensor(A, Form(self.problem.J)) return A
def _setup_mass(self, rho): """ Calculate mass """ self._form_mass = Form(rho * dx(domain=self.mesh))