Debian Codegraph @

packages/trilinoscouplings/examples/scaling/example_Darcy.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Trilinos: An Object-Oriented Solver Framework
4 //
5 // Copyright 2001-2024 NTESS and the Trilinos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 /** \file example_DivLSFEM.cpp
11 \brief Example solution of a div-curl system on a hexahedral mesh using
12 div-conforming (face) elements.
13
14 This example uses Pamgen to generate a hexahedral mesh, Intrepid to
15 build mass and stiffness matrices, and ML to solve.
16
17 \verbatim
18
19 System
20
21 div v + \phi = f in Omega
22 v+ A grad \phi = 0 in Omega
23 Dirichlet BC: \phi given on Gamma
24
25 Omega is the box [0,1]^3
26
27 where f is derived from a prescribed solution
28
29 \phi(x,y,z)=-sin^2(\pi x)*sin^2(\pi y)*sin^2(\pi z)
30
31 or other, discontinuous solution, aka "patch test"
32
33
34
35 \endverbatim
36
37 \remark Usage
38 \verbatim
39
40 ./TrilinosCouplings_examples_scaling_Example_DivLSFEM.exe inputfile.xml
41
42 inputfile.xml (optional) - xml input file containing Pamgen mesh description
43 and material parameters for each Pamgen block,
44 if not present code attempts to read DivLSFEMin.xml.
45
46
47 \endverbatim
48 **/
49
50 // TrilinosCouplings includes
51 #include "TrilinosCouplings_config.h"
52 #include "TrilinosCouplings_Pamgen_Utils.hpp"
53
54 // Intrepid includes
55 #include "Intrepid_FunctionSpaceTools.hpp"
56 #include "Intrepid_FieldContainer.hpp"
57 #include "Intrepid_CellTools.hpp"
58 #include "Intrepid_ArrayTools.hpp"
59 #include "Intrepid_HCURL_HEX_I1_FEM.hpp"
60 #include "Intrepid_HGRAD_HEX_C1_FEM.hpp"
61 #include "Intrepid_HDIV_HEX_I1_FEM.hpp"
62 #include "Intrepid_RealSpaceTools.hpp"
63 #include "Intrepid_DefaultCubatureFactory.hpp"
64 #include "Intrepid_Utils.hpp"
65
66 // Epetra includes
67 #include "Epetra_Time.h"
68 #include "Epetra_Map.h"
69 #ifdef HAVE_MPI
70 #include "Epetra_MpiComm.h"
71 #else
72 #include "Epetra_SerialComm.h"
73 #endif
74 #include "Epetra_FECrsMatrix.h"
75 #include "Epetra_FEVector.h"
76 #include "Epetra_Vector.h"
77 #include "Epetra_LinearProblem.h"
78 #include "Epetra_Import.h"
79 #include "Epetra_Export.h"
80
81 // Teuchos includes
82 #include "Teuchos_oblackholestream.hpp"
83 #include "Teuchos_RCP.hpp"
84 #include "Teuchos_BLAS.hpp"
85 #include "Teuchos_GlobalMPISession.hpp"
86 #include "Teuchos_ParameterList.hpp"
87 #include "Teuchos_XMLParameterListHelpers.hpp"
88
89 // Shards includes
90 #include "Shards_CellTopology.hpp"
91
92 // EpetraExt includes
93 #include "EpetraExt_RowMatrixOut.h"
94 #include "EpetraExt_MultiVectorOut.h"
95 #include "EpetraExt_VectorOut.h"
96 #include "EpetraExt_MatrixMatrix.h"
97
98 // Pamgen includes
99 #include "create_inline_mesh.h"
100 #include "pamgen_im_exodusII_l.h"
101 #include "pamgen_im_ne_nemesisI_l.h"
102 #include "pamgen_extras.h"
103
104 // AztecOO includes
105 #include "AztecOO.h"
106
107 // ML Includes
108 #include "ml_config.h" // minor abuse
109 #include "ml_epetra_utils.h"
110 #include "ml_GradDiv.h"
111 #include "ml_MultiLevelPreconditioner.h"
112
113 // Teko includes
114 #include "Teko_CloneFactory.hpp"
115 #include "Teko_BlockedEpetraOperator.hpp"
116 #include "Teko_JacobiPreconditionerFactory.hpp"
117 #include "Teko_EpetraBlockPreconditioner.hpp"
118
119 // Thyra includes
120 #include "Thyra_EpetraLinearOp.hpp"
121
122
123 #define ABS(x) ((x)>0?(x):-(x))
124
125 //#define DUMP_DATA
126
127 using namespace std;
128 using namespace Intrepid;
129 using namespace Teuchos;
130 using namespace Thyra;
131 using namespace Teko;
132 using namespace ML_Epetra;
133
134 struct fecomp{
135 bool operator () ( topo_entity* x, topo_entity* y )const
136 {
137 if(x->sorted_local_node_ids < y->sorted_local_node_ids)return true;
138 return false;
139 }
140 };
141
142
143 template<class ArrayOut, class ArrayIn>
144 void evaluateMaterialTensor(ArrayOut & matTensorValues,
145 const ArrayIn & evaluationPoints);
146
147 template<class ArrayOut, class ArrayIn>
148 void evaluateMaterialTensorInv(ArrayOut & matTensorValues,
149 const ArrayIn & evaluationPoints);
150
151 template<typename Scalar>
152 void materialTensor(Scalar material[][3], const Scalar& x, const Scalar& y, const Scalar& z);
153
154 template<typename Scalar>
155 void materialTensorInv(Scalar material[][3], const Scalar& x, const Scalar& y, const Scalar& z);
156
157 template<typename Scalar>
158 Scalar evalKappa(const Scalar& x, const Scalar& y, const Scalar& z);
159
160 template<typename Scalar>
161 Scalar evalF(const Scalar& x, const Scalar& y, const Scalar& z);
162
163 template<typename Scalar>
164 Scalar evalPhi(const Scalar& x, const Scalar& y, const Scalar& z);
165
166 template<typename Scalar> int evalu(Scalar & uExact0, Scalar & uExact1, Scalar & uExact2, Scalar & x, Scalar & y, Scalar & z);
167
168 template<typename Scalar> Scalar evalDivu(Scalar & x, Scalar & y, Scalar & z);
169
170 template<typename Scalar> int evalGradPhi(Scalar & phiGExact0, Scalar & phiGExact1, Scalar & phiGExact2, Scalar & x, Scalar & y, Scalar & z);
171
172
173 int main(int argc, char *argv[]) {
174
175 int error = 0;
176 #ifdef HAVE_MPI
177 Teuchos::GlobalMPISession mpiSession(&argc, &argv,0);
178 int rank=mpiSession.getRank();
179 int numProcs=mpiSession.getNProc();
180 Epetra_MpiComm Comm(MPI_COMM_WORLD);
181 int MyPID = Comm.MyPID();
182 #else
183 int rank=0;
184 int numProcs=1;
185 int MyPID = 0;
186 Epetra_SerialComm Comm;
187 #endif
188 Epetra_Time Time(Comm);
189
190 //Check number of arguments
191 if (argc > 2) {
192 std::cout <<"\n>>> ERROR: Invalid number of arguments.\n\n";
193 std::cout <<"Usage:\n\n";
194 std::cout <<" ./TrilinosCouplings_examples_scaling_Example_DivLSFEM.exe [inputfile.xml] \n\n";
195 std::cout <<" inputfile.xml(optional) - xml file with description of Pamgen mesh \n";
196 std::cout <<" and material parameters for each block \nn";
197 exit(1);
198 }
199
200 if (MyPID == 0) {
201 std::cout \
202 << "===============================================================================\n" \
203 << "| |\n" \
204 << "| Example: Darcy Flow on Hexahedral Mesh |\n" \
205 << "| |\n" \
206 << "| Questions? Contact |\n" \
207 << "| |\n" \
208 << "| |\n" \
209 << "| |\n" \
210 << "| Intrepid's website: http://trilinos.sandia.gov/packages/intrepid |\n" \
211 << "| Pamgen's website: http://trilinos.sandia.gov/packages/pamgen |\n" \
212 << "| ML's website: http://trilinos.sandia.gov/packages/ml |\n" \
213 << "| Trilinos website: http://trilinos.sandia.gov |\n" \
214 << "| |\n" \
215 << "===============================================================================\n";
216 }
217
218 long long * node_comm_proc_ids = NULL;
219 long long * node_cmap_node_cnts = NULL;
220 long long * node_cmap_ids = NULL;
221 long long ** comm_node_ids = NULL;
222 long long ** comm_node_proc_ids = NULL;
223
224 std::set < topo_entity * , fecomp > edge_set;
225 std::set < topo_entity * , fecomp > face_set;
226
227 std::vector < topo_entity * > edge_vector;
228 std::vector < topo_entity * > face_vector;
229
230 std::vector < int > edge_comm_procs;
231
232 int dim = 3;
233
234 // ************************************ GET INPUTS **************************************
235
236 //what problem do we solve?
237 // "patch test" or...
238 //#define PATCH
239 // ...smooth solution
240 #define SMOOTH
241
242
243
244 // Command line for xml file, otherwise use default
245 std::string xmlInFileName;
246 if(argc>=2) xmlInFileName=string(argv[1]);
247 else xmlInFileName="Darcy.xml";
248
249 // Read xml file into parameter list
250 Teuchos::ParameterList inputList;
251
252 if(xmlInFileName.length()) {
253 if (MyPID == 0) {
254 std::cout << "\nReading parameter list from the XML file \""<<xmlInFileName<<"\" ...\n\n";
255 }
256 Teuchos::updateParametersFromXmlFile(xmlInFileName, Teuchos::ptr (&inputList));
257 if (MyPID == 0) {
258 inputList.print(std::cout,2,true,true);
259 std::cout << "\n";
260 }
261 }
262 else
263 {
264 std::cout << "Cannot read input file: " << xmlInFileName << "\n";
265 return 0;
266 }
267
268 // Get pamgen mesh definition
269 std::string meshInput = Teuchos::getParameter<std::string>(inputList,"meshInput");
270
271
272 // *********************************** CELL TOPOLOGY **********************************
273
274 // Get cell topology for base hexahedron
275 typedef shards::CellTopology CellTopology;
276 CellTopology hex_8(shards::getCellTopologyData<shards::Hexahedron<8> >() );
277
278 // Get dimensions
279 int numNodesPerElem = hex_8.getNodeCount();
280 int numEdgesPerElem = hex_8.getEdgeCount();
281 int numFacesPerElem = hex_8.getSideCount();
282 int numNodesPerEdge = 2;
283 int numNodesPerFace = 4;
284 int numEdgesPerFace = 4;
285 int spaceDim = hex_8.getDimension();
286
287 // Build reference element edge to node map
288 FieldContainer<int> refEdgeToNode(numEdgesPerElem,numNodesPerEdge);
289 for (int i=0; i<numEdgesPerElem; i++){
290 refEdgeToNode(i,0)=hex_8.getNodeMap(1, i, 0);
291 refEdgeToNode(i,1)=hex_8.getNodeMap(1, i, 1);
292 }
293
294 // Build reference element face to node map
295 FieldContainer<int> refFaceToNode(numFacesPerElem,numNodesPerFace);
296 for (int i=0; i<numFacesPerElem; i++){
297 refFaceToNode(i,0)=hex_8.getNodeMap(2, i, 0);
298 refFaceToNode(i,1)=hex_8.getNodeMap(2, i, 1);
299 refFaceToNode(i,2)=hex_8.getNodeMap(2, i, 2);
300 refFaceToNode(i,3)=hex_8.getNodeMap(2, i, 3);
301 }
302
303 // Build reference element face to edge map (Hardcoded for now)
304 FieldContainer<int> refFaceToEdge(numFacesPerElem,numEdgesPerFace);
305 refFaceToEdge(0,0)=0; refFaceToEdge(0,1)=9;
306 refFaceToEdge(0,2)=4; refFaceToEdge(0,3)=8;
307 refFaceToEdge(1,0)=1; refFaceToEdge(1,1)=10;
308 refFaceToEdge(1,2)=5; refFaceToEdge(1,3)=9;
309 refFaceToEdge(2,0)=2; refFaceToEdge(2,1)=11;
310 refFaceToEdge(2,2)=6; refFaceToEdge(2,3)=10;
311 refFaceToEdge(3,0)=8; refFaceToEdge(3,1)=7;
312 refFaceToEdge(3,2)=11; refFaceToEdge(3,3)=3;
313 refFaceToEdge(4,0)=3; refFaceToEdge(4,1)=2;
314 refFaceToEdge(4,2)=1; refFaceToEdge(4,3)=0;
315 refFaceToEdge(5,0)=4; refFaceToEdge(5,1)=5;
316 refFaceToEdge(5,2)=6; refFaceToEdge(5,3)=7;
317
318 // *********************************** GENERATE MESH ************************************
319
320 if (MyPID == 0) {
321 std::cout << "Generating mesh ... \n\n";
322 }
323
324 // Generate mesh with Pamgen
325 long long maxInt = 9223372036854775807LL;
326 long long cr_result = Create_Pamgen_Mesh(meshInput.c_str(), dim, rank, numProcs, maxInt);
327 TrilinosCouplings::pamgen_error_check(std::cout,cr_result);
328
329 // Get local mesh size info
330 char title[100];
331 long long numDim;
332 long long numNodes;
333 long long numElems;
334 long long numElemBlk;
335 long long numNodeSets;
336 long long numSideSets;
337 int id = 0;
338
339 im_ex_get_init_l(id, title, &numDim, &numNodes,
340 &numElems, &numElemBlk, &numNodeSets,
341 &numSideSets);
342 // Get global mesh size info
343 long long numNodesGlobal;
344 long long numElemsGlobal;
345 long long numElemBlkGlobal;
346 long long numNodeSetsGlobal;
347 long long numSideSetsGlobal;
348
349 im_ne_get_init_global_l(id, &numNodesGlobal, &numElemsGlobal,
350 &numElemBlkGlobal, &numNodeSetsGlobal,
351 &numSideSetsGlobal);
352
353
354
355 long long * block_ids = new long long [numElemBlk];
356 error += im_ex_get_elem_blk_ids_l(id, block_ids);
357
358
359 long long *nodes_per_element = new long long [numElemBlk];
360 long long *element_attributes = new long long [numElemBlk];
361 long long *elements = new long long [numElemBlk];
362 char **element_types = new char * [numElemBlk];
363 long long **elmt_node_linkage = new long long * [numElemBlk];
364
365
366 for(long long i = 0; i < numElemBlk; i ++){
367 element_types[i] = new char [MAX_STR_LENGTH + 1];
368 error += im_ex_get_elem_block_l(id,
369 block_ids[i],
370 element_types[i],
371 (long long*)&(elements[i]),
372 (long long*)&(nodes_per_element[i]),
373 (long long*)&(element_attributes[i]));
374 }
375
376 /*connectivity*/
377 for(long long b = 0; b < numElemBlk; b++){
378 elmt_node_linkage[b] = new long long [nodes_per_element[b]*elements[b]];
379 error += im_ex_get_elem_conn_l(id,block_ids[b],elmt_node_linkage[b]);
380 }
381
382 // Get mu value for each block of elements from parameter list
383 /* double *mu = new double [numElemBlk];
384 for(int b = 0; b < numElemBlk; b++){
385 stringstream muBlock;
386 muBlock.clear();
387 muBlock << "mu" << b;
388 mu[b] = inputList.get(muBlock.str(),1.0);
389 }*/
390
391 // Get node-element connectivity and set element mu value
392 int telct = 0;
393 FieldContainer<int> elemToNode(numElems,numNodesPerElem);
394 FieldContainer<double> muVal(numElems);
395 for(long long b = 0; b < numElemBlk; b++){
396 for(long long el = 0; el < elements[b]; el++){
397 for (int j=0; j<numNodesPerElem; j++) {
398 elemToNode(telct,j) = elmt_node_linkage[b][el*numNodesPerElem + j]-1;
399 }
400 // muVal(telct) = mu[b];
401 telct ++;
402 }
403 }
404
405 // Read node coordinates and place in field container
406 FieldContainer<double> nodeCoord(numNodes,dim);
407 double * nodeCoordx = new double [numNodes];
408 double * nodeCoordy = new double [numNodes];
409 double * nodeCoordz = new double [numNodes];
410 im_ex_get_coord_l(id,nodeCoordx,nodeCoordy,nodeCoordz);
411 for (int i=0; i<numNodes; i++) {
412 nodeCoord(i,0)=nodeCoordx[i];
413 nodeCoord(i,1)=nodeCoordy[i];
414 nodeCoord(i,2)=nodeCoordz[i];
415 }
416
417 /*parallel info*/
418 long long num_internal_nodes;
419 long long num_border_nodes;
420 long long num_external_nodes;
421 long long num_internal_elems;
422 long long num_border_elems;
423 long long num_node_comm_maps;
424 long long num_elem_comm_maps;
425 im_ne_get_loadbal_param_l( id,
426 &num_internal_nodes,
427 &num_border_nodes,
428 &num_external_nodes,
429 &num_internal_elems,
430 &num_border_elems,
431 &num_node_comm_maps,
432 &num_elem_comm_maps,
433 0/*unused*/ );
434
435 if(num_node_comm_maps > 0){
436 node_comm_proc_ids = new long long [num_node_comm_maps];
437 node_cmap_node_cnts = new long long [num_node_comm_maps];
438 node_cmap_ids = new long long [num_node_comm_maps];
439 comm_node_ids = new long long* [num_node_comm_maps];
440 comm_node_proc_ids = new long long* [num_node_comm_maps];
441
442 long long * elem_cmap_ids = new long long [num_elem_comm_maps];
443 long long * elem_cmap_elem_cnts = new long long [num_elem_comm_maps];
444
445
446 if ( im_ne_get_cmap_params_l( id,
447 node_cmap_ids,
448 (long long*)node_cmap_node_cnts,
449 elem_cmap_ids,
450 (long long*)elem_cmap_elem_cnts,
451 0/*not used proc_id*/ ) < 0 )++error;
452
453 for(long long j = 0; j < num_node_comm_maps; j++) {
454 comm_node_ids[j] = new long long [node_cmap_node_cnts[j]];
455 comm_node_proc_ids[j] = new long long [node_cmap_node_cnts[j]];
456 if ( im_ne_get_node_cmap_l( id,
457 node_cmap_ids[j],
458 comm_node_ids[j],
459 comm_node_proc_ids[j],
460 0/*not used proc_id*/ ) < 0 )++error;
461 node_comm_proc_ids[j] = comm_node_proc_ids[j][0];
462 }
463 }
464
465
466 //Calculate global node ids
467 long long * globalNodeIds = new long long[numNodes];
468 bool * nodeIsOwned = new bool[numNodes];
469
470 calc_global_node_ids(globalNodeIds,
471 nodeIsOwned,
472 numNodes,
473 num_node_comm_maps,
474 node_cmap_node_cnts,
475 node_comm_proc_ids,
476 comm_node_ids,
477 rank);
478 // Count owned nodes
479 int numOwnedNodes=0;
480 for(int i=0;i<numNodes;i++)
481 if(nodeIsOwned[i]) numOwnedNodes++;
482
483 // Build a list of the OWNED global ids...
484 int *ownedGIDs=new int [numOwnedNodes];
485 int oidx=0;
486 for(int i=0;i<numNodes;i++)
487 if(nodeIsOwned[i]){
488 ownedGIDs[oidx]=(int)globalNodeIds[i];
489 oidx++;
490 }
491
492
493 FieldContainer<int> elemToEdge(numElems,numEdgesPerElem);
494 FieldContainer<int> elemToFace(numElems,numFacesPerElem);
495
496 // calculate edge and face ids
497 int elct = 0;
498 for(long long b = 0; b < numElemBlk; b++){
499 if(nodes_per_element[b] == 4){
500 }
501 else if (nodes_per_element[b] == 8){
502 //loop over all elements and push their edges onto a set if they are not there already
503 for(long long el = 0; el < elements[b]; el++){
504 std::set< topo_entity *, fecomp > ::iterator fit;
505 for (int i=0; i < numEdgesPerElem; i++){
506 topo_entity * teof = new topo_entity;
507 for(int j = 0; j < numNodesPerEdge;j++){
508 teof->add_node(elmt_node_linkage[b][el*numNodesPerElem + refEdgeToNode(i,j)],globalNodeIds);
509 }
510 teof->sort();
511 fit = edge_set.find(teof);
512 if(fit == edge_set.end()){
513 teof->local_id = edge_vector.size();
514 edge_set.insert(teof);
515 elemToEdge(elct,i)= edge_vector.size();
516 edge_vector.push_back(teof);
517 }
518 else{
519 elemToEdge(elct,i) = (*fit)->local_id;
520 delete teof;
521 }
522 }
523 for (int i=0; i < numFacesPerElem; i++){
524 topo_entity * teof = new topo_entity;
525 for(int j = 0; j < numNodesPerFace;j++){
526 teof->add_node(elmt_node_linkage[b][el*numNodesPerElem + refFaceToNode(i,j)],globalNodeIds);
527 }
528 teof->sort();
529 fit = face_set.find(teof);
530 if(fit == face_set.end()){
531 teof->local_id = face_vector.size();
532 face_set.insert(teof);
533 elemToFace(elct,i)= face_vector.size();
534 face_vector.push_back(teof);
535 }
536 else{
537 elemToFace(elct,i) = (*fit)->local_id;
538 delete teof;
539 }
540 }
541 elct ++;
542 }
543 }
544 }
545
546 // Edge to Node connectivity
547 FieldContainer<int> edgeToNode(edge_vector.size(), numNodesPerEdge);
548 for(unsigned ect = 0; ect != edge_vector.size(); ect++){
549 std::list<long long>::iterator elit;
550 int nct = 0;
551 for(elit = edge_vector[ect]->local_node_ids.begin();
552 elit != edge_vector[ect]->local_node_ids.end();
553 elit ++){
554 edgeToNode(ect,nct) = *elit-1;
555 nct++;
556 }
557 }
558
559 // Face to Node connectivity
560 FieldContainer<int> faceToNode(face_vector.size(), numNodesPerFace);
561 for(unsigned fct = 0; fct != face_vector.size(); fct++){
562 std::list<long long>::iterator flit;
563 int nct = 0;
564 for(flit = face_vector[fct]->local_node_ids.begin();
565 flit != face_vector[fct]->local_node_ids.end();
566 flit ++){
567 faceToNode(fct,nct) = *flit-1;
568 nct++;
569 }
570 }
571
572
573 // Face to Edge connectivity
574 FieldContainer<int> faceToEdge(face_vector.size(), numEdgesPerFace);
575 FieldContainer<bool> faceDone(face_vector.size());
576 for (int ielem = 0; ielem < numElems; ielem++){
577 for (int iface = 0; iface < numFacesPerElem; iface++){
578 if (!faceDone(elemToFace(ielem,iface))){
579 for (int iedge = 0; iedge < numEdgesPerFace; iedge++){
580 faceToEdge(elemToFace(ielem,iface),iedge) =
581 elemToEdge(ielem,refFaceToEdge(iface,iedge));
582 faceDone(elemToFace(ielem,iface))=1;
583 }
584 }
585 }
586 }
587
588 int numEdges = edge_vector.size();
589 int numFaces = face_vector.size();
590
591 // Calculate global edge and face numbering
592 std::string doing_type;
593 doing_type = "EDGES";
594 calc_global_ids(edge_vector,
595 comm_node_ids,
596 node_comm_proc_ids,
597 node_cmap_node_cnts,
598 num_node_comm_maps,
599 rank,
600 doing_type);
601
602
603 doing_type = "FACES";
604 calc_global_ids(face_vector,
605 comm_node_ids,
606 node_comm_proc_ids,
607 node_cmap_node_cnts,
608 num_node_comm_maps,
609 rank,
610 doing_type);
611
612 // Build list of owned global edge ids
613 long long * globalEdgeIds = new long long[numEdges];
614 bool * edgeIsOwned = new bool[numEdges];
615 int numOwnedEdges=0;
616 for (int i=0; i<numEdges; i++) {
617 edgeIsOwned[i] = edge_vector[i]->owned;
618 globalEdgeIds[i] = edge_vector[i]->global_id;
619 if (edgeIsOwned[i]){
620 numOwnedEdges++;
621 }
622 }
623 int * ownedEdgeIds = new int[numOwnedEdges];
624 int nedge=0;
625 for (int i=0; i<numEdges; i++) {
626 if (edgeIsOwned[i]){
627 ownedEdgeIds[nedge]=(int)globalEdgeIds[i];
628 nedge++;
629 }
630 }
631
632 // Build list of owned global face ids
633 long long * globalFaceIds = new long long[numFaces];
634 bool * faceIsOwned = new bool[numFaces];
635 int numOwnedFaces=0;
636 for (int i=0; i<numFaces; i++) {
637 faceIsOwned[i] = face_vector[i]->owned;
638 globalFaceIds[i] = face_vector[i]->global_id;
639 if (faceIsOwned[i]){
640 numOwnedFaces++;
641 }
642 }
643 int * ownedFaceIds = new int[numOwnedFaces];
644 int nface=0;
645 for (int i=0; i<numFaces; i++) {
646 if (faceIsOwned[i]){
647 ownedFaceIds[nface]=(int)globalFaceIds[i];
648 nface++;
649 }
650 }
651
652 // Calculate number of global edges and faces
653 int numEdgesGlobal;
654 int numFacesGlobal;
655 #ifdef HAVE_MPI
656 Comm.SumAll(&numOwnedEdges,&numEdgesGlobal,1);
657 Comm.SumAll(&numOwnedFaces,&numFacesGlobal,1);
658 #else
659 numEdgesGlobal = numEdges;
660 numFacesGlobal = numFaces;
661 #endif
662
663 // Define global epetra maps
664 Epetra_Map globalMapG(-1,numOwnedNodes,ownedGIDs,0,Comm);
665 Epetra_Map globalMapC(-1,numOwnedEdges,ownedEdgeIds,0,Comm);
666 Epetra_Map globalMapD(-1,numOwnedFaces,ownedFaceIds,0,Comm);
667
668 //define a global map of a joint variable [faces, nodes]\in hdiv x hgrad
669 //this is what we need to construct whole system.
670 //also, here we need to define how we address facial and nodal values in joint vector
671 //so we form a new map, for joint vector
672 int jointLocalVarSize = numOwnedFaces + numOwnedNodes;
673
674 int * ownedFaceNodeIds = new int[jointLocalVarSize];
675 for (int i=0; i<numOwnedFaces; i++)
676 ownedFaceNodeIds[i]=ownedFaceIds[i];
677
678 for (int i=numOwnedFaces; i<jointLocalVarSize; i++)
679 ownedFaceNodeIds[i]=ownedGIDs[i-numOwnedFaces]+numFacesGlobal;
680
681 Epetra_Map globalMapJoint(-1,jointLocalVarSize,ownedFaceNodeIds,0,Comm);
682
683
684 // Print mesh size information
685 if (MyPID == 0) {
686 std::cout << " Number of Elements: " << numElemsGlobal << " \n";
687 std::cout << " Number of Nodes: " << numNodesGlobal << " \n";
688 std::cout << " Number of Edges: " << numEdgesGlobal << " \n";
689 std::cout << " Number of Faces: " << numFacesGlobal << " \n\n";
690 }
691
692 #ifdef DUMP_DATA
693 // Output element to face connectivity
694 std::stringstream e2nfname;
695 e2nfname << "elem2node";
696 e2nfname << MyPID << ".dat";
697 std::stringstream e2ffname;
698 e2ffname << "elem2face";
699 e2ffname << MyPID << ".dat";
700 ofstream el2fout(e2ffname.str().c_str());
701 ofstream el2nout(e2nfname.str().c_str());
702 for (int i=0; i<numElems; i++) {
703 for (int l=0; l<numFacesPerElem; l++) {
704 el2fout << globalFaceIds[elemToFace(i,l)] << " ";
705 }
706 el2fout << "\n";
707 for (int m=0; m<numNodesPerElem; m++) {
708 el2nout << globalNodeIds[elemToNode(i,m)] << " ";
709 }
710 el2nout << "\n";
711 }
712 el2fout.close();
713 el2nout.close();
714
715 // Output face to edge and face to node connectivity
716 std::stringstream f2nfname;
717 f2nfname << "face2node";
718 f2nfname << MyPID << ".dat";
719 std::stringstream f2efname;
720 f2efname << "face2edge";
721 f2efname << MyPID << ".dat";
722 ofstream f2edout(f2efname.str().c_str());
723 ofstream f2nout(f2nfname.str().c_str());
724 for (int k=0; k<numFaces; k++) {
725 if (faceIsOwned[k]){
726 for (int i=0; i<numEdgesPerFace; i++) {
727 f2edout << globalEdgeIds[faceToEdge(k,i)] << " ";
728 }
729 for (int j=0; j<numNodesPerFace; j++) {
730 f2nout << globalNodeIds[faceToNode(k,j)] << " ";
731 }
732 f2edout << "\n";
733 f2nout << "\n";
734 }
735 }
736 f2edout.close();
737 f2nout.close();
738 #endif
739
740 // Container indicating whether a face is on the boundary (1-yes 0-no)
741 FieldContainer<int> edgeOnBoundary(numEdges);
742 FieldContainer<int> faceOnBoundary(numFaces);
743
744 // Get boundary (side set) information
745 long long * sideSetIds = new long long [numSideSets];
746 long long numSidesInSet;
747 long long numDFinSet;
748 int numBndyFaces=0;
749 im_ex_get_side_set_ids_l(id,sideSetIds);
750 for (int i=0; i<numSideSets; i++) {
751 im_ex_get_side_set_param_l(id,sideSetIds[i],&numSidesInSet,&numDFinSet);
752 if (numSidesInSet > 0){
753 long long * sideSetElemList = new long long [numSidesInSet];
754 long long * sideSetSideList = new long long [numSidesInSet];
755 im_ex_get_side_set_l(id,sideSetIds[i],sideSetElemList,sideSetSideList);
756 for (int j=0; j<numSidesInSet; j++) {
757 int iface = sideSetSideList[j]-1;
758 faceOnBoundary(elemToFace(sideSetElemList[j]-1,iface))=1;
759 numBndyFaces++;
760 edgeOnBoundary(faceToEdge(elemToFace(sideSetElemList[j]-1,iface),0))=1;
761 edgeOnBoundary(faceToEdge(elemToFace(sideSetElemList[j]-1,iface),1))=1;
762 edgeOnBoundary(faceToEdge(elemToFace(sideSetElemList[j]-1,iface),2))=1;
763 edgeOnBoundary(faceToEdge(elemToFace(sideSetElemList[j]-1,iface),3))=1;
764 }
765 delete [] sideSetElemList;
766 delete [] sideSetSideList;
767 }
768 }
769
770 delete [] sideSetIds;
771
772
773 // Container indicating whether a node is on the boundary (1-yes 0-no)
774 FieldContainer<int> nodeOnBoundary(numNodes);
775 for (int i=0; i<numEdges; i++) {
776 if (edgeOnBoundary(i)){
777 nodeOnBoundary(edgeToNode(i,0))=1;
778 nodeOnBoundary(edgeToNode(i,1))=1;
779 }
780 }
781
782
783 // Build the coordinate vectors for ML solver (including owned nodes only)
784 Epetra_Vector Nx(globalMapG), Ny(globalMapG),Nz(globalMapG);
785 for(int i=0,nlid=0;i<numNodes;i++)
786 if(nodeIsOwned[i]) {
787 Nx[nlid]=nodeCoordx[i];
788 Ny[nlid]=nodeCoordy[i];
789 Nz[nlid]=nodeCoordz[i];
790 nlid++;
791 }
792
793
794 #ifdef DUMP_DATA
795 EpetraExt::VectorToMatrixMarketFile("coordx.dat",Nx,0,0,false);
796 EpetraExt::VectorToMatrixMarketFile("coordy.dat",Ny,0,0,false);
797 EpetraExt::VectorToMatrixMarketFile("coordz.dat",Nz,0,0,false);
798 #endif
799
800 // Get numerical integration points and weights
801 if (MyPID == 0) {
802 std::cout << "Getting cubature ... \n\n";
803 }
804
805 DefaultCubatureFactory<double> cubFactory;
806 int cubDegree = 2;
807 Teuchos::RCP<Cubature<double> > hexCub = cubFactory.create(hex_8, cubDegree);
808
809 int cubDim = hexCub->getDimension();
810 int numCubPoints = hexCub->getNumPoints();
811
812 FieldContainer<double> cubPoints(numCubPoints, cubDim);
813 FieldContainer<double> cubWeights(numCubPoints);
814
815 hexCub->getCubature(cubPoints, cubWeights);
816
817 // Get numerical integration points and weights for hexahedron face
818 // (needed for rhs boundary term)
819
820 // Define topology of the face parametrization domain as [-1,1]x[-1,1]
821 CellTopology paramQuadFace(shards::getCellTopologyData<shards::Quadrilateral<4> >() );
822
823 // Define cubature
824 DefaultCubatureFactory<double> cubFactoryFace;
825 Teuchos::RCP<Cubature<double> > hexFaceCubature = cubFactoryFace.create(paramQuadFace, 3);
826 int cubFaceDim = hexFaceCubature -> getDimension();
827 int numFacePoints = hexFaceCubature -> getNumPoints();
828
829 // Define storage for cubature points and weights on [-1,1]x[-1,1]
830 FieldContainer<double> paramGaussWeights(numFacePoints);
831 FieldContainer<double> paramGaussPoints(numFacePoints,cubFaceDim);
832
833 // Define storage for cubature points on workset faces
834 hexFaceCubature -> getCubature(paramGaussPoints, paramGaussWeights);
835
836
837 // ************************************** BASIS ***************************************
838
839 // Define basis
840 if (MyPID == 0) {
841 std::cout << "Getting basis ... \n\n";
842 }
843 Basis_HCURL_HEX_I1_FEM<double, FieldContainer<double> > hexHCurlBasis;
844 Basis_HDIV_HEX_I1_FEM<double, FieldContainer<double> > hexHDivBasis;
845 Basis_HGRAD_HEX_C1_FEM<double, FieldContainer<double> > hexHGradBasis;
846
847 int numFieldsC = hexHCurlBasis.getCardinality();
848 int numFieldsD = hexHDivBasis.getCardinality();
849 int numFieldsG = hexHGradBasis.getCardinality();
850
851 // Evaluate basis at cubature points
852 FieldContainer<double> hexDVals(numFieldsD, numCubPoints, spaceDim);
853 FieldContainer<double> hexDivs(numFieldsD, numCubPoints);
854 FieldContainer<double> hexGVals(numFieldsG, numCubPoints);
855 FieldContainer<double> hexGrads(numFieldsG, numCubPoints, spaceDim);
856
857 hexHDivBasis.getValues(hexDVals, cubPoints, OPERATOR_VALUE);
858 hexHDivBasis.getValues(hexDivs, cubPoints, OPERATOR_DIV);
859 hexHGradBasis.getValues(hexGVals, cubPoints, OPERATOR_VALUE);
860 hexHGradBasis.getValues(hexGrads, cubPoints, OPERATOR_GRAD);
861
862 /**********************************************************************************/
863 /*************************BUILD INCIDENCE MATRIX***********************************/
864 /**********************************************************************************/
865 Epetra_FECrsMatrix DCurl(Epetra_DataAccess::Copy, globalMapD, 4);
866 Epetra_FECrsMatrix DGrad(Epetra_DataAccess::Copy, globalMapC, 2);
867
868 // Edge to node incidence matrix
869 double vals[2];
870 vals[0]=-1.0; vals[1]=1.0;
871 for (int j=0; j<numEdges; j++){
872 if (edgeIsOwned[j]){
873 int rowNum = globalEdgeIds[j];
874 int colNum[2];
875 colNum[0] = globalNodeIds[edgeToNode(j,0)];
876 colNum[1] = globalNodeIds[edgeToNode(j,1)];
877 DGrad.InsertGlobalValues(1, &rowNum, 2, colNum, vals);
878 }
879 }
880
881 DGrad.GlobalAssemble(false);
882 DGrad.FillComplete(globalMapG,globalMapC);
883
884 // Edge to face incidence matrix
885 FieldContainer<bool> faceDone2(numFaces);
886 for (int i=0; i<numElems; i++){
887 for (int k=0; k<numFacesPerElem; k++){
888 int iface=elemToFace(i,k);
889 if (faceIsOwned[iface] && !faceDone2(iface)){
890 double vals[4];
891 int rowNum = globalFaceIds[iface];
892 int colNum[4];
893
894 for (int m=0; m<numEdgesPerFace; m++){
895 colNum[m] = globalEdgeIds[faceToEdge(iface,m)];
896 int indm = m+1;
897 if (indm >= numEdgesPerFace) indm=0;
898 if (edgeToNode(faceToEdge(iface,m),1) == edgeToNode(faceToEdge(iface,indm),0) ||
899 edgeToNode(faceToEdge(iface,m),1) == edgeToNode(faceToEdge(iface,indm),1)){
900 vals[m]=1.0;}
901 else vals[m]=-1.0;
902
903 // This is a convoluted way to account for edge orientations that
904 // may be incorrect on the local processor because the edge is
905 // not owned by the local processor.
906 int edgeIndex = -1;
907 if (!edgeIsOwned[faceToEdge(iface,m)]){
908 for (int l=0; l<numEdgesPerElem; l++){
909 if (faceToEdge(iface,m)==elemToEdge(i,l))
910 edgeIndex=l;
911 }
912 }
913 if (edgeIndex != -1 && edgeIndex < 8){
914 if (edgeIndex < 4 && faceIsOwned[elemToFace(i,4)]){
915 vals[m]=-1.0*vals[m];
916 }
917 else if (edgeIndex > 3 && faceIsOwned[elemToFace(i,5)]){
918 vals[m]=-1.0*vals[m];
919 }
920 }
921 } // end loop over face edges
922
923 DCurl.InsertGlobalValues(1, &rowNum, 4, colNum, vals);
924 faceDone2(iface)=1;
925
926 } // end if face is owned and face not done
927 } // end loop over element faces
928 } // end loop over elements
929
930 DCurl.GlobalAssemble(false);
931 DCurl.FillComplete(globalMapC,globalMapD);
932
933
934 if(MyPID==0) {std::cout << "Building incidence matrices "
935 << Time.ElapsedTime() << " sec \n" ; Time.ResetStartTime();}
936
937
938 // Build Face-Node Incidence Matrix
939 Epetra_CrsMatrix DGrad1(DGrad);
940 Epetra_CrsMatrix DCurl1(DCurl);
941 Epetra_CrsMatrix FaceNode(Epetra_DataAccess::Copy,globalMapD,0);
942 DGrad1.PutScalar(1.0);
943 DCurl1.PutScalar(1.0);
944 EpetraExt::MatrixMatrix::Multiply(DCurl1,false,DGrad1,false,FaceNode);
945 FaceNode.PutScalar(1.0);
946
947 /**********************************************************************************/
948 /******************** INHOMOGENEOUS BOUNDARY CONDITIONS ***************************/
949 /**********************************************************************************/
950
951 typedef CellTools<double> CellTools;
952 typedef FunctionSpaceTools fst;
953
954 FieldContainer<double> bndyFaceVal(numBndyFaces);
955 FieldContainer<int> bndyFaceToFace(numFaces);
956 FieldContainer<double> refFacePoints(numFacePoints,spaceDim);
957 FieldContainer<double> bndyFacePoints(1,numFacePoints,spaceDim);
958 FieldContainer<double> bndyFaceJacobians(1,numFacePoints,spaceDim,spaceDim);
959 FieldContainer<double> faceNorm(1,numFacePoints,spaceDim);
960 FieldContainer<double> uDotNormal(1,numFacePoints);
961 FieldContainer<double> uFace(numFacePoints,spaceDim);
962 FieldContainer<double> nodes(1, numNodesPerElem, spaceDim);
963 int ibface=0;
964
965 // Evaluate normal at face quadrature points
966 for (int ielem=0; ielem<numElems; ielem++) {
967 for (int inode=0; inode<numNodesPerElem; inode++) {
968 nodes(0,inode,0) = nodeCoord(elemToNode(ielem,inode),0);
969 nodes(0,inode,1) = nodeCoord(elemToNode(ielem,inode),1);
970 nodes(0,inode,2) = nodeCoord(elemToNode(ielem,inode),2);
971 }
972 for (int iface=0; iface<numFacesPerElem; iface++){
973 if(faceOnBoundary(elemToFace(ielem,iface))){
974 // map evaluation points from reference face to reference cell
975 CellTools::mapToReferenceSubcell(refFacePoints,
976 paramGaussPoints,
977 2, iface, hex_8);
978
979 // calculate Jacobian
980 CellTools::setJacobian(bndyFaceJacobians, refFacePoints,
981 nodes, hex_8);
982
983 // map evaluation points from reference cell to physical cell
984 CellTools::mapToPhysicalFrame(bndyFacePoints,
985 refFacePoints,
986 nodes, hex_8);
987
988 // Compute face normals
989 CellTools::getPhysicalFaceNormals(faceNorm,
990 bndyFaceJacobians,
991 iface, hex_8);
992
993
994 bndyFaceToFace(elemToFace(ielem,iface))=ibface;
995 ibface++;
996 }
997 }
998 }
999
1000
1001 // ******** LOOP OVER ELEMENTS TO CREATE LOCAL MASS and STIFFNESS MATRICES *************
1002
1003
1004 if (MyPID == 0) {
1005 std::cout << "Building mass and stiffness matrices ... \n\n";
1006 }
1007
1008
1009 // Settings and data structures for mass and stiffness matrices
1010 int numCells = 1;
1011
1012 // Containers for nodes, edge and face signs
1013 FieldContainer<double> hexNodes(numCells, numNodesPerElem, spaceDim);
1014 FieldContainer<double> hexEdgeSigns(numCells, numFieldsC);
1015 FieldContainer<double> hexFaceSigns(numCells, numFieldsD);
1016
1017 // Containers for Jacobian
1018 FieldContainer<double> hexJacobian(numCells, numCubPoints, spaceDim, spaceDim);
1019 FieldContainer<double> hexJacobInv(numCells, numCubPoints, spaceDim, spaceDim);
1020 FieldContainer<double> hexJacobDet(numCells, numCubPoints);
1021
1022 FieldContainer<double> weightedMeasure(numCells, numCubPoints);
1023
1024 // Containers for element HDIV mass matrix
1025 FieldContainer<double> massMatrixD(numCells, numFieldsD, numFieldsD);
1026 FieldContainer<double> hexDValsTransformed(numCells, numFieldsD, numCubPoints, spaceDim);
1027 FieldContainer<double> hexDValsTransformedWeighted(numCells, numFieldsD, numCubPoints, spaceDim);
1028 FieldContainer<double> hexDValsTransformedMatrAinv(numCells, numFieldsD, numCubPoints, spaceDim);
1029
1030 // Containers for element HDIV stiffness matrix
1031 FieldContainer<double> stiffMatrixD(numCells, numFieldsD, numFieldsD);
1032 FieldContainer<double> hexDivsTransformed(numCells, numFieldsD, numCubPoints);
1033 FieldContainer<double> hexDivsTransformedWeighted(numCells, numFieldsD, numCubPoints);
1034
1035 // Containers for element HDIV stiffness matrix
1036 FieldContainer<double> stiffMatrixG(numCells, numFieldsG, numFieldsG);
1037 // Containers for element HDIV stiffness matrix
1038 FieldContainer<double> massMatrixG(numCells, numFieldsG, numFieldsG);
1039
1040 FieldContainer<double> hexGValsTransformed(numCells, numFieldsG, numCubPoints);
1041 FieldContainer<double> hexGValsTransformedWeighted(numCells, numFieldsG, numCubPoints);
1042
1043 FieldContainer<double> hexGradsTransformed(numCells, numFieldsG, numCubPoints, spaceDim);
1044 FieldContainer<double> hexGradsTransformedWeighted(numCells, numFieldsG, numCubPoints, spaceDim);
1045 FieldContainer<double> hexGradsTransformedMatrA(numCells, numFieldsG, numCubPoints, spaceDim);
1046 FieldContainer<double> hexGradsTransformedWeightedMatrA(numCells, numFieldsG, numCubPoints, spaceDim);
1047
1048 // Containers for element GRAD(HGRAD) x HDIV matrix
1049 FieldContainer<double> stiffMatrixDG(numCells, numFieldsD, numFieldsG);
1050 FieldContainer<double> stiffMatrixDG2(numCells, numFieldsD, numFieldsG);
1051
1052 // Containers for element HDIV x GRAD(HGRAD) matrix (not really necessary, it is a transpose of stiffMatrixDG)
1053 FieldContainer<double> stiffMatrixGD(numCells, numFieldsG, numFieldsD);
1054 FieldContainer<double> stiffMatrixGD2(numCells, numFieldsG, numFieldsD);
1055
1056 // Containers for right hand side vectors
1057 FieldContainer<double> rhsDatah(numCells, numCubPoints);
1058 FieldContainer<double> hD(numCells, numFieldsD);
1059 FieldContainer<double> kD(numCells, numFieldsG);
1060 FieldContainer<double> gDBoundary(numCells, numFieldsD);
1061 FieldContainer<double> refGaussPoints(numFacePoints,spaceDim);
1062 FieldContainer<double> worksetGaussPoints(numCells,numFacePoints,spaceDim);
1063 FieldContainer<double> worksetJacobians(numCells, numFacePoints, spaceDim, spaceDim);
1064 FieldContainer<double> worksetJacobDet(numCells, numFacePoints);
1065 FieldContainer<double> worksetFaceN(numCells, numFacePoints, spaceDim);
1066 FieldContainer<double> worksetVFieldVals(numCells, numFacePoints, spaceDim);
1067 FieldContainer<double> worksetDValsTransformed(numCells, numFieldsD, numFacePoints, spaceDim);
1068 FieldContainer<double> curluFace(numCells, numFacePoints, spaceDim);
1069 FieldContainer<double> worksetDataCrossField(numCells, numFieldsD, numFacePoints, spaceDim);
1070
1071 // Container for cubature points in physical space
1072 FieldContainer<double> physCubPoints(numCells,numCubPoints, cubDim);
1073
1074 FieldContainer<double> worksetMaterialVals (numCells, numCubPoints, spaceDim, spaceDim);
1075 FieldContainer<double> worksetMaterialValsInv (numCells, numCubPoints, spaceDim, spaceDim);
1076
1077 // Global matrices arrays in Epetra format
1078
1079 //THIS MATRIX IS FOR PRECONDITIONING
1080 Epetra_FECrsMatrix StiffG(Epetra_DataAccess::Copy, globalMapG, numFieldsG);
1081
1082 //last agr here is not that important, epetra will extend storage if needed
1083 Epetra_FECrsMatrix jointMatrix(Epetra_DataAccess::Copy, globalMapJoint, numFieldsD);
1084
1085 Epetra_FEVector rhsD(globalMapD);
1086
1087 Epetra_FEVector jointVector(globalMapJoint);
1088
1089 //this vector is only for output
1090 Epetra_FEVector jointVectorGIDs(globalMapJoint);
1091
1092
1093 #ifdef DUMP_DATA
1094 std::stringstream fSignfname;
1095 fSignfname << "faceSigns";
1096 fSignfname << MyPID << ".dat";
1097 ofstream fSignsout(fSignfname.str().c_str());
1098 #endif
1099
1100
1101 // *** Element loop ***
1102 for (int k=0; k<numElems; k++) {
1103
1104 // Physical cell coordinates
1105 for (int i=0; i<numNodesPerElem; i++) {
1106 hexNodes(0,i,0) = nodeCoord(elemToNode(k,i),0);
1107 hexNodes(0,i,1) = nodeCoord(elemToNode(k,i),1);
1108 hexNodes(0,i,2) = nodeCoord(elemToNode(k,i),2);
1109 }
1110
1111 // Face signs
1112 for (int j=0; j<numFacesPerElem; j++) {
1113 hexFaceSigns(0,j) = -1.0;
1114 for (int i=0; i<numNodesPerFace; i++) {
1115 int indf=i+1;
1116 if (indf >= numNodesPerFace) indf=0;
1117 if (elemToNode(k,refFaceToNode(j,0))==faceToNode(elemToFace(k,j),i) &&
1118 elemToNode(k,refFaceToNode(j,1))==faceToNode(elemToFace(k,j),indf))
1119 hexFaceSigns(0,j) = 1.0;
1120 }
1121 if (!faceIsOwned[elemToFace(k,j)]){
1122 hexFaceSigns(0,j)=-1.0*hexFaceSigns(0,j);
1123 }
1124
1125 #ifdef DUMP_DATA
1126 fSignsout << hexFaceSigns(0,j) << " ";
1127 #endif
1128 }
1129 #ifdef DUMP_DATA
1130 fSignsout << "\n";
1131 #endif
1132
1133 // Edge signs
1134 for (int j=0; j<numEdgesPerElem; j++) {
1135 if (elemToNode(k,refEdgeToNode(j,0))==edgeToNode(elemToEdge(k,j),0) &&
1136 elemToNode(k,refEdgeToNode(j,1))==edgeToNode(elemToEdge(k,j),1))
1137 hexEdgeSigns(0,j) = 1.0;
1138 else
1139 hexEdgeSigns(0,j) = -1.0;
1140 }
1141
1142 // modify signs for edges whose signs were defined on another processor
1143 if (!faceIsOwned[elemToFace(k,0)]) {
1144 hexEdgeSigns(0,0)=-1.0*hexEdgeSigns(0,0);
1145 hexEdgeSigns(0,4)=-1.0*hexEdgeSigns(0,4);
1146 }
1147 if (!faceIsOwned[elemToFace(k,1)]) {
1148 hexEdgeSigns(0,1)=-1.0*hexEdgeSigns(0,1);
1149 hexEdgeSigns(0,5)=-1.0*hexEdgeSigns(0,5);
1150 }
1151 if (!faceIsOwned[elemToFace(k,2)]) {
1152 hexEdgeSigns(0,2)=-1.0*hexEdgeSigns(0,2);
1153 hexEdgeSigns(0,6)=-1.0*hexEdgeSigns(0,6);
1154 }
1155 if (!faceIsOwned[elemToFace(k,3)]) {
1156 hexEdgeSigns(0,3)=-1.0*hexEdgeSigns(0,3);
1157 hexEdgeSigns(0,7)=-1.0*hexEdgeSigns(0,7);
1158 }
1159
1160
1161 // Compute cell Jacobians, their inverses and their determinants
1162 CellTools::setJacobian(hexJacobian, cubPoints, hexNodes, hex_8);
1163 CellTools::setJacobianInv(hexJacobInv, hexJacobian );
1164 CellTools::setJacobianDet(hexJacobDet, hexJacobian );
1165
1166 ////////////////////////////////////////////////////////
1167
1168 // transform integration points to physical points
1169
1170 CellTools::mapToPhysicalFrame(physCubPoints, cubPoints, hexNodes, hex_8);
1171
1172
1173 // get A at cubature points
1174 evaluateMaterialTensor (worksetMaterialVals, physCubPoints);
1175 // get A^{-1} at cubature points
1176 evaluateMaterialTensorInv (worksetMaterialValsInv, physCubPoints);
1177
1178 // ************************** Compute element HDiv mass matrices *******************************
1179
1180 // compute weighted measure
1181 fst::computeCellMeasure<double>(weightedMeasure, hexJacobDet, cubWeights);
1182
1183 // transform to physical coordinates
1184 fst::HDIVtransformVALUE<double>(hexDValsTransformed, hexJacobian, hexJacobDet,
1185 hexDVals);
1186
1187 // multiply by weighted measure
1188 fst::multiplyMeasure<double>(hexDValsTransformedWeighted,
1189 weightedMeasure, hexDValsTransformed);
1190
1191 // Compute A*Dvals
1192 fst::tensorMultiplyDataField<double>(hexDValsTransformedMatrAinv, worksetMaterialValsInv, hexDValsTransformed);
1193
1194 // integrate to compute element mass matrix
1195 fst::integrate<double>(massMatrixD,
1196 hexDValsTransformedMatrAinv, hexDValsTransformedWeighted,
1197 COMP_BLAS);
1198
1199 // apply face signs
1200 fst::applyLeftFieldSigns<double>(massMatrixD, hexFaceSigns);
1201 fst::applyRightFieldSigns<double>(massMatrixD, hexFaceSigns);
1202
1203 // assemble into global matrix
1204 for (int row = 0; row < numFieldsD; row++){
1205 for (int col = 0; col < numFieldsD; col++){
1206 int rowIndex = globalFaceIds[elemToFace(k,row)];
1207 int colIndex = globalFaceIds[elemToFace(k,col)];
1208 double val = massMatrixD(0,row,col);
1209 jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val);
1210 }
1211 }
1212
1213
1214
1215 // ************************ Compute element HDiv stiffness matrices *****************************
1216
1217 // transform to physical coordinates
1218 fst::HDIVtransformDIV<double>(hexDivsTransformed, hexJacobDet,
1219 hexDivs);
1220
1221 // multiply by weighted measure
1222 fst::multiplyMeasure<double>(hexDivsTransformedWeighted,
1223 weightedMeasure, hexDivsTransformed);
1224
1225 // integrate to compute element stiffness matrix
1226 fst::integrate<double>(stiffMatrixD,
1227 hexDivsTransformed, hexDivsTransformedWeighted,
1228 COMP_BLAS);
1229
1230 // apply face signs
1231 fst::applyLeftFieldSigns<double>(stiffMatrixD, hexFaceSigns);
1232 fst::applyRightFieldSigns<double>(stiffMatrixD, hexFaceSigns);
1233
1234 // assemble into global matrix
1235 for (int row = 0; row < numFieldsD; row++){
1236 for (int col = 0; col < numFieldsD; col++){
1237 int rowIndex = globalFaceIds[elemToFace(k,row)];
1238 int colIndex = globalFaceIds[elemToFace(k,col)];
1239 double val = stiffMatrixD(0,row,col);
1240 jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val);
1241 }
1242 }
1243
1244
1245 // **************** Compute element HGrad stiffness matrices *******************************
1246
1247 // transform to physical coordinates
1248 fst::HGRADtransformGRAD<double>(hexGradsTransformed, hexJacobInv, hexGrads);
1249
1250 // multiply values with weighted measure
1251 fst::multiplyMeasure<double>(hexGradsTransformedWeighted,
1252 weightedMeasure, hexGradsTransformed);
1253
1254 // Compute A*Hgrads
1255 fst::tensorMultiplyDataField<double>(hexGradsTransformedWeightedMatrA, worksetMaterialVals, hexGradsTransformedWeighted);
1256
1257 // integrate to compute element stiff matrix
1258 fst::integrate<double>(stiffMatrixG,
1259 hexGradsTransformed, hexGradsTransformedWeightedMatrA, COMP_BLAS);
1260
1261 // assemble into global matrix
1262 for (int row = 0; row < numFieldsG; row++){
1263 int rowIndex,colIndex;
1264 for (int col = 0; col < numFieldsG; col++){
1265 rowIndex = globalNodeIds[elemToNode(k,row)];
1266 colIndex = globalNodeIds[elemToNode(k,col)];
1267 double val = stiffMatrixG(0,row,col);
1268 StiffG.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val);
1269 rowIndex+=numFacesGlobal; colIndex+=numFacesGlobal;
1270 jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val);
1271 }
1272 }
1273
1274
1275 // **************** Compute element HGrad mass matrices *******************************
1276
1277 // transform to physical coordinates
1278 fst::HGRADtransformVALUE<double>(hexGValsTransformed, hexGVals);
1279
1280 // multiply values with weighted measure
1281 fst::multiplyMeasure<double>(hexGValsTransformedWeighted,
1282 weightedMeasure, hexGValsTransformed);
1283
1284 // integrate to compute element stiff matrix
1285 fst::integrate<double>(massMatrixG,
1286 hexGValsTransformed, hexGValsTransformedWeighted, COMP_BLAS);
1287
1288 // assemble into global matrix
1289 for (int row = 0; row < numFieldsG; row++){
1290 int rowIndex,colIndex;
1291 for (int col = 0; col < numFieldsG; col++){
1292 rowIndex = globalNodeIds[elemToNode(k,row)];
1293 colIndex = globalNodeIds[elemToNode(k,col)];
1294 double val = massMatrixG(0,row,col);
1295 //do we need this for preconditioning too?
1296 //StiffG.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val);
1297 rowIndex+=numFacesGlobal; colIndex+=numFacesGlobal;
1298 jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val);
1299 }
1300 }
1301
1302
1303 // *********************** Compute element Div x GRAD(HGrad) matrices **********************
1304 // Compute A*Hgrads
1305 fst::tensorMultiplyDataField<double>(hexGradsTransformedWeightedMatrA, worksetMaterialVals, hexGradsTransformedWeighted);
1306
1307 fst::integrate<double>(stiffMatrixDG,
1308 hexDValsTransformedMatrAinv,hexGradsTransformedWeightedMatrA, COMP_BLAS);
1309
1310 // apply face signs
1311 fst::applyLeftFieldSigns<double>(stiffMatrixDG, hexFaceSigns);
1312
1313 // assemble into global matrix
1314 for (int row = 0; row < numFieldsD; row++){
1315 int rowIndex,colIndex;
1316 for (int col = 0; col < numFieldsG; col++){
1317 rowIndex = globalFaceIds[elemToFace(k,row)];
1318 colIndex = globalNodeIds[elemToNode(k,col)];
1319 double val = stiffMatrixDG(0,row,col);
1320 colIndex+=numFacesGlobal;
1321 jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val);
1322 }
1323 }
1324
1325 /////////////and now matrix divs by gvals
1326 fst::integrate<double>(stiffMatrixDG2,
1327 hexDivsTransformed,hexGValsTransformedWeighted, COMP_BLAS);
1328
1329 // apply face signs
1330 fst::applyLeftFieldSigns<double>(stiffMatrixDG2, hexFaceSigns);
1331
1332 // assemble into global matrix
1333 for (int row = 0; row < numFieldsD; row++){
1334 int rowIndex,colIndex;
1335 for (int col = 0; col < numFieldsG; col++){
1336 rowIndex = globalFaceIds[elemToFace(k,row)];
1337 colIndex = globalNodeIds[elemToNode(k,col)];
1338 double val = stiffMatrixDG2(0,row,col);
1339 colIndex+=numFacesGlobal;
1340 jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val);
1341
1342 }
1343 }
1344 // ************************** Compute element GRAD(HGrad) x Div matrices *************
1345
1346 fst::integrate<double>(stiffMatrixGD,
1347 hexGradsTransformed,hexDValsTransformedWeighted, COMP_BLAS);
1348
1349 // apply face signs
1350 fst::applyRightFieldSigns<double>(stiffMatrixGD, hexFaceSigns);
1351
1352 // assemble into global matrix
1353 for (int row = 0; row < numFieldsG; row++){
1354 int rowIndex,colIndex;
1355 for (int col = 0; col < numFieldsD; col++){
1356 rowIndex = globalNodeIds[elemToNode(k,row)];
1357 colIndex = globalFaceIds[elemToFace(k,col)];
1358 double val = stiffMatrixGD(0,row,col);
1359 rowIndex+=numFacesGlobal;
1360 jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val);
1361 }
1362 }
1363
1364
1365 fst::integrate<double>(stiffMatrixGD2,
1366 hexGValsTransformed,hexDivsTransformedWeighted, COMP_BLAS);
1367
1368 // apply face signs
1369 fst::applyRightFieldSigns<double>(stiffMatrixGD2, hexFaceSigns);
1370
1371 // assemble into global matrix
1372 for (int row = 0; row < numFieldsG; row++){
1373 int rowIndex,colIndex;
1374 for (int col = 0; col < numFieldsD; col++){
1375 rowIndex = globalNodeIds[elemToNode(k,row)];
1376 colIndex = globalFaceIds[elemToFace(k,col)];
1377 double val = stiffMatrixGD2(0,row,col);
1378 rowIndex+=numFacesGlobal;
1379 jointMatrix.InsertGlobalValues(1, &rowIndex, 1, &colIndex, &val);
1380 }
1381 }
1382
1383
1384 // ******************************* Build right hand side ************************************
1385
1386
1387
1388 // evaluate right hand side functions at physical points
1389
1390 for (int nPt = 0; nPt < numCubPoints; nPt++){
1391
1392 double x = physCubPoints(0,nPt,0);
1393 double y = physCubPoints(0,nPt,1);
1394 double z = physCubPoints(0,nPt,2);
1395
1396 rhsDatah(0,nPt) = evalF(x,y,z);
1397
1398 }
1399
1400 // integrate (f,div v_hat) term
1401 fst::integrate<double>(hD, rhsDatah, hexDivsTransformedWeighted,
1402 COMP_BLAS);
1403
1404 // apply signs
1405 fst::applyFieldSigns<double>(hD, hexFaceSigns);
1406
1407 // assemble into global vector
1408 for (int row = 0; row < numFieldsD; row++){
1409 int rowIndex = globalFaceIds[elemToFace(k,row)];
1410 double val = hD(0,row);
1411 rhsD.SumIntoGlobalValues(1, &rowIndex, &val);
1412 jointVector.SumIntoGlobalValues(1, &rowIndex, &val);
1413 }
1414
1415 // integrate (f,\phi_hat) term
1416 fst::integrate<double>(kD, rhsDatah, hexGValsTransformedWeighted,
1417 COMP_BLAS);
1418
1419 // assemble into global vector
1420 for (int row = 0; row < numFieldsG; row++){
1421 int rowIndex = globalNodeIds[elemToNode(k,row)] + numFacesGlobal;
1422 double val = kD(0,row);
1423 jointVector.SumIntoGlobalValues(1, &rowIndex, &val);
1424 }
1425
1426 } // *** end element loop ***
1427
1428
1429
1430 for(int i=0;i<jointVector.MyLength();i++){
1431 int id=jointVector.Map().GID(i);
1432 jointVectorGIDs[0][i]=id;
1433 }
1434 // Assemble over multiple processors, if necessary
1435 StiffG.GlobalAssemble(); StiffG.FillComplete();
1436
1437 rhsD.GlobalAssemble();
1438 jointMatrix.GlobalAssemble(); jointMatrix.FillComplete();
1439 jointVector.GlobalAssemble();
1440 jointVectorGIDs.GlobalAssemble();
1441
1442
1443
1444 // Adjust matrices and rhs due to boundary conditions
1445 //for this, we need number of structures on boundary
1446 int numBCNodes=0;
1447 for (int i=0; i<numNodes; i++){
1448 if (nodeOnBoundary(i) && nodeIsOwned[i]){
1449 numBCNodes++;
1450 }
1451 }
1452
1453 int indbc=0;
1454 int indOwned=0;
1455
1456 // Vector for use in applying BCs
1457 Epetra_MultiVector v(globalMapJoint,true);
1458 v.PutScalar(0.0);
1459
1460 int * BCNodes = new int [numBCNodes];
1461 indbc=0;
1462 indOwned=0;
1463 for (int i=0; i<numNodes; i++){
1464 if (nodeIsOwned[i]){
1465 if (nodeOnBoundary(i)){
1466 BCNodes[indbc]=indOwned+numOwnedFaces;
1467 indbc++;
1468 double x = nodeCoordx[i]; double y = nodeCoordy[i]; double z = nodeCoordz[i];
1469 v[0][indOwned+numOwnedFaces]= evalPhi(x,y,z);
1470 }
1471 indOwned++;
1472 }
1473 }
1474
1475 //now multiply VectorBConly by full matrix
1476 Epetra_MultiVector rhsDir(globalMapJoint,true);
1477 jointMatrix.Apply(v,rhsDir);
1478
1479 //now subtract
1480 // Update right-hand side
1481 jointVector.Update(-1.0,rhsDir,1.0);
1482
1483 //now substitute vals corresponding to BC rows into RHS
1484 indbc=0;
1485 indOwned=0;
1486 for (int i=0; i<numNodes; i++){
1487 if (nodeIsOwned[i]){
1488 if (nodeOnBoundary(i)){
1489 indbc++;
1490 double x = nodeCoordx[i]; double y = nodeCoordy[i]; double z = nodeCoordz[i];
1491 jointVector[0][indOwned+numOwnedFaces]= evalPhi(x,y,z);
1492 }
1493 indOwned++;
1494 }
1495 }
1496
1497 ML_Epetra::Apply_OAZToMatrix(BCNodes, numBCNodes, jointMatrix);
1498
1499 delete [] BCNodes;
1500
1501
1502
1503 #ifdef DUMP_DATA
1504 // Dump matrices without boundary condition corrections to disk
1505 EpetraExt::RowMatrixToMatlabFile("facenode.dat",FaceNode);
1506 EpetraExt::RowMatrixToMatlabFile("d1.dat",DCurl);
1507 EpetraExt::RowMatrixToMatlabFile("d0.dat",DGrad);
1508 EpetraExt::RowMatrixToMatlabFile("stiffGp.dat",StiffG);
1509 EpetraExt::MultiVectorToMatrixMarketFile("rhsDp.dat",rhsD,0,0,false);
1510
1511 EpetraExt::RowMatrixToMatlabFile("jointMatrixDarcy1.dat",jointMatrix);
1512 EpetraExt::MultiVectorToMatrixMarketFile("jointVectorDarcy1.dat",jointVector,0,0,false);
1513 EpetraExt::MultiVectorToMatrixMarketFile("jointVectorGIDsDarcy1.dat",jointVectorGIDs,0,0,false);
1514 {
1515 char str2[80];
1516 sprintf(str2,"myvector_%d.dat",MyPID);
1517 FILE *f=fopen(str2,"w");
1518 for(int i=0;i<jointVector.MyLength();i++)
1519 fprintf(f,"%d %22.16e\n",jointVector.Map().GID(i),jointVector[0][i]);
1520 fclose(f);
1521 }
1522 #endif
1523
1524
1525 // Build GID lists and BlockedEpetraOperator for Teko
1526 std::vector<std::vector<int> > tvec;
1527 tvec.resize(2);
1528 tvec[0].resize(globalMapD.NumMyElements());
1529 tvec[1].resize(globalMapG.NumMyElements());
1530 for(int i=0;i<globalMapD.NumMyElements();i++)
1531 tvec[0][i]=globalMapD.GID(i);
1532 for(int i=0;i<globalMapG.NumMyElements();i++)
1533 tvec[1][i]=globalMapG.GID(i)+numFacesGlobal;
1534 RCP<Teko::Epetra::BlockedEpetraOperator> BlockOp=rcp(new Teko::Epetra::BlockedEpetraOperator(tvec,rcp(&jointMatrix,false)));
1535
1536 // Grab diagonal blocks
1537 RCP<const Epetra_CrsMatrix> A00=rcp_dynamic_cast<const Epetra_CrsMatrix>(BlockOp->GetBlock(0,0));
1538 RCP<const Epetra_CrsMatrix> A11=rcp_dynamic_cast<const Epetra_CrsMatrix>(BlockOp->GetBlock(1,1));
1539
1540 // Make H(Div) preconditioner A00
1541 Teuchos::ParameterList ListHdiv, List_Coarse;
1542 List_Coarse.set("PDE equations",3);
1543 List_Coarse.set("x-coordinates",Nx.Values());
1544 List_Coarse.set("y-coordinates",Ny.Values());
1545 List_Coarse.set("z-coordinates",Nz.Values());
1546 List_Coarse.set("ML output",10);
1547 List_Coarse.set("smoother: type","Chebyshev");
1548 List_Coarse.set("smoother: sweeps",2);
1549 List_Coarse.set("coarse: max size",1000);
1550
1551 Teuchos::ParameterList List11,List11c,List22,List22c;
1552 ML_Epetra::UpdateList(List_Coarse,List11,true);
1553 List11.set("smoother: type","do-nothing");
1554 List11.set("smoother: sweeps",0);
1555 ML_Epetra::UpdateList(List_Coarse,List22,true);
1556 ML_Epetra::UpdateList(List_Coarse,List11c,true);
1557 #ifdef HAVE_ML_ZOLTAN
1558 List11c.set("aggregation: type","Uncoupled");
1559 List11c.set("repartition: enable",1);
1560 List11c.set("repartition: Zoltan dimensions",3);
1561 List11c.set("repartition: max min ratio",1.4);
1562 List11c.set("repartition: min per proc",1000);
1563 #endif
1564 ML_Epetra::UpdateList(List_Coarse,List22c,true);
1565 #ifdef HAVE_ML_ZOLTAN
1566 List22c.set("aggregation: type","Uncoupled");
1567 List22c.set("repartition: enable",1);
1568 List22c.set("repartition: Zoltan dimensions",3);
1569 List22c.set("repartition: max min ratio",1.4);
1570 List22c.set("repartition: min per proc",1000);
1571 #endif
1572
1573 List11.set("face matrix free: coarse",List11c);
1574 List22.set("edge matrix free: coarse",List22c);
1575 ListHdiv.setName("graddiv list");
1576 ListHdiv.set("graddiv: 11list",List11);
1577 ListHdiv.set("graddiv: 22list",List22);
1578 ListHdiv.set("smoother: sweeps",2);
1579 ListHdiv.set("ML output",10);
1580 ListHdiv.set("smoother: type","Chebyshev");
1581
1582 ML_Epetra::SetDefaultsGradDiv(ListHdiv,false);
1583
1584
1585 RCP<GradDivPreconditioner> Prec0=rcp(new GradDivPreconditioner(*A00,FaceNode,DCurl,DGrad,StiffG,ListHdiv));
1586
1587
1588 // Make H(Grad) preconditioner A11
1589 Teuchos::ParameterList ListHgrad;
1590 ML_Epetra::SetDefaults("SA",ListHgrad);
1591 ListHgrad.set("smoother: sweeps",2);
1592 ListHgrad.set("smoother: type","Chebyshev");
1593 ListHgrad.set("coarse: type","Amesos-KLU");
1594 ListHgrad.set("coarse: max size",1000);
1595 ListHgrad.set("ML output",10);
1596 ListHgrad.set("ML label","Poisson solver");
1597 ListHgrad.set("aggregation: threshold",0.01);
1598
1599 #ifdef HAVE_ML_ZOLTAN
1600 ListHgrad.set("aggregation: type","Uncoupled");
1601 ListHgrad.set("repartition: enable",1);
1602 ListHgrad.set("repartition: Zoltan dimensions",3);
1603 ListHgrad.set("repartition: max min ratio",1.4);
1604 ListHgrad.set("repartition: min per proc",1000);
1605 ListHgrad.set("x-coordinates",Nx.Values());
1606 ListHgrad.set("y-coordinates",Ny.Values());
1607 ListHgrad.set("z-coordinates",Nz.Values());
1608 #endif
1609
1610 RCP<MultiLevelPreconditioner> Prec1=rcp(new MultiLevelPreconditioner(*A11,ListHgrad));
1611
1612 // Build the linear ops with the Apply/ApplyInverse switcheroo
1613 RCP<const EpetraLinearOp> invD0=epetraLinearOp(Prec0, NOTRANS,EPETRA_OP_APPLY_APPLY_INVERSE);
1614 RCP<const EpetraLinearOp> invD1=epetraLinearOp(Prec1, NOTRANS,EPETRA_OP_APPLY_APPLY_INVERSE);
1615
1616 // Build the full preconditioner
1617 JacobiPreconditionerFactory precFact(rcp(new Teko::StaticInvDiagStrategy(invD0,invD1)));
1618 Teko::Epetra::EpetraBlockPreconditioner FullPrec(rcpFromRef(precFact));
1619 FullPrec.buildPreconditioner(BlockOp);
1620
1621
1622 //set up linear system
1623 Epetra_FEVector xx(globalMapJoint);
1624 xx.PutScalar(0.0);
1625
1626 Epetra_LinearProblem Problem(&jointMatrix,&xx,&jointVector);
1627 Epetra_Time Time2(jointMatrix.Comm());
1628
1629 AztecOO solver(Problem);
1630
1631 // tell AztecOO to use this preconditioner, then solve
1632 solver.SetPrecOperator(&FullPrec);
1633 //solver.SetAztecOption(AZ_solver, AZ_gmres);
1634 solver.SetAztecOption(AZ_solver, AZ_cg);
1635 solver.SetAztecOption(AZ_output, 10);
1636
1637 //solve linear system
1638 solver.Iterate(1000, 1e-8);
1639
1640 Epetra_FEVector globalSoln(globalMapJoint);
1641 globalSoln = xx;
1642 //or
1643 //Epetra_MultiVector globalSoln = *lhs;
1644
1645 // note: solution map will be same globalJointMap!
1646
1647
1648 // Get exact solution at nodes, keep the order of soln same as in jointMap
1649 double * exactSoln = new double [numOwnedNodes];
1650
1651 for (int nn = 0; nn<numOwnedNodes; nn++) {
1652 int globId = ownedGIDs[nn];
1653 for (int search = 0; search<numNodes; search++){
1654 if ((nodeIsOwned[search])&&(globalNodeIds[search]==globId)){
1655 int ind=search;
1656 double x = nodeCoordx[ind];
1657 double y = nodeCoordy[ind];;
1658 double z = nodeCoordz[ind];;
1659 double exactu = evalPhi(x,y,z);
1660 exactSoln[nn]=exactu;
1661 search=numNodes+1;
1662 }
1663 }
1664 }
1665
1666
1667 //primitive max error check, no measures, just max of Error over all nodes
1668 double maxerr=0,maxerrGlobal=0;
1669 for (int i=numOwnedFaces; i<jointLocalVarSize; i++)
1670 maxerr=max(abs(exactSoln[i-numOwnedFaces]-globalSoln[0][i]),maxerr);
1671
1672 Comm.MaxAll(&maxerr,&maxerrGlobal,1);
1673 if(!Comm.MyPID())
1674 std::cout << "Max Error over all nodes: "<<maxerrGlobal<<endl;
1675
1676 #ifdef DUMP_DATA
1677 EpetraExt::MultiVectorToMatrixMarketFile("solnVectorDarcy.dat",globalSoln,0,0,false);
1678 #endif
1679 // ******** Calculate Error in Solution ***************
1680 // Import solution onto current processor
1681
1682 Epetra_Map solnMap(static_cast<int>(numFacesGlobal+numNodesGlobal), static_cast<int>(numFacesGlobal+numNodesGlobal), 0, Comm);
1683 Epetra_Import solnImporter(solnMap, globalMapJoint);
1684 Epetra_FEVector uCoeff(solnMap);
1685 uCoeff.Import(globalSoln, solnImporter, Insert);
1686
1687 double L2err_u = 0.0;
1688 double HDiverr_u = 0.0;
1689 double Linferr_u = 0.0;
1690 double L2errTot_u = 0.0;
1691 double HDiverrTot_u = 0.0;
1692 double LinferrTot_u = 0.0;
1693
1694 double L2err_phi = 0.0;
1695 double H1err_phi = 0.0;
1696 double L2errTot_phi = 0.0;
1697 double H1errTot_phi = 0.0;
1698
1699
1700 // Get cubature points and weights for error calc (may be different from previous)
1701 DefaultCubatureFactory<double> cubFactoryErr;
1702 int cubDegErr = 3;
1703 Teuchos::RCP<Cubature<double> > hexCubErr = cubFactoryErr.create(hex_8, cubDegErr);
1704 int cubDimErr = hexCubErr->getDimension();
1705 int numCubPointsErr = hexCubErr->getNumPoints();
1706 FieldContainer<double> cubPointsErr(numCubPointsErr, cubDimErr);
1707 FieldContainer<double> cubWeightsErr(numCubPointsErr);
1708 hexCubErr->getCubature(cubPointsErr, cubWeightsErr);
1709 FieldContainer<double> physCubPointsE(numCells,numCubPointsErr, cubDimErr);
1710
1711 // Containers for Jacobian
1712 FieldContainer<double> hexJacobianE(numCells, numCubPointsErr, spaceDim, spaceDim);
1713 FieldContainer<double> hexJacobInvE(numCells, numCubPointsErr, spaceDim, spaceDim);
1714 FieldContainer<double> hexJacobDetE(numCells, numCubPointsErr);
1715 FieldContainer<double> weightedMeasureE(numCells, numCubPointsErr);
1716
1717 // Evaluate basis values and divs at cubature points
1718 FieldContainer<double> uhDVals(numFieldsD, numCubPointsErr, spaceDim);
1719 FieldContainer<double> uhDValsTrans(numCells,numFieldsD, numCubPointsErr, spaceDim);
1720 FieldContainer<double> uhDivs(numFieldsD, numCubPointsErr);
1721 FieldContainer<double> uhDivsTrans(numCells, numFieldsD, numCubPointsErr);
1722 hexHDivBasis.getValues(uhDVals, cubPointsErr, OPERATOR_VALUE);
1723 hexHDivBasis.getValues(uhDivs, cubPointsErr, OPERATOR_DIV);
1724
1725
1726 FieldContainer<double> phihGVals(numFieldsG, numCubPointsErr);
1727 FieldContainer<double> phihGValsTrans(numCells,numFieldsG, numCubPointsErr);
1728 FieldContainer<double> phihGrads(numFieldsG, numCubPointsErr, spaceDim);
1729 FieldContainer<double> phihGradsTrans(numCells, numFieldsG, numCubPointsErr, spaceDim);
1730 hexHGradBasis.getValues(phihGVals, cubPointsErr, OPERATOR_VALUE);
1731 hexHGradBasis.getValues(phihGrads, cubPointsErr, OPERATOR_GRAD);
1732
1733
1734 // Loop over elements
1735 for (int k=0; k<numElems; k++){
1736
1737 double L2errElem_u = 0.0;
1738 double HDiverrElem_u = 0.0;
1739 double uExact1, uExact2, uExact3;
1740 double divuExact;
1741
1742 double L2errElem_phi = 0.0;
1743 double H1errElem_phi = 0.0;
1744 double phiExact;
1745 double gradphiExact1, gradphiExact2, gradphiExact3;
1746
1747 // physical cell coordinates
1748 for (int i=0; i<numNodesPerElem; i++) {
1749 hexNodes(0,i,0) = nodeCoord(elemToNode(k,i),0);
1750 hexNodes(0,i,1) = nodeCoord(elemToNode(k,i),1);
1751 hexNodes(0,i,2) = nodeCoord(elemToNode(k,i),2);
1752 }
1753 // Face signs
1754 for (int j=0; j<numFacesPerElem; j++) {
1755 hexFaceSigns(0,j) = -1.0;
1756 for (int i=0; i<numNodesPerFace; i++) {
1757 int indf=i+1;
1758 if (indf > numNodesPerFace) indf=0;
1759 if (elemToNode(k,refFaceToNode(j,0))==faceToNode(elemToFace(k,j),i) &&
1760 elemToNode(k,refFaceToNode(j,1))==faceToNode(elemToFace(k,j),indf))
1761 hexFaceSigns(0,j) = 1.0;
1762 }
1763 if (!faceIsOwned[elemToFace(k,j)]){
1764 hexFaceSigns(0,j)=-1.0*hexFaceSigns(0,j);
1765 }
1766 }
1767
1768 // compute cell Jacobians, their inverses and their determinants
1769 CellTools::setJacobian(hexJacobianE, cubPointsErr, hexNodes, hex_8);
1770 CellTools::setJacobianInv(hexJacobInvE, hexJacobianE );
1771 CellTools::setJacobianDet(hexJacobDetE, hexJacobianE );
1772
1773 // transform integration points to physical points
1774 CellTools::mapToPhysicalFrame(physCubPointsE, cubPointsErr, hexNodes, hex_8);
1775
1776 // transform basis values to physical coordinates
1777 fst::HDIVtransformVALUE<double>(uhDValsTrans, hexJacobianE, hexJacobDetE, uhDVals);
1778 fst::HDIVtransformDIV<double>(uhDivsTrans, hexJacobDetE, uhDivs);
1779
1780 // transform basis values to physical coordinates
1781 fst::HGRADtransformVALUE<double>(phihGValsTrans, phihGVals);
1782 fst::HGRADtransformGRAD<double>(phihGradsTrans, hexJacobInvE, phihGrads);
1783
1784 // compute weighted measure
1785 fst::computeCellMeasure<double>(weightedMeasureE, hexJacobDetE, cubWeightsErr);
1786
1787 // loop over cubature points
1788 for (int nPt = 0; nPt < numCubPointsErr; nPt++){
1789
1790 // get exact solution and divs
1791 double x = physCubPointsE(0,nPt,0);
1792 double y = physCubPointsE(0,nPt,1);
1793 double z = physCubPointsE(0,nPt,2);
1794 evalu(uExact1, uExact2, uExact3, x, y, z);
1795 divuExact = evalDivu(x, y, z);
1796 phiExact = evalPhi(x, y, z);
1797 evalGradPhi(gradphiExact1, gradphiExact2, gradphiExact3, x, y, z);
1798
1799 // calculate approximate solution and divs
1800 double uApprox1 = 0.0;
1801 double uApprox2 = 0.0;
1802 double uApprox3 = 0.0;
1803 double divuApprox = 0.0;
1804
1805 double phiApprox = 0.;
1806 double gradphiApprox1 = 0.;
1807 double gradphiApprox2 = 0.;
1808 double gradphiApprox3 = 0.;
1809
1810 for (int i = 0; i < numFieldsD; i++){
1811 int rowIndex = globalFaceIds[elemToFace(k,i)];
1812
1813 //was rowIndex
1814 double uh1 = uCoeff.Values()[rowIndex];
1815
1816 uApprox1 += uh1*uhDValsTrans(0,i,nPt,0)*hexFaceSigns(0,i);
1817 uApprox2 += uh1*uhDValsTrans(0,i,nPt,1)*hexFaceSigns(0,i);
1818 uApprox3 += uh1*uhDValsTrans(0,i,nPt,2)*hexFaceSigns(0,i);
1819 divuApprox += uh1*uhDivsTrans(0,i,nPt)*hexFaceSigns(0,i);
1820 }
1821
1822 // evaluate the error at cubature points
1823 Linferr_u = max(Linferr_u, abs(uExact1 - uApprox1));
1824 Linferr_u = max(Linferr_u, abs(uExact2 - uApprox2));
1825 Linferr_u = max(Linferr_u, abs(uExact3 - uApprox3));
1826 L2errElem_u+=(uExact1 - uApprox1)*(uExact1 - uApprox1)*weightedMeasureE(0,nPt);
1827 L2errElem_u+=(uExact2 - uApprox2)*(uExact2 - uApprox2)*weightedMeasureE(0,nPt);
1828 L2errElem_u+=(uExact3 - uApprox3)*(uExact3 - uApprox3)*weightedMeasureE(0,nPt);
1829 HDiverrElem_u+=((divuExact - divuApprox)*(divuExact - divuApprox))
1830 *weightedMeasureE(0,nPt);
1831
1832
1833 L2err_u+=L2errElem_u;
1834 HDiverr_u+=HDiverrElem_u;
1835
1836
1837 for (int i = 0; i < numFieldsG; i++){
1838 int rowIndex = globalNodeIds[elemToNode(k,i)];
1839
1840 double phih1 = uCoeff.Values()[rowIndex+numFacesGlobal];
1841
1842 phiApprox += phih1*phihGValsTrans(0,i,nPt);
1843 gradphiApprox1 += phih1*phihGradsTrans(0,i,nPt,0);
1844 gradphiApprox2 += phih1*phihGradsTrans(0,i,nPt,1);
1845 gradphiApprox3 += phih1*phihGradsTrans(0,i,nPt,2);
1846 }
1847
1848 // evaluate the error at cubature points
1849
1850 //GET IT! later
1851 //Linferr_phi = max(Linferr_phi, abs(phiExact - phiApprox));
1852
1853 L2errElem_phi+=(phiExact - phiApprox)*(phiExact - phiApprox)*weightedMeasureE(0,nPt);
1854 H1errElem_phi+=((gradphiExact1 - gradphiApprox1)*(gradphiExact1 - gradphiApprox1))
1855 *weightedMeasureE(0,nPt);
1856 H1errElem_phi+=((gradphiExact2 - gradphiApprox2)*(gradphiExact2 - gradphiApprox2))
1857 *weightedMeasureE(0,nPt);
1858 H1errElem_phi+=((gradphiExact3 - gradphiApprox3)*(gradphiExact3 - gradphiApprox3))
1859 *weightedMeasureE(0,nPt);
1860
1861 }
1862
1863 L2err_phi+=L2errElem_phi;
1864 H1err_phi+=H1errElem_phi;
1865 }
1866
1867 // sum over all processors
1868 Comm.SumAll(&L2err_u,&L2errTot_u,1);
1869 Comm.SumAll(&HDiverr_u,&HDiverrTot_u,1);
1870 Comm.MaxAll(&Linferr_u,&LinferrTot_u,1);
1871 Comm.SumAll(&L2err_phi,&L2errTot_phi,1);
1872 Comm.SumAll(&H1err_phi,&H1errTot_phi,1);
1873
1874
1875 if (MyPID == 0) {
1876 std::cout << "\n" << "L2 Error U: " << sqrt(L2errTot_u) <<"\n";
1877 std::cout << "HDiv Error U: " << sqrt(HDiverrTot_u) <<"\n";
1878 std::cout << "LInf Error U: " << LinferrTot_u <<"\n\n";
1879 std::cout << "\n" << "L2 Error: Phi " << sqrt(L2errTot_phi) <<"\n";
1880 std::cout << "H1 Error: Phi " << sqrt(H1errTot_phi) <<"\n";
1881 }
1882
1883
1884 // delete mesh
1885 Delete_Pamgen_Mesh();
1886
1887
1888
1889 //clean up
1890 for(long long b = 0; b < numElemBlk; b++){
1891 delete [] elmt_node_linkage[b];
1892 delete [] element_types[b];
1893 }
1894 delete [] block_ids;
1895 delete [] nodes_per_element;
1896 delete [] element_attributes;
1897 delete [] element_types;
1898 delete [] elmt_node_linkage;
1899 delete [] elements;
1900 delete [] nodeCoordx;
1901 delete [] nodeCoordy;
1902 delete [] nodeCoordz;
1903
1904 delete [] globalNodeIds;
1905 delete [] nodeIsOwned;
1906 delete [] globalEdgeIds;
1907 delete [] edgeIsOwned;
1908 delete [] globalFaceIds;
1909 delete [] faceIsOwned;
1910
1911 delete [] ownedGIDs;
1912 delete [] exactSoln;
1913 delete [] ownedFaceIds;
1914 delete [] ownedEdgeIds;
1915 delete [] ownedFaceNodeIds;
1916
1917
1918 if(num_node_comm_maps > 0){
1919 delete [] node_comm_proc_ids;
1920 delete [] node_cmap_node_cnts;
1921 delete [] node_cmap_ids;
1922 for(long long i=0;i<num_node_comm_maps;i++){
1923 delete [] comm_node_ids[i];
1924 delete [] comm_node_proc_ids[i];
1925 }
1926
1927 delete [] comm_node_ids;
1928 delete [] comm_node_proc_ids;
1929 }
1930
1931
1932 return 0;
1933 }
1934
1935 template<typename Scalar>
1936 void materialTensor(Scalar material[][3], const Scalar& x, const Scalar& y, const Scalar& z) {
1937
1938 Scalar kappa=evalKappa(x,y,z);
1939
1940
1941
1942 material[0][0] = kappa;
1943 material[0][1] = 0.;
1944 material[0][2] = 0.;
1945 //
1946 material[1][0] = 0.;
1947 material[1][1] = kappa;
1948 material[1][2] = 0.;
1949 //
1950 material[2][0] = 0.;
1951 material[2][1] = 0.;
1952 material[2][2] = kappa;
1953
1954 }
1955
1956 template<typename Scalar>
1957 void materialTensorInv(Scalar material[][3], const Scalar& x, const Scalar& y, const Scalar& z) {
1958
1959 Scalar kappa=evalKappa(x,y,z);
1960
1961 kappa=1./kappa;
1962
1963 material[0][0] = kappa;
1964 material[0][1] = 0.;
1965 material[0][2] = 0.;
1966 //
1967 material[1][0] = 0.;
1968 material[1][1] = kappa;
1969 material[1][2] = 0.;
1970 //
1971 material[2][0] = 0.;
1972 material[2][1] = 0.;
1973 material[2][2] = kappa;
1974
1975
1976
1977 }
1978
1979
1980 template<class ArrayOut, class ArrayIn>
1981 void evaluateMaterialTensor(ArrayOut & matTensorValues,
1982 const ArrayIn & evaluationPoints){
1983
1984 //int numWorksetCells = evaluationPoints.dimension(0);
1985 int numPoints = evaluationPoints.dimension(1);
1986 int spaceDim = evaluationPoints.dimension(2);
1987
1988 double material[3][3];
1989
1990 //for(int cell = 0; cell < numWorksetCells; cell++){
1991 for(int pt = 0; pt < numPoints; pt++){
1992
1993 double x = evaluationPoints(0, pt, 0);
1994 double y = evaluationPoints(0, pt, 1);
1995 double z = evaluationPoints(0, pt, 2);
1996
1997 materialTensor<double>(material, x, y, z);
1998
1999 for(int row = 0; row < spaceDim; row++){
2000 for(int col = 0; col < spaceDim; col++){
2001 matTensorValues(0, pt, row, col) = material[row][col];
2002 }
2003 }
2004 }
2005 //}
2006 }
2007
2008
2009 template<class ArrayOut, class ArrayIn>
2010 void evaluateMaterialTensorInv(ArrayOut & matTensorValues,
2011 const ArrayIn & evaluationPoints){
2012
2013 //int numWorksetCells = evaluationPoints.dimension(0);
2014 int numPoints = evaluationPoints.dimension(1);
2015 int spaceDim = evaluationPoints.dimension(2);
2016
2017 double material[3][3];
2018
2019 //for(int cell = 0; cell < numWorksetCells; cell++){
2020 for(int pt = 0; pt < numPoints; pt++){
2021
2022 double x = evaluationPoints(0, pt, 0);
2023 double y = evaluationPoints(0, pt, 1);
2024 double z = evaluationPoints(0, pt, 2);
2025
2026 materialTensorInv<double>(material, x, y, z);
2027
2028 for(int row = 0; row < spaceDim; row++){
2029 for(int col = 0; col < spaceDim; col++){
2030 matTensorValues(0, pt, row, col) = material[row][col];
2031 }
2032 }
2033 }
2034 //}
2035 }
2036
2037
2038 template<typename Scalar>
2039 Scalar evalKappa(const Scalar& x, const Scalar& y, const Scalar& z){
2040
2041 Scalar kappa=0;
2042
2043 #ifdef SMOOTH
2044 kappa=1.;
2045 #endif
2046
2047
2048 #ifdef PATCH
2049 if((y>=0)&&(y<=.2)) kappa=16;
2050 if((y>.2)&&(y<=.4)) kappa=6;
2051 if((y>.4)&&(y<=.6)) kappa=1;
2052 if((y>.6)&&(y<=.8)) kappa=10;
2053 if((y>.8)&&(y<=1.0)) kappa=2;
2054 #endif
2055
2056 return kappa;
2057 }
2058
2059 template<typename Scalar>
2060 Scalar evalF(const Scalar& x, const Scalar& y, const Scalar& z){
2061
2062 //patch test
2063 #ifdef PATCH
2064 return 1 - x;
2065 #endif
2066 //smooth solution
2067
2068 #ifdef SMOOTH
2069 Scalar a;
2070 double pi = 3.1415926535897932384626433;
2071 a = 2*pi*pi*cos(2*pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z);
2072 a += 2*pi*pi*cos(2*pi*y)*sin(pi*x)*sin(pi*x)*sin(pi*z)*sin(pi*z);
2073 a += 2*pi*pi*cos(2*pi*z)*sin(pi*y)*sin(pi*y)*sin(pi*x)*sin(pi*x);
2074 a += -sin(pi*x)*sin(pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z);
2075 return a;
2076 #endif
2077
2078 }
2079
2080 template<typename Scalar>
2081 Scalar evalPhi(const Scalar& x, const Scalar& y, const Scalar& z){
2082
2083 //patch test
2084 #ifdef PATCH
2085 return 1 - x;
2086 #endif
2087
2088 //smooth solution
2089 #ifdef SMOOTH
2090 double pi = 3.1415926535897932384626433;
2091 return -sin(pi*x)*sin(pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z);
2092 #endif
2093 }
2094
2095 // Calculates divergence of exact solution u
2096 template<typename Scalar> Scalar evalDivu(Scalar & x, Scalar & y, Scalar & z){
2097
2098 //patch test
2099 #ifdef PATCH
2100 return 0.0;
2101 #endif
2102 //smooth solution
2103 #ifdef SMOOTH
2104 Scalar a;
2105 double pi = 3.1415926535897932384626433;
2106 a = 2*pi*pi*cos(2*pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z);
2107 a += 2*pi*pi*cos(2*pi*y)*sin(pi*x)*sin(pi*x)*sin(pi*z)*sin(pi*z);
2108 a += 2*pi*pi*cos(2*pi*z)*sin(pi*y)*sin(pi*y)*sin(pi*x)*sin(pi*x);
2109 return a;
2110 #endif
2111 }
2112
2113 // Calculates value of exact solution u
2114 template<typename Scalar> int evalu(Scalar & uExact0, Scalar & uExact1, Scalar & uExact2, Scalar & x, Scalar & y, Scalar & z){
2115
2116 //patch test
2117
2118 #ifdef PATCH
2119 uExact0 = evalKappa(x,y,z);
2120 uExact1 = 0.0;
2121 uExact2 = 0.0;
2122 #endif
2123
2124 #ifdef SMOOTH
2125 //smooth solution
2126 double pi = 3.1415926535897932384626433;
2127 uExact0 = pi*sin(2*pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z);
2128 uExact1 = pi*sin(2*pi*y)*sin(pi*x)*sin(pi*x)*sin(pi*z)*sin(pi*z);
2129 uExact2 = pi*sin(2*pi*z)*sin(pi*y)*sin(pi*y)*sin(pi*x)*sin(pi*x);
2130 #endif
2131
2132 return 0;
2133 }
2134
2135 // Calculates value of exact solution grad phi
2136 template<typename Scalar> int evalGradPhi(Scalar & phiGExact0, Scalar & phiGExact1, Scalar & phiGExact2, Scalar & x, Scalar & y, Scalar & z){
2137
2138 //patch test
2139
2140 #ifdef PATCH
2141 phiGExact0 = -1.;
2142 phiGExact1 = 0.0;
2143 phiGExact2 = 0.0;
2144 #endif
2145
2146 #ifdef SMOOTH
2147 //smooth solution
2148 double pi = 3.1415926535897932384626433;
2149 phiGExact0 = -pi*sin(2*pi*x)*sin(pi*y)*sin(pi*y)*sin(pi*z)*sin(pi*z);
2150 phiGExact1 = -pi*sin(2*pi*y)*sin(pi*x)*sin(pi*x)*sin(pi*z)*sin(pi*z);
2151 phiGExact2 = -pi*sin(2*pi*z)*sin(pi*y)*sin(pi*y)*sin(pi*x)*sin(pi*x);
2152 #endif
2153
2154 return 0;
2155 }