// @HEADER // ***************************************************************************** // Trilinos: An Object-Oriented Solver Framework // // Copyright 2001-2024 NTESS and the Trilinos contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifndef __TrilinosCouplings_Statistics_hpp #define __TrilinosCouplings_Statistics_hpp // Intrepid includes #include #include #include #include #include // Xpetra #include #include #include #include #include #ifdef HAVE_XPETRA_TPETRA #include #include #endif #ifdef HAVE_XPETRA_EPETRA #include #include #endif // Teuchos #include template class MachineLearningStatistics_Hex3D { using ST = Scalar; using LO = LocalOrdinal; using GO = GlobalOrdinal; using crsgraph_type = Xpetra::CrsGraph; using multivector_type = Xpetra::MultiVector; using vector_type = Xpetra::Vector; public: MachineLearningStatistics_Hex3D(long long numElemsGlobal_):numElemsGlobal(numElemsGlobal_) { local_stat_max.resize(NUM_STATISTICS); local_stat_min.resize(NUM_STATISTICS); local_stat_sum.resize(NUM_STATISTICS); global_stat_max.resize(NUM_STATISTICS); global_stat_min.resize(NUM_STATISTICS); global_stat_sum.resize(NUM_STATISTICS); for(int i=0; i::max(); local_stat_sum[i] = 0.0; } } double distance2(Intrepid::FieldContainer & coord, int n1, int n2) { double dist = 0.0; for(int i=0; i &v, int i0, int i1) { const size_t numVectors = v.getNumVectors(); double distance = 0.0; for (size_t j=0; j&elemToNode,Intrepid::FieldContainer&elemToEdge,Intrepid::FieldContainer&edgeToNode,Intrepid::FieldContainer&nodeCoord,Intrepid::FieldContainer&sigmaVal) { double maxmin_ratio = 0; double stretch = 0; double diag_ratio = 0; double diag_length_max = 0; double diag_length_min = 0; double edge_length_max = 0; double edge_length_min = 0; double principle_axis_1[] = {0, 0, 0}; double principle_axis_2[] = {0, 0, 0}; double principle_axis_3[] = {0, 0, 0}; double hex_0[] = {0, 0, 0}; double hex_1[] = {0, 0, 0}; double hex_2[] = {0, 0, 0}; double hex_3[] = {0, 0, 0}; double hex_4[] = {0, 0, 0}; double hex_5[] = {0, 0, 0}; double hex_6[] = {0, 0, 0}; double hex_7[] = {0, 0, 0}; double dist = 0; int diag_nodes1[] = {0, 1, 2, 3}; int diag_nodes2[] = {6, 7, 4, 5}; int edge_nodes_1[] = {0, 0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 6}; int edge_nodes_2[] = {1, 3, 4, 2, 5, 3, 6, 7, 5, 7, 6, 7}; int edge_opposites_1[] = { 0, 1, 2, 3, 4, 5}; int edge_opposites_2[] = {11, 10, 6, 9, 7, 8}; double edge_length_1 = 0; double edge_length_2 = 0; int edge = elemToEdge(0, 0); int node1 = edgeToNode(edge, 0); int node2 = edgeToNode(edge, 1); int node3 = edgeToNode(edge, 0); int node4 = edgeToNode(edge, 1); int numElems = elemToNode.dimension(0); for(int i=0; i &worksetJacobDet,Intrepid::FieldContainer &worksetCubWeights) { int worksetSize = worksetJacobDet.dimension(0); int numCubPoints = worksetJacobDet.dimension(1); bool weightsWorkset = (worksetCubWeights.rank()==2)?true:false; for(int i=0; i > gl_StiffGraph, Teuchos::RCP > coords) { using multivector_factory = Xpetra::MultiVectorFactory; using vector_factory = Xpetra::VectorFactory; Teuchos::RCP coordsOwnedPlusShared; comm = gl_StiffGraph->getRowMap()->getComm(); if (!(gl_StiffGraph->getImporter().is_null())) { coordsOwnedPlusShared = multivector_factory::Build(gl_StiffGraph->getColMap(), 3, true); coordsOwnedPlusShared->doImport(*coords, *gl_StiffGraph->getImporter(), Xpetra::CombineMode::ADD); } else { coordsOwnedPlusShared = coords; } Teuchos::RCP > rowMap = gl_StiffGraph->getRowMap(); Teuchos::RCP laplDiagOwned = vector_factory::Build(rowMap, true); Teuchos::ArrayView indices; size_t numOwnedRows = rowMap->getLocalNumElements(); for (size_t row=0; rowgetLocalRowView(row, indices); size_t numIndices = indices.size(); for (size_t j=0; jsumIntoLocalValue(row, 1/myDistance2(*coordsOwnedPlusShared, row, col)); } } Teuchos::RCP laplDiagOwnedPlusShared; if (!gl_StiffGraph->getImporter().is_null()) { laplDiagOwnedPlusShared = vector_factory::Build(gl_StiffGraph->getColMap(), true); laplDiagOwnedPlusShared->doImport(*laplDiagOwned, *gl_StiffGraph->getImporter(), Xpetra::CombineMode::ADD); } else { laplDiagOwnedPlusShared = laplDiagOwned; } for (size_t row=0; rowgetLocalRowView(row, indices); size_t numIndices = indices.size(); for(size_t j=0; jgetData(0)[row]*laplDiagOwnedPlusShared->getData(0)[col]); double aij = laplVal * laplVal; double ratio = sqrt(aij / aiiajj); local_stat_max[7] = std::max(local_stat_max[7], ratio); local_stat_min[7] = std::min(local_stat_min[7], ratio); local_stat_sum[7] += ratio; } } globalNumMatrixEntries = gl_StiffGraph->getGlobalNumEntries(); } #ifdef HAVE_XPETRA_TPETRA void Phase2b(Teuchos::RCP > gl_StiffGraph, Teuchos::RCP > coords) { Teuchos::RCP coords_X = Teuchos::rcp(new Xpetra::TpetraMultiVector(coords)); Teuchos::RCP graph_X = Teuchos::rcp(new Xpetra::TpetraCrsGraph(Teuchos::rcp_const_cast >(gl_StiffGraph))); Phase2b(graph_X, coords_X); } #endif #ifdef HAVE_XPETRA_EPETRA void Phase2b(Teuchos::RCP gl_StiffGraph, Teuchos::RCP coords) { Teuchos::RCP coords_X = Teuchos::rcp(new Xpetra::EpetraMultiVectorT(coords)); Teuchos::RCP graph_X = Teuchos::rcp(new Xpetra::EpetraCrsGraphT(Teuchos::rcp_const_cast(gl_StiffGraph))); Phase2b(graph_X, coords_X); } #endif /**********************************************************************************/ /***************************** STATISTICS (Part III) ******************************/ /**********************************************************************************/ void Phase3() { Teuchos::reduceAll(*comm,Teuchos::REDUCE_MIN,NUM_STATISTICS,local_stat_min.data(),global_stat_min.data()); Teuchos::reduceAll(*comm,Teuchos::REDUCE_MAX,NUM_STATISTICS,local_stat_max.data(),global_stat_max.data()); Teuchos::reduceAll(*comm,Teuchos::REDUCE_SUM,NUM_STATISTICS,local_stat_sum.data(),global_stat_sum.data()); // NOTE: All output properties should be unitless if we want to compare across problems. // NOTE: Should the mean be weighted by cell volume? That is not currently done. // 0 - Material property problemStatistics.set("sigma: min/mean",global_stat_min[0]/global_stat_sum[0]*numElemsGlobal); problemStatistics.set("sigma: max/mean",global_stat_max[0]/global_stat_sum[0]*numElemsGlobal); // 1 - Max/min edge ratio problemStatistics.set("element edge ratio: min",global_stat_min[1]); problemStatistics.set("element edge ratio: max",global_stat_max[1]); problemStatistics.set("element edge ratio: mean",global_stat_sum[1] / numElemsGlobal); // 2 - det of cell Jacobian (later) problemStatistics.set("element det jacobian: min/mean",global_stat_min[2]/global_stat_sum[2]*numElemsGlobal); problemStatistics.set("element det jacobian: max/mean",global_stat_max[2]/global_stat_sum[2]*numElemsGlobal); // 3 - Stretch problemStatistics.set("Stretch max", global_stat_max[3]); problemStatistics.set("Stretch min", global_stat_min[3]); problemStatistics.set("Stretch mean", global_stat_sum[3] / numElemsGlobal); // 4 - Diagonal Ratio problemStatistics.set("Diagonal Ratio max", global_stat_max[4]); problemStatistics.set("Diagonal Ratio min", global_stat_min[4]); problemStatistics.set("Diagonal Ratio mean", global_stat_sum[4]/numElemsGlobal); // 5 - Inverse Taper problemStatistics.set("Inverse Taper max", global_stat_max[5]); problemStatistics.set("Inverse Taper min", global_stat_min[5]); problemStatistics.set("Inverse Taper mean", global_stat_sum[5] / numElemsGlobal); // 6 - Skew problemStatistics.set("Skew max", global_stat_max[6]); problemStatistics.set("Skew min", global_stat_min[6]); problemStatistics.set("Skew mean", global_stat_sum[6] / numElemsGlobal); // 7 - Lapl Diag problemStatistics.set("Lapl Diag max", global_stat_max[7]); problemStatistics.set("Lapl Diag min", global_stat_min[7]); problemStatistics.set("Lapl Diag mean", global_stat_sum[7] / globalNumMatrixEntries); } Teuchos::ParameterList GetStatistics() {return problemStatistics;} private: // Internal long long numElemsGlobal; long long globalNumMatrixEntries; const int NUM_STATISTICS = 8; std::vector local_stat_max; std::vector local_stat_min; std::vector local_stat_sum; std::vector global_stat_max; std::vector global_stat_min; std::vector global_stat_sum; const int NUM_NODE_PAIRS = 4; const int NUM_EDGE_PAIRS = 6; const int dim = 3; Teuchos::RCP > crs_graph; Teuchos::RCP > comm; Teuchos::ParameterList problemStatistics; }; #endif