| 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 |
#include "TrilinosCouplings_IntrepidPoissonExampleHelpers.hpp" |
| 11 |
#include <iostream> |
| 12 |
#include <sstream> |
| 13 |
|
| 14 |
|
| 15 |
namespace TrilinosCouplings { |
| 16 |
namespace IntrepidPoissonExample { |
| 17 |
|
| 18 |
|
| 19 |
// Row-major matrix |
| 20 |
class Matrix3{ |
| 21 |
public: |
| 22 |
Matrix3():v_(9,0){} |
| 23 |
|
| 24 |
Matrix3(const std::vector<double> & vals):v_(vals){}; |
| 25 |
|
| 26 |
// Computes C = this * B |
| 27 |
Matrix3 operator*(const Matrix3& B) const { |
| 28 |
Matrix3 C; |
| 29 |
for(int i=0; i<3; i++) |
| 30 |
for(int j=0; j<3; j++) |
| 31 |
for(int k=0; k<3; k++) |
| 32 |
C(i,j) += (*this)(i,k)* B(k,j); |
| 33 |
return C; |
| 34 |
} |
| 35 |
|
| 36 |
double & operator()(int i, int j) { |
| 37 |
return v_[i*3+j]; |
| 38 |
} |
| 39 |
|
| 40 |
const double & operator()(int i, int j) const{ |
| 41 |
return v_[i*3+j]; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
void print(std::ostream & os) { |
| 46 |
os<<"[ "<<v_[0]<<" "<<v_[1]<<" "<<v_[2]<<" ]\n" |
| 47 |
<<"[ "<<v_[3]<<" "<<v_[4]<<" "<<v_[5]<<" ]\n" |
| 48 |
<<"[ "<<v_[6]<<" "<<v_[7]<<" "<<v_[8]<<" ]"<<std::endl; |
| 49 |
} |
| 50 |
|
| 51 |
const std::vector<double> & get() { return v_;}; |
| 52 |
|
| 53 |
private: |
| 54 |
std::vector<double> v_; |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
Matrix3 DiagonalMatrix(const std::vector<double> &d) { |
| 59 |
Matrix3 rv; |
| 60 |
for(int i=0; i<3; i++) |
| 61 |
rv(i,i) = d[i]; |
| 62 |
return rv; |
| 63 |
} |
| 64 |
|
| 65 |
Matrix3 X_Rotation(double theta) { |
| 66 |
std::vector<double> v {1.0, 0.0, 0.0, /**/ 0.0, cos(theta), -sin(theta), /**/ 0.0, sin(theta), cos(theta)}; |
| 67 |
return Matrix3(v); |
| 68 |
} |
| 69 |
|
| 70 |
Matrix3 Y_Rotation(double theta) { |
| 71 |
std::vector<double> v {cos(theta), 0.0, sin(theta), /**/ 0.0, 1.0, 0.0, /**/-sin(theta), 0.0, cos(theta)}; |
| 72 |
return Matrix3(v); |
| 73 |
} |
| 74 |
|
| 75 |
Matrix3 Z_Rotation(double theta) { |
| 76 |
std::vector<double> v {cos(theta), -sin(theta), 0.0, /**/ sin(theta), cos(theta), 0.0,/**/ 0.0, 0.0, 1.0}; |
| 77 |
return Matrix3(v); |
| 78 |
} |
| 79 |
|
| 80 |
Matrix3 Transpose(const Matrix3 & A) { |
| 81 |
Matrix3 B; |
| 82 |
for(int i=0; i<3; i++) |
| 83 |
for(int j=0; j<3; j++) |
| 84 |
B(j,i) = A(i,j); |
| 85 |
return B; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
bool use_diffusion_ = false; |
| 91 |
double materialTensorOffDiagonalValue_; |
| 92 |
|
| 93 |
Matrix3 rotation_, strength_,diff_total_; |
| 94 |
|
| 95 |
std::vector<double> matrix2D_; |
| 96 |
|
| 97 |
bool useDiffusionMatrix() { |
| 98 |
return use_diffusion_; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
double getMaterialTensorOffDiagonalValue () { |
| 103 |
if(use_diffusion_) |
| 104 |
throw std::runtime_error("setMaterialTensorOffDiagonalValue has not been called"); |
| 105 |
return materialTensorOffDiagonalValue_; |
| 106 |
} |
| 107 |
void setMaterialTensorOffDiagonalValue (const double newVal) { |
| 108 |
materialTensorOffDiagonalValue_ = newVal; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
const std::vector<double>& getDiffusionMatrix() { |
| 113 |
if(!use_diffusion_) |
| 114 |
throw std::runtime_error("setDiffusionRotationStrength has not been called"); |
| 115 |
return diff_total_.get(); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
const std::vector<double>& getDiffusionMatrix2D() { |
| 121 |
// Gets the x/y sub-matrix |
| 122 |
if(!use_diffusion_) |
| 123 |
throw std::runtime_error("setDiffusionRotationStrength has not been called"); |
| 124 |
matrix2D_.resize(4); |
| 125 |
matrix2D_[0] = diff_total_(0,0); |
| 126 |
matrix2D_[1] = diff_total_(0,1); |
| 127 |
matrix2D_[2] = diff_total_(1,0); |
| 128 |
matrix2D_[3] = diff_total_(1,1); |
| 129 |
|
| 130 |
return matrix2D_; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
void setDiffusionRotationAndStrength(const std::vector<double>& theta, const std::vector<double>& diagonal) { |
| 135 |
|
| 136 |
rotation_ = Z_Rotation(theta[2]*M_PI/180.0) * Y_Rotation(theta[1]*M_PI/180.0) * X_Rotation(theta[0]*M_PI/180.0); |
| 137 |
strength_ = DiagonalMatrix(diagonal); |
| 138 |
diff_total_ = rotation_ * strength_ * Transpose(rotation_); |
| 139 |
use_diffusion_ = true; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
std::string |
| 145 |
makeMeshInput (const int nx, const int ny, const int nz) |
| 146 |
{ |
| 147 |
using std::endl; |
| 148 |
std::ostringstream os; |
| 149 |
|
| 150 |
TEUCHOS_TEST_FOR_EXCEPTION( nx <= 0 || ny <= 0 || nz <= 0, |
| 151 |
std::invalid_argument, "nx, ny, and nz must all be positive."); |
| 152 |
|
| 153 |
os << "mesh" << endl |
| 154 |
<< "\trectilinear" << endl |
| 155 |
<< "\t\tnx = " << nx << endl |
| 156 |
<< "\t\tny = " << ny << endl |
| 157 |
<< "\t\tnz = " << nz << endl |
| 158 |
<< "\t\tbx = 1" << endl |
| 159 |
<< "\t\tby = 1" << endl |
| 160 |
<< "\t\tbz = 1" << endl |
| 161 |
<< "\t\tgmin = 0 0 0" << endl |
| 162 |
<< "\t\tgmax = 1 1 1" << endl |
| 163 |
<< "\tend" << endl |
| 164 |
<< "\tset assign" << endl |
| 165 |
<< "\t\tsideset, ilo, 1" << endl |
| 166 |
<< "\t\tsideset, jlo, 2" << endl |
| 167 |
<< "\t\tsideset, klo, 3" << endl |
| 168 |
<< "\t\tsideset, ihi, 4" << endl |
| 169 |
<< "\t\tsideset, jhi, 5" << endl |
| 170 |
<< "\t\tsideset, khi, 6" << endl |
| 171 |
<< "\tend" << endl |
| 172 |
<< "end"; |
| 173 |
return os.str (); |
| 174 |
} |
| 175 |
|
| 176 |
void |
| 177 |
setCommandLineArgumentDefaults (int& nx, |
| 178 |
int& ny, |
| 179 |
int& nz, |
| 180 |
std::string& xmlInputParamsFile, |
| 181 |
std::string& solverName, |
| 182 |
bool& verbose, |
| 183 |
bool& debug) |
| 184 |
{ |
| 185 |
nx = 20; |
| 186 |
ny = 20; |
| 187 |
nz = 20; |
| 188 |
xmlInputParamsFile = ""; |
| 189 |
solverName = "GMRES"; |
| 190 |
verbose = false; |
| 191 |
debug = false; |
| 192 |
} |
| 193 |
|
| 194 |
void |
| 195 |
setUpCommandLineArguments (Teuchos::CommandLineProcessor& cmdp, |
| 196 |
int& nx, |
| 197 |
int& ny, |
| 198 |
int& nz, |
| 199 |
std::string& xmlInputParamsFile, |
| 200 |
std::string& solverName, |
| 201 |
double& tol, |
| 202 |
int& maxNumIters, |
| 203 |
bool& verbose, |
| 204 |
bool& debug) |
| 205 |
{ |
| 206 |
cmdp.setOption ("nx", &nx, "Number of cells along the x dimension"); |
| 207 |
cmdp.setOption ("ny", &ny, "Number of cells along the y dimension"); |
| 208 |
cmdp.setOption ("nz", &nz, "Number of cells along the z dimension"); |
| 209 |
cmdp.setOption ("inputParams", &xmlInputParamsFile, "XML file of input " |
| 210 |
"parameters, which we read if specified and not \"\". " |
| 211 |
"If it has a \"meshInput\" parameter, we use its " |
| 212 |
"std::string value as the Pamgen mesh specification. " |
| 213 |
"Otherwise, we tell Pamgen to make a cube, using " |
| 214 |
"nx, ny, and nz."); |
| 215 |
cmdp.setOption ("solverName", &solverName, "Name of iterative linear solver " |
| 216 |
"to use for solving the linear system. You may use any name " |
| 217 |
"that Belos::SolverFactory understands. Examples include " |
| 218 |
"\"GMRES\" and \"CG\"."); |
| 219 |
cmdp.setOption ("tol", &tol, "Tolerance for the linear solve. If not " |
| 220 |
"specified, this is read from the input ParameterList (read " |
| 221 |
"from the XML file). If specified, this overrides any value " |
| 222 |
"in the input ParameterList."); |
| 223 |
cmdp.setOption ("maxNumIters", &maxNumIters, "Maximum number of iterations " |
| 224 |
"in the linear solve. If not specified, this is read from " |
| 225 |
"the input ParameterList (read from the XML file). If " |
| 226 |
"specified, this overrides any value in the input " |
| 227 |
"ParameterList."); |
| 228 |
cmdp.setOption ("verbose", "quiet", &verbose, |
| 229 |
"Whether to print verbose status output."); |
| 230 |
cmdp.setOption ("debug", "release", &debug, |
| 231 |
"Whether to print copious debugging output to stderr."); |
| 232 |
} |
| 233 |
|
| 234 |
void |
| 235 |
parseCommandLineArguments (Teuchos::CommandLineProcessor& cmdp, |
| 236 |
bool& printedHelp, |
| 237 |
int argc, |
| 238 |
char* argv[], |
| 239 |
int& nx, |
| 240 |
int& ny, |
| 241 |
int& nz, |
| 242 |
std::string& xmlInputParamsFile, |
| 243 |
std::string& solverName, |
| 244 |
bool& verbose, |
| 245 |
bool& debug) |
| 246 |
{ |
| 247 |
using Teuchos::CommandLineProcessor; |
| 248 |
|
| 249 |
const CommandLineProcessor::EParseCommandLineReturn parseResult = |
| 250 |
cmdp.parse (argc, argv); |
| 251 |
if (parseResult == CommandLineProcessor::PARSE_HELP_PRINTED) { |
| 252 |
printedHelp = true; |
| 253 |
} |
| 254 |
else { |
| 255 |
printedHelp = false; |
| 256 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 257 |
parseResult != CommandLineProcessor::PARSE_SUCCESSFUL, |
| 258 |
std::invalid_argument, "Failed to parse command-line arguments."); |
| 259 |
TEUCHOS_TEST_FOR_EXCEPTION( |
| 260 |
xmlInputParamsFile == "" && (nx <= 0 || ny <= 0 || nz <= 0), |
| 261 |
std::invalid_argument, "If no XML parameters filename is specified (via " |
| 262 |
"--inputParams), then the number of cells along each dimension of the " |
| 263 |
"mesh (--nx, --ny, and --nz) must be positive."); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
} // namespace IntrepidPoissonExample |
| 268 |
} // namespace TrilinosCouplings |