VenantKirchhofSolver.hpp 14.6 KB
Newer Older
prudhomm's avatar
prudhomm committed
1
2
/*
  This file is part of the LifeV library
3
  Copyright (C) 2001,2002,2003,2004 EPFL, INRIA and Politechnico di Milano
prudhomm's avatar
prudhomm committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  
  This library 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 2.1 of the License, or (at your option) any later version.
  
  This library 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 for more details.
  
  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*!
  \file VenantKirchhofSolver.h
  \author M.A. Fernandez
  \date 6/2003 
  \version 1.0

  \brief 
  This file contains solvers for St. Venant-Kirchhof materials (linear for the moment)

*/
#ifndef _VENANTKIRCHHOFSOLVER_H_
#define _VENANTKIRCHHOFSOLVER_H_

#include "ElasticStructureHandler.hpp"
#include "elemMat.hpp"
#include "elemVec.hpp"
#include "elemOper.hpp"
#include "values.hpp"
#include "pattern.hpp"
#include "assemb.hpp"
#include "bc_manage.hpp"
#include "bcCond.hpp"
#include "chrono.hpp"
#include "dataAztec.hpp"
#include "dataNewton.hpp"
#include "newton.hpp"


/*! 
  \class VenantKirchhofSolver

  \brief
  This class solves the linear elastodynamics equations for a (only linear right now) 
  St. Venant-Kirchoff material
  

*/
template<typename Mesh>
class VenantKirchhofSolver:
public ElasticStructureHandler<Mesh>, public DataNewton {
 
 public:
  
  typedef  typename  ElasticStructureHandler<Mesh>::Function Function; 

  //! Constructor 
  /*!
    \param data_file GetPot data file
    \param refFE reference FE for the displacement
    \param Qr volumic quadrature rule 
    \param bdQr surface quadrature rule 
    \param BCh boundary conditions for the displacement
  */
  VenantKirchhofSolver(const GetPot& data_file, const RefFE& refFE, const QuadRule& Qr,
		       const QuadRule& bdQr, BC_Handler& BCh);

  //! Update the right  hand side  for time advancing 
  /*! 
    \param source volumic source  
    \param time present time
  */
  void timeAdvance(const Function source, const Real& time);

  //! Solve the non-linear system
  void iterate();

  //! Output
  void showMe(ostream& c=cout) const;

 //! friends classes related to the newton solver
  template<class Fct,class Vector,class Real, class Norm>
    friend int newton(Vector& sol,Fct& f,Norm& norm, Real abstol,Real reltol,int& maxit,
		      Real eta_max,int linesearch, ofstream& out_res, const Real& time);
  template<class Fct,class Vector,class Real, class Norm>
    friend void lineSearch_cubic(Fct& f,Norm& norm, Vector& residual,Vector& sol,Vector& step,
				 Real& normRes,Real& lambda,Real slope,int iter);
  template<class Fct,class Vector,class Real, class Norm>
    friend void lineSearch_parab(Fct& f,Norm& norm, Vector& residual,Vector& sol,Vector& step,Real& normRes,
				 Real& lambda,int iter);

 private:

  //! Block pattern of M
  MSRPatt _pattM_block;
  
  //! Pattern for M
  MixedPattern<3,3,MSRPatt> _pattM;

  //! Block pattern of K
  MSRPatt _pattK;

  //! Matrix M: mass
  MixedMatr<3,3,MSRPatt,double> _M;
   
  MSRMatr<double> _Kl;
  
 //! Matrix Knl: stiffness non-linear
  MSRMatr<double> _K;
  
  //! Matrix C: mass + linear stiffness
  MSRMatr<double> _C; 

  //! Matrix J: jacobian 
  MSRMatr<double> _J;
 
  //! Elementary matrices and vectors
  ElemMat _elmatK; // stiffnes 
  ElemMat _elmatM; // mass 
  ElemMat _elmatC; // mass + stiffness
  ElemVec _elvec;  // Elementary right hand side
  ElemVec _dk_loc; // Local displacement

  //! right  hand  side displacement
  PhysVectUnknown<Vector> _rhs; 

  //! right  hand  side velocity
  PhysVectUnknown<Vector> _rhs_w; 


  //! right  hand  side
  PhysVectUnknown<Vector> _rhsWithoutBC;
  
  //! right  hand  side
  PhysVectUnknown<Vector> _f;
 
  //! data for solving tangent problem with aztec
  DataAztec _dataAztec;
 
  //! evaluates residual for newton interations
  void evalResidual(Vector&res, const Vector& sol, int iter);

  //! updates the tangent matrix for newton iterations  
  void updateJac(Vector& sol,int iter);

  //! solves the tangent problem for newton iterations
  void solveJac(Vector& step, const Vector& res, double& linear_rel_tol);

  //! files for lists of iterations and residuals per timestep 
  ofstream _out_iter;
  ofstream _out_res;
  
  //! the present time
  Real _time;

  //! level of recursion for Aztec (has a sens with FSI coupling)
  UInt _recur;

  friend class operFS;

};


//
//                                         IMPLEMENTATION
//
template<typename Mesh> VenantKirchhofSolver<Mesh>::
VenantKirchhofSolver(const GetPot& data_file, const RefFE& refFE, const QuadRule& Qr,
		     const QuadRule& bdQr, BC_Handler& BCh):
  ElasticStructureHandler<Mesh>(data_file,refFE,Qr,bdQr,BCh),
  DataNewton(data_file,"solid/newton"),
     _pattM_block(_dof),
     _pattM(_pattM_block,"diag"),
     _pattK(_dof,3),
     _M(_pattM),
     _Kl(_pattK), 
     _K(_pattK),
     _C(_pattK), 
     _J(_pattK),
     _elmatK(_fe.nbNode,nDimensions,nDimensions), 
     _elmatM(_fe.nbNode,nDimensions,nDimensions), 
     _elmatC(_fe.nbNode,nDimensions,nDimensions),
     _elvec(_fe.nbNode,nDimensions), 
     _dk_loc(_fe.nbNode,nDimensions), 
     _rhs(_dim), 
     _rhs_w(_dim),
     _rhsWithoutBC(_dim),
     _f(_dim),
     _dataAztec(data_file,"solid/aztec"), 
     _out_iter("out_iter_solid"),
     _out_res("out_res_solid"),
     _time(0.0),
     _recur(0) {
  
  cout << endl;
  cout << "O-  Displacement unknowns: " << _dim << endl; 
  cout << "O-  Computing mass and linear strain matrices... ";  
    
  Chrono chrono;
  chrono.start();
 
  // Matrices initialization 
  _M.zeros();
  _Kl.zeros();
  _C.zeros();
  // Number of displacement components  
  UInt nc=_d.nbcomp();
  
  //inverse of dt:
  Real dti2 = 2.0/(_dt*_dt);

  // Elementary computation and matrix assembling  
  // Loop on elements
  for(UInt i = 1; i <= _mesh.numVolumes(); i++){

    _fe.updateFirstDerivQuadPt(_mesh.volumeList(i));
    
    _elmatK.zero();
    _elmatM.zero();
   
    // stiffness
    stiff_strain(_mu,_elmatK,_fe);
    stiff_div   (0.5*_lambda,_elmatK,_fe);

    _elmatC.mat() = _elmatK.mat();

    // mass
    mass(dti2*_rho,_elmatM,_fe,0,0,nDimensions);

    _elmatC.mat() += _elmatM.mat();

    // assembling
    for(UInt ic=0;ic<nc;ic++){   
      for(UInt jc=0;jc<nc;jc++) {
	assemb_mat(_Kl,_elmatK,_fe,_dof,ic,jc);
	assemb_mat(_C,_elmatC,_fe,_dof,ic,jc);
      }
      
      //mass
      assemb_mat(_M,_elmatM,_fe,_dof,ic,ic);
    }
  }

  chrono.stop();
  cout << "done in " << chrono.diff() << " s." << endl;

}

template<typename Mesh>  
void VenantKirchhofSolver<Mesh>::
timeAdvance(const Function source, const Real& time) {

  UInt ig;

  _time = time; 
  
  cout << endl;
  cout << "O== SOLID: Now we are at time "<< _time << " s." << endl;

  cout << "  o-  Updating mass term on right hand side... ";
  
  Chrono chrono;
  chrono.start();

  _K = _Kl;
  
  // Number of displacement components  
  UInt nc=_d.nbcomp();

  if (_maxiter > 1 ) {
  
    // l`oop on volumes: assembling source term
    for(UInt i=1; i<=_mesh.numVolumes(); ++i){
      
      _fe.updateFirstDerivQuadPt(_mesh.volumeList(i));
      
      _elmatK.zero();   
      
      // _dk_loc contains the displacement in the nodes
      for (UInt j=0 ; j<(UInt)_fe.nbNode ; ++j) {
	for (UInt ic=0; ic<nc; ++ic){     
	  ig=_dof.localToGlobal(i,j+1)-1+ic*_dim;       
	  _dk_loc.vec()[j+ic*_fe.nbNode] = _d.vec()(ig); 
	}
      }
      
      // stiffness for non-linear terms 
      // 1/2 * \mu * ( [\grad d^k]^T \grad d : \grad v  ) 
      stiff_dergradbis( _mu*0.5, _dk_loc, _elmatK, _fe);
      
      // 1/4 * \lambda * ( \tr { [\grad d^k]^T \grad d }, \div v  ) 
      stiff_derdiv( _lambda*0.25, _dk_loc,_elmatK, _fe);
      
      for (UInt ic=0; ic<nc; ++ic){ 
	for(UInt jc=0;jc<nc;jc++) 
	  assemb_mat(_K,_elmatK,_fe,_dof,ic,jc);     
      }
    }
  }
  
  // Right hand side for the velocity at time
  _rhsWithoutBC.vec()=0.;
  
  // loop on volumes: assembling source term
  for(UInt i=1; i<=_mesh.numVolumes(); ++i){
	
    _fe.updateFirstDerivQuadPt(_mesh.volumeList(i));
    
    _elvec.zero();
          
    for (UInt ic=0; ic<nc; ++ic){ 
      compute_vec(source,_elvec,_fe,_time,ic); // compute local vector
      assemb_vec(_rhsWithoutBC.vec(),_elvec,_fe,_dof,ic); // assemble local vector into global one     
    }
  }
  
  // right hand side without boundary load terms
  _rhsWithoutBC.vec() += _M * ( _d.vec() + _dt * _w.vec() );
  _rhsWithoutBC.vec() -= _K * _d.vec();

  _rhs_w.vec() =  (2.0/_dt) * _d.vec()  +  _w.vec();
  
  //
  chrono.stop();
  cout << "done in " << chrono.diff() << " s." << endl;

}


template<typename Mesh>  
void VenantKirchhofSolver<Mesh>::
iterate() {

  int status;

  int maxiter = _maxiter;

  status = newton( _d.vec(), *this, maxnorm,_abstol, _reltol, maxiter, _etamax, (int)_linesearch, _out_res, _time);  

  if(status == 1) {
    cout << "Inners iterations failed\n";
    exit(1);
  }  
  else {
    cout << "Number of inner iterations       : " << maxiter << endl; 
    _out_iter << _time << " " << maxiter << endl; 
  }

  _w.vec() = (2.0/_dt) *  _d.vec() - _rhs_w.vec();

}
 


template<typename Mesh> 
void VenantKirchhofSolver<Mesh>::
showMe(ostream& c) const{
  DataElasticStructure<Mesh>::showMe(c); 
  c << "\n*** Values for data [solid/newton]\n\n";
  DataNewton::showMe(c);
} 

template<typename Mesh> 
void VenantKirchhofSolver<Mesh>::
evalResidual(Vector&res, const Vector& sol, int iter) {

  
  cout << "O-    Computing residual... ";  
  
 
  Chrono chrono;
  chrono.start();
  
  // Matrices initialization 
  _K = _C;
  
  if (_maxiter > 1 ) {
  
    UInt ig;
    
    // Number of displacement components  
    UInt nc=_d.nbcomp();
    
    // Elementary computation and matrix assembling  
    // Loop on elements
    for(UInt i = 1; i <= _mesh.numVolumes(); i++){
      
      _fe.updateFirstDerivQuadPt(_mesh.volumeList(i));
      
      _elmatK.zero();
      
      // _dk_loc contains the displacement in the nodes
      for (UInt j=0 ; j<(UInt)_fe.nbNode ; ++j) {
	for (UInt ic=0; ic<nc; ++ic){     
	  ig=_dof.localToGlobal(i,j+1)-1+ic*_dim;       
	  _dk_loc.vec()[j+ic*_fe.nbNode] = sol(ig); 
	}
      }
      // stiffness for non-linear terms 
      
      // 1/2 * \mu * ( [\grad d^k]^T \grad d : \grad v  ) 
      stiff_dergradbis( _mu*0.5, _dk_loc, _elmatK, _fe);
      
      // 1/4 * \lambda * ( \tr { [\grad d^k]^T \grad d }, \div v  ) 
      stiff_derdiv( _lambda*0.25, _dk_loc ,_elmatK, _fe);
      
      // assembling
      for(UInt ic=0;ic<nc;ic++) 
	for(UInt jc=0;jc<nc;jc++) 
	  assemb_mat(_K,_elmatK,_fe,_dof,ic,jc);    
    }
  }




  if ( !_BCh.bdUpdateDone() )  
    _BCh.bdUpdate(_mesh, _feBd, _dof);
  bc_manage_matrix(_K, _mesh, _dof, _BCh, _feBd,   1.0);
  
  _rhs.vec() = _rhsWithoutBC.vec();
  bc_manage_vector(_rhs.vec(), _mesh, _dof, _BCh, _feBd, _time, 1.0);
    
  res  = _K*sol - _rhs.vec();

  chrono.stop();
  cout << "done in " << chrono.diff() << " s." << endl;

}



template<typename Mesh> 
void VenantKirchhofSolver<Mesh>::
updateJac(Vector& sol,int iter) {


  cout << "    o-  Updating JACOBIAN in iter " << iter << endl << "  ... ";

  Chrono chrono;
  chrono.start();


  // copy of the linear part
  _J = _C;

  if (_maxiter > 1 ) {
  
    UInt ig;

    // Number of displacement components  
    UInt nc=_d.nbcomp();
    
    // loop on volumes: assembling source term
    for(UInt i=1; i<=_mesh.numVolumes(); ++i){

      _fe.updateFirstDerivQuadPt(_mesh.volumeList(i));
    
      _elmatK.zero();

      // _dk_loc contains the displacement in the nodes
      for (UInt j=0 ; j<(UInt)_fe.nbNode ; ++j) {
	for (UInt ic=0; ic<nc; ++ic){     
	  ig=_dof.localToGlobal(i,j+1)-1+ic*_dim;       
	  _dk_loc.vec()[j+ic*_fe.nbNode] = sol[ig]; 
	}
      }
    
      // stiffness for non-linear terms 
      // 1/2 * \mu * ( [\grad \delta d]^T \grad d^k + [\grad d^k]^T \grad \delta d : \grad v  )
      stiff_dergrad( _mu*0.5, _dk_loc, _elmatK, _fe);
      
      // 1/2 * \lambda * ( \tr { [\grad u^k]^T \grad u }, \div v  )
      stiff_derdiv( 0.5*_lambda, _dk_loc, _elmatK, _fe);
   
      // assembleing
      for (UInt ic=0; ic<nc; ++ic) 
	for(UInt jc=0; jc<nc; jc++) 
	  assemb_mat(_J,_elmatK,_fe,_dof,ic,jc);     
      
    }
    
  }
  
  chrono.stop();
  cout << "done in " << chrono.diff() << " s." << endl;

}




template<typename Mesh> 
void VenantKirchhofSolver<Mesh>::
solveJac(Vector& step, const Vector& res, double& linear_rel_tol){

 Chrono  chrono;


 _f.vec() = res;
  
  // for BC treatment (done at each time-step)
  Real tgv=1.0; 
  cout << "  o-  Applying boundary conditions... ";
  chrono.start(); 

  // BC manage for the velocity
  if ( !_BCh.bdUpdateDone() )  
    _BCh.bdUpdate(_mesh, _feBd, _dof);
 
  bc_manage_matrix(_J,  _mesh, _dof, _BCh, _feBd, tgv); 
  chrono.stop();
  cout << "done in " << chrono.diff() << "s." << endl;
 
  // AZTEC specifications for the first system
  int    data_org[AZ_COMM_SIZE];   // data organisation for C
  int    proc_config[AZ_PROC_SIZE];// Processor information:  
  int    options[AZ_OPTIONS_SIZE]; // Array used to select solver options.
  double params[AZ_PARAMS_SIZE];   // User selected solver paramters.
  double status[AZ_STATUS_SIZE];   // Information returned from AZ_solve()
                                     // indicating success or failure.
  AZ_set_proc_config(proc_config, AZ_NOT_MPI);

  //AZTEC matrix and preconditioner
  AZ_MATRIX *J;
  AZ_PRECOND *prec_J;

  int N_eq= 3*_dim; // number of DOF for each component
 // data_org assigned "by hands" while no parallel computation is performed
  data_org[AZ_N_internal]= N_eq;
  data_org[AZ_N_border]= 0;
  data_org[AZ_N_external]= 0;
  data_org[AZ_N_neigh]= 0;
  data_org[AZ_name]= 0;

  // create matrix and preconditionner 
  J= AZ_matrix_create(N_eq);
  prec_J= AZ_precond_create(J, AZ_precondition, NULL);

  AZ_set_MSR(J, (int*) _pattK.giveRaw_bindx(), (double*) _J.giveRaw_value(), data_org, 0, NULL, AZ_LOCAL);

  _dataAztec.aztecOptionsFromDataFile(options,params); 

  options[AZ_recursion_level]=_recur;


  //keep  factorisation and preconditioner reused in my_matvec
  // options_i[AZ_keep_info]= 1;       

  //params[AZ_tol]       = linear_rel_tol;

  cout << "  o-  Solving system...  "; 
  chrono.start();
  AZ_iterate(&step[0], _f.giveVec(), options, params, status,
  	     proc_config, J, prec_J, NULL);
  chrono.stop();
  cout << "done in " << chrono.diff() << " s." << endl;
  
  //--options[AZ_recursion_level];

  AZ_matrix_destroy(&J);
  AZ_precond_destroy(&prec_J);

}



 
#endif