def run_test(): m = mfem.DenseMatrix(3, 3) m.Assign(np.arange(9.).reshape(3, 3)) m.Print() print(np.arange(9.).reshape(3, 3)) x = np.zeros(5) + 1 y = np.zeros(5) + 2 z = np.zeros(5) + 3 mm = mfem.DenseMatrix(3, 5) mm.Assign(np.vstack([x, y, z])) mm.Print() mfem.Vector(mm.GetData(), 15).Print()
def run_test(): #meshfile =expanduser(join(mfem_path, 'data', 'beam-tri.mesh')) meshfile = expanduser(join(mfem_path, 'data', 'semi_circle.mesh')) mesh = mfem.Mesh(meshfile, 1, 1) dim = mesh.Dimension() sdim = mesh.SpaceDimension() fec = mfem.H1_FECollection(1, dim) fespace = mfem.FiniteElementSpace(mesh, fec, 1) print('Number of finite element unknowns: ' + str(fespace.GetTrueVSize())) c = mfem.ConstantCoefficient(1.0) gf = mfem.GridFunction(fespace) gf.ProjectCoefficient(c) gf.Save('out_test_gridfunc.gf')
def run_test(): mesh = mfem.Mesh(10, 10, "TRIANGLE") fec = mfem.H1_FECollection(1, mesh.Dimension()) fespace = mfem.FiniteElementSpace(mesh, fec) gf = mfem.GridFunction(fespace) gf.Assign(1.0) odata = gf.GetDataArray().copy() visit_dc = mfem.VisItDataCollection("test_gf", mesh) visit_dc.RegisterField("gf", gf) visit_dc.Save() mesh2 = mfem.Mesh("test_gf_000000/mesh.000000") check_mesh(mesh, mesh2, ".mesh2 does not agree with original") gf2 = mfem.GridFunction(mesh2, "test_gf_000000/gf.000000") odata2 = gf2.GetDataArray().copy() check(odata, odata2, "odata2 file does not agree with original") visit_dc = mfem.VisItDataCollection("test_gf_gz", mesh) visit_dc.SetCompression(True) visit_dc.RegisterField("gf", gf) visit_dc.Save() with gzip.open("test_gf_gz_000000/mesh.000000", 'rt') as f: sio = io.StringIO(f.read()) mesh3 = mfem.Mesh(sio) check_mesh(mesh, mesh3, ".mesh3 does not agree with original") with gzip.open("test_gf_gz_000000/gf.000000", 'rt') as f: sio = io.StringIO(f.read()) # This is where the error is: gf3 = mfem.GridFunction(mesh3, sio) odata3 = gf3.GetDataArray() check(odata, odata3, "gf3 file does not agree with original") mesh4 = mfem.Mesh("test_gf_gz_000000/mesh.000000") check_mesh(mesh, mesh4, ".mesh4 does not agree with original") gf4 = mfem.GridFunction(mesh3, "test_gf_gz_000000/gf.000000") odata4 = gf4.GetDataArray() check(odata, odata4, "gf3 file does not agree with original") print("PASSED")
def test_course_to_file_map(): mesh_file = "../data/inline-quad.mesh" device = mfem.Device('cpu') mesh = mfem.Mesh(mesh_file, 1, 1) refinements = mfem.RefinementArray() refinements.Append(mfem.Refinement(0, 0b11)) refinements.Append(mfem.Refinement(1, 0b10)) refinements.Append(mfem.Refinement(2, 0b01)) mesh.GeneralRefinement(refinements) cft = mesh.GetRefinementTransforms() coarse_to_fine = mfem.Table() coarse_to_ref_type = mfem.intArray() ref_type_to_matrix = mfem.Table() ref_type_to_geom = mfem.GeometryTypeArray() cft.GetCoarseToFineMap(mesh, coarse_to_fine, coarse_to_ref_type, ref_type_to_matrix, ref_type_to_geom) print("coarse_to_fine element number mapping:") coarse_to_fine.Print() print("ref_type_to_geom mapping:") for i in range(ref_type_to_geom.Size()): g = ref_type_to_geom[i] print("ref_type: " + str(i) + "=> geom: " + str(g))
def run_test(): #meshfile = expanduser(join(mfem_path, 'data', 'semi_circle.mesh')) meshfile = "../data/amr-quad.mesh" mesh = mfem.Mesh(meshfile, 1, 1) dim = mesh.Dimension() sdim = mesh.SpaceDimension() fec = mfem.H1_FECollection(1, dim) fespace = mfem.FiniteElementSpace(mesh, fec, 1) print('Number of finite element unknowns: ' + str(fespace.GetTrueVSize())) c = mfem.ConstantCoefficient(1.0) gf = mfem.GridFunction(fespace) gf.ProjectCoefficient(c) odata = gf.GetDataArray().copy() gf.Save("out_test_gz.gf") gf2 = mfem.GridFunction(mesh, "out_test_gz.gf") odata2 = gf2.GetDataArray().copy() check(odata, odata2, "text file does not agree with original") gf.Save("out_test_gz.gz") gf2.Assign(0.0) gf2 = mfem.GridFunction(mesh, "out_test_gz.gz") odata2 = gf2.GetDataArray().copy() check(odata, odata2, ".gz file does not agree with original") gf.Print("out_test_gz.dat") gf2.Assign(0.0) gf2.Load("out_test_gz.dat", gf.Size()) odata2 = gf2.GetDataArray().copy() check(odata, odata2, ".dat file does not agree with original") gf.Print("out_test_gz.dat.gz") gf2.Assign(0.0) gf2.Load("out_test_gz.dat.gz", gf.Size()) odata2 = gf2.GetDataArray().copy() check(odata, odata2, ".dat file does not agree with original (gz)") import gzip import io gf.Print("out_test_gz.dat2.gz") with gzip.open("out_test_gz.dat2.gz", 'rt') as f: sio = io.StringIO(f.read()) gf3 = mfem.GridFunction(fespace) gf3.Load(sio, gf.Size()) odata3 = gf3.GetDataArray().copy() check(odata, odata3, ".dat file does not agree with original(gz-io)") c = mfem.ConstantCoefficient(2.0) gf.ProjectCoefficient(c) odata = gf.GetDataArray().copy() o = io.StringIO() gf.Print(o) gf2.Load(o, gf.Size()) odata2 = gf2.GetDataArray().copy() check(odata, odata2, "StringIO does not agree with original") print("GridFunction .gf, .gz .dat and StringIO agree with original") mesh2 = mfem.Mesh() mesh.Print("out_test_gz.mesh") mesh2.Load("out_test_gz.mesh") check_mesh(mesh, mesh2, ".mesh does not agree with original") mesh2 = mfem.Mesh() mesh.Print("out_test_gz.mesh.gz") mesh2.Load("out_test_gz.mesh.gz") check_mesh(mesh, mesh2, ".mesh.gz does not agree with original") mesh3 = mfem.Mesh() mesh.PrintGZ("out_test_gz3.mesh") mesh3.Load("out_test_gz3.mesh") check_mesh(mesh, mesh3, ".mesh (w/o .gz exntension) does not agree with original") o = io.StringIO() mesh2 = mfem.Mesh() mesh.Print(o) mesh2.Load(o) check_mesh(mesh, mesh2, ".mesh.gz does not agree with original") print("Mesh .mesh, .mesh.gz and StringIO agree with original") print("PASSED")
def run_test(): #meshfile = expanduser(join(mfem_path, 'data', 'semi_circle.mesh')) mesh = mfem.Mesh(3, 3, 3, "TETRAHEDRON") mesh.ReorientTetMesh() order = 1 dim = mesh.Dimension() sdim = mesh.SpaceDimension() fec1 = mfem.H1_FECollection(order, dim) fespace1 = mfem.FiniteElementSpace(mesh, fec1, 1) fec2 = mfem.ND_FECollection(order, dim) fespace2 = mfem.FiniteElementSpace(mesh, fec2, 1) print("Element order :", order) print('Number of H1 finite element unknowns: ' + str(fespace1.GetTrueVSize())) print('Number of ND finite element unknowns: ' + str(fespace2.GetTrueVSize())) print("Checking scalar") gf = mfem.GridFunction(fespace1) c1 = mfem.NumbaFunction(s_func, sdim).GenerateCoefficient() c2 = s_coeff() gf.Assign(0.0) start = time.time() gf.ProjectCoefficient(c1) end = time.time() data1 = gf.GetDataArray().copy() print("Numba time (scalar)", end - start) gf.Assign(0.0) start = time.time() gf.ProjectCoefficient(c2) end = time.time() data2 = gf.GetDataArray().copy() print("Python time (scalar)", end - start) check(data1, data2, "scalar coefficient does not agree with original") print("Checking vector") gf = mfem.GridFunction(fespace2) c3 = mfem.VectorNumbaFunction(v_func, sdim, dim).GenerateCoefficient() c4 = v_coeff(dim) gf.Assign(0.0) start = time.time() gf.ProjectCoefficient(c3) end = time.time() data1 = gf.GetDataArray().copy() print("Numba time (vector)", end - start) gf.Assign(0.0) start = time.time() gf.ProjectCoefficient(c4) end = time.time() data2 = gf.GetDataArray().copy() print("Python time (vector)", end - start) check(data1, data2, "vector coefficient does not agree with original") print("Checking matrix") a1 = mfem.BilinearForm(fespace2) a2 = mfem.BilinearForm(fespace2) c4 = mfem.MatrixNumbaFunction(m_func, sdim, dim).GenerateCoefficient() c5 = m_coeff(dim) a1.AddDomainIntegrator(mfem.VectorFEMassIntegrator(c4)) a2.AddDomainIntegrator(mfem.VectorFEMassIntegrator(c5)) start = time.time() a1.Assemble() end = time.time() a1.Finalize() M1 = a1.SpMat() print("Numba time (matrix)", end - start) start = time.time() a2.Assemble() end = time.time() a2.Finalize() M2 = a2.SpMat() print("Python time (matrix)", end - start) #from mfem.commmon.sparse_utils import sparsemat_to_scipycsr #csr1 = sparsemat_to_scipycsr(M1, float) #csr2 = sparsemat_to_scipycsr(M2, float) check(M1.GetDataArray(), M2.GetDataArray(), "matrix coefficient does not agree with original") check(M1.GetIArray(), M2.GetIArray(), "matrix coefficient does not agree with original") check(M1.GetJArray(), M2.GetJArray(), "matrix coefficient does not agree with original") print("PASSED")
def run_test(): #meshfile = expanduser(join(mfem_path, 'data', 'semi_circle.mesh')) meshfile = "../data/amr-quad.mesh" mesh = mfem.Mesh(meshfile, 1, 1) dim = mesh.Dimension() sdim = mesh.SpaceDimension() fec = mfem.H1_FECollection(1, dim) fespace = mfem.FiniteElementSpace(mesh, fec, 1) print('Number of finite element unknowns: ' + str(fespace.GetTrueVSize())) c = mfem.ConstantCoefficient(1.0) gf = mfem.GridFunction(fespace) gf.ProjectCoefficient(c) print("write mesh to STDOUT") mesh.Print(mfem.STDOUT) print("creat VTK file to file") mesh.PrintVTK('mesh.vtk', 1) print("creat VTK to STDOUT") mesh.PrintVTK(mfem.STDOUT, 1) print("save GridFunction to file") gf.Save('out_test_gridfunc.gf') gf.SaveVTK(mfem.wFILE('out_test_gridfunc1.vtk'), 'data', 1) print("save GridFunction to file in VTK format") gf.SaveVTK('out_test_gridfunc2.vtk', 'data', 1) print("Gridfunction to STDOUT") gf.Save(mfem.STDOUT) o = io.StringIO() count = gf.Save(o) count2 = gf.SaveVTK(o, 'data', 1) print("length of data ", count, count2) print('result: ', o.getvalue())
def run_test(): print("Test complex_operator module") Nvert = 6 Nelem = 8 Nbelem = 2 mesh = mfem.Mesh(2, Nvert, Nelem, 2, 3) tri_v = [[1., 0., 0.], [0., 1., 0.], [-1., 0., 0.], [0., -1., 0.], [0., 0., 1.], [0., 0., -1.]] tri_e = [[0, 1, 4], [1, 2, 4], [2, 3, 4], [3, 0, 4], [1, 0, 5], [2, 1, 5], [3, 2, 5], [0, 3, 5]] tri_l = [[1, 4], [1, 2]] for j in range(Nvert): mesh.AddVertex(tri_v[j]) for j in range(Nelem): mesh.AddTriangle(tri_e[j], 1) for j in range(Nbelem): mesh.AddBdrSegment(tri_l[j], 1) mesh.FinalizeTriMesh(1, 1, True) dim = mesh.Dimension() order = 1 fec = mfem.H1_FECollection(order, dim) if use_parallel: mesh = mfem.ParMesh(MPI.COMM_WORLD, mesh) fes = mfem.ParFiniteElementSpace(mesh, fec) a1 = mfem.ParBilinearForm(fes) a2 = mfem.ParBilinearForm(fes) else: fes = mfem.FiniteElementSpace(mesh, fec) a1 = mfem.BilinearForm(fes) a2 = mfem.BilinearForm(fes) one = mfem.ConstantCoefficient(1.0) a1.AddDomainIntegrator(mfem.DiffusionIntegrator(one)) a1.Assemble() a1.Finalize() a2.AddDomainIntegrator(mfem.DiffusionIntegrator(one)) a2.Assemble() a2.Finalize() if use_parallel: M1 = a1.ParallelAssemble() M2 = a2.ParallelAssemble() M1.Print('M1') width = fes.GetTrueVSize() #X = mfem.HypreParVector(fes) #Y = mfem.HypreParVector(fes) #X.SetSize(fes.TrueVSize()) #Y.SetSize(fes.TrueVSize()) #from mfem.common.parcsr_extra import ToScipyCoo #MM1 = ToScipyCoo(M1) #print(MM1.toarray()) #print(MM1.dot(np.ones(6))) else: M1 = a1.SpMat() M2 = a2.SpMat() M1.Print('M1') width = fes.GetVSize() #X = mfem.Vector() #Y = mfem.Vector() #X.SetSize(M1.Width()) #Y.SetSize(M1.Height()) #from mfem.common.sparse_utils import sparsemat_to_scipycsr #MM1 = sparsemat_to_scipycsr(M1, np.float) #print(MM1.toarray()) #print(MM1.dot(np.ones(6))) #X.Assign(0.0) #X[0] = 1.0 #M1.Mult(X, Y) #print(Y.GetDataArray()) Mc = mfem.ComplexOperator(M1, M2, hermitan=True) offsets = mfem.intArray([0, width, width]) offsets.PartialSum() x = mfem.BlockVector(offsets) y = mfem.BlockVector(offsets) x.GetBlock(0).Assign(0) if myid == 0: x.GetBlock(0)[0] = 1.0 x.GetBlock(1).Assign(0) if myid == 0: x.GetBlock(1)[0] = 1.0 Mc.Mult(x, y) print("x", x.GetDataArray()) print("y", y.GetDataArray()) if myid == 0: x.GetBlock(1)[0] = -1.0 x.Print() Mc.Mult(x, y) print("x", x.GetDataArray()) print("y", y.GetDataArray())