Trilinos based (stochastic) FEM solvers
nrl_PCA_Likelihood.hpp
Go to the documentation of this file.
1 /*
2 Brian Staber (brian.staber@gmail.com)
3 */
4 
5 #ifndef NRL_PCA_LIKELIHOOD
6 #define NRL_PCA_LIKELIHOOD
7 
8 #include <math.h>
10 #include "newtonRaphson.hpp"
11 #include "distributenrldata.hpp"
12 #include <boost/random/mersenne_twister.hpp>
13 #include <boost/random/normal_distribution.hpp>
14 #include <boost/random/uniform_real_distribution.hpp>
15 #include <boost/math/special_functions/gamma.hpp>
16 
18 {
19 private:
20 
21  Teuchos::ParameterList _paramList;
22  Teuchos::RCP<newtonRaphson> newton;
23  std::string fullOutputPath;
24 
25  Epetra_Comm * comm;
26  Epetra_Map * MapExpPoints;
27  Epetra_SerialDenseVector lb, ub;
28 
29 public:
30 
31  Teuchos::RCP<tiMooneyRandomField> interface;
32  Teuchos::RCP<distributenrldata> nrldata;
33 
34  nrl_PCA_Likelihood(Epetra_Comm & Comm, Teuchos::ParameterList & paramList){
35  comm = &Comm;
36  _paramList = paramList;
37  std::string pathnrl = Teuchos::getParameter<std::string>(paramList.sublist("nrldata"),"pathnrl");
38  std::string station = Teuchos::getParameter<std::string>(paramList.sublist("nrldata"),"station");
39  fullOutputPath = "/home/s/staber/Trilinos_results/nrl/random_generator_for_pca_likelihood/" + station + "/";
40  interface = Teuchos::rcp(new tiMooneyRandomField(Comm,paramList));
41  newton = Teuchos::rcp(new newtonRaphson(*interface,paramList));
42  nrldata = Teuchos::rcp(new distributenrldata(*interface->Mesh,pathnrl));
43 
44  MapExpPoints = new Epetra_Map(-1,nrldata->global_id_faces.size(),&nrldata->global_id_faces[0],0,Comm);
45 
46  }
47 
49  }
50 
51  int rnd(unsigned int & nmc ,
52  Epetra_IntSerialDenseVector & seeds ,
53  Epetra_SerialDenseVector & mean_parameters ,
54  Epetra_SerialDenseVector & exponents ,
55  Epetra_SerialDenseVector & correlation_lengths ,
56  Epetra_SerialDenseVector & coeff_of_variation ,
57  double & plyagl_deg ,
58  bool printNewtonIterations,
59  bool printDisplacements ,
60  bool printDeformations ,
61  bool printRandomVariableY)
62  {
63  Epetra_SerialDenseVector omega(6);
64  omega(0) = coeff_of_variation(0);
65  omega(1) = coeff_of_variation(1);
66  omega(2) = coeff_of_variation(2);
67  omega(3) = coeff_of_variation(3);
68  omega(4) = correlation_lengths(0);
69  omega(5) = correlation_lengths(1);
70 
71  double plyagl = plyagl_deg*2.0*M_PI/360.0;
72  interface->setParameters(mean_parameters,exponents,omega);
73  interface->set_plyagl(plyagl);
74  interface->RandomFieldGenerator(seeds);
75 
76  Epetra_Vector RandomVariableX(*MapExpPoints);
77  RandomVariableX.PutScalar(0.0);
78 
79  int error;
80  newton->Initialization();
81  for (unsigned int i=0; i<nrldata->boundaryconditions.Length(); ++i){
82  newton->setParameters(_paramList);
83  if(i==0){
84  newton->bc_disp=nrldata->boundaryconditions(i);
85  }
86  else{
87  newton->bc_disp=nrldata->boundaryconditions(i)-nrldata->boundaryconditions(i-1);
88  }
89 
90  error = newton->Solve_with_Aztec(printNewtonIterations);
91 
92  if (!error){
93 
94  if (printDisplacements){
95  std::string path = fullOutputPath + "u_nmc=" + std::to_string(nmc) + "_angle=" + std::to_string(int(plyagl_deg)) + "_k=" + std::to_string(i) + ".mtx";
96  int flag = newton->print_newton_solution(path);
97  if (flag){
98  if (comm->MyPID()==0){
99  std::cout << "Failed printing displacements.";
100  }
101  }
102  }
103 
104  if (printDeformations){
105  std::string path = fullOutputPath + "e_nmc" + std::to_string(nmc) + "_angle=" + std::to_string(int(plyagl_deg)) + "_k=" + std::to_string(i) + ".mtx";
106  double xi = 0.0;
107  int flag = interface->compute_green_lagrange(*newton->x,xi,xi,xi,path);
108  if (flag){
109  if (comm->MyPID()==0){
110  std::cout << "Failed printing deformations.";
111  }
112  }
113  }
114 
115  Epetra_SerialDenseMatrix eij(nrldata->local_id_faces.size(),3);
116  compute_green_lagrange(*newton->x,eij);
117 
118  for (unsigned int j=0; j<nrldata->local_id_faces.size(); ++j){
119  RandomVariableX[j] += eij(j,0)*eij(j,0)+eij(j,1)*eij(j,1)+2.0*eij(j,2)*eij(j,2);
120  }
121  }
122  else{
123  if (comm->MyPID()==0){
124  std::cout << "Newton failed at loading " << i << ".\n";
125  std::cout << "GIndicator(" << i << ") set to 0.0.\n";
126  }
127  error = 1;
128  break;
129  }
130  }
131  if (printRandomVariableY && !error){
132  std::string pathToRandomVariableX = fullOutputPath + "RandomVariableY_angle=" + std::to_string(int(plyagl_deg)) + "_nmc=" + std::to_string(nmc) + ".mtx";
133  int error = printIndicatorY(pathToRandomVariableX, RandomVariableX);
134  return error;
135  }
136  else{
137  return error;
138  }
139  }
140 
141  int printIndicatorY(std::string filename, Epetra_Vector & X){
142  int NumTargetElements = 0;
143  if (comm->MyPID()==0){
144  NumTargetElements = nrldata->npoints;
145  }
146  Epetra_Map MapOnRoot(-1,NumTargetElements,0,*comm);
147  Epetra_Export ExportOnRoot(*MapExpPoints,MapOnRoot);
148  Epetra_MultiVector lhs_root(MapOnRoot,true);
149  lhs_root.Export(X,ExportOnRoot,Insert);
150 
151  int error = EpetraExt::MultiVectorToMatrixMarketFile(filename.c_str(),lhs_root,0,0,false);
152  return error;
153  }
154 
155  void compute_green_lagrange(Epetra_Vector & x, Epetra_SerialDenseMatrix & eij){
156  Epetra_Vector u(*(interface->OverlapMap));
157  u.Import(x, *(interface->ImportToOverlapMap), Insert);
158 
159  int e_gid, node;
160  double det_jac;
161  Epetra_SerialDenseMatrix matrix_X(2,interface->Mesh->face_type);
162  Epetra_SerialDenseMatrix matrix_x(2,interface->Mesh->face_type);
163  Epetra_SerialDenseMatrix D(interface->Mesh->face_type,2);
164  Epetra_SerialDenseMatrix dx_shape_functions(interface->Mesh->face_type,2);
165  Epetra_SerialDenseMatrix deformation_gradient(2,2);
166  Epetra_SerialDenseMatrix JacobianMatrix(2,2);
167  Epetra_SerialDenseMatrix InverseJacobianMatrix(2,2);
168  Epetra_SerialDenseMatrix right_cauchy(2,2);
169 
170  for (unsigned int e=0; e<nrldata->local_id_faces.size(); ++e){
171  e_gid = interface->Mesh->local_faces[nrldata->local_id_faces[e]];
172  for (unsigned int inode=0; inode<interface->Mesh->face_type; ++inode){
173  node = interface->Mesh->faces_nodes[interface->Mesh->face_type*e_gid+inode];
174  matrix_X(0,inode) = interface->Mesh->nodes_coord[3*node+0];
175  matrix_X(1,inode) = interface->Mesh->nodes_coord[3*node+1];
176  matrix_x(0,inode) = u[interface->OverlapMap->LID(3*node+0)] + interface->Mesh->nodes_coord[3*node+0];
177  matrix_x(1,inode) = u[interface->OverlapMap->LID(3*node+1)] + interface->Mesh->nodes_coord[3*node+1];
178  }
179  switch (interface->Mesh->face_type){
180  case 3:
181  tri3::d_shape_functions(D, nrldata->local_xi[e], nrldata->local_eta[e]);
182  break;
183  case 4:
184  quad4::d_shape_functions(D, nrldata->local_xi[e], nrldata->local_eta[e]);
185  break;
186  case 6:
187  tri6::d_shape_functions(D, nrldata->local_xi[e], nrldata->local_eta[e]);
188  break;
189  }
190  jacobian_matrix(matrix_X,D,JacobianMatrix);
191  det_jac = fabs(JacobianMatrix(0,0)*JacobianMatrix(1,1) - JacobianMatrix(1,0)*JacobianMatrix(0,1));
192  InverseJacobianMatrix(0,0) = (1.0/det_jac)*JacobianMatrix(1,1);
193  InverseJacobianMatrix(1,1) = (1.0/det_jac)*JacobianMatrix(0,0);
194  InverseJacobianMatrix(0,1) = -(1.0/det_jac)*JacobianMatrix(0,1);
195  InverseJacobianMatrix(1,0) = -(1.0/det_jac)*JacobianMatrix(1,0);
196  dx_shape_functions.Multiply('N','N',1.0,D,InverseJacobianMatrix,0.0);
197 
198  deformation_gradient.Multiply('N','N',1.0,matrix_x,dx_shape_functions,0.0);
199  right_cauchy.Multiply('T','N',1.0,deformation_gradient,deformation_gradient,0.0);
200 
201  eij(e,0) = (1.0/2.0)*(right_cauchy(0,0)-1.0);
202  eij(e,1) = (1.0/2.0)*(right_cauchy(1,1)-1.0);
203  eij(e,2) = (1.0/2.0)*right_cauchy(0,1);
204  }
205  }
206 
207 };
208 #endif
for j
Definition: costFunction.m:42
u
Definition: run.m:9
void d_shape_functions(Epetra_SerialDenseMatrix &D, double &xi, double &eta)
Definition: fepp.cpp:34
nrl_PCA_Likelihood(Epetra_Comm &Comm, Teuchos::ParameterList &paramList)
Teuchos::RCP< tiMooneyRandomField > interface
void compute_green_lagrange(Epetra_Vector &x, Epetra_SerialDenseMatrix &eij)
optimParameters nmc
filename
Definition: costFunction.m:44
Teuchos::RCP< distributenrldata > nrldata
int printIndicatorY(std::string filename, Epetra_Vector &X)
int rnd(unsigned int &nmc, Epetra_IntSerialDenseVector &seeds, Epetra_SerialDenseVector &mean_parameters, Epetra_SerialDenseVector &exponents, Epetra_SerialDenseVector &correlation_lengths, Epetra_SerialDenseVector &coeff_of_variation, double &plyagl_deg, bool printNewtonIterations, bool printDisplacements, bool printDeformations, bool printRandomVariableY)
void d_shape_functions(Epetra_SerialDenseMatrix &D, double &xi, double &eta)
Definition: fepp.cpp:12
for i
Definition: costFunction.m:38
e
Definition: run.m:10
void jacobian_matrix(Epetra_SerialDenseMatrix &X, Epetra_SerialDenseMatrix &D, Epetra_SerialDenseMatrix &JacobianMatrix)
Definition: fepp.cpp:171
void d_shape_functions(Epetra_SerialDenseMatrix &D, double &xi, double &eta)
Definition: fepp.cpp:61
optimParameters station