//  non linear elasticity model 
//   
//  -------------------------------
//  with huge utilisation of  macro  new version
// more simple 
// ---------------------------
//   optimize version 
// ------------
//  problem is  find $(uu,vn)$  minimizing  $J$
//  $ min J(un,vn) = \int 1/2 (F2) -  int Pa * un $
//   $ dJ(u,u,uu,vv) = int dF2(u,v,uu,vv) df(F2(u,v)) $
//   where $F2 =  (^t {E}  A {E} ) $,
//   $E(U) =  1/2 (\nabla U + \nabla U^t + \nabla U^t  \nabla U) $
//         ($u_1$)
//  with U=(   )
//         ($u_2$)
// so: 
//$$ E_ij = 0.5 ( d_i u_j + d_j u_i ) + \sum_k d_i u_k * d_j*u_k  \leqno(1)$$
//  the symetric tensor $t_{ij}$ are a vector  [t11,2*t12,t22] 
// date from
/*
Proposal for numerical benchmarking of fluid-structure interaction between an elastic object and laminar incompressible flow

Stefan Turek and Jaroslav Hron⋆
Institute for Applied Mathematics and Numerics, University of Dortmund,
Vogelpothsweg 87, 44227 Dortmund, Germany

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.550.1689&rep=rep1&type=pdf

section 4.2 CSM test: 
*/

macro EL(u,v) [dx(u),(dx(v)+dy(u)),dy(v)] // is $[\epsilon_{11},2\epsilon_{12},\epsilon_{22}]$

macro ENL(u,v) [ 
(dx(u)*dx(u)+dx(v)*dx(v))*0.5,
(dx(u)*dy(u)+dx(v)*dy(v))    ,
(dy(u)*dy(u)+dy(v)*dy(v))*0.5 ] // EOM ENL 

macro dENL(u,v,uu,vv) [(dx(u)*dx(uu)+dx(v)*dx(vv)),
 (dx(u)*dy(uu)+dx(v)*dy(vv)+dx(uu)*dy(u)+dx(vv)*dy(v)),
 (dy(u)*dy(uu)+dy(v)*dy(vv)) ] // 
 
 
macro E(u,v) (EL(u,v)+ENL(u,v)) // is $[\E{11},\sqrt2\E{12},\E{22}]
macro dE(u,v,uu,vv) (EL(uu,vv)+dENL(u,v,uu,vv)) //
macro ddE(u,v,uu,vv,uuu,vvv) dENL(uuu,vvv,uu,vv) //
macro F2(u,v) (E(u,v)'*A*E(u,v)) // 
macro dF2(u,v,uu,vv)  (E(u,v)'*A*dE(u,v,uu,vv) ) //
macro ddF2(u,v,uu,vv,uuu,vvv) (
            (dE(u,v,uu,vv)'*A*dE(u,v,uuu,vvv)) 
          + (E(u,v)'*A*ddE(u,v,uu,vv,uuu,vvv))  )// EOM

//  for hyper elasticity problem 
//  -----------------------------
macro f(u) (u) // end of macro
macro df(u) (1) // end of macro
macro ddf(u) (0) // end of macro

// -------------------------------
//  
//   $  \sigma = 2 \mu E + \lambda tr(E) Id $
//   $   A(u,v)= \sigma(u):\E(v) $
//   
//   ( a b )
//   ( b c )
//
//  tr*Id : (a,b,c) -> (a+c,0,a+c) 
// so the associed matrix is:
//   ( 1 0 1 )
//   ( 0 0 0 )
//   ( 1 0 1 ) 
// ------------------
verbosity=0;

// data CMS 1 
real xp=0.35,yp=0.0;//
real upbench= -0.007187, vpbench= -0.066;

real EE = 1.4e6;
real rho = 1000;
real sigma = 0.4;
real mu = EE/(2*(1+sigma));
real lambda = EE*sigma/((1+sigma)*(1-2*sigma));
real gravity = -2;
int nnn = 10;

mesh Th = square(17*nnn,nnn,[0.35*x,0.02*(y-0.5)]);
cout<<" Th nt : " << Th.nt<<endl;
int bottom=1, right=2,upper=3,left=4;

plot(Th);

real a11= 2*mu +  lambda  ;
real a22= mu ; //  because [0,2*t12,0]' A [0,2*s12,0]  = 2*mu*(t12*s12+t21*s21) = 4*mu*t12*s12
real a33= 2*mu +   lambda ;
real a12= 0 ;
real a13= lambda ;
real a23= 0 ;
//  symetric part
real a21= a12 ;
real a31= a13 ;
real a32= a23 ;
func A = [ [ a11,a12,a13],[ a21,a22,a23],[ a31,a32,a33] ];  





 
fespace Vh(Th,[P2,P2]);






Vh [uu,vv], [w,s],[un,vn];
[un,vn]=[0,0];//  intialisation 
[uu,vv]=[0,0];



real errb=0; 
// Newton's method
// ---------------
for (int i=0;i<10;i++)
{
  cout << "Loop " << i << endl;
  solve NonLin([uu,vv],[w,s])=
    int2d(Th)(        ddF2(un,vn,uu,vv,w,s)  )
     - int2d(Th)(  dF2(un,vn,w,s) - rho*gravity*s)    
     + on(left,uu=0,vv=0);
  ;  
  real res = uu[].linfty ; //  norme  L^2 of [uu,vv]
  errb = max(abs(un(xp,yp)-upbench) , abs(vn(xp,yp)-vpbench));

  un[] -= uu[]; 
  cout << "     Linfty residual = " << res << endl;
  cout << "     u of A =" << un(xp,yp) << " " << vn(xp,yp)  <<  " bench " << upbench  << " "<< vpbench << " err bench =" << errb << endl; 
  plot(movemesh(Th, [x+un, y+vn]),wait=1,cmm=" i =" +i + " err =" + res);

  if (res<1e-10) break;
}

cout << " err bench = " << errb << endl;  
//plot([un,vn],wait=1);
mesh th1 = movemesh(Th, [x+un, y+vn]);
plot(th1,wait=1,ps="nl-elas.eps");
assert( errb < 1e-2);
