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 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
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") # ============================================================================== # OPTIMISATION