def test_spacepool_base_functionality(mesh): p = SpacePool(mesh) d = mesh.geometry().dim() spaces = [] shapes = [(), (3,), (2,4)] degrees = [0,1,2] for shape in shapes: for degree in degrees: V = p.get_custom_space("DG", degree, shape) assert V.ufl_element().degree() == degree assert V.ufl_element().value_shape() == shape rank = len(shape) shape2 = (d,)*rank U = p.get_space(degree, rank) assert U.ufl_element().degree() == degree assert U.ufl_element().value_shape() == shape2 spaces.append((V,U)) k = 0 for shape in shapes: for degree in degrees: V0, U0 = spaces[k]; k += 1 V = p.get_custom_space("DG", degree, shape) U = p.get_space(degree, len(shape)) assert id(V0) == id(V) assert id(U0) == id(U)
def before_first_compute(self, get): u = get("Velocity") V = u.function_space() spaces = SpacePool(V.mesh()) degree = V.ufl_element().degree() if degree <= 1: Q = spaces.get_grad_space(V, shape=(spaces.d,)) else: if degree > 2: cbc_warning("Unable to handle higher order WSS space. Using CG1.") Q = spaces.get_space(1,1) Q_boundary = spaces.get_space(Q.ufl_element().degree(), 1, boundary=True) self.v = TestFunction(Q) self.tau = Function(Q, name="WSS_full") self.tau_boundary = Function(Q_boundary, name="WSS") local_dofmapping = mesh_to_boundarymesh_dofmap(spaces.BoundaryMesh, Q, Q_boundary) self._keys = np.array(local_dofmapping.keys(), dtype=np.intc) self._values = np.array(local_dofmapping.values(), dtype=np.intc) self._temp_array = np.zeros(len(self._keys), dtype=np.float_) Mb = assemble(inner(TestFunction(Q_boundary), TrialFunction(Q_boundary))*dx) self.solver = create_solver("gmres", "jacobi") self.solver.set_operator(Mb) self.b = Function(Q_boundary).vector() self._n = FacetNormal(V.mesh())
def before_first_compute(self, get): u = get("Velocity") U = u.function_space() spaces = SpacePool(U.mesh()) if self.params.expr2function == "assemble": V = spaces.get_space(0, 0) else: V = spaces.get_space(2*U.ufl_element().degree(), 0) self._function = Function(V, name=self.name)
def before_first_compute(self, get): u = get("Velocity") U = u.function_space() spaces = SpacePool(U.mesh()) if self.params.expr2function == "assemble": V = spaces.get_space(0, 0) else: V = spaces.get_space(2 * U.ufl_element().degree(), 0) self._function = Function(V, name=self.name)
def before_first_compute(self, get): u = get("Velocity") mesh = u.function_space().mesh() spaces = SpacePool(mesh) DG0 = spaces.get_space(0, 0) self._v = TestFunction(DG0) self._cfl = Function(DG0) self._hF = Circumradius(mesh) self._hK = CellVolume(mesh)
def test_default_save(mesh, casedir): spacepool = SpacePool(mesh) Q = spacepool.get_space(1, 0) V = spacepool.get_space(1, 1) mff = MockFunctionField(Q, dict(save=True)) mvff = MockVectorFunctionField(V, dict(save=True)) mtf = MockTupleField(dict(save=True)) msf = MockScalarField(dict(save=True)) pp = PostProcessor(dict(casedir=casedir)) pp.add_fields([mff, mvff, mtf, msf]) pp.update_all({}, 0.0, 0) pp.update_all({}, 0.1, 1) pp.update_all({}, 0.2, 2) pp.finalize_all() for mf in [mff, mvff]: assert os.path.isdir(pp.get_savedir(mf.name)) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), "metadata.db")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + ".hdf5")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + ".h5")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + ".xdmf")) assert len(os.listdir(pp.get_savedir(mf.name))) == 4 md = shelve.open(os.path.join(pp.get_savedir(mf.name), "metadata.db"), 'r') assert 'hdf5' in md["0"] assert 'hdf5' in md['saveformats'] assert 'xdmf' in md["0"] assert 'xdmf' in md['saveformats'] assert set(md['saveformats']) == set(['hdf5', 'xdmf']) md.close() for mf in [mtf, msf]: assert os.path.isdir(pp.get_savedir(mf.name)) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), "metadata.db")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + ".db")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + ".txt")) md = shelve.open(os.path.join(pp.get_savedir(mf.name), "metadata.db"), 'r') assert 'txt' in md["0"] assert 'txt' in md['saveformats'] assert 'shelve' in md["0"] assert 'shelve' in md['saveformats'] assert set(md['saveformats']) == set(['txt', 'shelve']) md.close()
def test_basic_replay(mesh, casedir): spacepool = SpacePool(mesh) Q = spacepool.get_space(1,0) V = spacepool.get_space(1,1) pp = PostProcessor(dict(casedir=casedir)) pp.add_fields([ MockFunctionField(Q, dict(save=True)), MockVectorFunctionField(V, dict(save=True)) ]) replay_fields = lambda save: [Norm("MockFunctionField", dict(save=save)), Norm("MockVectorFunctionField", dict(save=save)), TimeIntegral("Norm_MockFunctionField", dict(save=save)),] rf_names = [f.name for f in replay_fields(False)] # Add fields, but don't save (for testing) pp.add_fields(replay_fields(False)) # Solutions to check against checks = {} pp.update_all({}, 0.0, 0) checks[0] = dict([(name, pp.get(name)) for name in rf_names]) pp.update_all({}, 0.1, 1) checks[1] = dict([(name, pp.get(name)) for name in rf_names]) pp.update_all({}, 0.2, 2) pp.finalize_all() checks[2] = dict([(name, pp.get(name)) for name in rf_names]) # Make sure that nothing is saved yet for name in rf_names: assert not os.path.isfile(os.path.join(pp.get_savedir(name), name+".db")) # ----------- Replay ----------------- pp = PostProcessor(dict(casedir=casedir)) pp.add_fields([ MockFunctionField(Q), MockVectorFunctionField(V), ]) # This time, save the fields pp.add_fields(replay_fields(True)) replayer = Replay(pp) replayer.replay() # Test that replayed solution is the same as computed in the original "solve" for name in rf_names: data = shelve.open(os.path.join(pp.get_savedir(name), name+".db"), 'r') for i in range(3): assert data.get(str(i), None) == checks[i][name] or abs(data.get(str(i), None) - checks[i][name]) < 1e-8 data.close()
def test_dolfinplot(mesh): pp = PostProcessor() spacepool = SpacePool(mesh) Q = spacepool.get_space(1, 0) V = spacepool.get_space(1, 1) pp.add_field(MockFunctionField(Q, dict(plot=True))) pp.add_field(MockVectorFunctionField(V, dict(plot=True))) pp.update_all({}, 0.0, 0) pp.update_all({}, 0.1, 1) pp.update_all({}, 0.6, 2) pp.update_all({}, 1.6, 3)
def test_grad_spaces(mesh): p = SpacePool(mesh) d = mesh.geometry().dim() #Q = get_custom_space("CG", 1, (1,)) Q = p.get_space(1,0) DQ = p.get_grad_space(Q) assert DQ.ufl_element().family() == "Discontinuous Lagrange" assert DQ.ufl_element().degree() == Q.ufl_element().degree()-1 assert DQ.ufl_element().value_shape() == (d,) V = p.get_space(2,1) DV = p.get_grad_space(V) assert DV.ufl_element().family() == "Discontinuous Lagrange" assert DV.ufl_element().degree() == V.ufl_element().degree()-1 assert DV.ufl_element().value_shape() == (d,d) W = p.get_space(2,2) DW = p.get_grad_space(W) assert DW.ufl_element().family() == "Discontinuous Lagrange" assert DW.ufl_element().degree() == V.ufl_element().degree()-1 assert DW.ufl_element().value_shape() == (d,d,d)
def before_first_compute(self, get): u = get("Velocity") V = u.function_space() spaces = SpacePool(V.mesh()) degree = V.ufl_element().degree() if degree <= 1: Q = spaces.get_grad_space(V, shape=(spaces.d, )) else: if degree > 2: cbc_warning( "Unable to handle higher order WSS space. Using CG1.") Q = spaces.get_space(1, 1) Q_boundary = spaces.get_space(Q.ufl_element().degree(), 1, boundary=True) self.v = TestFunction(Q) self.tau = Function(Q, name="WSS_full") self.tau_boundary = Function(Q_boundary, name="WSS") local_dofmapping = mesh_to_boundarymesh_dofmap(spaces.BoundaryMesh, Q, Q_boundary) self._keys = np.array(local_dofmapping.keys(), dtype=np.intc) self._values = np.array(local_dofmapping.values(), dtype=np.intc) self._temp_array = np.zeros(len(self._keys), dtype=np.float_) Mb = assemble( inner(TestFunction(Q_boundary), TrialFunction(Q_boundary)) * dx) self.solver = create_solver("gmres", "jacobi") self.solver.set_operator(Mb) self.b = Function(Q_boundary).vector() self._n = FacetNormal(V.mesh())
def test_xmlgz_save(mesh, casedir): spacepool = SpacePool(mesh) Q = spacepool.get_space(1, 0) V = spacepool.get_space(1, 1) mff = MockFunctionField(Q, dict(save=True, save_as="xml.gz")) mvff = MockVectorFunctionField(V, dict(save=True, save_as="xml.gz")) pp = PostProcessor(dict(casedir=casedir)) pp.add_fields([mff, mvff]) pp.update_all({}, 0.0, 0) pp.update_all({}, 0.1, 1) pp.update_all({}, 0.2, 2) pp.finalize_all() for mf, FS in [(mff, Q), (mvff, V)]: assert os.path.isdir(pp.get_savedir(mf.name)) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), "metadata.db")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), "mesh.hdf5")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "0.xml.gz")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "1.xml.gz")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "2.xml.gz")) md = shelve.open(os.path.join(pp.get_savedir(mf.name), "metadata.db"), 'r') assert 'xml.gz' in md["0"] assert 'xml.gz' in md["1"] assert 'xml.gz' in md["2"] assert 'xml.gz' in md['saveformats'] assert md['saveformats'] == ['xml.gz'] md.close() assert len(os.listdir(pp.get_savedir(mf.name))) == 1 + 1 + 3 # Read back for i in ["0", "1", "2"]: f = Function( FS, os.path.join(pp.get_savedir(mf.name), mf.name + i + ".xml.gz")) assert norm(f) == norm(pp.get(mf.name))
def before_first_compute(self, get): u = get("Velocity") U = u.function_space() spaces = SpacePool(U.mesh()) if self.params.expr2function == "assemble": degree = 0 elif True: degree = 1 else: # TODO: Use accurate degree? Plotting will be projected to CG1 anyway... degree = 2 * (U.ufl_element().degree() - 1) V = spaces.get_space(degree, 0) self._function = Function(V, name=self.name)
def before_first_compute(self, get): u = get("Velocity") assert len(u) == 2, "Can only compute stream function for 2D problems" V = u.function_space() spaces = SpacePool(V.mesh()) degree = V.ufl_element().degree() V = spaces.get_space(degree, 0) psi = TrialFunction(V) self.q = TestFunction(V) a = dot(grad(psi), grad(self.q))*dx() self.bc = DirichletBC(V, Constant(0), DomainBoundary()) self.A = assemble(a) self.L = Vector() self.bc.apply(self.A) self.solver = KrylovSolver(self.A, "cg") self.psi = Function(V)
def before_first_compute(self, get): u = get("Velocity") assert len(u) == 2, "Can only compute stream function for 2D problems" V = u.function_space() spaces = SpacePool(V.mesh()) degree = V.ufl_element().degree() V = spaces.get_space(degree, 0) psi = TrialFunction(V) self.q = TestFunction(V) a = dot(grad(psi), grad(self.q)) * dx() self.bc = DirichletBC(V, Constant(0), DomainBoundary()) self.A = assemble(a) self.L = Vector() self.bc.apply(self.A) self.solver = KrylovSolver(self.A, "cg") self.psi = Function(V)
def test_hdf5_save(mesh, casedir): spacepool = SpacePool(mesh) Q = spacepool.get_space(1, 0) V = spacepool.get_space(1, 1) mff = MockFunctionField(Q, dict(save=True, save_as="hdf5")) mvff = MockVectorFunctionField(V, dict(save=True, save_as="hdf5")) pp = PostProcessor(dict(casedir=casedir)) pp.add_fields([mff, mvff]) pp.update_all({}, 0.0, 0) pp.update_all({}, 0.1, 1) pp.update_all({}, 0.2, 2) pp.finalize_all() for mf, FS in [(mff, Q), (mvff, V)]: assert os.path.isdir(pp.get_savedir(mf.name)) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), "metadata.db")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + ".hdf5")) md = shelve.open(os.path.join(pp.get_savedir(mf.name), "metadata.db"), 'r') assert 'hdf5' in md["0"] assert 'hdf5' in md["1"] assert 'hdf5' in md["2"] assert 'hdf5' in md['saveformats'] assert md['saveformats'] == ['hdf5'] md.close() assert len(os.listdir(pp.get_savedir(mf.name))) == 2 # Read back hdf5file = HDF5File( mpi_comm_world(), os.path.join(pp.get_savedir(mf.name), mf.name + ".hdf5"), 'r') f = Function(FS) for i in ["0", "1", "2"]: hdf5file.read(f, mf.name + i) assert norm(f) == norm(pp.get(mf.name))
class NSSpacePool(): "A function space pool with custom named spaces for use with Navier-Stokes schemes." def __init__(self, mesh, u_degree, p_degree, u_family="auto", p_family="auto"): self.spacepool = SpacePool(mesh) assert isinstance(u_degree, int) assert isinstance(p_degree, int) assert isinstance(u_family, str) assert isinstance(p_family, str) self.u_degree = u_degree self.p_degree = p_degree self.u_family = u_family self.p_family = p_family self._spaces = {} # Get dimensions for convenience cell = mesh.ufl_cell() self.gdim = cell.geometric_dimension() self.tdim = cell.topological_dimension() self.gdims = range(self.gdim) self.tdims = range(self.tdim) # For compatibility, remove when code has been converted self.d = self.gdim self.dims = self.gdims @property def U(self): "Scalar valued space for velocity components." return self.spacepool.get_space(self.u_degree, 0, family=self.u_family) @property def V(self): "Vector valued space for velocity vector." return self.spacepool.get_space(self.u_degree, 1, family=self.u_family) @property def Q(self): "Scalar valued space for pressure." return self.spacepool.get_space(self.p_degree, 0, family=self.p_family) @property def DU0(self): "Scalar valued space for gradient component of single velocity component." return self.spacepool.get_space(self.u_degree-1, 0, family=self.u_family) @property def DU(self): "Vector valued space for gradients of single velocity components." return self.spacepool.get_space(self.u_degree-1, 1, family=self.u_family) @property def DV(self): "Tensor valued space for gradients of velocity vector." return self.spacepool.get_space(self.u_degree-1, 2, family=self.u_family) @property def DQ0(self): "Scalar valued space for pressure gradient component." return self.spacepool.get_space(self.p_degree-1, 0, family=self.p_family) @property def DQ(self): "Vector valued space for pressure gradient." return self.spacepool.get_space(self.p_degree-1, 1, family=self.p_family) @property def W(self): "Mixed velocity-pressure space." space = self._spaces.get("W") if space is None: if LooseVersion(dolfin_version()) > LooseVersion("1.6.0"): space = FunctionSpace(self.spacepool.mesh, MixedElement(self.V.ufl_element(),self.Q.ufl_element())) else: space = self.V*self.Q self._spaces["W"] = space return space
def test_pvd_save(mesh, casedir): spacepool = SpacePool(mesh) Q = spacepool.get_space(1, 0) V = spacepool.get_space(1, 1) mff = MockFunctionField(Q, dict(save=True, save_as="pvd")) mvff = MockVectorFunctionField(V, dict(save=True, save_as="pvd")) pp = PostProcessor(dict(casedir=casedir)) pp.add_fields([mff, mvff]) pp.update_all({}, 0.0, 0) pp.update_all({}, 0.1, 1) pp.update_all({}, 0.2, 2) pp.finalize_all() for mf, FS in [(mff, Q), (mvff, V)]: assert os.path.isdir(pp.get_savedir(mf.name)) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), "metadata.db")) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + ".pvd")) if MPI.size(mpi_comm_world()) == 1: assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "%0.6d.vtu" % 0)) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "%0.6d.vtu" % 1)) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "%0.6d.vtu" % 2)) else: assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "%0.6d.pvtu" % 0)) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "%0.6d.pvtu" % 1)) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "%0.6d.pvtu" % 2)) for i in range(MPI.size(mpi_comm_world())): assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "_p%d_%0.6d.vtu" % (i, 0))) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "_p%d_%0.6d.vtu" % (i, 1))) assert os.path.isfile( os.path.join(pp.get_savedir(mf.name), mf.name + "_p%d_%0.6d.vtu" % (i, 2))) md = shelve.open(os.path.join(pp.get_savedir(mf.name), "metadata.db"), 'r') assert 'pvd' in md["0"] assert 'pvd' in md["1"] assert 'pvd' in md["2"] assert md['saveformats'] == ['pvd'] md.close() assert len(os.listdir(pp.get_savedir(mf.name))) == 1 + 1 + 3 + int( MPI.size(mpi_comm_world()) != 1) * MPI.size(mpi_comm_world()) * 3