Esempio n. 1
0
 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)
Esempio n. 2
0
 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)
Esempio n. 3
0
def add_noise(function, noise_level):
    noise = fenics.Function(function.function_space())
    noise.vector()[:] = noise_level * np.random.randn(
        len(function.function_space().dofmap().dofs()))
    fun_with_noise = fenics.project(function + noise,
                                    function.function_space(),
                                    annotate=False)
    return fun_with_noise
    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}
Esempio n. 5
0
    def compare_displacement_field_simulated_registered(self, plot=None):
        if plot is None:
            plot = self.plot

        disp_sim, mesh_sim, subdomains_sim, boundaries_sim = dio.load_function_mesh(
            self.path_forward_disp, functionspace='vector')
        disp_est, mesh_est, subdomains_est, boundaries_est = dio.load_function_mesh(
            self.path_forward_disp_reconstructed, functionspace='vector')
        # -- chose simulation mesh as reference
        funspace_ref = disp_sim.function_space()
        # -- project/interpolate estimated displacement field over that mesh
        disp_est_ref = self.interpolate_non_matching(disp_est, funspace_ref)

        # compute errornorm
        error = fenics.errornorm(disp_sim, disp_est_ref)
        self.measures['errornorm_displacement_simulated_vs_registered'] = error

        # compute difference field
        disp_diff = fenics.project(disp_sim - disp_est_ref,
                                   funspace_ref,
                                   annotate=False)

        if plot:
            plott.show_img_seg_f(function=disp_sim,
                                 show=False,
                                 path=os.path.join(
                                     self.steps_path_map['plots'],
                                     'displacement_field_from_simulation.png'),
                                 dpi=300)
            plott.show_img_seg_f(
                function=disp_est_ref,
                show=False,
                path=os.path.join(
                    self.steps_path_map['plots'],
                    'displacement_field_from_registration_ref_space.png'),
                dpi=300)
            plott.show_img_seg_f(function=disp_diff,
                                 show=False,
                                 path=os.path.join(
                                     self.steps_path_map['plots'],
                                     'displacement_field_difference.png'),
                                 dpi=300)
        self._save_state()
Esempio n. 6
0
    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
# ==============================================================================


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)),
    #                    'subdomain_boundary': 'A_B',
    #                    'subspace_id': 0}
Esempio n. 8
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},
                           }
Esempio n. 9
0
    def compare_original_optimized(self, plot=None):
        if plot is None:
            plot = self.plot

        output_path = self.steps_path_map['comparison']
        self.sim_forward.init_postprocess(output_path)
        self.sim_optimized.init_postprocess(output_path)
        # -- get solution at sim time from original forward model
        conc_orig_full = self.sim_forward.postprocess.get_solution_concentration(
            self.params_forward['sim_params']['sim_time']).copy()
        disp_orig_full = self.sim_forward.postprocess.get_solution_displacement(
            self.params_forward['sim_params']['sim_time']).copy()

        # -- get solution at sim time from optimized simulation
        conc_opt = self.sim_optimized.postprocess.get_solution_concentration(
            self.params_optimized['sim_params']['sim_time']).copy()
        disp_opt = self.sim_optimized.postprocess.get_solution_displacement(
            self.params_optimized['sim_params']['sim_time']).copy()

        # -- project original solution into domain of optimized solution
        # -- chose simulation mesh as reference
        funspace_disp_opt = self.sim_optimized.functionspace.get_functionspace(
            subspace_id=0)
        funspace_conc_opt = self.sim_optimized.functionspace.get_functionspace(
            subspace_id=1)
        conc_orig = self.interpolate_non_matching(conc_orig_full,
                                                  funspace_conc_opt)
        disp_orig = self.interpolate_non_matching(disp_orig_full,
                                                  funspace_disp_opt)

        # -- compute error norms
        error_conc = fenics.errornorm(conc_orig, conc_opt)
        error_disp = fenics.errornorm(disp_orig, disp_opt)

        self.measures[
            'errornorm_displacement_forward_vs_optimized'] = error_disp
        self.measures[
            'errornorm_concentration_forward_vs_optimized'] = error_conc

        if plot:
            plott.show_img_seg_f(function=conc_orig,
                                 path=os.path.join(output_path,
                                                   'conc_forward.png'))
            plott.show_img_seg_f(function=conc_opt,
                                 path=os.path.join(output_path,
                                                   'conc_opt.png'))
            conc_diff = fenics.project(conc_orig - conc_opt,
                                       funspace_conc_opt,
                                       annotate=False)
            plott.show_img_seg_f(function=conc_diff,
                                 path=os.path.join(output_path,
                                                   'conc_diff.png'))

            plott.show_img_seg_f(function=disp_orig,
                                 path=os.path.join(output_path,
                                                   'disp_forward.png'))
            plott.show_img_seg_f(function=disp_opt,
                                 path=os.path.join(output_path,
                                                   'disp_opt.png'))
            disp_diff = fenics.project(disp_orig - disp_opt,
                                       funspace_disp_opt,
                                       annotate=False)
            plott.show_img_seg_f(function=disp_diff,
                                 path=os.path.join(output_path,
                                                   'disp_diff.png'))

        self._save_state()
    test_config.output_path,
    '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")