File: demo_nonlinear_elasticity.m

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 (243 lines) | stat: -rw-r--r-- 9,921 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
243
% Copyright (C) 2005-2016 Julien Pommier.
%
% 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 option) 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.

gf_workspace('clear all');
gf_util('trace level', 1);

% set a custom colormap
r=[0.7 .7 .7]; l = r(end,:); s=63; s1=20; s2=25; s3=48;s4=55;
for i=1:s, c1 = max(min((i-s1)/(s2-s1),1),0);c2 = max(min((i-s3)/(s4-s3),1),0); r(end+1,:)=(1-c2)*((1-c1)*l + c1*[1 0 0]) + c2*[1 .8 .2]; end;
colormap(r);

dirichlet_version = 2; % 1 = simplification, 2 = penalisation
drawing = true;
test_tangent_matrix = false;
incompressible = true;

% lawname = 'Ciarlet Geymonat';
% params = [1;1;0.25];
lawname = 'SaintVenant Kirchhoff';
params = [1;1];
if (incompressible)
    lawname = 'Incompressible Mooney Rivlin';
    params = [1;1];
end

N1=2; N2=4; h=20;
m=gf_mesh('cartesian',(0:N1)/N1 - .5, (0:N2)/N2*h, ((0:N1)/N1 - .5)*3);
mfu=gf_mesh_fem(m,3);     % mesh-fem supporting a 3D-vector field
% the mesh_im stores the integration methods for each tetrahedron
mim=gf_mesh_im(m,gf_Integ('IM_GAUSS_PARALLELEPIPED(3,4)'));
% we choose a P2 fem for the main unknown
gf_mesh_fem_set(mfu, 'fem',gf_Fem('FEM_QK(3,2)'));
mfdu=gf_mesh_fem(m,1);
% the material is homogeneous, hence we use a P0 fem for the data
if (dirichlet_version == 1)
  mfd=mfu;
else
  mfd=gf_mesh_fem(m,1);     % scalar mesh_fem
  gf_mesh_fem_set(mfd,'fem',gf_fem('FEM_QK(3,1)'));
end
% the P2 fem is not derivable across elements, hence we use a discontinuous
% fem for the derivative of U.
gf_mesh_fem_set(mfdu,'fem',gf_fem('FEM_QK_DISCONTINUOUS(3,2)'));


m_char=gf_mesh_get(m, 'char');
mfu_char=gf_mesh_fem_get(mfu, 'char');
mfdu_char=gf_mesh_fem_get(mfdu, 'char');

% display some informations about the mesh
disp(sprintf('nbcvs=%d, nbpts=%d, nbdof=%d',gf_mesh_get(m,'nbcvs'),...
            gf_mesh_get(m,'nbpts'),gf_mesh_fem_get(mfu,'nbdof')));
P=gf_mesh_get(m,'pts'); % get list of mesh points coordinates
%pidtop=find(abs(P(2,:)-13)<1e-6); % find those on top of the object
%pidbot=find(abs(P(2,:)+10)<1e-6); % find those on the bottom

pidtop=find(abs(P(2,:)-h)<1e-6); % find those on top of the object
pidbot=find(abs(P(2,:)-0)<1e-6); % find those on the bottom


% build the list of faces from the list of points
ftop=gf_mesh_get(m, 'faces from pid', pidtop); 
fbot=gf_mesh_get(m, 'faces from pid', pidbot);
% assign boundary numbers
gf_mesh_set(m,'boundary', 1, ftop);
gf_mesh_set(m,'boundary', 2, fbot);
gf_mesh_set(m,'boundary', 3, [ftop fbot]);


md=gf_model('real');
gf_model_set(md, 'add fem variable', 'u', mfu);
gf_model_set(md,'add initialized data','params', params);
% gf_model_set(md, 'add nonlinear elasticity brick', mim, 'u', lawname, 'params');
gf_model_set(md, 'add finite strain elasticity brick', mim, lawname, 'u',  'params');
% gf_model_set(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');
% gf_model_set(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');
% gf_model_set(md, 'add nonlinear generic assembly brick', mim, 'Saint_Venant_Kirchhoff_potential(Grad_u,params)'); 
% gf_model_set(md, 'add nonlinear generic assembly brick', mim, ...
%           '((Id(meshdim)+Grad_u)*(Ciarlet_Geymonat_sigma(Grad_u,params))):Grad_Test_u');
% gf_model_set(md, 'add nonlinear generic assembly brick', mim, ...
%                'Ciarlet_Geymonat_potential(Grad_u,params)');
% gf_model_set(md, 'add nonlinear generic assembly brick', mim, ...
%         '((Id(meshdim)+Grad_u)*(Incompressible_Mooney_Rivlin_sigma(Grad_u,params))):Grad_Test_u');
% gf_model_set(md, 'add nonlinear generic assembly brick', mim, ...
%                  'Incompressible_Mooney_Rivlin_potential(Grad_u,params)');
% gf_model_set(md, 'add nonlinear generic assembly brick', mim, ...
%      '((Id(meshdim)+Grad_u)*(Saint_Venant_Kirchhoff_sigma(Grad_u,params))):Grad_Test_u');

    
if (incompressible || true)
  mfp = gf_Mesh_Fem(m,1); 
  gf_mesh_fem_set(mfp, 'classical discontinuous fem', 1);
  gf_model_set(md, 'add fem variable', 'p', mfp);
  % gf_model_set(md, 'add nonlinear incompressibility brick',  mim, 'u', 'p');
  gf_model_set(md, 'add finite strain incompressibility brick',  mim, 'u', 'p');
  % gf_model_set(md, 'add nonlinear generic assembly brick', mim, ...
  %                'p*(1-Det(Id(meshdim)+Grad_u))');
  % gf_model_set(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))');
end

if (dirichlet_version == 1)
  gf_model_set(md, 'add fem data', 'DirichletData', mfu);
  gf_model_set(md, 'add Dirichlet condition with simplification', 'u', 3, 'DirichletData');
else
  gf_model_set(md, 'add fem data', 'DirichletData', mfd, 3);
  gf_model_set(md, 'add Dirichlet condition with penalization', mim, 'u', 1e4, 3, 'DirichletData');
end

VM=zeros(1,gf_mesh_fem_get(mfdu, 'nbdof'));

reload = 0;

if (reload == 0),
  UU=[];
  VVM=[];
  nbstep=40;
else
  load 'demo_nonlinear_elasticity_U.mat';
  nb_step = size(UU,1);
end;


P=gf_mesh_fem_get(mfd, 'basic dof_nodes');
r = sqrt(P(1 ,:).^2 + P(3, :).^2);
theta = atan2(P(3,:),P(1,:));

for step=1:nbstep,
  w = 3*step/nbstep;
  %set(b2, 'param', 'R', [0;0;0]);

  if (~reload)
    dtheta =  pi;
    dtheta2 = pi/2;
      
    if (dirichlet_version == 1)   
      R=zeros(gf_mesh_fem_get(mfd, 'nbdof'), 1);
    else
      R=zeros(3, gf_mesh_fem_get(mfd, 'nbdof'));
    end
    
    i_top = gf_mesh_fem_get(mfd, 'basic dof on region', 1);
    i_bot = gf_mesh_fem_get(mfd, 'basic dof on region', 2);
    
    
    dd = max(P(1,i_top)*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], sqrt(w)*dtheta2);
      RB1 = axrot_matrix([0 h*.25 0], [0 h*.25 1], -w*dtheta);
      RB2 = RT2';
    elseif (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';
    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';    
    end;
    if (dirichlet_version == 1)
      for i=i_top,
        ro = RT1*RT2*[P(:,i);1];
        R(i) = ro(1+mod(i-1,3)) - P(1+mod(i-1,3),i);
      end
      for i=i_bot,
        ro = RB1*RB2*[P(:,i);1];
        R(i) = ro(1+mod(i-1,3)) - P(1+mod(i-1,3),i);
      end 
    else
      for i=i_top,
        ro = RT1*RT2*[P(:,i);1];
        R(:, i) = ro(1:3) - P(:,i);
      end
      for i=i_bot,
        ro = RB1*RB2*[P(:,i);1];
        R(:, i) = ro(1:3) - P(:,i);
      end
    end
    
    gf_model_set(md, 'variable', 'DirichletData', R);
    gf_model_get(md, 'solve', 'very noisy', 'max_iter', 100, 'max_res', 1e-5, 'lsearch', 'simplest');
      
    if (test_tangent_matrix)
      gf_model_get(md, 'test tangent matrix', 1E-8, 10, 0.0001);
    end;
       
    U = gf_model_get(md, 'variable', 'u');
    % VM0 = gf_model_get(md, 'compute Von Mises or Tresca', 'u', lawname, 'params', mfdu);
    % sigma = gf_model_get(md, 'compute second Piola Kirchhoff tensor', 'u', lawname, 'params', mfdu);
    
    % Direct interpolation of the Von Mises stress
    % VM = gf_model_get(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 = gf_model_get(md, 'interpolation', '(sqrt(3/2)/Det(Id(meshdim)+Grad_u))*Norm(Deviator((Id(meshdim)+Grad_u)*Saint_Venant_Kirchhoff_sigma(Grad_u,params)*(Id(meshdim)+Grad_u'')))', mfdu);
    % VM = gf_model_get(md, 'interpolation', 'sqrt(3/2)*Norm(Deviator(Cauchy_stress_from_PK2(Saint_Venant_Kirchhoff_sigma(Grad_u,params),Grad_u)))', mfdu);
    VM = gf_model_get(md, 'compute finite strain elasticity Von Mises', lawname, 'u', 'params', mfdu);
    % norm(VM-VM0)
    
    UU = [UU;U]; 
    VVM = [VVM;VM];
    save demo_nonlinear_elasticity_U.mat UU VVM m_char mfu_char mfdu_char;
  else
    U=UU(step,:);
    VM=VVM(step,:);
  end;
  disp(sprintf('step %d/%d : |U| = %g',step,nbstep,norm(U)));

  if (drawing)
    gf_plot(mfdu,VM,'mesh','off', 'cvlst',gf_mesh_get(mfdu,'outer faces'), 'deformation',U,'deformation_mf',mfu,'deformation_scale', 1, 'refine', 8); colorbar;
    axis([-3     6     0    20    -2     2]); caxis([0 .3]);
    view(30+20*w, 23+30*w);  
    campos([50 -30 80]);
    camva(8);
    camup
    camlight; 
    axis off;
    pause(1);
    % save a picture..
    %print(gcf, '-dpng', '-r150', sprintf('torsion%03d',step));
  end
end;
  
disp('end of computations, you can now replay the animation with')
disp('demo_nonlinear_elasticity_anim')