File: demo_nonlinear_elasticity.py

package info (click to toggle)
getfem%2B%2B 5.1%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 32,668 kB
  • ctags: 20,930
  • sloc: cpp: 110,660; ansic: 72,312; python: 6,064; sh: 3,608; perl: 1,710; makefile: 1,343
file content (242 lines) | stat: -rw-r--r-- 8,425 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Python GetFEM++ interface
#
# Copyright (C) 2012-2016 Yves Renard.
#
# This file is a part of GetFEM++
#
# GetFEM++  is  free software;  you  can  redistribute  it  and/or modify it
# under  the  terms  of the  GNU  Lesser General Public License as published
# by  the  Free Software Foundation;  either version 3 of the License,  or
# (at your option) any later version along with the GCC Runtime Library
# Exception either version 3.1 or (at your optifn) any later version.
# This program  is  distributed  in  the  hope  that it will be useful,  but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or  FITNESS  FOR  A PARTICULAR PURPOSE.  See the GNU Lesser General Public
# License and GCC Runtime Library Exception for more details.
# You  should  have received a copy of the GNU Lesser General Public License
# along  with  this program;  if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
#
############################################################################

import getfem as gf
import numpy as np
from numpy import linalg as npla

gf.util_trace_level(1)

dirichlet_version = 2          # 1 = simplification, 2 = penalisation
test_tangent_matrix = False    # Test or not tangent system validity
incompressible = False;        # Incompressibility option
explicit_potential = True;     # Elasticity law with explicit potential

# lawname = 'Ciarlet Geymonat'
# params = [1.,1.,0.25]
lawname = 'SaintVenant Kirchhoff'
params = [1.,1.]
if (incompressible):
    lawname = 'Incompressible Mooney Rivlin'
    params = [1.,1.]

N1 = 2; N2 = 4; h = 20.; DX = 1./N1; DY = (1.*h)/N2;
m = gf.Mesh('cartesian', np.arange(-0.5, 0.5+DX,DX), np.arange(0., h+DY,DY),
            np.arange(-1.5, 1.5+3*DX,3*DX))
mfu  = gf.MeshFem(m, 3)            # mesh-fem supporting a 3D-vector field
mfdu = gf.MeshFem(m,1)
# The mesh_im stores the integration methods for each tetrahedron
mim = gf.MeshIm(m, gf.Integ('IM_GAUSS_PARALLELEPIPED(3,4)'))
# We choose a P2 fem for the main unknown
mfu.set_fem(gf.Fem('FEM_QK(3,2)'))

if (dirichlet_version == 1):
  mfd = mfu;
else:
  mfd = gf.MeshFem(m,1)
  mfd.set_fem(gf.Fem('FEM_QK(3,1)'))

# The P2 fem is not derivable across elements, hence we use a discontinuous
# fem for the derivative of U.
mfdu.set_fem(gf.Fem('FEM_QK_DISCONTINUOUS(3,2)'));

# Display some information about the mesh
print('nbcvs=%d, nbpts=%d, nbdof=%d' % (m.nbcvs(), m.nbpts(), mfu.nbdof()))

# Assign boundary numbers

ftop = m.outer_faces_with_direction([0.,  1., 0.], 0.5)
fbot = m.outer_faces_with_direction([0., -1., 0.], 0.5)

m.set_region(1, ftop);
m.set_region(2, fbot);
m.set_region(3, np.append(ftop,fbot,axis=1));

# Model definition

md=gf.Model('real')
md.add_fem_variable('u', mfu)
md.add_initialized_data('params', params)

if (not(explicit_potential)):
    md.add_finite_strain_elasticity_brick(mim, lawname, 'u', 'params')
else:
    print "Explicit elastic potential"
    K = 1.2; mu = 3.0;
    _F_ = "(Id(3)+Grad_u)"
    _J_= "Det{F}".format(F=_F_)
    _be_ = "(Left_Cauchy_Green{F})".format(F=_F_)

    _expr_1 = "{K_over_2}*sqr(log({J}))+{mu_over_2}*(Matrix_j1{be}-3)"\
              .format(K_over_2=K/2., J=_J_, mu_over_2=mu/2., be=_be_)

    _expr_2 = "{K_over_2}*sqr(log({J}))+{mu_over_2}*(pow(Det{be},-1./3.)*Trace{be}-3)"\
              .format(K_over_2=K/2., J=_J_, mu_over_2=mu/2., be=_be_)

    md.add_nonlinear_generic_assembly_brick(mim, _expr_2);


# md.add_nonlinear_generic_assembly_brick(mim, 'sqr(Trace(Green_Lagrangian(Id(meshdim)+Grad_u)))/8 + Norm_sqr(Green_Lagrangian(Id(meshdim)+Grad_u))/4')
# md.add_nonlinear_generic_assembly_brick(mim, '((Id(meshdim)+Grad_u)*(params(1)*Trace(Green_Lagrangian(Id(meshdim)+Grad_u))*Id(meshdim)+2*params(2)*Green_Lagrangian(Id(meshdim)+Grad_u))):Grad_Test_u')
# md.add_nonlinear_generic_assembly_brick(mim, 'Saint_Venant_Kirchhoff_potential(Grad_u,params)')
    
if (incompressible):
    mfp = gf.MeshFem(m,1)
    mfp.set_classical_discontinuous_fem(1)
    md.add_fem_variable('p', mfp)
    md.add_finite_strain_incompressibility_brick(mim, 'u', 'p')
    # md.add_nonlinear_generic_assembly brick(mim, 'p*(1-Det(Id(meshdim)+Grad_u))')
    # md.add_nonlinear_generic_assembly_brick(mim, '-p*Det(Id(meshdim)+Grad_u)*(Inv(Id(meshdim)+Grad_u))'':Grad_Test_u + Test_p*(1-Det(Id(meshdim)+Grad_u))')


if (dirichlet_version == 1):
    md.add_fem_data('DirichletData', mfu)
    md.add_Dirichlet_condition_with_simplification('u', 3, 'DirichletData')
else:
    md.add_fem_data('DirichletData', mfd, 3)
    md.add_Dirichlet_condition_with_penalization(mim, 'u', 1e4, 3, 'DirichletData')


VM=np.zeros(mfdu.nbdof());
UU=np.zeros(0);
VVM=np.zeros(0);
nbstep=40;


P = mfd.basic_dof_nodes()
r = np.sqrt(np.square(P[0 ,:]) + np.square(P[2, :]))
theta = np.arctan2(P[2,:], P[0,:]);


def axrot_matrix(A, B, theta):
    n=(np.array(B)-np.array(A)); n = n/npla.norm(n)
    a=n[0]; b=n[1]; c=n[2]
    d=np.sqrt(b*b+c*c)
    T=np.eye(4); T[0:3,3]=-np.array(A)
    Rx=np.eye(4)
    if (npla.norm(n[1:3])>1e-6):
        Rx[1:3,1:3]=np.array([[c/d,-b/d],[b/d,c/d]])
    Ry=np.eye(4); Ry[[[0],[2]],[0,2]]=[[d,-a],[a,d]]
    Rz=np.eye(4)
    Rz[0:2,0:2]=np.array([[np.cos(theta),np.sin(theta)],
                          [-np.sin(theta),np.cos(theta)]])
    R = np.dot(np.dot(np.dot(npla.inv(T),npla.inv(Rx)),
                      np.dot(npla.inv(Ry),Rz)),np.dot(np.dot(Ry,Rx),T))
    return R;
  



for step in range(1,nbstep+1):
    w = (3.*step)/nbstep

    # Computation of the rotation for Dirichlet's condition
    dtheta =  np.pi
    dtheta2 = np.pi/2.
      
    if (dirichlet_version == 1):
        R=np.zeros(mfd.nbdof())
    else:
        R=np.zeros((3, mfd.nbdof()))
    
    i_top = mfd.basic_dof_on_region(1)
    i_bot = mfd.basic_dof_on_region(2)
    
    dd = np.amax(P[1,i_top]*np.sin(w*dtheta))
    
    if (w < 1): 
        RT1 = axrot_matrix([0, h*.75, 0], [0, h*.75, 1], w*dtheta)
        RT2 = axrot_matrix([0, 0, 0], [0, 1, 0], np.sqrt(w)*dtheta2)
        RB1 = axrot_matrix([0, h*.25, 0], [0, h*.25, 1], -w*dtheta)
        RB2 = RT2.transpose()
    elif (w < 2):
        RT1 = axrot_matrix([0, h*.75, 0], [0, h*.75, 1], (2-w)*dtheta);
        RT2 = axrot_matrix([0, 0, 0], [0, 1, 0], w*dtheta2);
        RB1 = axrot_matrix([0, h*.25, 0], [0, h*.25, 1], -(2-w)*dtheta);
        RB2 = RT2.transpose()
    else:
        RT1 = axrot_matrix([0, h*.75, 0], [0, h*.75, 1], 0);
        RT2 = axrot_matrix([0, 0, 0], [0, 1, 0], (3-w)*2*dtheta2);
        RB1 = axrot_matrix([0, h*.25, 0], [0, h*.25, 1], 0);
        RB2 = RT2.transpose()

    if (dirichlet_version == 1):
        for i in i_top:
            ro = np.dot(RT1, np.dot(RT2,np.append(P[:,i],[1])))
            R[i] = ro[mod(i,3)] - P[mod(i,3),i]
        for i in i_bot:
            ro = np.dot(RB1,np.dot(RB2,np.append(P[:,i],[1])))
            R[i] = ro[mod(i,3)] - P[mod(i,3),i] 
    else:
        for i in i_top:
            ro = np.dot(RT1,np.dot(RT2,np.append(P[:,i],[1])))
            R[:, i] = ro[0:3] - P[:,i]
        for i in i_bot:
            ro = np.dot(RB1,np.dot(RB2,np.append(P[:,i],[1])))
            R[:, i] = ro[0:3] - P[:,i]
    
    md.set_variable('DirichletData', R)

    # Nonlinear solve
    md.solve('very noisy', 'max_iter', 50, 'max_res', 1e-7, 'lsearch',
             'simplest')

    print("Iteration %d done" % step)

    if (test_tangent_matrix):
       md.test_tangent_matrix(1E-8, 10, 0.0001)

    U = md.variable('u')

    VM0 = md.compute_Von_Mises_or_Tresca('u', lawname, 'params', mfdu)

    # Direct interpolation of the Von Mises stress
    # VM = md.interpolation('(sqrt(3/2)/Det(Id(meshdim)+Grad_u))*Norm((Id(meshdim)+Grad_u)*Saint_Venant_Kirchhoff_sigma(Grad_u,params)*(Id(meshdim)+Grad_u'') - Id(meshdim)*Trace((Id(meshdim)+Grad_u)*Saint_Venant_Kirchhoff_sigma(Grad_u,params)*(Id(meshdim)+Grad_u''))/meshdim)', mfdu);

    VM = md.compute_finite_strain_elasticity_Von_Mises(lawname, 'u', 'params', mfdu)
    print(npla.norm(VM-VM0))
    
    sl=gf.Slice(('boundary',), mfu, 4)
    sl.export_to_vtk('demo_nonlinear_elasticity_iter_%d.vtk' % step, 'ascii',
                     mfdu,  VM, 'Von Mises Stress', mfu, U, 'Displacement')



print('You can vizualize the loading steps by launching for instance')
print('mayavi2 -d demo_nonlinear_elasticity_iter_1.vtk -f WarpVector -m Surface')