Debian Codegraph @

packages/trilinoscouplings/examples/scaling/TrilinosCouplings_Statistics.hpp

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 #ifndef __TrilinosCouplings_Statistics_hpp
11 #define __TrilinosCouplings_Statistics_hpp
12
13 // Intrepid includes
14 #include <Intrepid_FunctionSpaceTools.hpp>
15 #include <Intrepid_CellTools.hpp>
16 #include <Intrepid_ArrayTools.hpp>
17 #include <Intrepid_RealSpaceTools.hpp>
18 #include <Intrepid_Utils.hpp>
19
20 // Xpetra
21 #include <Xpetra_MultiVector.hpp>
22 #include <Xpetra_MultiVectorFactory.hpp>
23 #include <Xpetra_Vector.hpp>
24 #include <Xpetra_VectorFactory.hpp>
25 #include <Xpetra_CrsGraph.hpp>
26
27 #ifdef HAVE_XPETRA_TPETRA
28 #include <Xpetra_TpetraMultiVector.hpp>
29 #include <Xpetra_TpetraCrsGraph.hpp>
30 #endif
31
32 #ifdef HAVE_XPETRA_EPETRA
33 #include <Xpetra_EpetraMultiVector.hpp>
34 #include <Xpetra_EpetraCrsGraph.hpp>
35 #endif
36
37
38 // Teuchos
39 #include <Teuchos_Comm.hpp>
40
41
42 template<class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
43 class MachineLearningStatistics_Hex3D {
44 using ST = Scalar;
45 using LO = LocalOrdinal;
46 using GO = GlobalOrdinal;
47 using crsgraph_type = Xpetra::CrsGraph<LO, GO, Node>;
48 using multivector_type = Xpetra::MultiVector<ST, LO, GO, Node>;
49 using vector_type = Xpetra::Vector<ST, LO, GO, Node>;
50
51 public:
52
53 MachineLearningStatistics_Hex3D(long long numElemsGlobal_):numElemsGlobal(numElemsGlobal_) {
54 local_stat_max.resize(NUM_STATISTICS);
55 local_stat_min.resize(NUM_STATISTICS);
56 local_stat_sum.resize(NUM_STATISTICS);
57 global_stat_max.resize(NUM_STATISTICS);
58 global_stat_min.resize(NUM_STATISTICS);
59 global_stat_sum.resize(NUM_STATISTICS);
60
61 for(int i=0; i<NUM_STATISTICS; i++) {
62 local_stat_max[i] = 0.0;
63 local_stat_min[i] = std::numeric_limits<double>::max();
64 local_stat_sum[i] = 0.0;
65 }
66 }
67
68 double distance2(Intrepid::FieldContainer<Scalar> & coord, int n1, int n2) {
69 double dist = 0.0;
70 for(int i=0; i<coord.dimension(1); i++)
71 dist += (coord(n2,i) -coord(n1,i)) * (coord(n2,i) -coord(n1,i));
72 return sqrt(dist);
73 }
74
75 double myDistance2(const Xpetra::MultiVector<ST, LO, GO, Node> &v, int i0, int i1) {
76 const size_t numVectors = v.getNumVectors();
77 double distance = 0.0;
78 for (size_t j=0; j<numVectors; j++) {
79 distance += (v.getData(j)[i0]-v.getData(j)[i1])*(v.getData(j)[i0]-v.getData(j)[i1]);
80 }
81 return distance;
82 }
83
84
85
86 /**********************************************************************************/
87 /****************************** STATISTICS (Part I) *******************************/
88 /**********************************************************************************/
89 // Statistics: Compute max / min of sigma parameter, mesh information
90 void Phase1(Intrepid::FieldContainer<int>&elemToNode,Intrepid::FieldContainer<int>&elemToEdge,Intrepid::FieldContainer<int>&edgeToNode,Intrepid::FieldContainer<double>&nodeCoord,Intrepid::FieldContainer<double>&sigmaVal) {
91 double maxmin_ratio = 0;
92 double stretch = 0;
93 double diag_ratio = 0;
94 double diag_length_max = 0;
95 double diag_length_min = 0;
96 double edge_length_max = 0;
97 double edge_length_min = 0;
98 double principle_axis_1[] = {0, 0, 0};
99 double principle_axis_2[] = {0, 0, 0};
100 double principle_axis_3[] = {0, 0, 0};
101 double hex_0[] = {0, 0, 0};
102 double hex_1[] = {0, 0, 0};
103 double hex_2[] = {0, 0, 0};
104 double hex_3[] = {0, 0, 0};
105 double hex_4[] = {0, 0, 0};
106 double hex_5[] = {0, 0, 0};
107 double hex_6[] = {0, 0, 0};
108 double hex_7[] = {0, 0, 0};
109
110 double dist = 0;
111 int diag_nodes1[] = {0, 1, 2, 3};
112 int diag_nodes2[] = {6, 7, 4, 5};
113 int edge_nodes_1[] = {0, 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6};
114 int edge_nodes_2[] = {1, 3, 4, 2, 5, 3, 6, 7, 5, 7, 6, 7};
115 int edge_opposites_1[] = { 0, 1, 2, 3, 4, 5};
116 int edge_opposites_2[] = {11, 10, 6, 9, 7, 8};
117 double edge_length_1 = 0;
118 double edge_length_2 = 0;
119
120 int edge = elemToEdge(0, 0);
121 int node1 = edgeToNode(edge, 0);
122 int node2 = edgeToNode(edge, 1);
123 int node3 = edgeToNode(edge, 0);
124 int node4 = edgeToNode(edge, 1);
125
126 int numElems = elemToNode.dimension(0);
127 for(int i=0; i<numElems; i++) {
128 // Set up hex nodes
129 int hexnode0 = elemToNode(i, 0);
130 int hexnode1 = elemToNode(i, 1);
131 int hexnode2 = elemToNode(i, 2);
132 int hexnode3 = elemToNode(i, 3);
133 int hexnode4 = elemToNode(i, 4);
134 int hexnode5 = elemToNode(i, 5);
135 int hexnode6 = elemToNode(i, 6);
136 int hexnode7 = elemToNode(i, 7);
137
138 // Now I have to get the node coordinates
139 for(int j=0; j<dim; j++) {
140 hex_0[j] = nodeCoord(hexnode0, j);
141 hex_1[j] = nodeCoord(hexnode1, j);
142 hex_2[j] = nodeCoord(hexnode2, j);
143 hex_3[j] = nodeCoord(hexnode3, j);
144 hex_4[j] = nodeCoord(hexnode4, j);
145 hex_5[j] = nodeCoord(hexnode5, j);
146 hex_6[j] = nodeCoord(hexnode6, j);
147 hex_7[j] = nodeCoord(hexnode7, j);
148 }
149
150 double pr1_norm = 0;
151 double pr2_norm = 0;
152 double pr3_norm = 0;
153 for(int j=0; j<dim; j++){
154 principle_axis_1[j] = hex_1[j] - hex_0[j] + hex_2[j] - hex_3[j] + hex_5[j] - hex_4[j] + hex_6[j] - hex_7[j];
155 pr1_norm += principle_axis_1[j] * principle_axis_1[j];
156 principle_axis_2[j] = hex_3[j] - hex_0[j] + hex_2[j] - hex_1[j] + hex_7[j] - hex_4[j] + hex_6[j] - hex_5[j];
157 pr2_norm += principle_axis_2[j] * principle_axis_2[j];
158 principle_axis_3[j] = hex_4[j] - hex_0[j] + hex_5[j] - hex_1[j] + hex_6[j] - hex_2[j] + hex_7[j] - hex_3[j];
159 pr3_norm += principle_axis_3[j] * principle_axis_3[j];
160 }
161
162 pr1_norm = sqrt(pr1_norm);
163 pr2_norm = sqrt(pr2_norm);
164 pr3_norm = sqrt(pr3_norm);
165
166 for(int j=0; j<dim; j++) {
167 principle_axis_1[j] = principle_axis_1[j] / pr1_norm;
168 principle_axis_2[j] = principle_axis_2[j] / pr2_norm;
169 principle_axis_3[j] = principle_axis_3[j] / pr3_norm;
170 }
171
172
173 // 0 - Material property
174 local_stat_max[0] = std::max(local_stat_max[0],sigmaVal(i));
175 local_stat_min[0] = std::min(local_stat_min[0],sigmaVal(i));
176 local_stat_sum[0] += sigmaVal(i);
177
178 edge = elemToEdge(i, 0);
179 node1 = edgeToNode(edge, 0);
180 node2 = edgeToNode(edge, 1);
181
182 // 1 - Max/min edge - ratio of max to min edge length
183 edge_length_max = distance2(nodeCoord, node1, node2);
184 edge_length_min = edge_length_max;
185 int numEdgesPerElem = elemToEdge.dimension(1);
186 for (int j=0; j<numEdgesPerElem; j++) {
187 edge = elemToEdge(i,j);
188 node1 = edgeToNode(edge,0);
189 node2 = edgeToNode(edge,1);
190 dist = distance2(nodeCoord,node1,node2);
191 edge_length_max = std::max(edge_length_max,dist);
192 edge_length_min = std::min(edge_length_min,dist);
193 }
194 maxmin_ratio = edge_length_max / edge_length_min;
195 local_stat_max[1] = std::max(local_stat_max[1],maxmin_ratio);
196 local_stat_min[1] = std::min(local_stat_min[1],maxmin_ratio);
197 local_stat_sum[1] += maxmin_ratio;
198
199 // 2 - det of cell Jacobian (later)
200
201 // 3 - Stretch
202 diag_length_max = distance2(nodeCoord, elemToNode(i, diag_nodes1[0]),
203 elemToNode(i, diag_nodes2[0]));
204 diag_length_min = distance2(nodeCoord, elemToNode(i, diag_nodes1[0]),
205 elemToNode(i, diag_nodes2[0]));
206 for (int j=0; j<NUM_NODE_PAIRS; j++) {
207 node1 = elemToNode(i, diag_nodes1[j]);
208 node2 = elemToNode(i, diag_nodes2[j]);
209 dist = distance2(nodeCoord, node1, node2);
210 diag_length_max = std::max(diag_length_max, dist);
211 diag_length_min = std::min(diag_length_min, dist);
212 }
213 stretch = sqrt(3) * edge_length_min / diag_length_max;
214 diag_ratio = diag_length_min / diag_length_max;
215 local_stat_max[3] = std::max(local_stat_max[3], stretch);
216 local_stat_min[3] = std::min(local_stat_min[3], stretch);
217 local_stat_sum[3] += stretch;
218
219 // 4 - Diagonal Ratio
220 local_stat_max[4] = std::max(local_stat_max[4], diag_ratio);
221 local_stat_min[4] = std::min(local_stat_min[4], diag_ratio);
222 local_stat_sum[4] += diag_ratio;
223
224 // 5 - Inverse Taper
225 node1 = elemToNode(i, diag_nodes1[0]);
226 node2 = elemToNode(i, diag_nodes1[1]);
227 node3 = elemToNode(i, diag_nodes2[0]);
228 node4 = elemToNode(i, diag_nodes2[1]);
229 edge_length_1 = distance2(nodeCoord, node1, node2);
230 edge_length_2 = distance2(nodeCoord, node3, node4);
231 double ratio = edge_length_1 / edge_length_2;
232 ratio = std::min(ratio, 1/ratio);
233 for (int j=0; j<NUM_EDGE_PAIRS; j++) {
234 node1 = elemToNode(i, edge_nodes_1[edge_opposites_1[j]]);
235 node2 = elemToNode(i, edge_nodes_2[edge_opposites_1[j]]);
236 node3 = elemToNode(i, edge_nodes_1[edge_opposites_2[j]]);
237 node4 = elemToNode(i, edge_nodes_2[edge_opposites_2[j]]);
238 edge_length_1 = distance2(nodeCoord, node1, node2);
239 edge_length_2 = distance2(nodeCoord, node3, node4);
240 double my_ratio = edge_length_1 / edge_length_2;
241 my_ratio = std::min(my_ratio, 1/my_ratio);
242 ratio = std::min(ratio, my_ratio);
243 }
244 local_stat_max[5] = std::max(local_stat_max[5], ratio);
245 local_stat_min[5] = std::min(local_stat_min[5], ratio);
246 local_stat_sum[5] += ratio;
247
248 // 6 - Skew
249 double skew = 0;
250 double skew1 = 0;
251 double skew2 = 0;
252 double skew3 = 0;
253
254 for(int j=0; j<dim; j++) {
255 skew1 += principle_axis_1[j] * principle_axis_2[j];
256 skew2 += principle_axis_1[j] * principle_axis_3[j];
257 skew3 += principle_axis_1[j] * principle_axis_3[j];
258 }
259 skew1 = std::abs(skew1);
260 skew2 = std::abs(skew2);
261 skew3 = std::abs(skew3);
262 skew = std::max(skew1, skew2);
263 skew = std::max(skew, skew3);
264 local_stat_max[6] = std::max(local_stat_max[6], skew);
265 local_stat_min[6] = std::min(local_stat_min[6], skew);
266 local_stat_sum[6] += skew;
267 }
268
269 }
270
271 /**********************************************************************************/
272 /***************************** STATISTICS (Part IIa) ******************************/
273 /**********************************************************************************/
274 void Phase2a(Intrepid::FieldContainer<Scalar> &worksetJacobDet,Intrepid::FieldContainer<Scalar> &worksetCubWeights) {
275 int worksetSize = worksetJacobDet.dimension(0);
276 int numCubPoints = worksetJacobDet.dimension(1);
277
278 bool weightsWorkset = (worksetCubWeights.rank()==2)?true:false;
279
280 for(int i=0; i<worksetSize; i++) {
281 // 0 - Material property
282 // 1 - Max/min edge - ratio of max to min edge length
283 // 2 - det of cell Jacobian (later)
284 double elementdetJ = 0.0, elementWeight=0.0;
285 for(int j=0; j<numCubPoints; j++) {
286 double weight = weightsWorkset ? worksetCubWeights(i,j) : worksetCubWeights(j);
287 elementdetJ += worksetJacobDet(i,j) * weight;
288 elementWeight += weight;
289 }
290 double detJ = elementdetJ / elementWeight;
291 local_stat_max[2] = std::max(local_stat_max[2],detJ);
292 local_stat_min[2] = std::min(local_stat_min[2],detJ);
293 local_stat_sum[2] += detJ;
294 }
295 }
296
297
298 /**********************************************************************************/
299 /***************************** STATISTICS (Part IIb) ******************************/
300 /**********************************************************************************/
301
302 void Phase2b(Teuchos::RCP<const Xpetra::CrsGraph<LO, GO, Node> > gl_StiffGraph, Teuchos::RCP<Xpetra::MultiVector<ST, LO, GO,Node> > coords) {
303 using multivector_factory = Xpetra::MultiVectorFactory<ST,LO,GO,Node>;
304 using vector_factory = Xpetra::VectorFactory<ST,LO,GO,Node>;
305
306 Teuchos::RCP<multivector_type> coordsOwnedPlusShared;
307 comm = gl_StiffGraph->getRowMap()->getComm();
308
309 if (!(gl_StiffGraph->getImporter().is_null())) {
310 coordsOwnedPlusShared = multivector_factory::Build(gl_StiffGraph->getColMap(), 3, true);
311 coordsOwnedPlusShared->doImport(*coords, *gl_StiffGraph->getImporter(), Xpetra::CombineMode::ADD);
312 }
313 else {
314 coordsOwnedPlusShared = coords;
315 }
316
317 Teuchos::RCP<const Xpetra::Map<LO, GO, Node> > rowMap = gl_StiffGraph->getRowMap();
318 Teuchos::RCP<vector_type > laplDiagOwned = vector_factory::Build(rowMap, true);
319 Teuchos::ArrayView<const LO> indices;
320 size_t numOwnedRows = rowMap->getLocalNumElements();
321 for (size_t row=0; row<numOwnedRows; row++) {
322 gl_StiffGraph->getLocalRowView(row, indices);
323 size_t numIndices = indices.size();
324 for (size_t j=0; j<numIndices; j++) {
325 size_t col = indices[j];
326 if (row == col) continue;
327 laplDiagOwned->sumIntoLocalValue(row, 1/myDistance2(*coordsOwnedPlusShared, row, col));
328 }
329 }
330 Teuchos::RCP<vector_type> laplDiagOwnedPlusShared;
331 if (!gl_StiffGraph->getImporter().is_null()) {
332 laplDiagOwnedPlusShared = vector_factory::Build(gl_StiffGraph->getColMap(), true);
333 laplDiagOwnedPlusShared->doImport(*laplDiagOwned, *gl_StiffGraph->getImporter(), Xpetra::CombineMode::ADD);
334 }
335 else {
336 laplDiagOwnedPlusShared = laplDiagOwned;
337 }
338
339 for (size_t row=0; row<numOwnedRows; row++) {
340 gl_StiffGraph->getLocalRowView(row, indices);
341 size_t numIndices = indices.size();
342 for(size_t j=0; j<numIndices; j++) {
343 size_t col = indices[j];
344 if (row==col) continue;
345 double laplVal = 1.0 / myDistance2(*coordsOwnedPlusShared, row, col);
346 double aiiajj = std::abs(laplDiagOwnedPlusShared->getData(0)[row]*laplDiagOwnedPlusShared->getData(0)[col]);
347 double aij = laplVal * laplVal;
348 double ratio = sqrt(aij / aiiajj);
349 local_stat_max[7] = std::max(local_stat_max[7], ratio);
350 local_stat_min[7] = std::min(local_stat_min[7], ratio);
351 local_stat_sum[7] += ratio;
352 }
353 }
354 globalNumMatrixEntries = gl_StiffGraph->getGlobalNumEntries();
355 }
356
357 #ifdef HAVE_XPETRA_TPETRA
358 void Phase2b(Teuchos::RCP<const Tpetra::CrsGraph<LO, GO, Node> > gl_StiffGraph, Teuchos::RCP<Tpetra::MultiVector<ST, LO, GO,Node> > coords) {
359 Teuchos::RCP<multivector_type> coords_X = Teuchos::rcp(new Xpetra::TpetraMultiVector<ST,LO,GO,Node>(coords));
360 Teuchos::RCP<const crsgraph_type> graph_X = Teuchos::rcp(new Xpetra::TpetraCrsGraph<LO,GO,Node>(Teuchos::rcp_const_cast<Tpetra::CrsGraph<LO,GO,Node> >(gl_StiffGraph)));
361
362 Phase2b(graph_X, coords_X);
363 }
364 #endif
365
366 #ifdef HAVE_XPETRA_EPETRA
367 void Phase2b(Teuchos::RCP<const Epetra_CrsGraph> gl_StiffGraph, Teuchos::RCP<Epetra_MultiVector> coords) {
368 Teuchos::RCP<multivector_type> coords_X = Teuchos::rcp(new Xpetra::EpetraMultiVectorT<GO,Node>(coords));
369 Teuchos::RCP<const crsgraph_type> graph_X = Teuchos::rcp(new Xpetra::EpetraCrsGraphT<GO,Node>(Teuchos::rcp_const_cast<Epetra_CrsGraph>(gl_StiffGraph)));
370
371 Phase2b(graph_X, coords_X);
372 }
373 #endif
374
375
376 /**********************************************************************************/
377 /***************************** STATISTICS (Part III) ******************************/
378 /**********************************************************************************/
379 void Phase3() {
380 Teuchos::reduceAll(*comm,Teuchos::REDUCE_MIN,NUM_STATISTICS,local_stat_min.data(),global_stat_min.data());
381 Teuchos::reduceAll(*comm,Teuchos::REDUCE_MAX,NUM_STATISTICS,local_stat_max.data(),global_stat_max.data());
382 Teuchos::reduceAll(*comm,Teuchos::REDUCE_SUM,NUM_STATISTICS,local_stat_sum.data(),global_stat_sum.data());
383 // NOTE: All output properties should be unitless if we want to compare across problems.
384 // NOTE: Should the mean be weighted by cell volume? That is not currently done.
385
386 // 0 - Material property
387 problemStatistics.set("sigma: min/mean",global_stat_min[0]/global_stat_sum[0]*numElemsGlobal);
388 problemStatistics.set("sigma: max/mean",global_stat_max[0]/global_stat_sum[0]*numElemsGlobal);
389
390 // 1 - Max/min edge ratio
391 problemStatistics.set("element edge ratio: min",global_stat_min[1]);
392 problemStatistics.set("element edge ratio: max",global_stat_max[1]);
393 problemStatistics.set("element edge ratio: mean",global_stat_sum[1] / numElemsGlobal);
394
395 // 2 - det of cell Jacobian (later)
396 problemStatistics.set("element det jacobian: min/mean",global_stat_min[2]/global_stat_sum[2]*numElemsGlobal);
397 problemStatistics.set("element det jacobian: max/mean",global_stat_max[2]/global_stat_sum[2]*numElemsGlobal);
398
399 // 3 - Stretch
400 problemStatistics.set("Stretch max", global_stat_max[3]);
401 problemStatistics.set("Stretch min", global_stat_min[3]);
402 problemStatistics.set("Stretch mean", global_stat_sum[3] / numElemsGlobal);
403
404 // 4 - Diagonal Ratio
405 problemStatistics.set("Diagonal Ratio max", global_stat_max[4]);
406 problemStatistics.set("Diagonal Ratio min", global_stat_min[4]);
407 problemStatistics.set("Diagonal Ratio mean", global_stat_sum[4]/numElemsGlobal);
408
409 // 5 - Inverse Taper
410 problemStatistics.set("Inverse Taper max", global_stat_max[5]);
411 problemStatistics.set("Inverse Taper min", global_stat_min[5]);
412 problemStatistics.set("Inverse Taper mean", global_stat_sum[5] / numElemsGlobal);
413
414 // 6 - Skew
415 problemStatistics.set("Skew max", global_stat_max[6]);
416 problemStatistics.set("Skew min", global_stat_min[6]);
417 problemStatistics.set("Skew mean", global_stat_sum[6] / numElemsGlobal);
418
419 // 7 - Lapl Diag
420 problemStatistics.set("Lapl Diag max", global_stat_max[7]);
421 problemStatistics.set("Lapl Diag min", global_stat_min[7]);
422 problemStatistics.set("Lapl Diag mean", global_stat_sum[7] / globalNumMatrixEntries);
423 }
424
425
426 Teuchos::ParameterList GetStatistics() {return problemStatistics;}
427
428 private:
429
430 // Internal
431 long long numElemsGlobal;
432 long long globalNumMatrixEntries;
433 const int NUM_STATISTICS = 8;
434 std::vector<double> local_stat_max;
435 std::vector<double> local_stat_min;
436 std::vector<double> local_stat_sum;
437 std::vector<double> global_stat_max;
438 std::vector<double> global_stat_min;
439 std::vector<double> global_stat_sum;
440
441 const int NUM_NODE_PAIRS = 4;
442 const int NUM_EDGE_PAIRS = 6;
443 const int dim = 3;
444
445
446 Teuchos::RCP<Tpetra::CrsGraph<LO, GO, Node> > crs_graph;
447 Teuchos::RCP<const Teuchos::Comm<int> > comm;
448 Teuchos::ParameterList problemStatistics;
449
450 };
451
452 #endif