| 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_Poisson_stk.cpp |
| 11 |
\brief Example solution of a Poisson equation on a hexahedral or |
| 12 |
tetrahedral mesh using nodal (Hgrad) elements. |
| 13 |
|
| 14 |
This example requires a hexahedral or tetrahedral mesh in Exodus |
| 15 |
format with a nodeset containing boundary nodes. STK is used to |
| 16 |
read the mesh and populate a mesh database, Intrepid is used to |
| 17 |
build the stiffness matrix and right-hand side, and ML is used |
| 18 |
to solve the resulting linear system. |
| 19 |
|
| 20 |
\verbatim |
| 21 |
|
| 22 |
Poisson system: |
| 23 |
|
| 24 |
div A grad u = f in Omega |
| 25 |
u = g on Gamma |
| 26 |
|
| 27 |
where |
| 28 |
A is a symmetric, positive definite material tensor |
| 29 |
f is a given source term |
| 30 |
|
| 31 |
|
| 32 |
Corresponding discrete linear system for nodal coefficients(x): |
| 33 |
|
| 34 |
Kx = b |
| 35 |
|
| 36 |
K - HGrad stiffness matrix |
| 37 |
b - right hand side vector |
| 38 |
|
| 39 |
\endverbatim |
| 40 |
|
| 41 |
\author Created by P. Bochev, D. Ridzal, K. Peterson C. Siefert. |
| 42 |
|
| 43 |
\remark Usage: |
| 44 |
\code ./example_Poisson_stk --help \endcode |
| 45 |
|
| 46 |
\remark Example requires a hexahedral or tetrahedral mesh in Exodus format |
| 47 |
*/ |
| 48 |
/********************************************************************************/ |
| 49 |
/********************************************************************************/ |
| 50 |
/********************************************************************************/ |
| 51 |
|
| 52 |
|
| 53 |
/**************************************************************/ |
| 54 |
/* Includes */ |
| 55 |
/**************************************************************/ |
| 56 |
|
| 57 |
#include <unistd.h> |
| 58 |
#include <vector> |
| 59 |
#include <map> |
| 60 |
|
| 61 |
#include "TrilinosCouplings_config.h" |
| 62 |
#include "TrilinosCouplings_TpetraIntrepidPoissonExample.hpp" |
| 63 |
#include "TrilinosCouplings_IntrepidPoissonExampleHelpers.hpp" |
| 64 |
|
| 65 |
|
| 66 |
// Teuchos includes |
| 67 |
#include <Teuchos_CommandLineProcessor.hpp> |
| 68 |
#include <Teuchos_DefaultMpiComm.hpp> |
| 69 |
#include <Teuchos_TimeMonitor.hpp> |
| 70 |
|
| 71 |
// Intrepid includes |
| 72 |
#include "Intrepid_FunctionSpaceTools.hpp" |
| 73 |
#include "Intrepid_CellTools.hpp" |
| 74 |
#include "Intrepid_ArrayTools.hpp" |
| 75 |
#include "Intrepid_Basis.hpp" |
| 76 |
#include "Intrepid_HGRAD_HEX_C1_FEM.hpp" |
| 77 |
#include "Intrepid_HGRAD_TET_C1_FEM.hpp" |
| 78 |
#include "Intrepid_HGRAD_QUAD_C1_FEM.hpp" |
| 79 |
#include "Intrepid_HGRAD_TRI_C1_FEM.hpp" |
| 80 |
#include "Intrepid_RealSpaceTools.hpp" |
| 81 |
#include "Intrepid_DefaultCubatureFactory.hpp" |
| 82 |
#include "Intrepid_Utils.hpp" |
| 83 |
|
| 84 |
// Tpetra includes |
| 85 |
#include "Tpetra_Map.hpp" |
| 86 |
#include "Tpetra_FECrsMatrix.hpp" |
| 87 |
#include "Tpetra_FECrsGraph.hpp" |
| 88 |
#include "Tpetra_FEMultiVector.hpp" |
| 89 |
#include "Tpetra_Import.hpp" |
| 90 |
#include "Tpetra_Assembly_Helpers.hpp" |
| 91 |
#include "MatrixMarket_Tpetra.hpp" |
| 92 |
|
| 93 |
// Teuchos includes |
| 94 |
#include "Teuchos_oblackholestream.hpp" |
| 95 |
#include "Teuchos_RCP.hpp" |
| 96 |
#include "Teuchos_BLAS.hpp" |
| 97 |
#include "Teuchos_GlobalMPISession.hpp" |
| 98 |
#include "Teuchos_XMLParameterListHelpers.hpp" |
| 99 |
|
| 100 |
// Shards includes |
| 101 |
#include "Shards_CellTopology.hpp" |
| 102 |
|
| 103 |
// MueLu includes |
| 104 |
#include "MueLu.hpp" |
| 105 |
#include "MueLu_ParameterListInterpreter.hpp" |
| 106 |
#include "MueLu_CreateTpetraPreconditioner.hpp" |
| 107 |
|
| 108 |
#ifdef HAVE_INTREPID_KOKKOS |
| 109 |
#include "Sacado.hpp" |
| 110 |
#else |
| 111 |
// Sacado includes |
| 112 |
#include "Sacado_No_Kokkos.hpp" |
| 113 |
#endif |
| 114 |
|
| 115 |
// STK includes |
| 116 |
#include "Ionit_Initializer.h" |
| 117 |
#include "Ioss_SubSystem.h" |
| 118 |
|
| 119 |
#include "stk_io/IossBridge.hpp" |
| 120 |
#include "stk_io/StkMeshIoBroker.hpp" |
| 121 |
|
| 122 |
#include "stk_util/parallel/Parallel.hpp" |
| 123 |
#include "stk_mesh/base/Types.hpp" |
| 124 |
#include "stk_mesh/base/MetaData.hpp" |
| 125 |
#include "stk_mesh/base/BulkData.hpp" |
| 126 |
#include "stk_mesh/base/ForEachEntity.hpp" |
| 127 |
#include "stk_mesh/base/GetEntities.hpp" |
| 128 |
#include "stk_mesh/base/Selector.hpp" |
| 129 |
#include "stk_mesh/base/Bucket.hpp" |
| 130 |
#include "stk_mesh/base/Entity.hpp" |
| 131 |
#include "stk_mesh/base/Field.hpp" |
| 132 |
|
| 133 |
#include "TrilinosCouplings_Statistics.hpp" |
| 134 |
|
| 135 |
/*********************************************************/ |
| 136 |
/* Typedefs */ |
| 137 |
/*********************************************************/ |
| 138 |
typedef shards::CellTopology ShardsCellTopology; |
| 139 |
typedef Intrepid::FunctionSpaceTools IntrepidFSTools; |
| 140 |
typedef Intrepid::RealSpaceTools<double> IntrepidRSTools; |
| 141 |
typedef Intrepid::CellTools<double> IntrepidCTools; |
| 142 |
typedef Intrepid::FieldContainer<double> IntrepidFieldContainer; |
| 143 |
|
| 144 |
using Teuchos::TimeMonitor; |
| 145 |
|
| 146 |
// Number of dimensions |
| 147 |
int spaceDim; |
| 148 |
|
| 149 |
/**********************************************************************************/ |
| 150 |
/******** FUNCTION DECLARATIONS FOR EXACT SOLUTION AND SOURCE TERMS ***************/ |
| 151 |
/**********************************************************************************/ |
| 152 |
|
| 153 |
/** \brief User-defined exact solution. |
| 154 |
|
| 155 |
\param x [in] x-coordinate of the evaluation point |
| 156 |
\param y [in] y-coordinate of the evaluation point |
| 157 |
\param z [in] z-coordinate of the evaluation point |
| 158 |
|
| 159 |
\return Value of the exact solution at (x,y,z) |
| 160 |
*/ |
| 161 |
template<typename Scalar> |
| 162 |
const Scalar exactSolution(const Scalar& x, const Scalar& y, const Scalar& z); |
| 163 |
|
| 164 |
/** \brief User-defined material tensor. |
| 165 |
|
| 166 |
\param material [out] 3 x 3 material tensor evaluated at (x,y,z) |
| 167 |
\param x [in] x-coordinate of the evaluation point |
| 168 |
\param y [in] y-coordinate of the evaluation point |
| 169 |
\param z [in] z-coordinate of the evaluation point |
| 170 |
|
| 171 |
\warning Symmetric and positive definite tensor is required for every (x,y,z). |
| 172 |
*/ |
| 173 |
template<typename Scalar> |
| 174 |
void materialTensor(Scalar material[][3], const Scalar& x, const Scalar& y, const Scalar& z); |
| 175 |
|
| 176 |
/** \brief Computes gradient of the exact solution. Requires user-defined exact solution. |
| 177 |
|
| 178 |
\param gradExact [out] gradient of the exact solution evaluated at (x,y,z) |
| 179 |
\param x [in] x-coordinate of the evaluation point |
| 180 |
\param y [in] y-coordinate of the evaluation point |
| 181 |
\param z [in] z-coordinate of the evaluation point |
| 182 |
*/ |
| 183 |
template<typename Scalar> |
| 184 |
void exactSolutionGrad(Scalar gradExact[3], const Scalar& x, const Scalar& y, const Scalar& z); |
| 185 |
|
| 186 |
|
| 187 |
/** \brief Computes source term: f = -div(A.grad u). Requires user-defined exact solution |
| 188 |
and material tensor. |
| 189 |
|
| 190 |
\param x [in] x-coordinate of the evaluation point |
| 191 |
\param y [in] y-coordinate of the evaluation point |
| 192 |
\param z [in] z-coordinate of the evaluation point |
| 193 |
|
| 194 |
\return Source term corresponding to the user-defined exact solution evaluated at (x,y,z) |
| 195 |
*/ |
| 196 |
template<typename Scalar> |
| 197 |
const Scalar sourceTerm(Scalar& x, Scalar& y, Scalar& z); |
| 198 |
|
| 199 |
|
| 200 |
/** \brief Computation of the material tensor at array of points in physical space. |
| 201 |
|
| 202 |
\param worksetMaterialValues [out] Rank-2, 3 or 4 array with dimensions (C,P), (C,P,D) or (C,P,D,D) |
| 203 |
with the values of the material tensor |
| 204 |
\param evaluationPoints [in] Rank-3 (C,P,D) array with the evaluation points in physical frame |
| 205 |
*/ |
| 206 |
template<class ArrayOut, class ArrayIn> |
| 207 |
void evaluateMaterialTensor(ArrayOut & worksetMaterialValues, |
| 208 |
const ArrayIn & evaluationPoints); |
| 209 |
|
| 210 |
|
| 211 |
/** \brief Computation of the source term at array of points in physical space. |
| 212 |
|
| 213 |
\param sourceTermValues [out] Rank-2 (C,P) array with the values of the source term |
| 214 |
\param evaluationPoints [in] Rank-3 (C,P,D) array with the evaluation points in physical frame |
| 215 |
*/ |
| 216 |
template<class ArrayOut, class ArrayIn> |
| 217 |
void evaluateSourceTerm(ArrayOut & sourceTermValues, |
| 218 |
const ArrayIn & evaluationPoints); |
| 219 |
|
| 220 |
/** \brief Computation of the exact solution at array of points in physical space. |
| 221 |
|
| 222 |
\param exactSolutionValues [out] Rank-2 (C,P) array with the values of the exact solution |
| 223 |
\param evaluationPoints [in] Rank-3 (C,P,D) array with the evaluation points in physical frame |
| 224 |
*/ |
| 225 |
template<class ArrayOut, class ArrayIn> |
| 226 |
void evaluateExactSolution(ArrayOut & exactSolutionValues, |
| 227 |
const ArrayIn & evaluationPoints); |
| 228 |
|
| 229 |
|
| 230 |
/** \brief Computation of the gradient of the exact solution at array of points in physical space. |
| 231 |
|
| 232 |
\param exactSolutionGradValues [out] Rank-3 (C,P,D) array with the values of the gradient of the exact solution |
| 233 |
\param evaluationPoints [in] Rank-3 (C,P,D) array with the evaluation points in physical frame |
| 234 |
*/ |
| 235 |
template<class ArrayOut, class ArrayIn> |
| 236 |
void evaluateExactSolutionGrad(ArrayOut & exactSolutionGradValues, |
| 237 |
const ArrayIn & evaluationPoints); |
| 238 |
|
| 239 |
|
| 240 |
/**********************************************************************************/ |
| 241 |
/**************** FUNCTION DECLARATION FOR ML PRECONDITIONER *********************/ |
| 242 |
/**********************************************************************************/ |
| 243 |
using Tpetra_CrsMatrix = Tpetra::CrsMatrix<>; |
| 244 |
using Tpetra_MultiVector = Tpetra::MultiVector<>; |
| 245 |
using Tpetra_Vector = Tpetra::Vector<>; |
| 246 |
int TestMultiLevelPreconditioner(char ProblemType[], |
| 247 |
Teuchos::ParameterList & MLList, |
| 248 |
Teuchos::RCP<Tpetra_CrsMatrix> & A, |
| 249 |
Teuchos::RCP<Tpetra_Vector> & xexact, |
| 250 |
Teuchos::RCP<Tpetra_MultiVector> & b, |
| 251 |
Teuchos::RCP<Tpetra_MultiVector> & uh, |
| 252 |
Teuchos::RCP<Tpetra_MultiVector> & coords, |
| 253 |
double & TotalErrorResidual, |
| 254 |
double & TotalErrorExactSol); |
| 255 |
|
| 256 |
/**********************************************************************************/ |
| 257 |
/************* FUNCTION DECLARATIONS FOR SIMPLE BASIS FACTORY *********************/ |
| 258 |
/**********************************************************************************/ |
| 259 |
|
| 260 |
/** \brief Simple factory that chooses basis function based on cell topology. |
| 261 |
|
| 262 |
\param cellTopology [in] Shards cell topology |
| 263 |
\param order [in] basis function order, currently unused |
| 264 |
\param basis [out] pointer to Intrepid basis |
| 265 |
|
| 266 |
\return Intrepid basis |
| 267 |
*/ |
| 268 |
|
| 269 |
void getBasis(Teuchos::RCP<Intrepid::Basis<double,IntrepidFieldContainer > > &basis, |
| 270 |
const ShardsCellTopology & cellTopology, |
| 271 |
int order); |
| 272 |
|
| 273 |
int getDimension(const ShardsCellTopology & cellTopology); |
| 274 |
|
| 275 |
/**********************************************************************************/ |
| 276 |
/**********************************************************************************/ |
| 277 |
/**********************************************************************************/ |
| 278 |
|
| 279 |
// Do the actual reading of the mesh database and |
| 280 |
// creation and population of the MetaData and BulkData. |
| 281 |
void mesh_read_write(const std::string &type, |
| 282 |
const std::string &working_directory, |
| 283 |
const std::string &filename, |
| 284 |
stk::io::StkMeshIoBroker &broker, |
| 285 |
int db_integer_size, |
| 286 |
stk::io::HeartbeatType hb_type) |
| 287 |
{ |
| 288 |
int myrank; |
| 289 |
MPI_Comm_rank(MPI_COMM_WORLD,&myrank); |
| 290 |
|
| 291 |
std::string absoluteFileName = working_directory + "/" + filename; |
| 292 |
if (myrank == 0) { |
| 293 |
std::cout<<"Reading mesh: "<<absoluteFileName<<std::endl; |
| 294 |
} |
| 295 |
size_t input_index = broker.add_mesh_database(absoluteFileName, type, stk::io::READ_MESH); |
| 296 |
broker.set_active_mesh(input_index); |
| 297 |
// creates metadata |
| 298 |
broker.create_input_mesh(); |
| 299 |
|
| 300 |
// commits the meta data |
| 301 |
broker.populate_bulk_data(); |
| 302 |
|
| 303 |
} //mesh_read_write |
| 304 |
|
| 305 |
|
| 306 |
/**********************************************************************************/ |
| 307 |
/**********************************************************************************/ |
| 308 |
/**********************************************************************************/ |
| 309 |
template<class crs_matrix_type, class multivector_type1, class multivector_type2, class solution_type> |
| 310 |
void Apply_Dirichlet_BCs(std::vector<int> &BCNodes, crs_matrix_type & A, multivector_type1 & x, multivector_type2 & b, solution_type & solution_values) { |
| 311 |
using SC = typename multivector_type1::scalar_type; |
| 312 |
using LO = typename multivector_type1::local_ordinal_type; |
| 313 |
int N=(int)BCNodes.size(); |
| 314 |
Teuchos::ArrayRCP<SC> xdata = x.getDataNonConst(0); |
| 315 |
Teuchos::ArrayRCP<SC> bdata = b.getDataNonConst(0); |
| 316 |
if (b.getMap()->getComm()->getRank() == 0) |
| 317 |
std::cout<<"Apply in Dirichlet BCs to "<<BCNodes.size() << " nodes"<<std::endl; |
| 318 |
|
| 319 |
Tpetra::beginModify(A,b); |
| 320 |
|
| 321 |
for(int i=0; i<N; i++) { |
| 322 |
LO lrid = BCNodes[i]; |
| 323 |
|
| 324 |
xdata[lrid]=bdata[lrid] = solution_values[i]; |
| 325 |
|
| 326 |
size_t numEntriesInRow = A.getNumEntriesInLocalRow(lrid); |
| 327 |
typename crs_matrix_type::nonconst_local_inds_host_view_type cols("cols", numEntriesInRow); |
| 328 |
typename crs_matrix_type::nonconst_values_host_view_type vals("vals", numEntriesInRow); |
| 329 |
A.getLocalRowCopy(lrid, cols, vals, numEntriesInRow); |
| 330 |
|
| 331 |
for(size_t j=0; j<vals.extent(0); j++) |
| 332 |
vals(j) = (cols(j) == lrid) ? 1.0 : 0.0; |
| 333 |
|
| 334 |
A.replaceLocalValues(lrid, cols, vals); |
| 335 |
} |
| 336 |
|
| 337 |
Tpetra::endModify(A,b); |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
/**********************************************************************************/ |
| 344 |
/******************************** MAIN ********************************************/ |
| 345 |
/**********************************************************************************/ |
| 346 |
|
| 347 |
|
| 348 |
int main(int argc, char *argv[]) { |
| 349 |
|
| 350 |
using Teuchos::RCP; |
| 351 |
using Teuchos::rcp; |
| 352 |
using entity_type = stk::mesh::Entity; |
| 353 |
using Tpetra_Map = Tpetra::Map<>; |
| 354 |
using Tpetra_FEMultiVector = Tpetra::FEMultiVector<>; |
| 355 |
using Tpetra_FECrsMatrix = Tpetra::FECrsMatrix<>; |
| 356 |
using Tpetra_FECrsGraph = Tpetra::FECrsGraph<>; |
| 357 |
using SC = Tpetra_Vector::scalar_type; |
| 358 |
using LO = Tpetra_Vector::local_ordinal_type; |
| 359 |
using GO = Tpetra_Vector::global_ordinal_type; |
| 360 |
//using NO = Tpetra_Vector::node_type; |
| 361 |
|
| 362 |
|
| 363 |
const stk::mesh::EntityRank NODE_RANK = stk::topology::NODE_RANK; |
| 364 |
const stk::mesh::EntityRank ELEMENT_RANK = stk::topology::ELEMENT_RANK; |
| 365 |
|
| 366 |
Teuchos::GlobalMPISession mpiSession(&argc, &argv, NULL); |
| 367 |
RCP<const Teuchos::Comm<int> > Comm = Teuchos::DefaultComm<int>::getComm(); |
| 368 |
RCP<const Teuchos::MpiComm<int> > MpiComm = Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int> >(Comm); |
| 369 |
int numRanks = Comm->getSize(); |
| 370 |
int MyPID = Comm->getRank(); |
| 371 |
|
| 372 |
|
| 373 |
Teuchos::CommandLineProcessor clp(false); |
| 374 |
|
| 375 |
std::string optMeshFile = "unit_cube_10int_hex.exo"; |
| 376 |
clp.setOption("mesh", &optMeshFile, "Exodus hexahedra, tetrahedral, quadrilateral or triangle mesh file with nodeset define for boundary"); |
| 377 |
std::string optXmlFile = ""; |
| 378 |
clp.setOption("xml", &optXmlFile, "xml file containing ML solver options"); |
| 379 |
bool optPrintLocalStats = false; clp.setOption("localstats", "nolocalstats", &optPrintLocalStats, "print per-process statistics"); |
| 380 |
// If matrixFilename is nonempty, dump the matrix to that file |
| 381 |
// in MatrixMarket format. |
| 382 |
std::string matrixFilename; |
| 383 |
clp.setOption ("matrixFilename", &matrixFilename, "If nonempty, dump the " |
| 384 |
"generated matrix to that file in MatrixMarket format."); |
| 385 |
|
| 386 |
// If rhsFilename is nonempty, dump the rhs to that file |
| 387 |
// in MatrixMarket format. |
| 388 |
std::string rhsFilename; |
| 389 |
clp.setOption ("rhsFilename", &rhsFilename, "If nonempty, dump the " |
| 390 |
"generated rhs to that file in MatrixMarket format."); |
| 391 |
|
| 392 |
// If rhsFilename is nonempty, dump the rhs to that file |
| 393 |
// in MatrixMarket format. |
| 394 |
std::string initialGuessFilename; |
| 395 |
clp.setOption ("initialGuessFilename", &initialGuessFilename, "If nonempty, dump the " |
| 396 |
"generated initial guess to that file in MatrixMarket format."); |
| 397 |
|
| 398 |
// If coordsFilename is nonempty, dump the coords to that file |
| 399 |
// in MatrixMarket format. |
| 400 |
std::string coordsFilename; |
| 401 |
clp.setOption ("coordsFilename", &coordsFilename, "If nonempty, dump the " |
| 402 |
"generated coordinates to that file in MatrixMarket format."); |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
switch (clp.parse(argc, argv)) { |
| 407 |
case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: |
| 408 |
return EXIT_SUCCESS; break; |
| 409 |
case Teuchos::CommandLineProcessor::PARSE_ERROR: |
| 410 |
case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: |
| 411 |
return EXIT_FAILURE; break; |
| 412 |
case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: |
| 413 |
break; |
| 414 |
} |
| 415 |
|
| 416 |
if (MyPID == 0) { |
| 417 |
std::cout |
| 418 |
<< "===============================================================================\n" \ |
| 419 |
<< "| |\n" \ |
| 420 |
<< "| Example: Solve Poisson Equation |\n" \ |
| 421 |
<< "| |\n" \ |
| 422 |
<< "| Questions? Contact Pavel Bochev ([email protected]), |\n" \ |
| 423 |
<< "| Denis Ridzal ([email protected]), |\n" \ |
| 424 |
<< "| Kara Peterson ([email protected]). |\n" \ |
| 425 |
<< "| |\n" \ |
| 426 |
<< "| Intrepid's website: http://trilinos.sandia.gov/packages/intrepid |\n" \ |
| 427 |
<< "| STK's website: http://trilinos.github.io/stk.html |\n" \ |
| 428 |
<< "| ML's website: http://trilinos.sandia.gov/packages/ml |\n" \ |
| 429 |
<< "| Trilinos website: http://trilinos.sandia.gov |\n" \ |
| 430 |
<< "| |\n" \ |
| 431 |
<< "===============================================================================\n"; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
/**********************************************************************************/ |
| 436 |
/********************************** GET XML INPUTS ********************************/ |
| 437 |
/**********************************************************************************/ |
| 438 |
|
| 439 |
// get xml file from command line if provided, otherwise use default |
| 440 |
std::string xmlSolverInFileName(optXmlFile); |
| 441 |
|
| 442 |
// Read xml file into parameter list |
| 443 |
Teuchos::ParameterList inputSolverList; |
| 444 |
|
| 445 |
if(xmlSolverInFileName.length()) { |
| 446 |
if (MyPID == 0) |
| 447 |
std::cout << "\nReading parameter list from the XML file \""<<xmlSolverInFileName<<"\" ...\n" << std::endl; |
| 448 |
Teuchos::updateParametersFromXmlFile (xmlSolverInFileName, Teuchos::ptr (&inputSolverList)); |
| 449 |
} |
| 450 |
else if (MyPID == 0) |
| 451 |
std::cout << "Using default solver values ..." << std::endl; |
| 452 |
|
| 453 |
|
| 454 |
/**********************************************************************************/ |
| 455 |
/*********************************** READ MESH ************************************/ |
| 456 |
/**********************************************************************************/ |
| 457 |
RCP<TimeMonitor> tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("1) Read Mesh"))); |
| 458 |
|
| 459 |
stk::io::StkMeshIoBroker broker(*MpiComm->getRawMpiComm()); |
| 460 |
broker.property_add(Ioss::Property("MAXIMUM_NAME_LENGTH", 180)); |
| 461 |
broker.property_add(Ioss::Property("DECOMPOSITION_METHOD", "HSFC")); |
| 462 |
broker.property_add(Ioss::Property("COMPOSE_RESULTS", false)); //Note! true results in an error in Seacas |
| 463 |
|
| 464 |
std::string type = "exodusii"; |
| 465 |
char buf[1024]; |
| 466 |
if (getcwd(buf,sizeof(buf)) == NULL) |
| 467 |
throw(std::runtime_error("Could not get current working directory")); |
| 468 |
std::string working_directory(buf); |
| 469 |
std::string filename(optMeshFile); |
| 470 |
int db_integer_size = 4; |
| 471 |
stk::io::HeartbeatType hb_type = stk::io::NONE; |
| 472 |
|
| 473 |
mesh_read_write(type, working_directory, filename, broker, db_integer_size, hb_type); |
| 474 |
|
| 475 |
stk::mesh::BulkData &bulkData = broker.bulk_data(); |
| 476 |
stk::mesh::MetaData &metaData = broker.meta_data(); |
| 477 |
|
| 478 |
// Count number of local nodes, record GIDs for Tpetra map. |
| 479 |
Teuchos::Array<GO> ownedGIDs, ownedPlusSharedGIDs; |
| 480 |
stk::mesh::Selector locallyOwnedSelector = metaData.locally_owned_part(); |
| 481 |
int numLocalNodes = stk::mesh::count_entities(bulkData, NODE_RANK, locallyOwnedSelector); |
| 482 |
|
| 483 |
stk::mesh::for_each_entity_run(bulkData, NODE_RANK, locallyOwnedSelector, |
| 484 |
[&](const stk::mesh::BulkData& mesh, stk::mesh::Entity node) |
| 485 |
{ |
| 486 |
ownedGIDs.push_back(mesh.identifier(node)-1); |
| 487 |
ownedPlusSharedGIDs.push_back(mesh.identifier(node)-1); |
| 488 |
}); |
| 489 |
|
| 490 |
// Now record the shared-but-not-owned nodal GIDs |
| 491 |
{ |
| 492 |
stk::mesh::Selector globallySharedSelector = metaData.globally_shared_part(); |
| 493 |
globallySharedSelector &= !locallyOwnedSelector; //not owned |
| 494 |
stk::mesh::for_each_entity_run(bulkData, NODE_RANK, globallySharedSelector, |
| 495 |
[&](const stk::mesh::BulkData& mesh, stk::mesh::Entity node) |
| 496 |
{ |
| 497 |
ownedPlusSharedGIDs.push_back(mesh.identifier(node)-1); |
| 498 |
}); |
| 499 |
} |
| 500 |
|
| 501 |
// Count # of local elements |
| 502 |
int numLocalElems = stk::mesh::count_entities(bulkData, ELEMENT_RANK, locallyOwnedSelector); |
| 503 |
|
| 504 |
if (optPrintLocalStats) { |
| 505 |
for (int i=0; i<numRanks; ++i) { |
| 506 |
if (MyPID == i) { |
| 507 |
std::cout << "(" << MyPID << ") Number of local Elements: " << numLocalElems << std::endl |
| 508 |
<< "(" << MyPID << ") Number of local Nodes : " << numLocalNodes << std::endl << std::endl; |
| 509 |
} |
| 510 |
Comm->barrier(); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
GO numGlobalNodes=0, numGlobalElements=0; |
| 515 |
Teuchos::reduceAll(*Comm, Teuchos::REDUCE_SUM, (GO)numLocalNodes, Teuchos::outArg(numGlobalNodes)); |
| 516 |
Teuchos::reduceAll(*Comm, Teuchos::REDUCE_SUM, (GO)numLocalElems, Teuchos::outArg(numGlobalElements)); |
| 517 |
if (MyPID == 0) { |
| 518 |
std::cout << " Number of global Nodes : " << numGlobalNodes << std::endl; |
| 519 |
std::cout << " Number of global Elements: " << numGlobalElements << std::endl; |
| 520 |
} |
| 521 |
tm = Teuchos::null; |
| 522 |
|
| 523 |
|
| 524 |
/**********************************************************************************/ |
| 525 |
/********************* BUILD MAPS/GRAPHS FOR GLOBAL SOLUTION **********************/ |
| 526 |
/**********************************************************************************/ |
| 527 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("2) Build Maps/Graph"))); |
| 528 |
|
| 529 |
RCP<Tpetra_Map> globalMapG = Teuchos::rcp(new Tpetra_Map( (Tpetra::global_size_t)numGlobalNodes,ownedGIDs(), (GO)0, Comm)); |
| 530 |
RCP<Tpetra_Map> ownedPlusSharedMapG = Teuchos::rcp(new Tpetra_Map( (Tpetra::global_size_t)numGlobalNodes,ownedPlusSharedGIDs(), (GO)0, Comm)); |
| 531 |
Kokkos::DualView<size_t*> nnzPerRowUpperBound("nnzbound",ownedPlusSharedGIDs.size()); |
| 532 |
nnzPerRowUpperBound.template modify<typename Kokkos::DualView<size_t*>::host_mirror_space>(); |
| 533 |
auto nnzPerRowUpperBound_h = nnzPerRowUpperBound.view_host(); |
| 534 |
|
| 535 |
// Count the local elements and get node index upper bound |
| 536 |
stk::mesh::for_each_entity_run(bulkData, ELEMENT_RANK, locallyOwnedSelector, |
| 537 |
[&](const stk::mesh::BulkData& mesh, stk::mesh::Entity elem) |
| 538 |
{ |
| 539 |
stk::mesh::ConnectedEntities nodes = mesh.get_connected_entities(elem,NODE_RANK); |
| 540 |
|
| 541 |
// NOTE: This will substantially overcount the NNZ needed. You should use hash table to be smarter |
| 542 |
for (unsigned inode = 0; inode < nodes.size(); ++inode) { |
| 543 |
GO GID = mesh.identifier(nodes[inode])-1; |
| 544 |
LO LID = ownedPlusSharedMapG->getLocalElement(GID); |
| 545 |
if (LID != Teuchos::OrdinalTraits<LO>::invalid()) |
| 546 |
nnzPerRowUpperBound_h[LID]+=nodes.size(); |
| 547 |
} |
| 548 |
}); |
| 549 |
|
| 550 |
// Build the Graph |
| 551 |
RCP<Tpetra_FECrsGraph> StiffGraph = rcp(new Tpetra_FECrsGraph(globalMapG,ownedPlusSharedMapG,nnzPerRowUpperBound)); |
| 552 |
Tpetra::beginAssembly(*StiffGraph); |
| 553 |
stk::mesh::for_each_entity_run(bulkData, ELEMENT_RANK, locallyOwnedSelector, |
| 554 |
[&](const stk::mesh::BulkData& mesh, stk::mesh::Entity elem) |
| 555 |
{ |
| 556 |
stk::mesh::ConnectedEntities nodes = mesh.get_connected_entities(elem,NODE_RANK); |
| 557 |
|
| 558 |
Teuchos::Array<GO> global_ids(nodes.size()); |
| 559 |
bool foundOwnedNode = false; |
| 560 |
for (unsigned inode = 0; inode < nodes.size(); ++inode) { |
| 561 |
if (mesh.bucket(nodes[inode]).owned()) { |
| 562 |
foundOwnedNode = true; |
| 563 |
} |
| 564 |
GO GID = mesh.identifier(nodes[inode])-1; |
| 565 |
global_ids[inode]=GID; |
| 566 |
} |
| 567 |
|
| 568 |
if (!foundOwnedNode) { |
| 569 |
std::cout<<"Warning, element "<<mesh.identifier(elem)<<" on P"<<mesh.parallel_rank()<<" doesn't have any locally-owned nodes."<<std::endl; |
| 570 |
} |
| 571 |
|
| 572 |
for (unsigned inode = 0; inode < nodes.size(); ++inode) { |
| 573 |
StiffGraph->insertGlobalIndices(global_ids[inode],global_ids()); |
| 574 |
} |
| 575 |
}); |
| 576 |
|
| 577 |
Tpetra::endAssembly(*StiffGraph); |
| 578 |
tm = Teuchos::null; |
| 579 |
|
| 580 |
/**********************************************************************************/ |
| 581 |
/******************** COMPUTE COORDINATES AND STATISTICS **************************/ |
| 582 |
/**********************************************************************************/ |
| 583 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("3) Compute Coordinates and Stats"))); |
| 584 |
|
| 585 |
std::vector<entity_type> bcNodes; |
| 586 |
|
| 587 |
stk::mesh::ExodusTranslator exodusTranslator(bulkData); |
| 588 |
stk::mesh::PartVector nodeSets = exodusTranslator.get_node_set_parts(); |
| 589 |
ShardsCellTopology cellType; |
| 590 |
for (const stk::mesh::Part* nodeSet : nodeSets) { |
| 591 |
|
| 592 |
stk::mesh::Selector bcNodeSelector = *nodeSet & locallyOwnedSelector; |
| 593 |
|
| 594 |
if(bcNodes.size() == 0) |
| 595 |
stk::mesh::get_entities(bulkData, NODE_RANK, bcNodeSelector, bcNodes); |
| 596 |
else { |
| 597 |
std::vector<entity_type> myBcNodes; |
| 598 |
stk::mesh::get_entities(bulkData, NODE_RANK, bcNodeSelector, myBcNodes); |
| 599 |
std::copy(myBcNodes.begin(),myBcNodes.end(),std::back_inserter(bcNodes)); |
| 600 |
} |
| 601 |
|
| 602 |
if(MyPID==0) printf("Adding nodeset %s\n",nodeSet->name().c_str()); |
| 603 |
} |
| 604 |
|
| 605 |
stk::mesh::PartVector elemBlocks = exodusTranslator.get_element_block_parts(); |
| 606 |
for(const stk::mesh::Part* elemBlock : elemBlocks) { |
| 607 |
// Here the topology is defined from the mesh. Note that it is assumed |
| 608 |
// that all parts with elements (aka ELEMENT_RANK) have the same topology type |
| 609 |
auto myCell = stk::mesh::get_cell_topology(metaData.get_topology( *elemBlock )); |
| 610 |
if(myCell.getCellTopologyData()) { |
| 611 |
cellType = myCell; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
int numNodesPerElem = cellType.getNodeCount(); |
| 616 |
int numEdgesPerElem = cellType.getEdgeCount(); |
| 617 |
|
| 618 |
if(MyPID==0) { |
| 619 |
std::cout<<"Cell Topology: "<<cellType.getName() << " ("<<cellType.getBaseName()<<")"<<std::endl; |
| 620 |
} |
| 621 |
|
| 622 |
//MachineLearningStatistics_Hex3D<SC,LO,GO,NO> MLStatistics(numGlobalElements); |
| 623 |
bool do_statistics = !strcmp(cellType.getName(),"Hexahedron_8") && (numRanks == 1); |
| 624 |
if(MyPID==0) std::cout<<"do_statistics = "<<do_statistics<<std::endl; |
| 625 |
|
| 626 |
// if no boundary node set was found give a warning |
| 627 |
int numLocalBCs = bcNodes.size(); |
| 628 |
int numGlobalBCs = 0; |
| 629 |
Teuchos::reduceAll(*Comm, Teuchos::REDUCE_SUM, numLocalBCs, Teuchos::outArg(numGlobalBCs)); |
| 630 |
|
| 631 |
if (MyPID == 0) { |
| 632 |
std::cout << " Number of Dirichlet Nodes: " << numGlobalBCs << std::endl; |
| 633 |
if (numGlobalBCs == 0) { |
| 634 |
std::cout << std::endl |
| 635 |
<< " Warning! - No boundary node set found." << std::endl |
| 636 |
<< " Boundary conditions will not be applied correctly.\n" |
| 637 |
<< std::endl; |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
if (optPrintLocalStats) { |
| 642 |
for (int i=0; i<numRanks; ++i) { |
| 643 |
if (MyPID == i) { |
| 644 |
std::cout << "(" << MyPID << ") Number of local b.c. nodes: " << bcNodes.size() << std::endl; |
| 645 |
} |
| 646 |
Comm->barrier(); |
| 647 |
} |
| 648 |
if(MyPID==0) std::cout << std::endl; |
| 649 |
} |
| 650 |
|
| 651 |
tm = Teuchos::null; |
| 652 |
|
| 653 |
/**********************************************************************************/ |
| 654 |
/********************************* GET CUBATURE ***********************************/ |
| 655 |
/**********************************************************************************/ |
| 656 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("4) Assembly Prep"))); |
| 657 |
// Define cubature of the specified degree for the cellType |
| 658 |
Intrepid::DefaultCubatureFactory<double> cubFactory; |
| 659 |
int cubDegree = 2; |
| 660 |
|
| 661 |
RCP<Intrepid::Cubature<double> > cellCubature = cubFactory.create(cellType, cubDegree); |
| 662 |
|
| 663 |
int cubDim = cellCubature -> getDimension(); |
| 664 |
int numCubPoints = cellCubature -> getNumPoints(); |
| 665 |
|
| 666 |
// Get numerical integration points and weights |
| 667 |
IntrepidFieldContainer cubPoints (numCubPoints, cubDim); |
| 668 |
IntrepidFieldContainer cubWeights(numCubPoints); |
| 669 |
|
| 670 |
cellCubature -> getCubature(cubPoints, cubWeights); |
| 671 |
|
| 672 |
/**********************************************************************************/ |
| 673 |
/*********************************** GET BASIS ************************************/ |
| 674 |
/**********************************************************************************/ |
| 675 |
|
| 676 |
// Select basis from the cell topology |
| 677 |
int order = 1; |
| 678 |
spaceDim = getDimension(cellType); |
| 679 |
RCP<Intrepid::Basis<double, IntrepidFieldContainer > > HGradBasis; |
| 680 |
getBasis(HGradBasis, cellType, order); |
| 681 |
|
| 682 |
|
| 683 |
int numFieldsG = HGradBasis->getCardinality(); |
| 684 |
|
| 685 |
IntrepidFieldContainer basisValues(numFieldsG, numCubPoints); |
| 686 |
IntrepidFieldContainer basisGrads(numFieldsG, numCubPoints, spaceDim); |
| 687 |
|
| 688 |
// Evaluate basis values and gradients at cubature points |
| 689 |
HGradBasis->getValues(basisValues, cubPoints, Intrepid::OPERATOR_VALUE); |
| 690 |
HGradBasis->getValues(basisGrads, cubPoints, Intrepid::OPERATOR_GRAD); |
| 691 |
|
| 692 |
|
| 693 |
/**********************************************************************************/ |
| 694 |
/******************************** STASH COORDINATES *******************************/ |
| 695 |
/**********************************************************************************/ |
| 696 |
typedef stk::mesh::Field<double> CoordFieldType; |
| 697 |
// get coordinates field |
| 698 |
CoordFieldType *coords = metaData.get_field<double>(NODE_RANK,"coordinates"); |
| 699 |
|
| 700 |
// Put coordinates in multivector for output |
| 701 |
RCP<Tpetra_MultiVector> nCoord = rcp(new Tpetra_MultiVector(globalMapG,spaceDim)); |
| 702 |
auto nCoord_h = nCoord->get2dViewNonConst(); |
| 703 |
// Loop over elements |
| 704 |
|
| 705 |
const stk::mesh::BucketVector &localElementBuckets = bulkData.get_buckets(ELEMENT_RANK, locallyOwnedSelector); |
| 706 |
for (const stk::mesh::Bucket* elemBucketPtr : localElementBuckets) { |
| 707 |
for (stk::mesh::Entity elem : *elemBucketPtr) { |
| 708 |
stk::mesh::ConnectedEntities nodes = bulkData.get_connected_entities(elem,NODE_RANK); |
| 709 |
for (unsigned inode = 0; inode < nodes.size(); ++inode) { |
| 710 |
double *coord = stk::mesh::field_data(*coords, nodes[inode]); |
| 711 |
LO lid = globalMapG->getLocalElement((int)bulkData.identifier(nodes[inode]) -1); |
| 712 |
if(lid != Teuchos::OrdinalTraits<LO>::invalid()) { |
| 713 |
nCoord_h[0][lid] = coord[0]; |
| 714 |
nCoord_h[1][lid] = coord[1]; |
| 715 |
if(spaceDim==3) |
| 716 |
nCoord_h[2][lid] = coord[2]; |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
} // end loop over elements |
| 721 |
|
| 722 |
|
| 723 |
/**********************************************************************************/ |
| 724 |
/****************************** STATISTICS (Part I) *******************************/ |
| 725 |
/**********************************************************************************/ |
| 726 |
if(do_statistics) { |
| 727 |
Intrepid::FieldContainer<int> elemToNode(numLocalElems,numNodesPerElem); |
| 728 |
Intrepid::FieldContainer<int> elemToEdge(numLocalElems,numEdgesPerElem); |
| 729 |
Intrepid::FieldContainer<double> nodeCoord (numLocalNodes, spaceDim); |
| 730 |
Intrepid::FieldContainer<double> sigmaVal(numLocalElems); |
| 731 |
|
| 732 |
int elem_ct=0; |
| 733 |
std::map<std::pair<int,int>,int> local_edge_hash; |
| 734 |
std::vector<std::pair<int,int> > edge_vector; |
| 735 |
|
| 736 |
for (const stk::mesh::Bucket* elemBucketPtr : localElementBuckets) { |
| 737 |
for (stk::mesh::Entity elem : *elemBucketPtr) { |
| 738 |
stk::mesh::ConnectedEntities nodes = bulkData.get_connected_entities(elem,NODE_RANK); |
| 739 |
for (unsigned inode = 0; inode < nodes.size(); ++inode) { |
| 740 |
const double *coord = stk::mesh::field_data(*coords, nodes[inode]); |
| 741 |
LO lid = globalMapG->getLocalElement((int)bulkData.identifier(nodes[inode]) -1); |
| 742 |
elemToNode(elem_ct,inode) = lid; |
| 743 |
if(lid != -1) { |
| 744 |
nodeCoord(lid,0) = coord[0]; |
| 745 |
nodeCoord(lid,1) = coord[1]; |
| 746 |
if(spaceDim==3) |
| 747 |
nodeCoord(lid,2) = coord[2]; |
| 748 |
} |
| 749 |
}//end node loop |
| 750 |
|
| 751 |
auto data = cellType.getCellTopologyData(); |
| 752 |
for(unsigned iedge=0; iedge<cellType.getEdgeCount(); iedge++) { |
| 753 |
int n0 = data->edge[iedge].node[0]; |
| 754 |
int n1 = data->edge[iedge].node[1]; |
| 755 |
int lid0 = globalMapG->getLocalElement((int)bulkData.identifier(nodes[n0]) -1); |
| 756 |
int lid1 = globalMapG->getLocalElement((int)bulkData.identifier(nodes[n1]) -1); |
| 757 |
if(lid0 != -1 && lid1 != -1) { |
| 758 |
int lo = std::min(lid0,lid1); |
| 759 |
int hi = std::max(lid0,lid1); |
| 760 |
std::pair<int,int> key(lo,hi); |
| 761 |
if (local_edge_hash.find(key) == local_edge_hash.end()) { |
| 762 |
int new_edge_id = edge_vector.size(); |
| 763 |
local_edge_hash[key] = new_edge_id; |
| 764 |
edge_vector.push_back(key); |
| 765 |
elemToEdge(elem_ct,iedge) = new_edge_id; |
| 766 |
} |
| 767 |
else { |
| 768 |
elemToEdge(elem_ct,iedge) = local_edge_hash[key]; |
| 769 |
} |
| 770 |
} |
| 771 |
}//end edge loop |
| 772 |
|
| 773 |
sigmaVal(elem_ct) = 1;// Not doing sigma here |
| 774 |
|
| 775 |
elem_ct++; |
| 776 |
}//end element loop |
| 777 |
}//end bucket loop |
| 778 |
|
| 779 |
Intrepid::FieldContainer<int> edgeToNode(edge_vector.size(), 2); |
| 780 |
for(int i=0; i<(int)edge_vector.size(); i++) { |
| 781 |
edgeToNode(i,0) = edge_vector[i].first; |
| 782 |
edgeToNode(i,1) = edge_vector[i].second; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
//MLStatistics.Phase1(elemToNode,elemToEdge,edgeToNode,nodeCoord,sigmaVal); |
| 787 |
|
| 788 |
} |
| 789 |
tm = Teuchos::null; |
| 790 |
|
| 791 |
/**********************************************************************************/ |
| 792 |
/******************** DEFINE WORKSETS AND LOOP OVER THEM **************************/ |
| 793 |
/**********************************************************************************/ |
| 794 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("5) Matrix/RHS Assembly"))); |
| 795 |
RCP<Tpetra_FECrsMatrix> StiffMatrix = rcp(new Tpetra_FECrsMatrix(StiffGraph)); |
| 796 |
|
| 797 |
// Define desired workset size and count how many worksets there are on this processor's mesh block |
| 798 |
//int desiredWorksetSize = numElems; // change to desired workset size! |
| 799 |
////int desiredWorksetSize = 100; // change to desired workset size! |
| 800 |
//int numWorksets = numElems/desiredWorksetSize; |
| 801 |
int desiredWorksetSize = numLocalElems; // change to desired workset size! |
| 802 |
//int desiredWorksetSize = 100; // change to desired workset size! |
| 803 |
int numWorksets = numLocalElems/desiredWorksetSize; |
| 804 |
|
| 805 |
// When numLocalElems is not divisible by desiredWorksetSize, increase workset count by 1 |
| 806 |
if(numWorksets*desiredWorksetSize < numLocalElems) numWorksets += 1; |
| 807 |
|
| 808 |
if (MyPID == 0) { |
| 809 |
std::cout << " Desired workset size: " << desiredWorksetSize << std::endl; |
| 810 |
std::cout << " Number of worksets (per processor): " << numWorksets << std::endl << std::endl; |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
// Start assembly |
| 816 |
RCP<Tpetra_FEMultiVector> rhsVector = rcp (new Tpetra_FEMultiVector(globalMapG,StiffGraph->getImporter(),1)); |
| 817 |
Tpetra::beginAssembly(*StiffMatrix,*rhsVector); |
| 818 |
|
| 819 |
// Right now, this loop only increments once: |
| 820 |
// numWorkset = 1 |
| 821 |
// start = 0 |
| 822 |
// end = numLocalElems |
| 823 |
// worksetSize = numLocalElems |
| 824 |
for(int workset = 0; workset < numWorksets; workset++) { |
| 825 |
|
| 826 |
// compute cell numbers where the workset starts and ends |
| 827 |
int worksetSize = 0; |
| 828 |
int worksetBegin = (workset + 0)*desiredWorksetSize; |
| 829 |
int worksetEnd = (workset + 1)*desiredWorksetSize; |
| 830 |
|
| 831 |
// when numLocalElems is not divisible by desiredWorksetSize, the last workset ends at numLocalElems |
| 832 |
worksetEnd = (worksetEnd <= numLocalElems) ? worksetEnd : numLocalElems; |
| 833 |
|
| 834 |
// allocate the array for the cell nodes |
| 835 |
worksetSize = worksetEnd - worksetBegin; |
| 836 |
IntrepidFieldContainer cellWorkset(worksetSize, numNodesPerElem, spaceDim); |
| 837 |
|
| 838 |
// copy coordinates into cell workset |
| 839 |
int cellCounter = 0; |
| 840 |
for (const stk::mesh::Bucket* elemBucket : localElementBuckets) { |
| 841 |
for (stk::mesh::Entity elem : *elemBucket) { |
| 842 |
stk::mesh::ConnectedEntities nodes = bulkData.get_connected_entities(elem,NODE_RANK); |
| 843 |
for (unsigned inode = 0; inode < nodes.size(); ++inode) { |
| 844 |
double *coord = stk::mesh::field_data(*coords, nodes[inode]); |
| 845 |
cellWorkset(cellCounter, inode, 0) = coord[0]; |
| 846 |
cellWorkset(cellCounter, inode, 1) = coord[1]; |
| 847 |
if(spaceDim==3) cellWorkset(cellCounter, inode, 2) = coord[2]; |
| 848 |
} |
| 849 |
cellCounter++; |
| 850 |
} |
| 851 |
} |
| 852 |
|
| 853 |
/**********************************************************************************/ |
| 854 |
/* Allocate arrays */ |
| 855 |
/**********************************************************************************/ |
| 856 |
|
| 857 |
// Containers for Jacobians, integration measure & cubature points in workset cells |
| 858 |
IntrepidFieldContainer worksetJacobian (worksetSize, numCubPoints, spaceDim, spaceDim); |
| 859 |
IntrepidFieldContainer worksetJacobInv (worksetSize, numCubPoints, spaceDim, spaceDim); |
| 860 |
IntrepidFieldContainer worksetJacobDet (worksetSize, numCubPoints); |
| 861 |
IntrepidFieldContainer worksetCubWeights(worksetSize, numCubPoints); |
| 862 |
IntrepidFieldContainer worksetCubPoints (worksetSize, numCubPoints, cubDim); |
| 863 |
|
| 864 |
// Containers for basis values transformed to workset cells and them multiplied by cubature weights |
| 865 |
IntrepidFieldContainer worksetBasisValues (worksetSize, numFieldsG, numCubPoints); |
| 866 |
IntrepidFieldContainer worksetBasisValuesWeighted(worksetSize, numFieldsG, numCubPoints); |
| 867 |
IntrepidFieldContainer worksetBasisGrads (worksetSize, numFieldsG, numCubPoints, spaceDim); |
| 868 |
IntrepidFieldContainer worksetBasisGradsWeighted (worksetSize, numFieldsG, numCubPoints, spaceDim); |
| 869 |
|
| 870 |
// Containers for diffusive & advective fluxes & non-conservative adv. term and reactive terms |
| 871 |
IntrepidFieldContainer worksetDiffusiveFlux(worksetSize, numFieldsG, numCubPoints, spaceDim); |
| 872 |
|
| 873 |
// Containers for material values and source term. Require user-defined functions |
| 874 |
IntrepidFieldContainer worksetMaterialVals (worksetSize, numCubPoints, spaceDim, spaceDim); |
| 875 |
IntrepidFieldContainer worksetSourceTerm (worksetSize, numCubPoints); |
| 876 |
|
| 877 |
// Containers for workset contributions to the discretization matrix and the right hand side |
| 878 |
IntrepidFieldContainer worksetStiffMatrix (worksetSize, numFieldsG, numFieldsG); |
| 879 |
IntrepidFieldContainer worksetRHS (worksetSize, numFieldsG); |
| 880 |
|
| 881 |
|
| 882 |
/**********************************************************************************/ |
| 883 |
/* Calculate Jacobians */ |
| 884 |
/**********************************************************************************/ |
| 885 |
|
| 886 |
IntrepidCTools::setJacobian(worksetJacobian, cubPoints, cellWorkset, cellType); |
| 887 |
IntrepidCTools::setJacobianInv(worksetJacobInv, worksetJacobian ); |
| 888 |
IntrepidCTools::setJacobianDet(worksetJacobDet, worksetJacobian ); |
| 889 |
|
| 890 |
/**********************************************************************************/ |
| 891 |
/* Cubature Points to Physical Frame and Compute Data */ |
| 892 |
/**********************************************************************************/ |
| 893 |
|
| 894 |
// map cubature points to physical frame |
| 895 |
IntrepidCTools::mapToPhysicalFrame (worksetCubPoints, cubPoints, cellWorkset, cellType); |
| 896 |
|
| 897 |
// get A at cubature points |
| 898 |
evaluateMaterialTensor (worksetMaterialVals, worksetCubPoints); |
| 899 |
|
| 900 |
// get source term at cubature points |
| 901 |
evaluateSourceTerm (worksetSourceTerm, worksetCubPoints); |
| 902 |
|
| 903 |
/**********************************************************************************/ |
| 904 |
/* Compute Stiffness Matrix */ |
| 905 |
/**********************************************************************************/ |
| 906 |
|
| 907 |
// Transform basis gradients to physical frame: DF^{-T}(grad u) |
| 908 |
IntrepidFSTools::HGRADtransformGRAD<double>(worksetBasisGrads, |
| 909 |
worksetJacobInv, basisGrads); |
| 910 |
|
| 911 |
// Compute integration measure for workset cells: Det(DF)*w = J*w |
| 912 |
IntrepidFSTools::computeCellMeasure<double>(worksetCubWeights, |
| 913 |
worksetJacobDet, cubWeights); |
| 914 |
|
| 915 |
|
| 916 |
// Multiply transformed (workset) gradients with weighted measure: DF^{-T}(grad u)*J*w |
| 917 |
IntrepidFSTools::multiplyMeasure<double>(worksetBasisGradsWeighted, |
| 918 |
worksetCubWeights, worksetBasisGrads); |
| 919 |
|
| 920 |
|
| 921 |
// Compute material tensor applied to basis grads: A*(DF^{-T}(grad u) |
| 922 |
IntrepidFSTools::tensorMultiplyDataField<double>(worksetDiffusiveFlux, |
| 923 |
worksetMaterialVals, |
| 924 |
worksetBasisGrads); |
| 925 |
|
| 926 |
// Integrate to compute contribution to global stiffness matrix: (DF^{-T}(grad u)*J*w)*(A*DF^{-T}(grad u)) |
| 927 |
IntrepidFSTools::integrate<double>(worksetStiffMatrix, |
| 928 |
worksetBasisGradsWeighted, |
| 929 |
worksetDiffusiveFlux, Intrepid::COMP_BLAS); |
| 930 |
|
| 931 |
/**********************************************************************************/ |
| 932 |
/* Compute RHS */ |
| 933 |
/**********************************************************************************/ |
| 934 |
|
| 935 |
// Transform basis values to physical frame: clones basis values (u) |
| 936 |
IntrepidFSTools::HGRADtransformVALUE<double>(worksetBasisValues, |
| 937 |
basisValues); |
| 938 |
|
| 939 |
// Multiply transformed (workset) values with weighted measure: (u)*J*w |
| 940 |
IntrepidFSTools::multiplyMeasure<double>(worksetBasisValuesWeighted, |
| 941 |
worksetCubWeights, worksetBasisValues); |
| 942 |
|
| 943 |
// Integrate worksetSourceTerm against weighted basis function set: f.(u)*J*w |
| 944 |
IntrepidFSTools::integrate<double>(worksetRHS, |
| 945 |
worksetSourceTerm, |
| 946 |
worksetBasisValuesWeighted, Intrepid::COMP_BLAS); |
| 947 |
|
| 948 |
/**********************************************************************************/ |
| 949 |
/***************************** STATISTICS (Part II) ******************************/ |
| 950 |
/**********************************************************************************/ |
| 951 |
/* |
| 952 |
if(do_statistics) |
| 953 |
MLStatistics.Phase2a(worksetJacobDet,worksetCubWeights); |
| 954 |
*/ |
| 955 |
|
| 956 |
/**********************************************************************************/ |
| 957 |
/* Assemble into Global Matrix */ |
| 958 |
/**********************************************************************************/ |
| 959 |
|
| 960 |
//"WORKSET CELL" loop: local cell ordinal is relative to numLocalElems |
| 961 |
//JJH runs from 0 to (#local cells - 1) |
| 962 |
int worksetCellOrdinal = 0; |
| 963 |
for (const stk::mesh::Bucket* elemBucketPtr : localElementBuckets) { |
| 964 |
for (stk::mesh::Entity elem : *elemBucketPtr) { |
| 965 |
|
| 966 |
// Compute cell ordinal relative to the current workset |
| 967 |
|
| 968 |
stk::mesh::ConnectedEntities worksetNodes = bulkData.get_connected_entities(elem,NODE_RANK); |
| 969 |
|
| 970 |
// "CELL EQUATION" loop for the workset cell: cellRow is relative to the cell DoF numbering |
| 971 |
for (int cellRow = 0; cellRow < numFieldsG; cellRow++) { |
| 972 |
|
| 973 |
int globalRow = bulkData.identifier(worksetNodes[cellRow]) - 1; |
| 974 |
double sourceTermContribution = worksetRHS(worksetCellOrdinal, cellRow); |
| 975 |
rhsVector->sumIntoGlobalValue(globalRow, 0, sourceTermContribution); |
| 976 |
|
| 977 |
// "CELL VARIABLE" loop for the workset cell: cellCol is relative to the cell DoF numbering |
| 978 |
for (int cellCol = 0; cellCol < numFieldsG; cellCol++){ |
| 979 |
int globalCol = bulkData.identifier(worksetNodes[cellCol]) - 1; |
| 980 |
double operatorMatrixContribution = worksetStiffMatrix(worksetCellOrdinal, cellRow, cellCol); |
| 981 |
|
| 982 |
Teuchos::Array<GO> col(1); col[0] = globalCol; |
| 983 |
Teuchos::Array<SC> val(1); val[0] = operatorMatrixContribution; |
| 984 |
StiffMatrix->sumIntoGlobalValues(globalRow, col(), val()); |
| 985 |
|
| 986 |
}// end cell col loop |
| 987 |
|
| 988 |
}// end cell row loop |
| 989 |
|
| 990 |
worksetCellOrdinal++; |
| 991 |
}// end workset cell loop |
| 992 |
|
| 993 |
|
| 994 |
} //for (localElementBuckets |
| 995 |
|
| 996 |
}// end workset loop |
| 997 |
|
| 998 |
|
| 999 |
Tpetra::endAssembly(*StiffMatrix,*rhsVector); |
| 1000 |
|
| 1001 |
/**********************************************************************************/ |
| 1002 |
/***************************** STATISTICS (Part IIb) ******************************/ |
| 1003 |
/**********************************************************************************/ |
| 1004 |
/* |
| 1005 |
if(do_statistics){ |
| 1006 |
MLStatistics.Phase2b(Xpetra::toXpetra(StiffMatrix->getGraph()),Teuchos::rcp(&nCoord,false)); |
| 1007 |
} |
| 1008 |
*/ |
| 1009 |
/**********************************************************************************/ |
| 1010 |
/***************************** STATISTICS (Part III) ******************************/ |
| 1011 |
/**********************************************************************************/ |
| 1012 |
/* |
| 1013 |
if(do_statistics){ |
| 1014 |
MLStatistics.Phase3(); |
| 1015 |
Teuchos::ParameterList problemStatistics = MLStatistics.GetStatistics(); |
| 1016 |
if(MyPID==0) std::cout<<"*** Problem Statistics ***"<<std::endl<<problemStatistics<<std::endl; |
| 1017 |
} |
| 1018 |
*/ |
| 1019 |
tm = Teuchos::null; |
| 1020 |
|
| 1021 |
/**********************************************************************************/ |
| 1022 |
/************************** DIRICHLET BC SETUP ************************************/ |
| 1023 |
/**********************************************************************************/ |
| 1024 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("6) Matrix/RHS Dirichlet BCs"))); |
| 1025 |
RCP<Tpetra_MultiVector> lhsVector = rcp(new Tpetra_MultiVector(globalMapG,1)); |
| 1026 |
|
| 1027 |
std::vector<SC> solution_values; solution_values.reserve(bcNodes.size()); |
| 1028 |
std::vector<int> ownedBoundaryNodes; ownedBoundaryNodes.reserve(bcNodes.size()); |
| 1029 |
// Loop over boundary nodes |
| 1030 |
for (unsigned i = 0; i < bcNodes.size(); i++) { |
| 1031 |
int bcNodeId = bulkData.identifier(bcNodes[i]); |
| 1032 |
int lid = globalMapG->getLocalElement((int) bcNodeId -1); |
| 1033 |
if(lid != -1) { |
| 1034 |
ownedBoundaryNodes.push_back(lid); |
| 1035 |
|
| 1036 |
// get coordinates for this node |
| 1037 |
entity_type bcnode = bulkData.get_entity(NODE_RANK,bcNodeId); |
| 1038 |
double * coord = stk::mesh::field_data(*coords, bcnode); |
| 1039 |
|
| 1040 |
// look up exact value of function on boundary |
| 1041 |
double x = coord[0]; |
| 1042 |
double y = coord[1]; |
| 1043 |
double z = (spaceDim==3) ? coord[2] : 0; |
| 1044 |
solution_values.push_back(exactSolution(x, y, z)); |
| 1045 |
} |
| 1046 |
} // end loop over boundary nodes |
| 1047 |
|
| 1048 |
// Apply the Dirichlet conditions to matrix, lhs and rhs |
| 1049 |
Apply_Dirichlet_BCs(ownedBoundaryNodes,*StiffMatrix,*lhsVector,*rhsVector,solution_values); |
| 1050 |
|
| 1051 |
tm = Teuchos::null; |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
// Optionally dump the matrix and/or its coords to files. |
| 1056 |
{ |
| 1057 |
typedef Tpetra::MatrixMarket::Writer<Tpetra_CrsMatrix> writer_type; |
| 1058 |
if (matrixFilename != "") { |
| 1059 |
writer_type::writeSparseFile (matrixFilename, StiffMatrix); |
| 1060 |
} |
| 1061 |
if (rhsFilename != "") { |
| 1062 |
writer_type::writeDenseFile (rhsFilename, rhsVector); |
| 1063 |
} |
| 1064 |
if (coordsFilename != "") { |
| 1065 |
writer_type::writeDenseFile (coordsFilename, nCoord); |
| 1066 |
} |
| 1067 |
} |
| 1068 |
|
| 1069 |
/**********************************************************************************/ |
| 1070 |
/*********************************** SOLVE ****************************************/ |
| 1071 |
/**********************************************************************************/ |
| 1072 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("7) Analytic Solution Generation"))); |
| 1073 |
|
| 1074 |
// Run the solver |
| 1075 |
Teuchos::ParameterList MLList = inputSolverList; |
| 1076 |
RCP<Tpetra_Vector> exactNodalVals = rcp(new Tpetra_Vector(globalMapG)); |
| 1077 |
auto exactNodalVals_h = exactNodalVals->getDataNonConst(); |
| 1078 |
double TotalErrorResidual = 0.0; |
| 1079 |
double TotalErrorExactSol = 0.0; |
| 1080 |
|
| 1081 |
// Get exact solution at nodes |
| 1082 |
const stk::mesh::BucketVector &localNodeBuckets = bulkData.get_buckets(NODE_RANK, locallyOwnedSelector); |
| 1083 |
for (const stk::mesh::Bucket* bucketPtr : localNodeBuckets) { |
| 1084 |
for (stk::mesh::Entity node : *bucketPtr) { |
| 1085 |
double * coord = stk::mesh::field_data(*coords, node); |
| 1086 |
// look up exact value of function on boundary |
| 1087 |
double x = coord[0]; |
| 1088 |
double y = coord[1]; |
| 1089 |
double z = (spaceDim==3) ? coord[2] : 0; |
| 1090 |
int gNodeId = bulkData.identifier(node) - 1; |
| 1091 |
//exactNodalVals[0][gNodeId]=exactSolution(x, y, z); |
| 1092 |
int lid = globalMapG->getLocalElement(gNodeId); |
| 1093 |
exactNodalVals_h[lid]=exactSolution(x, y, z); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
|
| 1097 |
char probType[10] = "laplace"; |
| 1098 |
|
| 1099 |
Teuchos::RCP<Tpetra_CrsMatrix> Acrs = Teuchos::rcp_dynamic_cast<Tpetra_CrsMatrix>(StiffMatrix); |
| 1100 |
Teuchos::RCP<Tpetra_MultiVector> rhsMV = Teuchos::rcp_dynamic_cast<Tpetra_MultiVector>(rhsVector); |
| 1101 |
tm = Teuchos::null; |
| 1102 |
tm = rcp(new TimeMonitor(*TimeMonitor::getNewTimer("8) Linear solve"))); |
| 1103 |
TestMultiLevelPreconditioner(probType, MLList, |
| 1104 |
Acrs, exactNodalVals, |
| 1105 |
rhsMV, lhsVector, nCoord, |
| 1106 |
TotalErrorResidual, TotalErrorExactSol); |
| 1107 |
|
| 1108 |
// Timer Output |
| 1109 |
tm = Teuchos::null; |
| 1110 |
const bool alwaysWriteLocal = false; |
| 1111 |
const bool writeGlobalStats = true; |
| 1112 |
const bool writeZeroTimers = false; |
| 1113 |
const bool ignoreZeroTimers = true; |
| 1114 |
const std::string filter = ""; |
| 1115 |
TimeMonitor::summarize(Comm.ptr(), std::cout, alwaysWriteLocal, writeGlobalStats, |
| 1116 |
writeZeroTimers, Teuchos::Union, filter, ignoreZeroTimers); |
| 1117 |
|
| 1118 |
return 0; |
| 1119 |
|
| 1120 |
} |
| 1121 |
/**********************************************************************************/ |
| 1122 |
/********************************* END MAIN ***************************************/ |
| 1123 |
/**********************************************************************************/ |
| 1124 |
|
| 1125 |
/**********************************************************************************/ |
| 1126 |
/************ USER DEFINED FUNCTIONS FOR EXACT SOLUTION ***************************/ |
| 1127 |
/**********************************************************************************/ |
| 1128 |
|
| 1129 |
template<typename Scalar> |
| 1130 |
const Scalar exactSolution(const Scalar& x, const Scalar& y, const Scalar& z) { |
| 1131 |
|
| 1132 |
// Patch test: tri-linear function is in the FE space and should be recovered |
| 1133 |
return 1. + x + y + z + x*y + x*z + y*z + x*y*z; |
| 1134 |
|
| 1135 |
|
| 1136 |
// Patch test - tet: function is in the FE space and should be recovered |
| 1137 |
// return 1. + x + y + z ; |
| 1138 |
|
| 1139 |
// Patch test - hex: tri-linear function is in the FE space and should be recovered |
| 1140 |
// return 1. + x + y + z + x*y + x*z + y*z + x*y*z; |
| 1141 |
|
| 1142 |
// Analytic solution with homogeneous Dirichlet boundary data for [0 1]x[0 1]x[0 1] |
| 1143 |
// return sin(M_PI*x)*sin(M_PI*y)*sin(M_PI*z)*exp(x+y+z); |
| 1144 |
|
| 1145 |
// Analytic solution with inhomogeneous Dirichlet boundary data |
| 1146 |
// return exp(x + y + z)/(1. + x*y + y*z + x*y*z); |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
template<typename Scalar> |
| 1151 |
void materialTensor(Scalar material[][3], const Scalar& x, const Scalar& y, const Scalar& z) { |
| 1152 |
|
| 1153 |
material[0][0] = 1.; |
| 1154 |
material[0][1] = 0.; |
| 1155 |
material[0][2] = 0.; |
| 1156 |
// |
| 1157 |
material[1][0] = 0.; |
| 1158 |
material[1][1] = 1.; |
| 1159 |
material[1][2] = 0.; |
| 1160 |
// |
| 1161 |
material[2][0] = 0.; |
| 1162 |
material[2][1] = 0.; |
| 1163 |
material[2][2] = 1.; |
| 1164 |
} |
| 1165 |
|
| 1166 |
/**********************************************************************************/ |
| 1167 |
/************** AUXILIARY FUNCTIONS FROM EXACT SOLUTION ***************************/ |
| 1168 |
/**********************************************************************************/ |
| 1169 |
|
| 1170 |
/************ Grad of Exact Solution ****************/ |
| 1171 |
template<typename Scalar> |
| 1172 |
void exactSolutionGrad(Scalar gradExact[3], const Scalar& x, const Scalar& y, const Scalar& z) { |
| 1173 |
|
| 1174 |
// To enable derivatives of the gradient (i.e., 2nd derivatives of the exact solution) need 2 levels of fad types |
| 1175 |
Sacado::Fad::SFad<Scalar,3> fad_x = x; |
| 1176 |
Sacado::Fad::SFad<Scalar,3> fad_y = y; |
| 1177 |
Sacado::Fad::SFad<Scalar,3> fad_z = z; |
| 1178 |
Sacado::Fad::SFad<Scalar,3> u; |
| 1179 |
|
| 1180 |
// Indicate the independent variables |
| 1181 |
fad_x.diff(0,3); |
| 1182 |
fad_y.diff(1,3); |
| 1183 |
fad_z.diff(2,3); |
| 1184 |
|
| 1185 |
u = exactSolution(fad_x, fad_y, fad_z); |
| 1186 |
|
| 1187 |
gradExact[0] = u.dx(0); |
| 1188 |
gradExact[1] = u.dx(1); |
| 1189 |
gradExact[2] = u.dx(2); |
| 1190 |
} |
| 1191 |
|
| 1192 |
/************ Source Term (RHS) ****************/ |
| 1193 |
template<typename Scalar> |
| 1194 |
const Scalar sourceTerm(Scalar& x, Scalar& y, Scalar& z){ |
| 1195 |
|
| 1196 |
Scalar u; |
| 1197 |
Scalar grad_u[3]; |
| 1198 |
Scalar flux[3] = {0.0, 0.0, 0.0}; |
| 1199 |
Scalar material[3][3]; |
| 1200 |
Scalar f = 0.; |
| 1201 |
|
| 1202 |
// Indicate the independent variables |
| 1203 |
x.diff(0,3); |
| 1204 |
y.diff(1,3); |
| 1205 |
z.diff(2,3); |
| 1206 |
|
| 1207 |
// Get exact solution and its gradient |
| 1208 |
u = exactSolution(x, y, z); |
| 1209 |
exactSolutionGrad(grad_u, x, y, z); |
| 1210 |
|
| 1211 |
// Get material tensor |
| 1212 |
materialTensor<Scalar>(material, x, y, z); |
| 1213 |
|
| 1214 |
// Compute total flux = (A.grad u) |
| 1215 |
for(int i = 0; i < 3; i++){ |
| 1216 |
|
| 1217 |
// Add diffusive flux |
| 1218 |
for(int j = 0; j < 3; j++){ |
| 1219 |
flux[i] += material[i][j]*grad_u[j]; |
| 1220 |
} |
| 1221 |
} |
| 1222 |
|
| 1223 |
// Compute source term (right hand side): f = -div(A.grad u) |
| 1224 |
f = -(flux[0].dx(0) + flux[1].dx(1) + flux[2].dx(2)); |
| 1225 |
|
| 1226 |
return f; |
| 1227 |
} |
| 1228 |
|
| 1229 |
/**********************************************************************************/ |
| 1230 |
/*************************** EVALUATION METHODS ***********************************/ |
| 1231 |
/**********************************************************************************/ |
| 1232 |
|
| 1233 |
/************ Material Tensor ****************/ |
| 1234 |
template<class ArrayOut, class ArrayIn> |
| 1235 |
void evaluateMaterialTensor(ArrayOut & matTensorValues, |
| 1236 |
const ArrayIn & evaluationPoints){ |
| 1237 |
|
| 1238 |
int numWorksetCells = evaluationPoints.dimension(0); |
| 1239 |
int numPoints = evaluationPoints.dimension(1); |
| 1240 |
int spaceDim = evaluationPoints.dimension(2); |
| 1241 |
|
| 1242 |
double material[3][3]; |
| 1243 |
|
| 1244 |
for(int cell = 0; cell < numWorksetCells; cell++){ |
| 1245 |
for(int pt = 0; pt < numPoints; pt++){ |
| 1246 |
|
| 1247 |
double x = evaluationPoints(cell, pt, 0); |
| 1248 |
double y = evaluationPoints(cell, pt, 1); |
| 1249 |
double z = 0.0; |
| 1250 |
if(spaceDim==3) z = evaluationPoints(cell, pt, 2); |
| 1251 |
|
| 1252 |
materialTensor<double>(material, x, y, z); |
| 1253 |
|
| 1254 |
for(int row = 0; row < spaceDim; row++){ |
| 1255 |
for(int col = 0; col < spaceDim; col++){ |
| 1256 |
matTensorValues(cell, pt, row, col) = material[row][col]; |
| 1257 |
} |
| 1258 |
} |
| 1259 |
} |
| 1260 |
} |
| 1261 |
} |
| 1262 |
|
| 1263 |
/************ Source Term (RHS) ****************/ |
| 1264 |
template<class ArrayOut, class ArrayIn> |
| 1265 |
void evaluateSourceTerm(ArrayOut & sourceTermValues, |
| 1266 |
const ArrayIn & evaluationPoints){ |
| 1267 |
|
| 1268 |
int numWorksetCells = evaluationPoints.dimension(0); |
| 1269 |
int numPoints = evaluationPoints.dimension(1); |
| 1270 |
|
| 1271 |
for(int cell = 0; cell < numWorksetCells; cell++){ |
| 1272 |
for(int pt = 0; pt < numPoints; pt++){ |
| 1273 |
|
| 1274 |
Sacado::Fad::SFad<double,3> x = evaluationPoints(cell, pt, 0); |
| 1275 |
Sacado::Fad::SFad<double,3> y = evaluationPoints(cell, pt, 1); |
| 1276 |
Sacado::Fad::SFad<double,3> z; |
| 1277 |
if(spaceDim==3) |
| 1278 |
z = evaluationPoints(cell, pt, 2); |
| 1279 |
|
| 1280 |
sourceTermValues(cell, pt) = sourceTerm<Sacado::Fad::SFad<double,3> >(x, y, z).val(); |
| 1281 |
} |
| 1282 |
} |
| 1283 |
} |
| 1284 |
|
| 1285 |
/************ Exact Solution ****************/ |
| 1286 |
template<class ArrayOut, class ArrayIn> |
| 1287 |
void evaluateExactSolution(ArrayOut & exactSolutionValues, |
| 1288 |
const ArrayIn & evaluationPoints){ |
| 1289 |
|
| 1290 |
int numWorksetCells = evaluationPoints.dimension(0); |
| 1291 |
int numPoints = evaluationPoints.dimension(1); |
| 1292 |
|
| 1293 |
for(int cell = 0; cell < numWorksetCells; cell++){ |
| 1294 |
for(int pt = 0; pt < numPoints; pt++){ |
| 1295 |
|
| 1296 |
double x = evaluationPoints(cell, pt, 0); |
| 1297 |
double y = evaluationPoints(cell, pt, 1); |
| 1298 |
double z=0.0; |
| 1299 |
if(spaceDim==3) |
| 1300 |
z = evaluationPoints(cell, pt, 2); |
| 1301 |
|
| 1302 |
exactSolutionValues(cell, pt) = exactSolution<double>(x, y, z); |
| 1303 |
} |
| 1304 |
} |
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
/************ Grad of Exact Solution ****************/ |
| 1309 |
template<class ArrayOut, class ArrayIn> |
| 1310 |
void evaluateExactSolutionGrad(ArrayOut & exactSolutionGradValues, |
| 1311 |
const ArrayIn & evaluationPoints){ |
| 1312 |
|
| 1313 |
int numWorksetCells = evaluationPoints.dimension(0); |
| 1314 |
int numPoints = evaluationPoints.dimension(1); |
| 1315 |
int spaceDim = evaluationPoints.dimension(2); |
| 1316 |
|
| 1317 |
double gradient[3]; |
| 1318 |
|
| 1319 |
for(int cell = 0; cell < numWorksetCells; cell++){ |
| 1320 |
for(int pt = 0; pt < numPoints; pt++){ |
| 1321 |
|
| 1322 |
double x = evaluationPoints(cell, pt, 0); |
| 1323 |
double y = evaluationPoints(cell, pt, 1); |
| 1324 |
double z = 0.0; |
| 1325 |
if(spaceDim==3) |
| 1326 |
z = evaluationPoints(cell, pt, 2); |
| 1327 |
|
| 1328 |
exactSolutionGrad<double>(gradient, x, y, z); |
| 1329 |
|
| 1330 |
for(int row = 0; row < spaceDim; row++){ |
| 1331 |
exactSolutionGradValues(cell, pt, row) = gradient[row]; |
| 1332 |
} |
| 1333 |
} |
| 1334 |
} |
| 1335 |
} |
| 1336 |
|
| 1337 |
/**********************************************************************************/ |
| 1338 |
/******************************* TEST MueLu****************************************/ |
| 1339 |
/**********************************************************************************/ |
| 1340 |
|
| 1341 |
// Test MueLu |
| 1342 |
int TestMultiLevelPreconditioner(char ProblemType[], |
| 1343 |
Teuchos::ParameterList & MLList, |
| 1344 |
Teuchos::RCP<Tpetra_CrsMatrix> & A, |
| 1345 |
Teuchos::RCP<Tpetra_Vector> & xexact, |
| 1346 |
Teuchos::RCP<Tpetra_MultiVector> & b, |
| 1347 |
Teuchos::RCP<Tpetra_MultiVector> & uh, |
| 1348 |
Teuchos::RCP<Tpetra_MultiVector> & coords, |
| 1349 |
double & TotalErrorResidual, |
| 1350 |
double & TotalErrorExactSol) |
| 1351 |
{ |
| 1352 |
using Teuchos::RCP; |
| 1353 |
using Teuchos::rcp; |
| 1354 |
using namespace TrilinosCouplings::TpetraIntrepidPoissonExample; |
| 1355 |
using MT = Teuchos::ScalarTraits<Tpetra_MultiVector::scalar_type>::magnitudeType; |
| 1356 |
|
| 1357 |
// FIXME: This should be CLI controlled |
| 1358 |
double tol = 1e-10; |
| 1359 |
int maxNumIters = 200; |
| 1360 |
int num_steps = 1; |
| 1361 |
std::string solverName = "cg"; |
| 1362 |
|
| 1363 |
// Multigrid Hierarchy |
| 1364 |
Teuchos::ParameterList mueluParams; |
| 1365 |
if (MLList.isSublist("MueLu")) |
| 1366 |
mueluParams = MLList.sublist("MueLu"); |
| 1367 |
// Xpetrify coordinates |
| 1368 |
mueluParams.sublist("user data").set("Coordinates",Xpetra::toXpetra(coords)); |
| 1369 |
if(A->getMap()->getComm()->getRank()==0) |
| 1370 |
std::cout<<"*** MueLu Params ***" <<std::endl<<mueluParams<<std::endl; |
| 1371 |
|
| 1372 |
RCP<Tpetra::Operator<> > Aop = Teuchos::rcp_dynamic_cast<Tpetra_CrsMatrix>(A); |
| 1373 |
auto M = MueLu::CreateTpetraPreconditioner(Aop,mueluParams); |
| 1374 |
|
| 1375 |
// Do the linear solve(s). |
| 1376 |
bool converged = false; |
| 1377 |
int numItersPerformed = 0; |
| 1378 |
{ |
| 1379 |
TEUCHOS_FUNC_TIME_MONITOR_DIFF("Total Solve", total_solve); |
| 1380 |
solveWithBelos (converged, numItersPerformed, solverName, tol, |
| 1381 |
maxNumIters, num_steps, uh, A, b, Teuchos::null, M); |
| 1382 |
} |
| 1383 |
|
| 1384 |
if (b->getMap()->getComm()->getRank() == 0) { |
| 1385 |
std::cout<<"Total Iterations: "<<numItersPerformed<<std::endl; |
| 1386 |
} |
| 1387 |
|
| 1388 |
// Compute ||X-X_exact||_2 |
| 1389 |
Teuchos::Array<MT> norm_x(1), norm_error(1); |
| 1390 |
xexact->norm2 (norm_x()); |
| 1391 |
xexact->update (-1.0, *uh, 1.0); |
| 1392 |
xexact->norm2 (norm_error()); |
| 1393 |
if (b->getMap()->getComm()->getRank() == 0) { |
| 1394 |
std::cout << std::endl |
| 1395 |
<< "||X - X_exact||_2 / ||X_exact||_2 = " << norm_error[0] / norm_x[0] |
| 1396 |
<< std::endl; |
| 1397 |
} |
| 1398 |
|
| 1399 |
TotalErrorExactSol += norm_error[0]; |
| 1400 |
TotalErrorResidual += norm_x[0]; |
| 1401 |
|
| 1402 |
|
| 1403 |
return( numItersPerformed ); |
| 1404 |
} |
| 1405 |
|
| 1406 |
/**********************************************************************************/ |
| 1407 |
/**************************** SIMPLE BASIS FACTORY ********************************/ |
| 1408 |
/**********************************************************************************/ |
| 1409 |
|
| 1410 |
|
| 1411 |
void getBasis(Teuchos::RCP<Intrepid::Basis<double,IntrepidFieldContainer > > &basis, |
| 1412 |
const ShardsCellTopology & cellTopology, |
| 1413 |
int order) { |
| 1414 |
|
| 1415 |
|
| 1416 |
// select basis based on cell topology only for now, and assume first order basis |
| 1417 |
switch (cellTopology.getKey()) { |
| 1418 |
|
| 1419 |
case shards::Tetrahedron<4>::key: |
| 1420 |
basis = Teuchos::rcp(new Intrepid::Basis_HGRAD_TET_C1_FEM<double, IntrepidFieldContainer > ); |
| 1421 |
break; |
| 1422 |
|
| 1423 |
case shards::Hexahedron<8>::key: |
| 1424 |
basis = Teuchos::rcp(new Intrepid::Basis_HGRAD_HEX_C1_FEM<double, IntrepidFieldContainer > ); |
| 1425 |
break; |
| 1426 |
|
| 1427 |
case shards::Triangle<3>::key: |
| 1428 |
basis = Teuchos::rcp(new Intrepid::Basis_HGRAD_TRI_C1_FEM<double, IntrepidFieldContainer > ); |
| 1429 |
break; |
| 1430 |
|
| 1431 |
case shards::Quadrilateral<4>::key: |
| 1432 |
basis = Teuchos::rcp(new Intrepid::Basis_HGRAD_QUAD_C1_FEM<double, IntrepidFieldContainer > ); |
| 1433 |
break; |
| 1434 |
|
| 1435 |
|
| 1436 |
default: |
| 1437 |
TEUCHOS_TEST_FOR_EXCEPTION(1,std::invalid_argument, |
| 1438 |
"Unknown cell topology for basis selction. Please use Hexahedron_8 or Tetrahedron_4, Quadrilateral_4 or Triangle_3"); |
| 1439 |
|
| 1440 |
} |
| 1441 |
|
| 1442 |
} |
| 1443 |
|
| 1444 |
int getDimension( const ShardsCellTopology & cellTopology) { |
| 1445 |
switch (cellTopology.getKey()) { |
| 1446 |
case shards::Tetrahedron<4>::key: |
| 1447 |
case shards::Hexahedron<8>::key: |
| 1448 |
return 3; |
| 1449 |
case shards::Triangle<3>::key: |
| 1450 |
case shards::Quadrilateral<4>::key: |
| 1451 |
return 2; |
| 1452 |
default: |
| 1453 |
TEUCHOS_TEST_FOR_EXCEPTION(1,std::invalid_argument, |
| 1454 |
"Unknown cell topology for basis selction. Please use Hexahedron_8 or Tetrahedron_4, Quadrilateral_4 or Triangle_3"); |
| 1455 |
} |
| 1456 |
} |