def test_write_mesh(): os.system('cashocs-convert ./mesh/mesh.msh ./mesh/mesh.xdmf') mesh, subdomains, boundaries, dx, ds, dS = cashocs.import_mesh( './mesh/mesh.xdmf') cashocs.utils.write_out_mesh(mesh, './mesh/mesh.msh', './mesh/test.msh') os.system('cashocs-convert ./mesh/test.msh ./mesh/test.xdmf') test, _, _, _, _, _ = cashocs.import_mesh('./mesh/test.xdmf') assert np.allclose(test.coordinates()[:, :], mesh.coordinates()[:, :]) os.system('rm ./mesh/test.msh') os.system('rm ./mesh/test.xdmf') os.system('rm ./mesh/test.h5') os.system('rm ./mesh/test_subdomains.xdmf') os.system('rm ./mesh/test_subdomains.h5') os.system('rm ./mesh/test_boundaries.xdmf') os.system('rm ./mesh/test_boundaries.h5') os.system('rm ./mesh/mesh.xdmf') os.system('rm ./mesh/mesh.h5') os.system('rm ./mesh/mesh_subdomains.xdmf') os.system('rm ./mesh/mesh_subdomains.h5') os.system('rm ./mesh/mesh_boundaries.xdmf') os.system('rm ./mesh/mesh_boundaries.h5')
def generate_references(): mesh, subdomains, boundaries, dx, ds, dS = import_mesh( './meshes/eit/reference.xdmf') cg_elem = FiniteElement('CG', mesh.ufl_cell(), 1) r_elem = FiniteElement('R', mesh.ufl_cell(), 0) V = FunctionSpace(mesh, MixedElement([cg_elem, r_elem])) u, c = TrialFunctions(V) v, d = TestFunctions(V) a = sigma_out * inner(grad(u), grad(v)) * dx(1) + sigma_in * inner( grad(u), grad(v)) * dx(2) + u * d * ds + v * c * ds L1 = Constant(1) * v * (ds(3) + ds(4)) + Constant(-1) * v * (ds(1) + ds(2)) L2 = Constant(1) * v * (ds(3) + ds(2)) + Constant(-1) * v * (ds(1) + ds(4)) L3 = Constant(1) * v * (ds(3) + ds(1)) + Constant(-1) * v * (ds(2) + ds(4)) reference1 = Function(V) reference2 = Function(V) reference3 = Function(V) solve(a == L1, reference1) solve(a == L2, reference2) solve(a == L3, reference3) ref1, _ = reference1.split(True) ref2, _ = reference2.split(True) ref3, _ = reference3.split(True) return [ref1, ref2, ref3]
def test_mesh_import(): os.system('cashocs-convert ./mesh/mesh.msh ./mesh/mesh.xdmf') mesh, subdomains, boundaries, dx, ds, dS = cashocs.import_mesh( './mesh/mesh.xdmf') gmsh_coords = np.array([[0, 0], [1, 0], [1, 1], [0, 1], [0.499999999998694, 0], [1, 0.499999999998694], [0.5000000000020591, 1], [0, 0.5000000000020591], [0.2500000000010297, 0.7500000000010296], [0.3749999970924328, 0.3750000029075671], [0.7187499979760099, 0.2812500030636815], [0.6542968741702071, 0.6542968818888233]]) assert abs(fenics.assemble(1 * dx) - 1) < 1e-14 assert abs(fenics.assemble(1 * ds) - 4) < 1e-14 assert abs(fenics.assemble(1 * ds(1)) - 1) < 1e-14 assert abs(fenics.assemble(1 * ds(2)) - 1) < 1e-14 assert abs(fenics.assemble(1 * ds(3)) - 1) < 1e-14 assert abs(fenics.assemble(1 * ds(4)) - 1) < 1e-14 assert np.allclose(mesh.coordinates(), gmsh_coords) os.system('rm ./mesh/mesh.xdmf') os.system('rm ./mesh/mesh.h5') os.system('rm ./mesh/mesh_subdomains.xdmf') os.system('rm ./mesh/mesh_subdomains.h5') os.system('rm ./mesh/mesh_boundaries.xdmf') os.system('rm ./mesh/mesh_boundaries.h5')
def generate_measurements(): mesh, subdomains, boundaries, dx, ds, dS = cashocs.import_mesh( './mesh/reference.xdmf') cg_elem = FiniteElement('CG', mesh.ufl_cell(), 1) r_elem = FiniteElement('R', mesh.ufl_cell(), 0) V = FunctionSpace(mesh, MixedElement([cg_elem, r_elem])) u, c = TrialFunctions(V) v, d = TestFunctions(V) a = kappa_out * inner(grad(u), grad(v)) * dx(1) + kappa_in * inner( grad(u), grad(v)) * dx(2) + u * d * ds + v * c * ds L1 = Constant(1) * v * (ds(3) + ds(4)) + Constant(-1) * v * (ds(1) + ds(2)) L2 = Constant(1) * v * (ds(3) + ds(2)) + Constant(-1) * v * (ds(1) + ds(4)) L3 = Constant(1) * v * (ds(3) + ds(1)) + Constant(-1) * v * (ds(2) + ds(4)) meas1 = Function(V) meas2 = Function(V) meas3 = Function(V) solve(a == L1, meas1) solve(a == L2, meas2) solve(a == L3, meas3) m1, _ = meas1.split(True) m2, _ = meas2.split(True) m3, _ = meas3.split(True) return [m1, m2, m3]
solve(a == L2, reference2) solve(a == L3, reference3) ref1, _ = reference1.split(True) ref2, _ = reference2.split(True) ref3, _ = reference3.split(True) return [ref1, ref2, ref3] config = configparser.ConfigParser() config.read('./eit_config.ini') config_list = generate_cg_benchmark_configs(config) mesh, subdomains, boundaries, dx, ds, dS = import_mesh( './meshes/eit/mesh.xdmf') coordinates = mesh.coordinates().copy() cg_elem = FiniteElement('CG', mesh.ufl_cell(), 1) r_elem = FiniteElement('R', mesh.ufl_cell(), 0) V = FunctionSpace(mesh, MixedElement([cg_elem, r_elem])) references = generate_references() bcs = None uc1 = Function(V) u1, c1 = split(uc1) pd1 = Function(V) p1, d1 = split(pd1) e1 = sigma_out*inner(grad(u1), grad(p1))*dx(1) + sigma_in*inner(grad(u1), grad(p1))*dx(2) \ + u1*d1*ds + p1*c1*ds - Constant(1)*p1*(ds(3) + ds(4)) - Constant(-1)*p1*(ds(1) + ds(2))
# GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with CASHOCS. If not, see <https://www.gnu.org/licenses/>. """For the documentation of this demo see https://cashocs.readthedocs.io/en/latest/demos/shape_optimization/doc_shape_stokes.html. """ from fenics import * import cashocs config = cashocs.load_config('./config.ini') mesh, subdomains, boundaries, dx, ds, dS = cashocs.import_mesh('./mesh/mesh.xdmf') v_elem = VectorElement('CG', mesh.ufl_cell(), 2) p_elem = FiniteElement('CG', mesh.ufl_cell(), 1) V = FunctionSpace(mesh, MixedElement([v_elem, p_elem])) up = Function(V) u, p = split(up) vq = Function(V) v, q = split(vq) e = inner(grad(u), grad(v))*dx - p*div(v)*dx - q*div(u)*dx u_in = Expression(('-1.0/4.0*(x[1] - 2.0)*(x[1] + 2.0)', '0.0'), degree=2) bc_in = DirichletBC(V.sub(0), u_in, boundaries, 1) bc_no_slip = cashocs.create_bcs_list(V.sub(0), Constant((0,0)), boundaries, [2,4])
# but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with CASHOCS. If not, see <https://www.gnu.org/licenses/>. """For the documentation of this demo see https://cashocs.readthedocs.io/en/latest/demos/shape_optimization/doc_remeshing.html. """ from fenics import * import cashocs config = cashocs.load_config('./config.ini') mesh, subdomains, boundaries, dx, ds, dS = cashocs.import_mesh(config) V = FunctionSpace(mesh, 'CG', 1) u = Function(V) p = Function(V) x = SpatialCoordinate(mesh) f = 2.5 * pow(x[0] + 0.4 - pow(x[1], 2), 2) + pow(x[0], 2) + pow(x[1], 2) - 1 e = inner(grad(u), grad(p)) * dx - f * p * dx bcs = DirichletBC(V, Constant(0), boundaries, 1) J = u * dx sop = cashocs.ShapeOptimizationProblem(e, bcs, J, u, p, boundaries, config) sop.solve()