nonlinear_problem.py 8.38 KB
Newer Older
1 2
from dolfin import *
from mfront_wrapper.utils import *
3
from mfront_wrapper.gradient_flux import *
4 5 6 7 8 9 10 11 12 13 14 15
import mgis.behaviour as mgis_bv

class MFrontNonlinearProblem(NonlinearProblem):
    def __init__(self, u, material, quadrature_degree=2):
        NonlinearProblem.__init__(self)
        self.u = u
        self.V = self.u.function_space()
        self.u_ = TestFunction(self.V)
        self.du = TrialFunction(self.V)
        self.mesh = self.V.mesh()
        self.material = material
        self.axisymmetric = self.material.hypothesis==mgis_bv.Hypothesis.Axisymmetrical
Jeremy BLEYER's avatar
Jeremy BLEYER committed
16
        self.integration_type = mgis_bv.IntegrationType.IntegrationWithConsistentTangentOperator
17 18

        self.quadrature_degree = quadrature_degree
19
#        self.set_quadrature_function_spaces()
20

Jeremy BLEYER's avatar
Jeremy BLEYER committed
21

22 23 24 25 26 27 28 29 30 31 32 33 34
        cell = self.mesh.ufl_cell()
        W0e = get_quadrature_element(cell, self.quadrature_degree)
        # scalar quadrature space
        self.W0 = FunctionSpace(self.mesh, W0e)
        # compute Gauss points numbers
        self.ngauss = self.W0.dim()
        # Set data manager
        self.material.set_data_manager(self.ngauss)
        self.finite_strain = self.material.behaviour.getBehaviourType()=="StandardFiniteStrainBehaviour"
        if self.material.hypothesis == mgis_bv.Hypothesis.Tridimensional:
            assert self.u.geometric_dimension()==3, "Conflicting geometric dimension and material hypothesis"
        else:
            assert self.u.geometric_dimension()==2, "Conflicting geometric dimension and material hypothesis"
Jeremy BLEYER's avatar
Jeremy BLEYER committed
35

36 37 38 39 40
        self.bc = []
        self.dx = Measure("dx", metadata={"quadrature_degree": self.quadrature_degree,
                                          "quadrature_scheme": "default"})
        if self.axisymmetric:
            x = SpatialCoordinate(self.mesh)
41
            self.axi_coeff = 2*pi*abs(x[0])
42
        else:
43
            self.axi_coeff = 1
44 45 46 47 48

        self.solver = NewtonSolver()

        self.state_variables = []

49 50 51 52 53 54
    def define_form(self, flux):
        self.flux = flux
        self.flux.initialize_functions(self.mesh, self.quadrature_degree)

        # tangent bilinear form
        dg = self.flux.dual_gradient.variation(self.u_)
55
        tangent_terms = [inner(dg, t*g.variation(self.du))
56 57 58 59 60
                         for (t, g) in zip(self.flux.tangents, self.flux.gradients)]
        self.a = sum(tangent_terms)*self.axi_coeff*self.dx
        # residual form (internal forces)
        self.L = inner(dg, self.flux.function)*self.axi_coeff*self.dx

Jeremy BLEYER's avatar
Jeremy BLEYER committed
61

62 63 64 65 66 67 68 69 70 71 72 73 74
    def set_loading(self, Fext):
        # adds external forces to residual form
        self.L -= ufl.replace(Fext, {self.u: self.u_})

    def set_quadrature_function_spaces(self):
        cell = self.mesh.ufl_cell()
        W0e = get_quadrature_element(cell, self.quadrature_degree)
        # scalar quadrature space
        self.W0 = FunctionSpace(self.mesh, W0e)
        # compute Gauss points numbers
        self.ngauss = self.W0.dim()
        # Set data manager
        self.material.set_data_manager(self.ngauss)
75

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
        # Get strain measure dimension
        self.strain_dim = ufl.shape(self.strain_measure(self.u))[0]
        # Define quadrature spaces for stress/strain and tangent matrix
        Wsige = get_quadrature_element(cell, self.quadrature_degree, self.strain_dim)
        # stress/strain quadrature space
        self.Wsig = FunctionSpace(self.mesh, Wsige)
        Wce = get_quadrature_element(cell, self.quadrature_degree, (self.strain_dim, self.strain_dim))
        # tangent matrix quadrature space
        self.WCt = FunctionSpace(self.mesh, Wce)

    def strain_measure(self, v):
        """ Strain measure associated with stress measure:
            * small strain behaviour: linearized strain tensor epsilon = sym(grad(u))
            * finite strain behaviour: transformation gradient F = Id + grad(u)
        """
        if self.axisymmetric:
            r = abs(SpatialCoordinate(self.mesh)[0])
            g = axi_grad(r, v)
            E = symmetric_tensor_to_vector(sym(g))
            if v.geometric_dimension()==2:
                return as_vector([E[i] for i in range(4)])
        else:
            g = grad(v)
            if self.finite_strain:
                return transformation_gradient(g, dim=v.geometric_dimension())
            else:
                return symmetric_gradient(g)

    def strain_variation(self, v):
        """ Variation of strain measure associated with stress measure:
            * small strain behaviour: linearized strain tensor d_epsilon = sym(grad(du))
            * finite strain behaviour: displacement gradient d_F = grad(du)
        """
Jeremy BLEYER's avatar
Jeremy BLEYER committed
109
        return derivative(self.strain_measure(v), v, v)
110 111 112 113 114 115

    def initialize_fields(self):
        self.stress = Function(self.Wsig, name="Current stress")
        self.strain = Function(self.Wsig, name="Current strain increment")
        self.Ct = Function(self.WCt, name="Consistent tangent operator")

Jeremy BLEYER's avatar
Jeremy BLEYER committed
116 117
        mgis_bv.integrate(self.material.data_manager,
                          self.integration_type, 0, 0, self.material.data_manager.n);
118 119 120 121 122 123 124 125
        if self.finite_strain:
            local_project(self.strain_measure(self.u), self.Wsig, self.dx, self.strain)
            # copy the strain values to `MGIS`
            self.material.data_manager.s0.gradients[:, :] = self.strain.vector().get_local().reshape((self.material.data_manager.n, self.strain_dim))
        else:
            self.Ct.vector().set_local(self.material.data_manager.K.flatten())

    def update_constitutive_law(self, u):
126 127
        g0 = self.flux.gradients[0]
        local_project(g0(self.u), g0.function_space, self.dx, g0.function)
128
        # copy the strain values to `MGIS`
129
        self.material.data_manager.s1.gradients[:, :] = g0.function.vector().get_local().reshape((self.material.data_manager.n, g0.shape))
130
        # integrate the behaviour
Jeremy BLEYER's avatar
Jeremy BLEYER committed
131 132
        mgis_bv.integrate(self.material.data_manager, self.integration_type,
                          0, 0, self.material.data_manager.n);
133 134 135 136 137 138 139 140 141 142 143 144
        # getting the stress and consistent tangent operator back to
        # the FEniCS world.
        if self.finite_strain:
            pk1v = self.stress.vector().get_local()
            mgis_bv.convertFiniteStrainStress(pk1v, self.material.data_manager,
                                              mgis_bv.FiniteStrainStress.PK1)
            self.stress.vector().set_local(pk1v)
            Ctv = self.Ct.vector().get_local()
            mgis_bv.convertFiniteStrainTangentOperator(Ctv, self.material.data_manager,
                                                       mgis_bv.FiniteStrainTangentOperator.DPK1_DF)
            self.Ct.vector().set_local(Ctv)
        else:
145 146 147 148
            self.flux.update(self.material.data_manager.s1.thermodynamic_forces.flatten())
            K = self.material.data_manager.K
            buff = 0
            shapes = [ufl.shape(t) for t in self.flux.tangents]
149 150 151 152 153 154 155
            flattened_shapes = [s[0]*s[1] if len(s)==2 else s[0] for s in shapes]
            for (i,t) in enumerate(self.flux.tangents):
                s = flattened_shapes[i]
                print(s, buff, K.shape, K[137,:])
                t.vector().set_local(K[:,buff:buff+s].flatten())
                #t.vector().set_local(K.flatten())
                buff += s
156 157 158 159 160
            print(self.flux.tangents[0].vector().get_local())
            # sizes = self.material.get_state_variable_sizes()
            # for (s, i) in self.state_variables:
            #     size = sizes[i]
            #     s.vector().set_local(self.material.data_manager.s1.internal_state_variables[:, i:(i+size)].flatten())
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

    def get_state_variable(self, name=None, position=None):
        if name is not None:
            position = self.material.get_state_variable_names().index(name)
        elif position is not None:
           name = self.material.get_state_variable_names()[position]
        else:
            raise ValueError("Name or position of state variable must be specified.")
        shape = self.material.get_state_variable_sizes()[position]
        We = get_quadrature_element(self.mesh.ufl_cell(), self.quadrature_degree, shape)
        W = FunctionSpace(self.mesh, We)
        self.state_variables.append([Function(W, name=name), position])
        return self.state_variables[-1][0]


    def form(self, A, P, b, x):
        self.update_constitutive_law(self.u)
        assemble_system(self.a, self.L, A_tensor=A, b_tensor=b, bcs=self.bc, x0=x)

    def F(self,b,x):
        pass

    def J(self,A,x):
        pass

    def solve(self, x):
        self.solver.solve(self, x)
        mgis_bv.update(self.material.data_manager)