Trilinos based (stochastic) FEM solvers
linearPatchTest.hpp
Go to the documentation of this file.
1 /*
2 Brian Staber (brian.staber@gmail.com)
3 */
4 
5 #ifndef LINEARPATCHTEST_HPP
6 #define LINEARPATCHTEST_HPP
7 
9 
11 {
12 public:
13 
14  Teuchos::ParameterList * Krylov;
15 
16  linearPatchTest(Epetra_Comm & comm, Teuchos::ParameterList & Parameters){
17 
18  Krylov = &Parameters.sublist("Krylov");
19 
20  std::string mesh_file = Teuchos::getParameter<std::string>(Parameters.sublist("Mesh"), "mesh_file");
21  Mesh = new mesh(comm, mesh_file, 1.0);
22  Comm = Mesh->Comm;
23 
24  StandardMap = new Epetra_Map(-1,3*Mesh->n_local_nodes_without_ghosts,&Mesh->local_dof_without_ghosts[0],0,*Comm);
25  OverlapMap = new Epetra_Map(-1,3*Mesh->n_local_nodes,&Mesh->local_dof[0],0,*Comm);
26  ImportToOverlapMap = new Epetra_Import(*OverlapMap,*StandardMap);
28 
30  }
31 
33  }
34 
35  Epetra_SerialDenseVector get_neumannBc(Epetra_SerialDenseMatrix & matrix_X, Epetra_SerialDenseMatrix & xg, unsigned int & gp){
36  std::cout << "Not using this method in this application.\n";
37  Epetra_SerialDenseVector f(3);
38  return f;
39  }
40  Epetra_SerialDenseVector get_forcing(double & x1, double & x2, double & x3, unsigned int & e_lid, unsigned int & gp){
41  std::cout << "Not using this method in this application.\n";
42  Epetra_SerialDenseVector f(3);
43  return f;
44  }
45 
46  double solve(bool doprint){
47  Epetra_FECrsMatrix linearOperator(Copy,*FEGraph);
48  Epetra_FEVector rhs(*StandardMap);
49  Epetra_Vector lhs(*StandardMap);
50  rhs.PutScalar(0.0);
51  double dummy = 0.0;
53  apply_dirichlet_conditions(linearOperator,rhs,dummy);
54  aztecSolver(linearOperator,rhs,lhs,*Krylov);
55  if (doprint){
56  print_solution(lhs,"/Users/brian/Documents/GitHub/Trilinos_results/cee530/linearpatchtest/linearPatchTest.mtx");
57  }
58  double error = errorL2(lhs);
59  return error;
60  }
61 
62  double errorL2(Epetra_Vector & uStandardMap){
63 
64  Epetra_Vector u(*OverlapMap);
65  u.Import(uStandardMap, *ImportToOverlapMap, Insert);
66  double totalError;
67  double error = 0.0;
68  double normVH;
69  double gauss_weight;
70  int n_gauss_points = Mesh->n_gauss_cells;
71  int e_gid, node;
72 
73  //Epetra_SerialDenseVector epsilon(6);
74  //Epetra_SerialDenseMatrix matrix_B(6,3*Mesh->el_type);
75  Epetra_SerialDenseVector uExact(3), vH(3);
76  Epetra_SerialDenseMatrix dx_shape_functions(Mesh->el_type,3), X_I(3,Mesh->el_type), u_I(3,Mesh->el_type);
77  Epetra_SerialDenseMatrix u_G(3,n_gauss_points), x_G(3,n_gauss_points);
78 
79  for (unsigned int e_lid=0; e_lid<Mesh->n_local_cells; ++e_lid){
80  e_gid = Mesh->local_cells[e_lid];
81  for (unsigned int inode=0; inode<Mesh->el_type; ++inode){
82  node = Mesh->cells_nodes[Mesh->el_type*e_gid+inode];
83  X_I(0,inode) = Mesh->nodes_coord[3*node+0];
84  X_I(1,inode) = Mesh->nodes_coord[3*node+1];
85  X_I(2,inode) = Mesh->nodes_coord[3*node+2];
86  u_I(0,inode) = u[OverlapMap->LID(3*node+0)];
87  u_I(1,inode) = u[OverlapMap->LID(3*node+1)];
88  u_I(2,inode) = u[OverlapMap->LID(3*node+2)];
89  }
90  x_G.Multiply('N','N',1.0,X_I,Mesh->N_cells,0.0);
91  u_G.Multiply('N','N',1.0,u_I,Mesh->N_cells,0.0);
92  for (unsigned int gp=0; gp<n_gauss_points; ++gp){
93  gauss_weight = Mesh->gauss_weight_cells(gp);
94  uExact = exactSolution(x_G(0,gp),x_G(1,gp),x_G(2,gp));
95  vH(0) = uExact(0) - u_G(0,gp);
96  vH(1) = uExact(1) - u_G(1,gp);
97  vH(2) = uExact(2) - u_G(2,gp);
98  normVH = vH.Norm2();
99  /*for (unsigned int inode=0; inode<Mesh->el_type; ++inode){
100  dx_shape_functions(inode,0) = Mesh->DX_N_cells(gp+n_gauss_points*inode,e_lid);
101  dx_shape_functions(inode,1) = Mesh->DY_N_cells(gp+n_gauss_points*inode,e_lid);
102  dx_shape_functions(inode,2) = Mesh->DZ_N_cells(gp+n_gauss_points*inode,e_lid);
103  }
104  compute_B_matrices(dx_shape_functions,matrix_B);
105  epsilon.Multiply('N','N',1.0,matrix_B,vector_u,0.0);*/
106  error += gauss_weight*normVH*normVH*Mesh->detJac_cells(e_lid,gp);
107  }
108  }
109  Comm->SumAll(&error,&totalError,1);
110  totalError = std::sqrt(totalError);
111  return totalError;
112  }
113 
114  Epetra_SerialDenseVector exactSolution(double & x1, double & x2, double & x3){
115  Epetra_SerialDenseVector u(3);
116  u(0) = 0.1*x1+0.2*x2+0.4*x3;
117  u(1) = 0.4*x1+0.5*x2+0.1*x3;
118  u(2) = 0.05*x1+0.25*x2+0.65*x3;
119  return u;
120  }
121 
123  n_bc_dof = 0;
124  double x,y,z;
125  unsigned int node;
126  for (unsigned int i=0; i<Mesh->n_local_nodes_without_ghosts; ++i){
127  node = Mesh->local_nodes[i];
128  x = Mesh->nodes_coord[3*node+0];
129  y = Mesh->nodes_coord[3*node+1];
130  z = Mesh->nodes_coord[3*node+2];
131  if(x==0||y==0||z==0||x==1.0||y==1.0||z==1.0){
132  n_bc_dof+=3;
133  }
134  }
135 
136  int indbc = 0;
137  dof_on_boundary = new int [n_bc_dof];
138  for (unsigned int inode=0; inode<Mesh->n_local_nodes_without_ghosts; ++inode){
139  node = Mesh->local_nodes[inode];
140  x = Mesh->nodes_coord[3*node+0];
141  y = Mesh->nodes_coord[3*node+1];
142  z = Mesh->nodes_coord[3*node+2];
143  if(x==0||y==0||z==0||x==1.0||y==1.0||z==1.0){
144  dof_on_boundary[indbc+0] = 3*inode+0;
145  dof_on_boundary[indbc+1] = 3*inode+1;
146  dof_on_boundary[indbc+2] = 3*inode+2;
147  indbc+=3;
148  }
149  }
150  }
151 
152  void apply_dirichlet_conditions(Epetra_FECrsMatrix & K, Epetra_FEVector & F, double & displacement){
153  Epetra_MultiVector v(*StandardMap,true);
154  v.PutScalar(0.0);
155 
156  int node;
157  double x,y,z;
158  for (unsigned int inode=0; inode<Mesh->n_local_nodes_without_ghosts; ++inode){
159  node = Mesh->local_nodes[inode];
160  x = Mesh->nodes_coord[3*node+0];
161  y = Mesh->nodes_coord[3*node+1];
162  z = Mesh->nodes_coord[3*node+2];
163  if (x==0||y==0||z==0||x==1.0||y==1.0||z==1.0){
164  Epetra_SerialDenseVector u(3);
165  u = exactSolution(x,y,z);
166  v[0][StandardMap->LID(3*node+0)] = u(0); //0.1*(0.1*x + 0.08*y + 0.05*z + 0.04*x*y + 0.03*y*z + 0.08*z*x);
167  v[0][StandardMap->LID(3*node+1)] = u(1); //x*y; //y*x; //0.1*(0.05*x + 0.04*y + 0.1*z + 0.07*x*y + 0.03*y*z + 0.08*z*x);
168  v[0][StandardMap->LID(3*node+2)] = u(2); //x*y*z; //x*y*z; //0.1*(0.75*x + 0.09*y + 0.06*z + 0.07*x*y + 0.03*y*z + 0.08*z*x);
169  }
170  }
171 
172  Epetra_MultiVector rhs_dir(*StandardMap,true);
173  K.Apply(v,rhs_dir);
174  F.Update(-1.0,rhs_dir,1.0);
175 
176  for (unsigned int inode=0; inode<Mesh->n_local_nodes_without_ghosts; ++inode){
177  node = Mesh->local_nodes[inode];
178  x = Mesh->nodes_coord[3*node+0];
179  y = Mesh->nodes_coord[3*node+1];
180  z = Mesh->nodes_coord[3*node+2];
181  if (x==0||y==0||z==0||x==1.0||y==1.0||z==1.0){
182  F[0][StandardMap->LID(3*node+0)] = v[0][StandardMap->LID(3*node+0)];
183  F[0][StandardMap->LID(3*node+1)] = v[0][StandardMap->LID(3*node+1)];
184  F[0][StandardMap->LID(3*node+2)] = v[0][StandardMap->LID(3*node+2)];
185  }
186  }
187  ML_Epetra::Apply_OAZToMatrix(dof_on_boundary,n_bc_dof,K);
188  }
189 
190  void get_elasticity_tensor(unsigned int & e_lid, unsigned int & gp, Epetra_SerialDenseMatrix & tangent_matrix){
191  int e_gid = Mesh->local_cells[e_lid];
192  int n_gauss_cells = Mesh->n_gauss_cells;
193  double c1 = 144.8969*1.0e3;
194  double c2 = 14.2500*1.0e3;
195  double c3 = 5.8442*1.0e3;
196  double c4 = 7.5462*1.0e3;
197  double c5 = 12.5580*1.0e3;
198  transverse_isotropic_matrix(tangent_matrix,c1,c2,c3,c4,c5);
199  }
200 
201  void transverse_isotropic_matrix(Epetra_SerialDenseMatrix & C, double & c1, double & c2, double & c3, double & c4, double & c5){
202  C(0,0) = c1/16.0 + (9.0*c2)/32.0 + (9.0*c4)/32.0 + (3.0*c5)/8.0 + (3.0*std::sqrt(2.0)*c3)/16.0;
203  C(0,1) = (3.0*c1)/16.0 + (3.0*c2)/32.0 + (3.0*c4)/32.0 - (3.0*c5)/8.0 + (5.0*std::sqrt(2.0)*c3)/16.0;
204  C(0,2) = (3.0*c2)/8.0 - (3.0*c4)/8.0 + (std::sqrt(2.0)*c3)/8.0;
205  C(0,3) = 0.0;
206  C(0,4) = 0.0;
207  C(0,5) = std::sqrt(6)*(c1/16.0 - (3.0*c2)/32.0 - (3.0*c4)/32.0 + c5/8.0 + (std::sqrt(2.0)*c3)/16.0);
208 
209  C(1,0) = (3.0*c1)/16.0 + (3.0*c2)/32.0 + (3.0*c4)/32.0 - (3.0*c5)/8.0 + (5*std::sqrt(2.0)*c3)/16.0;
210  C(1,1) = (9.0*c1)/16.0 + c2/32.0 + c4/32.0 + (3.0*c5)/8.0 + (3.0*std::sqrt(2.0)*c3)/16.0;
211  C(1,2) = c2/8.0 - c4/8.0 + (3.0*std::sqrt(2.0)*c3)/8.0;
212  C(1,3) = 0.0;
213  C(1,4) = 0.0;
214  C(1,5) = -std::sqrt(6.0)*(c2/32.0 - (3.0*c1)/16.0 + c4/32.0 + c5/8.0 + (std::sqrt(2.0)*c3)/16.0);
215 
216  C(2,0) = (3.0*c2)/8.0 - (3.0*c4)/8.0 + std::sqrt(2.0)*c3/8.0;
217  C(2,1) = c2/8.0 - c4/8.0 + (3.0*std::sqrt(2.0)*c3)/8.0;
218  C(2,2) = c2/2.0 + c4/2.0;
219  C(2,3) = 0.0;
220  C(2,4) = 0.0;
221  C(2,5) = (std::sqrt(3.0)*(2.0*c3 - std::sqrt(2.0)*c2 + std::sqrt(2.0)*c4))/8.0;
222 
223  C(3,0) = 0.0;
224  C(3,1) = 0.0;
225  C(3,2) = 0.0;
226  C(3,3) = c4/4.0 + (3.0*c5)/4.0;
227  C(3,4) = -(std::sqrt(3.0)*(c4 - c5))/4.0;
228  C(3,5) = 0.0;
229 
230  C(4,0) = 0.0;
231  C(4,1) = 0.0;
232  C(4,2) = 0.0;
233  C(4,3) = -(std::sqrt(3.0)*(c4 - c5))/4.0;
234  C(4,4) = (3.0*c4)/4.0 + c5/4.0;
235  C(4,5) = 0.0;
236 
237  C(5,0) = std::sqrt(6.0)*(c1/16.0 - (3.0*c2)/32.0 - (3.0*c4)/32.0 + c5/8.0 + std::sqrt(2.0)*c3/16.0);
238  C(5,1) = -std::sqrt(6.0)*(c2/32.0 - (3.0*c1)/16.0 + c4/32.0 + c5/8.0 + (std::sqrt(2.0)*c3)/16.0);
239  C(5,2) = (std::sqrt(3.0)*(2.0*c3 - std::sqrt(2.0)*c2 + std::sqrt(2.0)*c4))/8.0;
240  C(5,3) = 0.0;
241  C(5,4) = 0.0;
242  C(5,5) = (3.0*c1)/8.0 + (3.0*c2)/16.0 + (3.0*c4)/16.0 + c5/4.0 - (3.0*std::sqrt(2.0)*c3)/8.0;
243  }
244 
245  void get_elasticity_tensor_for_recovery(unsigned int & e_lid, Epetra_SerialDenseMatrix & tangent_matrix){
246  }
247 };
248 
249 
250 #endif
Epetra_Comm * Comm
u
Definition: run.m:9
int print_solution(Epetra_Vector &solution, std::string fileName)
std::vector< int > local_nodes
Definition: meshpp.hpp:80
Teuchos::ParameterList * Krylov
std::vector< double > nodes_coord
Definition: meshpp.hpp:77
Epetra_SerialDenseVector get_neumannBc(Epetra_SerialDenseMatrix &matrix_X, Epetra_SerialDenseMatrix &xg, unsigned int &gp)
Epetra_SerialDenseVector N_cells
Definition: meshpp.hpp:73
int n_local_cells
Definition: meshpp.hpp:94
double solve(bool doprint)
void transverse_isotropic_matrix(Epetra_SerialDenseMatrix &C, double &c1, double &c2, double &c3, double &c4, double &c5)
Epetra_SerialDenseVector detJac_cells
Definition: meshpp.hpp:73
Epetra_FECrsGraph * FEGraph
std::vector< int > local_cells
Definition: meshpp.hpp:81
clc clear all close all mesh
Definition: run.m:5
int n_local_nodes_without_ghosts
Definition: meshpp.hpp:92
void setup_dirichlet_conditions()
int el_type
Definition: meshpp.hpp:90
void get_elasticity_tensor(unsigned int &e_lid, unsigned int &gp, Epetra_SerialDenseMatrix &tangent_matrix)
void get_elasticity_tensor_for_recovery(unsigned int &e_lid, Epetra_SerialDenseMatrix &tangent_matrix)
std::vector< int > cells_nodes
Definition: meshpp.hpp:78
void assemblePureDirichlet_homogeneousForcing(Epetra_FECrsMatrix &K)
Epetra_Import * ImportToOverlapMap
Epetra_Map * StandardMap
unsigned int n_gauss_cells
Definition: meshpp.hpp:98
Epetra_SerialDenseVector get_forcing(double &x1, double &x2, double &x3, unsigned int &e_lid, unsigned int &gp)
for i
Definition: costFunction.m:38
Epetra_Map * OverlapMap
void aztecSolver(Epetra_FECrsMatrix &A, Epetra_FEVector &b, Epetra_Vector &u, Teuchos::ParameterList &paramList)
Epetra_SerialDenseVector gauss_weight_cells
Definition: meshpp.hpp:99
void apply_dirichlet_conditions(Epetra_FECrsMatrix &K, Epetra_FEVector &F, double &displacement)
Epetra_SerialDenseVector exactSolution(double &x1, double &x2, double &x3)
double errorL2(Epetra_Vector &uStandardMap)
linearPatchTest(Epetra_Comm &comm, Teuchos::ParameterList &Parameters)