def test_project_over_subspace(self): from glimslib import fenics_local as fenics nx = ny = nz = 10 mesh = fenics.RectangleMesh(fenics.Point(-2, -2), fenics.Point(2, 2), nx, ny) E1 = fenics.FiniteElement("Lagrange", mesh.ufl_cell(), 1) V1 = fenics.FunctionSpace(mesh, E1) E2 = fenics.VectorElement("Lagrange", mesh.ufl_cell(), 1) V2 = fenics.FunctionSpace(mesh, E2) expr_1 = fenics.Expression( ('exp(-a*pow(x[0]-x0, 2) - a*pow(x[1]-y0, 2))'), degree=1, a=1, x0=0.0, y0=0.0) expr_2 = fenics.Expression(('x[0]', 'x[1]'), degree=1) self.subspaces.set_functionspaces([V1, V2]) f_1 = self.subspaces.project_over_subspace(expr_1, 0) if fenics.is_version("<2018.1.x"): self.assertEqual(type(f_1), fenics.functions.Function) else: self.assertEqual(type(f_1), fenics.function.function.Function) f_2 = self.subspaces.project_over_subspace(expr_2, 0) self.assertEqual(f_2, None) f_1 = self.subspaces.project_over_subspace(expr_1, 1) self.assertEqual(f_1, None) f_2 = self.subspaces.project_over_subspace(expr_2, 1) if fenics.is_version("<2018.1.x"): self.assertEqual(type(f_2), fenics.functions.Function) else: self.assertEqual(type(f_2), fenics.function.function.Function)
def setUp(self): self.nx = 40 self.ny = 20 mesh = fenics.RectangleMesh(fenics.Point(-2, -2), fenics.Point(2, 2), self.nx, self.ny) # function spaces U = fenics.VectorFunctionSpace(mesh, "Lagrange", 1) V = fenics.FunctionSpace(mesh, "Lagrange", 1) u_0_conc_expr = fenics.Expression('sqrt(pow(x[0]-x0,2)+pow(x[1]-y0,2)) < 1 ? (1.0) : (0.0)', degree=1, x0=1, y0=1) u_0_disp_expr = fenics.Constant((1.0, 1.0)) self.conc = fenics.project(u_0_conc_expr, V) self.disp = fenics.project(u_0_disp_expr, U) # 3D mesh3d = fenics.BoxMesh(fenics.Point(-2, -2, -2), fenics.Point(2, 2, 2), 10, 20, 30) # function spaces U3 = fenics.VectorFunctionSpace(mesh3d, "Lagrange", 1) V3 = fenics.FunctionSpace(mesh3d, "Lagrange", 1) u_0_conc_expr = fenics.Expression('sqrt(pow(x[0]-x0,2)+pow(x[1]-y0,2)+pow(x[2]-z0,2)) < 1 ? (1.0) : (0.0)', degree=1, x0=1, y0=1, z0=1 ) u_0_disp_expr = fenics.Constant((1.0, 1.0, 1.0)) self.conc3 = fenics.project(u_0_conc_expr, V3) self.disp3 = fenics.project(u_0_disp_expr, U3) self.test_path = os.path.join(config.output_dir_testing, 'test_data_io') fu.ensure_dir_exists(self.test_path)
def image2fct2D(image_select): """ This function converts a 2D slice of a 3D SimpleITK image instance into a 2D FEniCS function. We use this function to extract a 2D 'labelmap' from a 3D segmentation image. :param image_select: slice id in z direction :return: instance of fenics.Function() """ image_select_np = sitk.GetArrayFromImage(image_select) image_select_np_flat = image_select_np.flatten() origin = image_select.GetOrigin() spacing = image_select.GetSpacing() height = image_select.GetHeight() width = image_select.GetWidth() depts = image_select.GetDepth() # construct rectangular mesh with dofs on pixels p1 = fenics.Point(origin[0], origin[1]) p2 = fenics.Point(origin[0] + spacing[0] * width, origin[1] + spacing[1] * height) nx = int(width - 1) ny = int(height - 1) fenics.parameters["reorder_dofs_serial"] = False mesh_image = fenics.RectangleMesh(p1, p2, nx, ny) n_components = image_select.GetNumberOfComponentsPerPixel() if n_components == 1: V = fenics.FunctionSpace(mesh_image, "CG", 1) else: V = fenics.VectorFunctionSpace(mesh_image, "CG", 1) gdim = mesh_image.geometry().dim() coords = V.tabulate_dof_coordinates().reshape((-1, gdim)) f_img = fenics.Function(V) f_img.vector()[:] = image_select_np_flat fenics.parameters["reorder_dofs_serial"] = True return f_img
def create_fenics_function_from_image_quick(image): origin, size, spacing, extent, dim, vdim = get_measures_from_image(image) # fenics expects number of elements as input argument to Rectangle/BoxMesh # i.e., n_nodes - 1 size_new = size - np.ones_like(size, dtype=int) # construct rectangular/box mesh with dofs on pixels p_min = fenics.Point(extent[0, :]) p_max = fenics.Point(extent[1, :]) if dim == 2: mesh_image = fenics.RectangleMesh(p_min, p_max, *list(size_new)) elif dim == 3: mesh_image = fenics.BoxMesh(p_min, p_max, *list(size_new)) # define value dimensionality if vdim == 1: fenics.parameters["reorder_dofs_serial"] = False V = fenics.FunctionSpace(mesh_image, "CG", 1) else: fenics.parameters["reorder_dofs_serial"] = True V = fenics.VectorFunctionSpace(mesh_image, "CG", 1) # get and assign values image_np = sitk.GetArrayFromImage(image) image_np_flat = image_np.flatten() f_img = fenics.Function(V) f_img.vector()[:] = image_np_flat fenics.parameters["reorder_dofs_serial"] = False return f_img
def setUp(self): # Domain nx = ny = nz = 10 self.mesh = fenics.RectangleMesh(fenics.Point(-2, -2), fenics.Point(2, 2), nx, ny) # function spaces self.displacement_element = fenics.VectorElement( "Lagrange", self.mesh.ufl_cell(), 1) self.concentration_element = fenics.FiniteElement( "Lagrange", self.mesh.ufl_cell(), 1) self.element = fenics.MixedElement( [self.displacement_element, self.concentration_element]) subspace_names = {0: 'displacement', 1: 'concentration'} self.functionspace = FunctionSpace(self.mesh) self.functionspace.init_function_space(self.element, subspace_names) # subdomains label_funspace = fenics.FunctionSpace(self.mesh, "DG", 1) label_expr = fenics.Expression('(x[0]>=0) ? (1.0) : (2.0)', degree=1) labels = fenics.project(label_expr, label_funspace) self.labels = labels self.tissue_id_name_map = {0: 'outside', 1: 'tissue', 2: 'tumor'} self.parameter = {'outside': 0.0, 'tissue': 1.0, 'tumor': 0.1} self.boundary = Boundary() boundary_dict = { 'boundary_1': self.boundary, 'boundary_2': self.boundary } self.boundary_dict = boundary_dict self.subdomains = SubDomains(self.mesh) self.subdomains.setup_subdomains(label_function=self.labels) self.subdomains.setup_boundaries(tissue_map=self.tissue_id_name_map, boundary_fct_dict=self.boundary_dict) self.subdomains.setup_measures() # parameter instance self.params = Parameters(self.functionspace, self.subdomains)
def setUp(self): # Domain nx = ny = nz = 10 self.mesh = fenics.RectangleMesh(fenics.Point(-2, -2), fenics.Point(2, 2), nx, ny) self.sim = TumorGrowth(self.mesh) label_funspace = fenics.FunctionSpace(self.mesh, "DG", 1) label_expr = fenics.Expression('(x[0]>=0.5) ? (1.0) : (2.0)', degree=1) self.labels = fenics.project(label_expr, label_funspace) self.tissue_map = {0: 'outside', 1: 'tissue', 2: 'tumor'} boundary = Boundary() self.boundary_dict = {'boundary_1': boundary, 'boundary_2': boundary} self.dirichlet_bcs = { 'clamped_0': { 'bc_value': fenics.Constant((0.0, 0.0)), 'boundary': Boundary(), 'subspace_id': 0 }, 'clamped_1': { 'bc_value': fenics.Constant((0.0, 0.0)), 'boundary_id': 0, 'subspace_id': 0 }, 'clamped_2': { 'bc_value': fenics.Constant((0.0, 0.0)), 'boundary_name': 'boundary_1', 'subspace_id': 0 } } self.von_neuman_bcs = { 'no_flux': { 'bc_value': fenics.Constant(0.0), 'boundary_id': 0, 'subspace_id': 1 }, 'no_flux_2': { 'bc_value': fenics.Constant(0.0), 'boundary_name': 'boundary_1', 'subspace_id': 1 }, } self.u_0_conc_expr = fenics.Expression( 'sqrt(pow(x[0]-x0,2)+pow(x[1]-y0,2)) < 0.1 ? (1.0) : (0.0)', degree=1, x0=0.25, y0=0.5) self.u_0_disp_expr = fenics.Constant((0.0, 0.0)) self.youngmod = {'outside': 10E6, 'tissue': 1, 'tumor': 1000} self.poisson = {'outside': 0.4, 'tissue': 0.4, 'tumor': 0.49} self.diffusion = {'outside': 0.0, 'tissue': 1.0, 'tumor': 0.1} self.prolif = {'outside': 0.0, 'tissue': 0.1, 'tumor': 1.0} self.coupling = {'outside': 0.0, 'tissue': 0.0, 'tumor': 0.5}
def load_function_mesh(path_to_hdf5_function, functionspace='function', degree=1): if path_to_hdf5_function.endswith('.h5'): path_to_hdf5_mesh = path_to_hdf5_function[:-3] + '_mesh.h5' else: print("Provide path to '.h5' file") if os.path.exists(path_to_hdf5_mesh): mesh, subdomains, boundaries = read_mesh_hdf5(path_to_hdf5_mesh) else: print("Could not find mesh file: '%s'" % path_to_hdf5_mesh) if os.path.exists(path_to_hdf5_function): if functionspace == 'function': functionspace = fenics.FunctionSpace(mesh, "Lagrange", degree) elif functionspace == 'vector': functionspace = fenics.VectorFunctionSpace(mesh, "Lagrange", degree) function = read_function_hdf5("function", functionspace, path_to_hdf5_function) return function, mesh, subdomains, boundaries
def create_fenics_function_from_image(image): origin, size, spacing, extent, dim, vdim = get_measures_from_image(image) # fenics expects number of elements as input argument to Rectangle/BoxMesh # i.e., n_nodes - 1 size_new = size - np.ones_like(size, dtype=int) # construct rectangular/box mesh with dofs on pixels p_min = fenics.Point(extent[0, :]) p_max = fenics.Point(extent[1, :]) if dim == 2: mesh_image = fenics.RectangleMesh(p_min, p_max, *list(size_new)) elif dim == 3: mesh_image = fenics.BoxMesh(p_min, p_max, *list(size_new)) # define value dimensionality if vdim == 1: V = fenics.FunctionSpace(mesh_image, "CG", 1) else: V = fenics.VectorFunctionSpace(mesh_image, "CG", 1, dim=2) # get and assign values f_img = fenics.Function(V) coord_array, value_array = get_coord_value_array_for_image(image, flat=True) unasigned_coords = assign_values_to_fenics_function( f_img, coord_array, value_array) return f_img
def setUp(self): nx = ny = nz = 5 self.ny = ny self.nx = nx mesh = fenics.RectangleMesh(fenics.Point(-2, -2), fenics.Point(2, 2), nx, ny) self.subdomains = SubDomains(mesh) # 'LabelMap' label_funspace = fenics.FunctionSpace(mesh, "DG", 1) label_expr = fenics.Expression('(x[0]>=0) ? (1.0) : (2.0)', degree=1) labels = fenics.project(label_expr, label_funspace) self.labels = labels # tissue_id_name_map self.tissue_id_name_map = {0: 'outside', 1: 'tissue', 2: 'tumor'} self.parameter = {'outside': 0.0, 'tissue': 1.0, 'tumor': 0.1} class Boundary(fenics.SubDomain): def inside(self, x, on_boundary): return on_boundary self.boundary = Boundary() boundary_dict = {'boundary_1': self.boundary} self.boundary_dict = boundary_dict
# ============================================================================== # Problem Settings # ============================================================================== class Boundary(fenics.SubDomain): def inside(self, x, on_boundary): return on_boundary # Mesh nx = ny = 50 mesh = fenics.RectangleMesh(fenics.Point(-5, -5), fenics.Point(5, 5), nx, ny) # LabelMap label_funspace = fenics.FunctionSpace(mesh, "DG", 1) label_expr = fenics.Expression('(x[0]>=0.0) ? (1.0) : (2.0)', degree=1) labels = fenics.project(label_expr, label_funspace) tissue_map = {0: 'outside', 1: 'A', 2: 'B'} # Boundaries & BCs boundary = Boundary() boundary_dict = {'boundary_all': boundary} dirichlet_bcs = { 'clamped_outside': { 'bc_value': fenics.Constant((0.0, 0.0)), 'named_boundary': 'boundary_all', 'subspace_id': 0 }, # Test to show that Dirichlet BCs can be applied to subdomain interfaces # 'clamped_A_B' : {'bc_value': fenics.Constant((0.0, 0.0)),
def setUp(self): # Domain nx = ny = nz = 10 self.nx, self.ny = nx, ny self.mesh = fenics.RectangleMesh(fenics.Point(-2, -2), fenics.Point(2, 2), nx, ny) # function spaces self.displacement_element = fenics.VectorElement("Lagrange", self.mesh.ufl_cell(), 1) self.concentration_element = fenics.FiniteElement("Lagrange", self.mesh.ufl_cell(), 1) self.element = fenics.MixedElement([self.displacement_element, self.concentration_element]) subspace_names = {0: 'displacement', 1: 'concentration'} self.functionspace = FunctionSpace(self.mesh) self.functionspace.init_function_space(self.element, subspace_names) # define 'solution' with concentration=1 everywhere self.conc_expr = fenics.Constant(1.0) self.conc = self.functionspace.project_over_space(self.conc_expr, subspace_name='concentration') # subdomains label_funspace = fenics.FunctionSpace(self.mesh, "DG", 1) label_expr = fenics.Expression('(x[0]>=0) ? (1.0) : (2.0)', degree=1) labels = fenics.project(label_expr, label_funspace) self.labels = labels self.tissue_id_name_map = {0: 'outside', 1: 'tissue', 2: 'tumor'} self.parameter = {'outside': 0.0, 'tissue': 1.0, 'tumor': 0.1} self.boundary_pos = BoundaryPos() self.boundary_neg = BoundaryNeg() boundary_dict = {'boundary_pos': self.boundary_pos, 'boundary_neg': self.boundary_neg} self.boundary_dict = boundary_dict self.subdomains = SubDomains(self.mesh) self.subdomains.setup_subdomains(label_function=self.labels) self.subdomains.setup_boundaries(tissue_map=self.tissue_id_name_map, boundary_fct_dict=self.boundary_dict) self.subdomains.setup_measures() # BCs self.bcs = BoundaryConditions(self.functionspace, self.subdomains) self.dirichlet_bcs = {'clamped_0': {'bc_value': fenics.Constant((0.0, 0.0)), 'boundary': BoundaryPos(), 'subspace_id': 0}, 'clamped_1': {'bc_value': fenics.Constant((0.0, 0.0)), 'subdomain_boundary': 'tissue_tumor', 'subspace_id': 0}, 'clamped_pos': {'bc_value': fenics.Constant((0.0, 0.0)), 'named_boundary': 'boundary_pos', 'subspace_id': 0}, 'clamped_neg': {'bc_value': fenics.Constant((0.0, 0.0)), 'named_boundary': 'boundary_neg', 'subspace_id': 0} } self.von_neuman_bcs = { 'flux_boundary_pos': {'bc_value': fenics.Constant(1.0), 'named_boundary': 'boundary_pos', 'subspace_id': 1}, 'flux_boundary_neg': {'bc_value': fenics.Constant(-5.0), 'named_boundary': 'boundary_neg', 'subspace_id': 1} # 'no_flux_domain_boundary': {'bc_value': fenics.Constant(1.0), # 'subdomain_boundary': 'tissue_tumor', # 'subspace_id': 1}, }
# ============================================================================== # Logging settings # ============================================================================== logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO) fenics.set_log_level(fenics.PROGRESS) # ============================================================================== # Load 2D Mesh from IMAGE # ============================================================================== path_to_atlas = os.path.join(config.test_data_dir, 'brain_atlas_mesh_2d.hdf5') mesh, subdomains, boundaries = dio.read_mesh_hdf5(path_to_atlas) path_to_label = os.path.join(config.test_data_dir, 'brain_atlas_labelfunction_2d.hdf5') V = fenics.FunctionSpace(mesh, "Lagrange", 1) labelfunction = dio.read_function_hdf5("labelfunction", V, path_to_label) # ============================================================================== # Problem Settings # ============================================================================== class Boundary(fenics.SubDomain): def inside(self, x, on_boundary): return on_boundary tissue_id_name_map = { 0: 'outside', 1: 'CSF',
'test_case_simulation_tumor_growth_2D_uniform_adjoint_reloaded') fu.ensure_dir_exists(output_path) D_target = 0.05 rho_target = 0.05 c_target = 0.1 u_target = sim.run_for_adjoint([D_target, rho_target, c_target], output_dir=output_path) disp_sim_target_0, conc_sim_target_0 = fenics.split(u_target) # conc_sim_target = sim.functionspace.project_over_space(conc_sim_target_0, subspace_id=1) # disp_sim_target = sim.functionspace.project_over_space(disp_sim_target_0, subspace_id=0) conc_sim_target = fenics.project(conc_sim_target_0, fenics.FunctionSpace(mesh, "Lagrange", 1)) disp_sim_target = fenics.project( disp_sim_target_0, fenics.VectorFunctionSpace(mesh, "Lagrange", 1)) # plott.show_img_seg_f(function=conc_sim_target, show=True, # path=os.path.join(output_path, 'conc_target_sim.png'), # dpi=300) # plott.show_img_seg_f(function=disp_sim_target, show=True, # path=os.path.join(output_path, 'disp_target_sim.png'), # dpi=300) path_to_xdmf = os.path.join(output_path, 'xdmf_from_simulation.xdmf') with fenics.XDMFFile(path_to_xdmf) as outfile: outfile.write_checkpoint(disp_sim_target, "disp") outfile.write_checkpoint(conc_sim_target, "conc")