def computeTangent(self):

        dy = df.Measure('dx', self.mesh)
        vol = df.assemble(df.Constant(1.0) * dy)
        y = df.SpatialCoordinate(self.mesh)
        Eps = df.Constant(((0., 0.), (0., 0.)))  # just placeholder

        form = self.multiscaleModel(self.mesh, self.sigmaLaw, Eps, self.others)
        a, f, bcs, W = form()

        start = timer()
        A = mp.block_assemble(a)
        if (len(bcs) > 0):
            bcs.apply(A)

        # decompose just once (the faster for single process)
        solver = df.PETScLUSolver('superlu')
        sol = mp.BlockFunction(W)

        end = timer()
        print('time assembling system', end - start)

        for i in range(self.nvoigt):
            start = timer()
            Eps.assign(df.Constant(macro_strain(i)))
            F = mp.block_assemble(f)
            if (len(bcs) > 0):
                bcs.apply(F)

            solver.solve(A, sol.block_vector(), F)
            sol.block_vector().block_function().apply("to subfunctions")

            sig_mu = self.sigmaLaw(df.dot(Eps, y) + sol[0])
            sigma_hom = Integral(sig_mu, dy, (2, 2)) / vol

            self.Chom_[:, i] = sigma_hom.flatten()[[0, 3, 1]]

            end = timer()
            print('time in solving system', end - start)

        print(self.Chom_)

        # from the second run onwards, just returns
        self.getTangent = self.getTangent_

        return self.Chom_
Exemplo n.º 2
0
    def get_tangent(self):
        if self.Chom_ is None:
            self.Chom_ = np.zeros((self.nvoigt, self.nvoigt))

            dy = ufl.Measure("dx", self.mesh)
            vol = df.assemble(df.Constant(1.0) * dy)
            y = ufl.SpatialCoordinate(self.mesh)
            Eps = df.Constant(((0., 0.), (0., 0.)))  # just placeholder

            form = self.multiscale_model(self.mesh, self.sigma_law, Eps,
                                         self.others)
            a, f, bcs, W = form()

            start = timer()
            A = mp.block_assemble(a)
            if len(bcs) > 0:
                bcs.apply(A)

            # decompose just once, since the matrix A is the same in every solve
            solver = df.PETScLUSolver("superlu")
            sol = mp.BlockFunction(W)

            end = timer()
            print("time assembling system", end - start)

            for i in range(self.nvoigt):
                start = timer()
                Eps.assign(df.Constant(self.macro_strain(i)))
                F = mp.block_assemble(f)
                if len(bcs) > 0:
                    bcs.apply(F)

                solver.solve(A, sol.block_vector(), F)
                sol.block_vector().block_function().apply("to subfunctions")

                sig_mu = self.sigma_law(df.dot(Eps, y) + sol[0])
                sigma_hom = self.integrate(sig_mu, dy, (2, 2)) / vol

                self.Chom_[:, i] = sigma_hom.flatten()[[0, 3, 1]]

                end = timer()
                print("time in solving system", end - start)

        return self.Chom_
Exemplo n.º 3
0
    def block_project(u, mesh, restriction, subdomain_data, project_id,
                      **kwargs):
        """
        Implement the FEniCS' project function for multiphenics. This method
        allows to extract and project the solution of implicit FEniCS functions
        into user-friendly functions, and then extract the solution of a
        specific subdomain (up to know subdomains of dimensions 1 or 2 are
        accepted). Applications of this method may be to extract the gradient
        of a function (when using FEniCS grad function) and get the solution
        in/on a desired subdomain, or do the same with the dot product when
        using FEniCS dot (or inner) function.
        The implemented method is the same one used when obtaining the weak
        form of a PDE. That is, we impose: f = u, where u is the function
        containing the desired data and f is the trial function. Then, we
        multiply by a test function and integrate. In that way, we are
        projecting the solution into a function that can be easily used for
        post-processing.
        Parameters
        ----------
        u: ufl.tensor...
            Tensor containing the desired solution.
        mesh: cpp.mesh.Mesh
            Mesh object.
        restriction: mesh.mesh_restriction.MeshRestriction
            Subdomain restriction.
        subdomain_data: cpp.mesh.MeshFunctionSizet
            Contains the all information of the subdomain.
        project_id: int
            Indentifier of the place (in the subdomain_data) where the
            solution is desired.
        **kwargs:
            Accepted kwargs are space_type, boundary_type sign and restricted.
            The space_type identifies the type of Function Space in which we
            want to project the solution into. It can be scalar (for scalar
            fields) or vectorial (for vectorial fields).
            The boundary_type is only required when subdomains of dimension 1
            are introduced, and it is used to specify the type of facets (they
            can be internal or external).
            The sign is only required when subdomains of dimension 1
            (boundaries) are introduced. It is used to specify the sign to
            evaluate the line integral For example, let us consider two
            subdomains whose ids are 0 and 1, respectively. To indicate that we
            are integrating quantities on subdomain 0, we should introduce '-'
            as sign, because it is the the subdomain with the lower id. Thus,
            for subdomain 1, we should introduce '+' as sign, which is the
            default one.
            The restricted kwarg indicates if the function to be projected is
            already restricted. This should be taken into account because
            UFL cannot restrict an expression twice.
        Raises
        ------
        TypeError
            This error will rise when the specified type of function space is
            unknown.
        NotImplementedError
            This error will raise when the dimension of the introduced
            subdomain is greater than 2.
        Returns
        -------
        sol: dolfin.function.function.Function
            Dolfin Function which can be easily used for post-processing tasks.
        """
        if 'space_type' not in kwargs:
            print(
                "** WARNING: No Function Space type was specified. Assuming scalar space.",
                flush=True)
        if subdomain_data.dim() == 1 and 'boundary_type' not in kwargs:
            print(
                "** WARNING: No boundary type specified. Assuming internal type.",
                flush=True)
        kwargs.setdefault('space_type', 'scalar')
        kwargs.setdefault('boundary_type', 'internal')
        kwargs.setdefault('sign', "+")
        kwargs.setdefault('restricted', False)
        kwargs.setdefault('function_space_degree', 2)
        # Create a Block Function Space.
        function_space_degree = kwargs.get('function_space_degree')

        if kwargs['space_type'] == 'scalar':
            aux_space = mp.BlockFunctionSpace(
                [fn.FunctionSpace(mesh, 'CG', function_space_degree)],
                restrict=[restriction])
        elif kwargs['space_type'] == 'vectorial':
            aux_space = mp.BlockFunctionSpace(
                [fn.VectorFunctionSpace(mesh, 'CG', function_space_degree)],
                restrict=[restriction])
        else:
            raise TypeError(
                f"Unknown type of Function Space: {kwargs['space_type']}")

        # Define the trial and test functions.
        trial, = mp.BlockTrialFunction(aux_space)
        test, = mp.BlockTestFunction(aux_space)

        # Define the measure of the subdomain and the variational problem.
        if subdomain_data.dim() == 2:
            dom_measure = fn.Measure('dx')(subdomain_data=subdomain_data)
            lhs = [[fn.inner(trial, test) * dom_measure(project_id)]]
            rhs = [fn.inner(u, test) * dom_measure(project_id)]
        elif subdomain_data.dim() == 1:
            if kwargs['boundary_type'] == 'internal':
                dom_measure = fn.Measure('dS')(subdomain_data=subdomain_data)
            elif kwargs['boundary_type'] == 'external':
                dom_measure = fn.Measure('ds')(subdomain_data=subdomain_data)
            # Check if the interface exists.
            assert fn.assemble(
                1 * dom_measure(domain=mesh)
            ) > 0., "The length of the interface is zero, wrong marking."
            lhs = [[
                fn.inner(trial, test)(kwargs['sign']) * dom_measure(project_id)
            ]]
            if not kwargs.get('restricted'):
                rhs = [
                    fn.inner(u, test)(kwargs['sign']) * dom_measure(project_id)
                ]
            else:
                rhs = [
                    fn.inner(u, test(kwargs['sign'])) * dom_measure(project_id)
                ]
        else:
            raise NotImplementedError(
                f"Domains of dimension {subdomain_data.dim()} are not supported."
            )

        # Define the variational form and solve.
        LHS = mp.block_assemble(lhs)
        RHS = mp.block_assemble(rhs)
        sol = mp.BlockFunction(aux_space)
        mp.block_solve(LHS, sol.block_vector(), RHS)

        return sol[0]
Exemplo n.º 4
0
    def solve_initial_problem(self):
        """
        Solve a simple intial problem for a first guess on the iteration
        process of the main problem. This simple problem is defined as:
            div(r*grad(phiv)) = 0, in vacuum subdomain
            phil              = 0, in liquid subdomain
            sigma             = 0, at interface
        Returns
        -------
        phiv : dolfin.function.function.Function
            Solution of the potential at vacuum.
        phil : dolfin.function.function.Function
            Solution of potential at liquid.
        sigma : dolfin.function.function.Function
            Solution of the surface charge density.
        """

        V = fn.FunctionSpace(self.mesh, 'Lagrange', 2)

        # Define the restrictions.
        restrictions_init = []
        for key in self.subdomains_ids.keys():
            key = key.lower() + '_rtc'
            restrictions_init.append(self.restrictions_dict[key])
        restrictions_init.append(self.restrictions_dict['interface_rtc'])

        # Define the block Function Space.
        W = mp.BlockFunctionSpace([V, V, V], restrict=restrictions_init)

        # Define the trial and test functions.
        test = mp.BlockTestFunction(W)
        (v1, v2, l) = mp.block_split(test)

        trial = mp.BlockTrialFunction(W)
        (phiv, phil, sigma) = mp.block_split(trial)

        # Define auxiliary terms.
        r = fn.SpatialCoordinate(self.mesh)[0]

        #                                       phiv                                                         phil                                sigma             #
        aa = [
            [
                r * fn.inner(fn.grad(phiv), fn.grad(v1)) *
                self.dx(self.subdomains_ids['Vacuum']), 0, 0
            ],  # Test Function v1
            [0, phil * v2 * self.dx(self.subdomains_ids['Liquid']),
             0],  # Test function v2
            [0, 0, sigma("+") * l("+") * self.dS]
        ]  # Test function l
        bb = [
            fn.Constant(0.) * v1 * self.dx(self.subdomains_ids['Vacuum']),
            fn.Constant(0.) * v2 * self.dx(self.subdomains_ids['Liquid']),
            fn.Constant(0.) * l("+") * self.dS
        ]

        # Assemble the previous expressions.
        AA = mp.block_assemble(aa)
        BB = mp.block_assemble(bb)

        # Define the boundary conditions.
        bcs_v = []
        bcs_l = []
        bcs_i = []
        for i in self.boundary_conditions:
            if 'Dirichlet' in self.boundary_conditions[i]:
                sub_id = self.boundary_conditions[i]['Dirichlet'][1]
                if sub_id.lower() == list(
                        self.subdomains_ids.keys())[0].lower():
                    sub_id = 0
                elif sub_id.lower() == list(
                        self.subdomains_ids.keys())[1].lower():
                    sub_id = 1
                else:
                    raise ValueError(
                        f'Subdomain {sub_id} is not defined on the .geo file.')
                bc_val = self.boundary_conditions[i]['Dirichlet'][0]
                bc = mp.DirichletBC(W.sub(sub_id), bc_val, self.boundaries,
                                    self.boundaries_ids[i])
                # Check the created boundary condition.
                assert len(bc.get_boundary_values()
                           ) > 0., f'Wrongly defined boundary {i}'
                if sub_id == 0:
                    bcs_v.append(bc)
                elif sub_id == 1:
                    bcs_l.append(bc)
                else:
                    bcs_i.append(bc)
        bcs = mp.BlockDirichletBC([bcs_v, bcs_l, bcs_i])

        # Apply the boundary conditions.
        bcs.apply(AA)
        bcs.apply(BB)

        # Define the solution function and solve.
        sol = mp.BlockFunction(W)
        mp.block_solve(AA, sol.block_vector(), BB)

        # Split the solution.
        (phiv, phil, sigma) = sol.block_split()

        return phiv, phil, sigma
Exemplo n.º 5
0
    def solve(self, **kwargs):
        """
        Solves the variational form of the electrostatics as defined in the
        End of Master thesis from Ximo Gallud Cidoncha:
            A comprehensive numerical procedure for solving the Taylor-Melcher
            leaky dielectric model with charge evaporation.
        Parameters
        ----------
        **kwargs : dict
            Accepted kwargs are:
                - electrostatics_solver_settings: The user may define its own solver parameters. They must be defined
                as follows:
                solver_parameters = {"snes_solver": {"linear_solver": "mumps",
                                      "maximum_iterations": 50,
                                      "report": True,
                                      "error_on_nonconvergence": True,
                                      'line_search': 'bt',
                                      'relative_tolerance': 1e-4}}
                where:
                    - snes_solver is the type of solver to be used. In this
                    case, it is compulsory to use snes, since it's the solver
                    accepted by multiphenics. However, one may try other
                    options if only FEniCS is used. These are: krylov_solver
                    and lu_solver.
                    - linear_solver is the type of linear solver to be used.
                    - maximum_iterations is the maximum number of iterations
                    the solver will try to solve the problem. In case no
                    convergence is achieved, the variable
                    error_on_nonconvergence will raise an error in case this
                    is True. If the user preferes not to raise an error when
                    no convergence, the script will continue with the last
                    results obtained in the iteration process.
                    - line_search is the type of line search technique to be
                    used for solving the problem. It is stronly recommended to
                    use the backtracking (bt) method, since it has been proven
                    to be the most robust one, specially in cases where sqrt
                    are defined, where NaNs may appear due to a bad initial
                    guess or a bad step in the iteration process.
                    - relative_tolerance will tell the solver the parameter to
                    consider convergence on the solution.
                All this options, as well as all the other options available
                can be consulted by calling the method
                Poisson.check_solver_options().
                - initial_potential: Dolfin/FEniCS function which will be used as an initial guess on the iterative
                    process. This must be introduced along with kwarg initial_surface_charge_density. Optional.
                - initial_surface_charge_density: Dolfin/FEniCS function which will be used as an initial guess on the
                    iterative process. This must be introduced along with kwarg initial_potential. Optional.
        Raises
        ------
        TypeError
            This error will raise when the convection charge has not one of the
            following types:
                - Dolfin Function.
                - FEniCS UserExpression.
                - FEniCS Constant.
                - Integer or float number, which will be converted to a FEniCS
                Constant.
        Returns
        -------
        phi : dolfin.function.function.Function
            Dolfin function containing the potential solution.
        surface_charge_density : dolfin.function.function.Function
            Dolfin function conataining the surface charge density solution.
        """

        # --------------------------------------------------------------------
        # EXTRACT THE INPUTS #
        # --------------------------------------------------------------------

        # Check if the type of j_conv is the proper one.
        if not isinstance(self.j_conv, (int, float)) \
            and not Poisson.isDolfinFunction(self.j_conv) \
            and not Poisson.isfenicsexpression(self.j_conv) \
                and not Poisson.isfenicsconstant(self.j_conv):
            conv_type = type(self.j_conv)
            raise TypeError(
                f'Convection charge must be an integer, float, Dolfin function, FEniCS UserExpression or FEniCS constant, not {conv_type}.'
            )
        else:
            if isinstance(self.j_conv, (int, float)):
                self.j_conv = fn.Constant(float(self.j_conv))

        # Extract the solver parameters.
        solver_parameters = kwargs.get('electrostatics_solver_settings')

        # --------------------------------------------------------------------
        # FUNCTION SPACES #
        # --------------------------------------------------------------------
        # Extract the restrictions to create the function spaces.
        """ This variable will be used by multiphenics when creating function spaces. It will create function spaces
            on the introduced restrictions. 
        """
        restrictions_block = [
            self.restrictions_dict['domain_rtc'],
            self.restrictions_dict['interface_rtc']
        ]

        # Base Function Space.
        V = fn.FunctionSpace(self.mesh, 'Lagrange', 2)

        # Block Function Space.
        """ Block Function Spaces are similar to FEniCS function spaces. However, since we are creating function spaces
        based on the block of restrictions, we need to create a 'block of function spaces' for each of the restrictions.
        That block of functions is the list [V, V] from the line of code below this comment. They are assigned in the
        same order in which the block of restrictions has been created, that is:
            - V -> domain_rtc
            - V -> interface_rtc
        """
        W = mp.BlockFunctionSpace([V, V], restrict=restrictions_block)

        # Check the dimensions of the created block function spaces.
        for ix, _ in enumerate(restrictions_block):
            assert W.extract_block_sub_space(
                (ix, )).dim() > 0., f'Subdomain {ix} has dimension 0.'

        # --------------------------------------------------------------------
        # TRIAL/TEST FUNCTIONS #
        # --------------------------------------------------------------------
        # Trial Functions.
        dphisigma = mp.BlockTrialFunction(W)

        # Test functions.
        vl = mp.BlockTestFunction(W)
        (v, l) = mp.block_split(vl)

        phisigma = mp.BlockFunction(W)
        (phi, sigma) = mp.block_split(phisigma)

        # --------------------------------------------------------------------
        # MEASURES #
        # --------------------------------------------------------------------
        self.get_measures()
        self.dS = self.dS(
            self.boundaries_ids['Interface'])  # Restrict to the interface.

        # Check proper marking of the interface.
        assert fn.assemble(
            1 * self.dS(domain=self.mesh)
        ) > 0., "The length of the interface is zero, wrong marking. Check the files in Paraview."

        # --------------------------------------------------------------------
        # DEFINE THE F TERM #
        # --------------------------------------------------------------------
        n = fn.FacetNormal(self.mesh)
        t = fn.as_vector((n[1], -n[0]))

        # Define auxiliary terms.
        r = fn.SpatialCoordinate(self.mesh)[0]
        K = 1 + self.Lambda * (self.T_h - 1)

        E_v_n_aux = fn.dot(-fn.grad(phi("-")), n("-"))

        def expFun():
            sqrterm = E_v_n_aux
            expterm = (self.Phi /
                       self.T_h) * (1 - pow(self.B, 0.25) * fn.sqrt(sqrterm))
            return fn.exp(expterm)

        def sigma_fun():
            num = K * E_v_n_aux + self.eps_r * self.j_conv
            den = K + (self.T_h / self.Chi) * expFun()
            return r * num / den

        # Define the relative permittivity.

        class relative_perm(fn.UserExpression):
            def __init__(self, markers, subdomain_ids, relative, **kwargs):
                super().__init__(**kwargs)
                self.markers = markers
                self.subdomain_ids = subdomain_ids
                self.relative = relative

            def eval_cell(self, values, x, cell):
                if self.markers[cell.index] == self.subdomain_ids['Vacuum']:
                    values[0] = 1.
                else:
                    values[0] = self.relative

        rel_perm = relative_perm(self.subdomains,
                                 self.subdomains_ids,
                                 relative=self.eps_r,
                                 degree=0)

        # Define the variational form.
        # vacuum_int = r*fn.inner(fn.grad(phi), fn.grad(v))*self.dx(self.subdomains_ids['Vacuum'])
        # liquid_int = self.eps_r*r*fn.inner(fn.grad(phi), fn.grad(v))*self.dx(self.subdomains_ids['Liquid'])

        F = [
            r * rel_perm * fn.inner(fn.grad(phi), fn.grad(v)) * self.dx -
            r * sigma("-") * v("-") * self.dS,
            r * sigma_fun() * l("-") * self.dS -
            r * sigma("-") * l("-") * self.dS
        ]

        J = mp.block_derivative(F, phisigma, dphisigma)

        # --------------------------------------------------------------------
        # BOUNDARY CONDITIONS #
        # --------------------------------------------------------------------
        bcs_block = []
        for i in self.boundary_conditions:
            if 'Dirichlet' in self.boundary_conditions[i]:
                bc_val = self.boundary_conditions[i]['Dirichlet'][0]
                bc = mp.DirichletBC(W.sub(0), bc_val, self.boundaries,
                                    self.boundaries_ids[i])
                # Check the created boundary condition.
                assert len(bc.get_boundary_values()
                           ) > 0., f'Wrongly defined boundary {i}'
                bcs_block.append(bc)

        bcs_block = mp.BlockDirichletBC([bcs_block])

        # --------------------------------------------------------------------
        # SOLVE #
        # --------------------------------------------------------------------
        # Define and assign the initial guesses.
        if kwargs.get('initial_potential') is None:
            """
            Check if the user is introducing a potential from a previous
            iteration.
            """
            phiv, phil, sigma_init = self.solve_initial_problem()
            # phi_init = self.solve_initial_problem_v2()
            phi.assign(phiv)
            sigma.assign(sigma_init)
        else:
            phi.assign(kwargs.get('initial_potential'))
            sigma.assign(kwargs.get('initial_surface_charge_density'))

        # Apply the initial guesses to the main function.
        phisigma.apply('from subfunctions')

        # Solve the problem with the solver options (either default or user).
        problem = mp.BlockNonlinearProblem(F, phisigma, bcs_block, J)
        solver = mp.BlockPETScSNESSolver(problem)
        solver_type = [i for i in solver_parameters.keys()][0]
        solver.parameters.update(solver_parameters[solver_type])
        solver.solve()

        # Extract the solutions.
        (phi, _) = phisigma.block_split()
        self.phi = phi
        # --------------------------------------------------------------------

        # Compute the electric field at vacuum and correct the surface charge density.
        self.E_v = self.get_electric_field('Vacuum')
        self.E_v_n = self.get_normal_field(n("-"), self.E_v)
        self.E_t = self.get_tangential_component(t("+"), self.E_v)
        C = self.Phi / self.T_h * (1 - self.B**0.25 * fn.sqrt(self.E_v_n))
        self.sigma = (K * self.E_v_n) / (K + self.T_h / self.Chi * fn.exp(-C))
Exemplo n.º 6
0
    def solve(self):
        """
        Solve the Stokes_sim problem based on the mathematical procedure presented by Ximo in this thesis.
        Returns:

        """

        # --------------------------------------------------------------------
        # DEFINE THE INPUTS #
        # --------------------------------------------------------------------
        self.get_mesh()
        self.get_boundaries()
        self.get_subdomains()
        self.get_restrictions()

        # Create a block of restrictions.
        """ This variable will be used by multiphenics when creating function spaces. It will create function spaces
        on the introduced restrictions. 
        """
        block_restrictions = [
            self.restrictions_dict['liquid_rtc'],
            self.restrictions_dict['liquid_rtc'],
            self.restrictions_dict['interface_rtc']
        ]

        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        # FUNCTION SPACES #
        # --------------------------------------------------------------------
        V = fn.VectorFunctionSpace(self.mesh, "CG", 2)
        Q = fn.FunctionSpace(self.mesh, "CG", 1)
        L = fn.FunctionSpace(self.mesh, "DGT", 0)  # DGT 0.

        # Create a block function space.
        """ Block Function Spaces are similar to FEniCS function spaces. However, since we are creating function spaces
        based on the block of restrictions, we need to create a 'block of function spaces' for each of the restrictions.
        That block of functions is the list [V, Q, L] from the line of code below this comment. They are assigned in the
        same order in which the block of restrictions has been created, that is:
            - V -> liquid_rtc
            - Q -> liquid_rtc
            - L -> interface_rtc
        """
        W = mp.BlockFunctionSpace([V, Q, L], restrict=block_restrictions)
        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        # TRIAL/TEST FUNCTIONS #
        # --------------------------------------------------------------------
        """ Trial and test functions are created the multiphenics commands for creating these functions. However, the
        difference wrt the FEniCS functions for this purpose, a trial/test function will be created for each of the
        restrictions (for each function space of the BlockFunctionSpace).
        """
        test = mp.BlockTestFunction(W)
        (v, q, l) = mp.block_split(test)

        trial = mp.BlockTrialFunction(W)
        (u, p, theta) = mp.block_split(trial)

        # Use a value of previous velocity to make the system linear, as explained by Ximo.
        u_prev = fn.Function(V)
        u_prev.assign(fn.Constant((0.1, 0.1)))

        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        # MEASURES #
        # --------------------------------------------------------------------
        self.get_measures()
        self.dS = self.dS(
            self.boundaries_ids['Interface'])  # Restrict to the interface.

        # Check proper marking of the interface.
        assert fn.assemble(
            1 * self.dS(domain=self.mesh)
        ) > 0., "The length of the interface is zero, wrong marking."

        # --------------------------------------------------------------------
        # DEFINE THE VARIATIONAL PROBLEM #
        # --------------------------------------------------------------------
        r = fn.SpatialCoordinate(self.mesh)[0]
        n = fn.FacetNormal(self.mesh)
        tan_vector = fn.as_vector((n[1], -n[0]))
        e_r = fn.Constant((1., 0.))  # Define unit radial vector
        e_z = fn.Constant((0., 1.))  # Define unit axial vector
        aux_term = (self.eps_r * self.Ca *
                    np.sqrt(self.B)) / (1 + self.Lambda * (self.T_h - 1))

        # Define the term a.
        a = r * aux_term * fn.inner((fn.grad(u) + fn.grad(u).T),
                                    (fn.grad(v) + fn.grad(v).T)) * self.dx(
                                        self.subdomains_ids['Liquid'])
        a += 2 / r * aux_term * fn.dot(u, e_r) * fn.dot(v, e_r) * self.dx(
            self.subdomains_ids['Liquid'])

        # Define the term d.
        del_operation = fn.dot(fn.grad(u), u_prev)
        d = r * self.eps_r**2 * self.We * fn.dot(del_operation, v) * self.dx(
            self.subdomains_ids['Liquid'])

        # Define the term l1.
        def evaporated_charge():
            return (self.sigma * self.T_h) / (self.eps_r * self.Chi) * fn.exp(
                -self.Phi / self.T_h *
                (1 - self.B**0.25 * fn.sqrt(self.E_v_n)))

        l1 = -r * evaporated_charge() * l("+") * self.dS

        # Define the term l2.
        l2 = r * self.sigma * fn.dot(self.E_v, tan_vector("-")) * fn.dot(
            v("+"), tan_vector("-")) * self.dS

        # Define the term b.
        def b(vector, scalar):
            radial_term = r * fn.dot(vector, e_r)
            axial_term = r * fn.dot(vector, e_z)
            return -(radial_term.dx(0) + axial_term.dx(1)) * scalar * self.dx(
                self.subdomains_ids['Liquid'])

        # Define the term c.
        c1 = -r * fn.dot(v("+"), n("-")) * theta("+") * self.dS
        c2 = -r * fn.dot(u("+"), n("-")) * l("+") * self.dS

        # Define the tensors to be solved.
        # The following order is used.
        #       u            p           theta       #
        aa = [
            [a + d, b(v, p), c1],  # Test function v
            [b(u, q), 0, 0],  # Test function q
            [c2, 0, 0]
        ]  # Test function l

        bb = [l2, fn.Constant(0.) * q("+") * self.dS, l1]

        # --------------------------------------------------------------------
        # DEFINE THE BOUNDARY CONDITIONS #
        # --------------------------------------------------------------------
        """ When creating Dirichlet boundary conditions with the multiphenics code, a function space from the Block
        must be selected, depending on which subdomain/boundary should it be applied. To do so, the .sub method is used.
        The input is an integer, which depends on the function space in which you want the BC to be applied. For this
        case, inputs of 0, 1 and 2 are accepted, because we have 3 restrictions. The assignments of these ids to the 
        function space is the one done in the block of restrictions.
        """
        bcs_u = []
        bcs_p = []
        for i in self.boundary_conditions:
            if 'Dirichlet' in self.boundary_conditions[i]:
                bc_val = self.boundary_conditions[i]['Dirichlet'][1]
                if self.boundary_conditions[i]['Dirichlet'][0] == 'v':
                    bc = mp.DirichletBC(W.sub(0), bc_val, self.boundaries,
                                        self.boundaries_ids[i])

                    # Check the created boundary condition.
                    assert len(bc.get_boundary_values()
                               ) > 0., f'Wrongly defined boundary {i}'
                    bcs_u.append(bc)
                elif self.boundary_conditions[i]['Dirichlet'][0] == 'p':
                    bc = mp.DirichletBC(W.sub(1), bc_val, self.boundaries,
                                        self.boundaries_ids[i])
                    # Check the created boundary condition.
                    assert len(bc.get_boundary_values()
                               ) > 0., f'Wrongly defined boundary {i}'
                    bcs_p.append(bc)

        bcs_block = mp.BlockDirichletBC([bcs_u, bcs_p])

        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        # SOLVE #
        # --------------------------------------------------------------------
        # Assemble the system.
        AA = mp.block_assemble(aa)
        BB = mp.block_assemble(bb)

        # Apply the boundary conditions.
        bcs_block.apply(AA)
        bcs_block.apply(BB)

        # Solve.
        uptheta = mp.BlockFunction(W)
        mp.block_solve(AA, uptheta.block_vector(), BB)
        (u, p, theta) = uptheta.block_split()

        self.u = u
        self.p_star = p
        self.theta = theta

        # Compute normal and tangential velocity components.
        u_n = fn.dot(u, n)
        self.u_n = Stokes.block_project(
            u_n,
            self.mesh,
            self.restrictions_dict['interface_rtc'],
            self.boundaries,
            self.boundaries_ids['Interface'],
            space_type='scalar',
            boundary_type='internal',
            sign='-')

        u_t = fn.dot(u, tan_vector)
        self.u_t = Stokes.block_project(
            u_t,
            self.mesh,
            self.restrictions_dict['interface_rtc'],
            self.boundaries,
            self.boundaries_ids['Interface'],
            space_type='scalar',
            boundary_type='internal',
            sign='+')

        # Compute the convection charge transport.
        special = (fn.Identity(self.mesh.topology().dim()) -
                   fn.outer(n, n)) * fn.grad(self.sigma)
        self.j_conv = self.Kc * self.B**(
            3 / 2) * (fn.dot(self.sigma * n, fn.dot(fn.grad(self.u), n)) -
                      fn.dot(self.u, special))
        self.j_conv = Stokes.block_project(
            self.j_conv,
            self.mesh,
            self.restrictions_dict['interface_rtc'],
            self.boundaries,
            self.boundaries_ids['Interface'],
            space_type='scalar',
            boundary_type='internal',
            sign='-')
    def computeTangent(self):

        self.readMesh()
        sigmaLaw = lambda u: self.lame[0] * nabla_div(u) * df.Identity(
            2) + 2 * self.lame[1] * symgrad(u)

        dy = self.mesh.dx  # specially for the case of enriched mesh, otherwise it does not work
        vol = df.assemble(df.Constant(1.0) * dy(0)) + df.assemble(
            df.Constant(1.0) * dy(1))

        y = df.SpatialCoordinate(self.mesh)
        Eps = df.Constant(((0., 0.), (0., 0.)))  # just placeholder

        form = self.multiscaleModel(self.mesh, sigmaLaw, Eps, self.others)
        a, f, bcs, W = form()

        start = timer()
        A = mp.block_assemble(a)
        if (len(bcs) > 0):
            bcs.apply(A)

        solver = df.PETScLUSolver('superlu')
        sol = mp.BlockFunction(W)

        if (self.model == 'lin' or self.model == 'dnn'):
            Vref = self.others['uD'].function_space()
            Mref = Vref.mesh()
            normal = df.FacetNormal(Mref)
            volMref = 4.0

        B = np.zeros((2, 2))

        for i in range(self.nvoigt):

            start = timer()
            if (self.model == 'lin' or self.model == 'dnn'):
                self.others['uD'].vector().set_local(
                    self.others['uD{0}_'.format(i)])

                B = -feut.Integral(df.outer(self.others['uD'], normal),
                                   Mref.ds, (2, 2)) / volMref
                T = feut.affineTransformationExpression(
                    np.zeros(2), B,
                    Mref)  # ignore a, since the basis is already translated
                self.others['uD'].vector().set_local(
                    self.others['uD'].vector().get_local()[:] +
                    df.interpolate(T, Vref).vector().get_local()[:])

            Eps.assign(df.Constant(mscm.macro_strain(i) - B))

            F = mp.block_assemble(f)
            if (len(bcs) > 0):
                bcs.apply(F)

            solver.solve(A, sol.block_vector(), F)
            sol.block_vector().block_function().apply("to subfunctions")

            sig_mu = sigmaLaw(df.dot(Eps, y) + sol[0])
            sigma_hom = sum([Integral(sig_mu, dy(i), (2, 2))
                             for i in [0, 1]]) / vol

            self.Chom_[:, i] = sigma_hom.flatten()[[0, 3, 1]]

            end = timer()
            print('time in solving system', end - start)  # Time in seconds

        self.getTangent = self.getTangent_  # from the second run onwards, just returns

        return self.Chom_