def create_forward_problem( mesh, activation, active_value=0.0, active_model="active_stain", T_ref=1.0, ): ffun = df.MeshFunction("size_t", mesh, 2) ffun.set_all(0) left = df.CompiledSubDomain("on_boundary && near(x[0], 0)") left_marker = 1 left.mark(ffun, left_marker) # Collect the functions containing the markers marker_functions = pulse.MarkerFunctions(ffun=ffun) def dirichlet_bc(W): V = W if W.sub(0).num_sub_spaces() == 0 else W.sub(0) return da.DirichletBC(V, da.Constant((0.0, 0.0, 0.0)), left) bcs = pulse.BoundaryConditions(dirichlet=(dirichlet_bc, ), ) f0 = df.as_vector([1, 0, 0]) microstructure = pulse.Microstructure(f0=f0) geometry = pulse.Geometry( mesh=mesh, marker_functions=marker_functions, microstructure=microstructure, ) material_parameters = dict( a=2.28, a_f=1.686, b=9.726, b_f=15.779, a_s=0.0, b_s=0.0, a_fs=0.0, b_fs=0.0, ) material = pulse.HolzapfelOgden( active_model=active_model, parameters=material_parameters, activation=activation, T_ref=T_ref, ) problem = pulse.MechanicsProblem(geometry, material, bcs) problem.solve() if active_value > 0.0: pulse.iterate.iterate(problem, activation, active_value) return problem
def create_mechanics_problem(solver_parameters): import pulse # from pulse import (MechanicsProblem, HeartGeometry, BoundaryConditions, # NeumannBC, RobinBC, MarkerFunctions, Marker, CRLBasis) from pulse.material import HolzapfelOgden mfun = pulse.MarkerFunctions(ffun=solver_parameters['facet_function'], cfun=solver_parameters['mesh_function']) material = solver_parameters['material'] microstructure = pulse.Microstructure(f0=material.f0, s0=material.s0, n0=material.f0) crl_basis = pulse.CRLBasis( c0=solver_parameters['crl_basis']['circumferential'], r0=solver_parameters['crl_basis']['radial'], l0=solver_parameters['crl_basis']['longitudinal']) geometry = pulse.HeartGeometry(mesh=solver_parameters['mesh'], markers=solver_parameters['markers'], marker_functions=mfun, microstructure=microstructure, crl_basis=crl_basis) neumann = [] names = ['lv', 'rv'] for i, n in enumerate(solver_parameters['bc']['neumann']): neumann.append( pulse.NeumannBC(traction=n[0], marker=n[1], name=names[i])) robin = [] for i, n in enumerate(solver_parameters['bc']['robin']): robin.append(pulse.RobinBC(value=n[0], marker=n[1])) if hasattr(solver_parameters['bc']['dirichlet'], '__len__'): dirichlet = solver_parameters['bc']['dirichlet'] else: dirichlet = (solver_parameters['bc']['dirichlet'], ) bcs = pulse.BoundaryConditions(dirichlet=dirichlet, neumann=neumann, robin=robin) problem = pulse.MechanicsProblem(geometry, material, bcs) return problem
fixed = Fixed() fixed_marker = 1 fixed.mark(ffun, fixed_marker) # Mark the second subdomain with value 2 free = Free() free_marker = 2 free.mark(ffun, free_marker) # Create a cell function (but we are not using it) cfun = dolfin.MeshFunction("size_t", mesh, 3) cfun.set_all(0) # Collect the functions containing the markers marker_functions = pulse.MarkerFunctions(ffun=ffun, cfun=cfun) # Create mictrotructure V_f = dolfin.VectorFunctionSpace(mesh, "CG", 1) # Fibers f0 = interpolate(Expression(("1.0", "0.0", "0.0"), degree=1), V_f) # Sheets s0 = interpolate(Expression(("0.0", "1.0", "0.0"), degree=1), V_f) # Fiber-sheet normal n0 = interpolate(Expression(("0.0", "0.0", "1.0"), degree=1), V_f) # Collect the mictrotructure microstructure = pulse.Microstructure(f0=f0, s0=s0, n0=n0) # Create the geometry
import dolfin from fenics_plotly import plot import pulse try: from dolfin_adjoint import Constant, DirichletBC, Function, Mesh, interpolate except ImportError: from dolfin import Mesh, Function, Constant, DirichletBC, interpolate # Mesh mesh = Mesh(pulse.utils.mpi_comm_world(), "data/mesh.xml") # Marker functions facet_function = dolfin.MeshFunction("size_t", mesh, "data/facet_function.xml") marker_functions = pulse.MarkerFunctions(ffun=facet_function) # Markers with open("data/markers.json", "r") as f: markers = json.load(f) # Fiber fiber_element = dolfin.VectorElement( family="Quadrature", cell=mesh.ufl_cell(), degree=4, quad_scheme="default", ) fiber_space = dolfin.FunctionSpace(mesh, fiber_element) fiber = Function(fiber_space, "data/fiber.xml")
def gmsh2dolfin(msh_file): msh = meshio.gmsh.read(msh_file) vertex_mesh = create_mesh(msh, "vertex") line_mesh = create_mesh(msh, "line") triangle_mesh = create_mesh(msh, "triangle") tetra_mesh = create_mesh(msh, "tetra") vertex_mesh_name = Path("vertex_mesh.xdmf") meshio.write(vertex_mesh_name, vertex_mesh) line_mesh_name = Path("line_mesh.xdmf") meshio.write(line_mesh_name, line_mesh) triangle_mesh_name = Path("triangle_mesh.xdmf") meshio.write(triangle_mesh_name, triangle_mesh) tetra_mesh_name = Path("mesh.xdmf") meshio.write( tetra_mesh_name, tetra_mesh, ) mesh = dolfin.Mesh() with dolfin.XDMFFile(tetra_mesh_name.as_posix()) as infile: infile.read(mesh) cfun = dolfin.MeshFunction("size_t", mesh, 3) read_meshfunction(tetra_mesh_name, cfun) tetra_mesh_name.unlink() tetra_mesh_name.with_suffix(".h5").unlink() ffun_val = dolfin.MeshValueCollection("size_t", mesh, 2) read_meshfunction(triangle_mesh_name, ffun_val) ffun = dolfin.MeshFunction("size_t", mesh, ffun_val) ffun.array()[ffun.array() == max(ffun.array())] = 0 triangle_mesh_name.unlink() triangle_mesh_name.with_suffix(".h5").unlink() efun_val = dolfin.MeshValueCollection("size_t", mesh, 1) read_meshfunction(line_mesh_name, efun_val) efun = dolfin.MeshFunction("size_t", mesh, efun_val) efun.array()[efun.array() == max(efun.array())] = 0 line_mesh_name.unlink() line_mesh_name.with_suffix(".h5").unlink() vfun_val = dolfin.MeshValueCollection("size_t", mesh, 0) read_meshfunction(vertex_mesh_name, vfun_val) vfun = dolfin.MeshFunction("size_t", mesh, vfun_val) vfun.array()[vfun.array() == max(vfun.array())] = 0 vertex_mesh_name.unlink() vertex_mesh_name.with_suffix(".h5").unlink() markers = msh.field_data ldrb_markers = { "base": markers["BASE"][0], "epi": markers["EPI"][0], "lv": markers["ENDO"][0], } fiber_sheet_system = ldrb.dolfin_ldrb(mesh, "CG_1", ffun, ldrb_markers) marker_functions = pulse.MarkerFunctions(vfun=vfun, efun=efun, ffun=ffun, cfun=cfun) microstructure = pulse.Microstructure( f0=fiber_sheet_system.fiber, s0=fiber_sheet_system.sheet, n0=fiber_sheet_system.sheet_normal, ) geo = pulse.HeartGeometry( mesh=mesh, markers=markers, marker_functions=marker_functions, microstructure=microstructure, ) return geo
# Create mesh mesh = dolfin.BoxMesh(dolfin.Point(0, 0, 0), dolfin.Point(L, W, W), 30, 3, 3) # Mark boundary subdomians left = dolfin.CompiledSubDomain("near(x[0], side) && on_boundary", side=0) bottom = dolfin.CompiledSubDomain("near(x[2], side) && on_boundary", side=0) boundary_markers = dolfin.MeshFunction("size_t", mesh, mesh.topology().dim() - 1) boundary_markers.set_all(0) left.mark(boundary_markers, 1) bottom.mark(boundary_markers, 2) marker_functions = pulse.MarkerFunctions(ffun=boundary_markers) left_marker = pulse.Marker(name='left', value=1, dimension=2) bottom_marker = pulse.Marker(name='bottom', value=2, dimension=2) markers = (left_marker, bottom_marker) # Create mictrotructure f0 = dolfin.Expression(("1.0", "0.0", "0.0"), degree=1, cell=mesh.ufl_cell()) s0 = dolfin.Expression(("0.0", "1.0", "0.0"), degree=1, cell=mesh.ufl_cell()) n0 = dolfin.Expression(("0.0", "0.0", "1.0"), degree=1, cell=mesh.ufl_cell()) # Collect the mictrotructure microstructure = pulse.Microstructure(f0=f0, s0=s0, n0=n0) # Create the geometry geometry = pulse.Geometry(mesh=mesh,