| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// LOCA: Library of Continuation Algorithms Package |
| 4 |
// |
| 5 |
// Copyright 2001-2005 NTESS and the LOCA contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include "LOCA.H" |
| 11 |
#include "LOCA_Epetra.H" |
| 12 |
|
| 13 |
#include "LOCA_MultiContinuation_CompositeConstraint.H" |
| 14 |
#include "NOX_TestCompare.H" |
| 15 |
|
| 16 |
// Trilinos Objects |
| 17 |
#ifdef HAVE_MPI |
| 18 |
#include "Epetra_MpiComm.h" |
| 19 |
#else |
| 20 |
#include "Epetra_SerialComm.h" |
| 21 |
#endif |
| 22 |
#include "Epetra_Map.h" |
| 23 |
#include "Epetra_RowMatrix.h" |
| 24 |
#include "Epetra_CrsMatrix.h" |
| 25 |
|
| 26 |
// User's application specific files |
| 27 |
#include "LOCALinearConstraint.H" |
| 28 |
|
| 29 |
int main(int argc, char *argv[]) |
| 30 |
{ |
| 31 |
int n = 10; |
| 32 |
int ierr = 0; |
| 33 |
double reltol = 1.0e-14; |
| 34 |
double abstol = 1.0e-14; |
| 35 |
int MyPID = 0; |
| 36 |
|
| 37 |
try { |
| 38 |
|
| 39 |
// Initialize MPI |
| 40 |
#ifdef HAVE_MPI |
| 41 |
MPI_Init(&argc,&argv); |
| 42 |
#endif |
| 43 |
|
| 44 |
// Create a communicator for Epetra objects |
| 45 |
#ifdef HAVE_MPI |
| 46 |
Epetra_MpiComm Comm( MPI_COMM_WORLD ); |
| 47 |
#else |
| 48 |
Epetra_SerialComm Comm; |
| 49 |
#endif |
| 50 |
|
| 51 |
MyPID = Comm.MyPID(); |
| 52 |
|
| 53 |
// Create the map |
| 54 |
Epetra_Map map(n, 0, Comm); |
| 55 |
|
| 56 |
bool verbose = false; |
| 57 |
// Check for verbose output |
| 58 |
if (argc>1) |
| 59 |
if (argv[1][0]=='-' && argv[1][1]=='v') |
| 60 |
verbose = true; |
| 61 |
|
| 62 |
// Seed the random number generator in Teuchos. We create random |
| 63 |
// bordering matrices and it is possible different processors might generate |
| 64 |
// different matrices. By setting the seed, this shouldn't happen. |
| 65 |
Teuchos::ScalarTraits<double>::seedrandom(12345); |
| 66 |
|
| 67 |
// Create and initialize the parameter vector |
| 68 |
LOCA::ParameterVector pVector; |
| 69 |
pVector.addParameter("Param 1", 1.69); |
| 70 |
pVector.addParameter("Param 2", -9.7); |
| 71 |
pVector.addParameter("Param 3", 0.35); |
| 72 |
pVector.addParameter("Param 4", -0.78); |
| 73 |
pVector.addParameter("Param 5", 2.53); |
| 74 |
|
| 75 |
// Create parameter list |
| 76 |
Teuchos::RCP<Teuchos::ParameterList> paramList = |
| 77 |
Teuchos::rcp(new Teuchos::ParameterList); |
| 78 |
|
| 79 |
Teuchos::ParameterList& nlParams = paramList->sublist("NOX"); |
| 80 |
Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing"); |
| 81 |
nlPrintParams.set("MyPID", MyPID); |
| 82 |
if (verbose) |
| 83 |
nlPrintParams.set("Output Information", |
| 84 |
NOX::Utils::Error + |
| 85 |
NOX::Utils::Details + |
| 86 |
NOX::Utils::OuterIteration + |
| 87 |
NOX::Utils::InnerIteration + |
| 88 |
NOX::Utils::Warning + |
| 89 |
NOX::Utils::TestDetails + |
| 90 |
NOX::Utils::StepperIteration + |
| 91 |
NOX::Utils::StepperDetails); |
| 92 |
else |
| 93 |
nlPrintParams.set("Output Information", NOX::Utils::Error); |
| 94 |
|
| 95 |
// Create global data object |
| 96 |
Teuchos::RCP<LOCA::GlobalData> globalData = |
| 97 |
LOCA::createGlobalData(paramList); |
| 98 |
|
| 99 |
Epetra_Vector clone_vec(map); |
| 100 |
NOX::Epetra::Vector nox_clone_vec(clone_vec); |
| 101 |
|
| 102 |
Teuchos::RCP<NOX::Abstract::Vector> x = |
| 103 |
nox_clone_vec.clone(NOX::ShapeCopy); |
| 104 |
x->random(); |
| 105 |
|
| 106 |
Teuchos::RCP<NOX::Abstract::MultiVector> dx1 = |
| 107 |
nox_clone_vec.createMultiVector(3); |
| 108 |
Teuchos::RCP<NOX::Abstract::MultiVector> dx2 = |
| 109 |
nox_clone_vec.createMultiVector(1); |
| 110 |
Teuchos::RCP<NOX::Abstract::MultiVector> dx3 = |
| 111 |
nox_clone_vec.createMultiVector(2); |
| 112 |
Teuchos::RCP<NOX::Abstract::MultiVector> dx4 = |
| 113 |
nox_clone_vec.createMultiVector(2); |
| 114 |
dx1->random(); |
| 115 |
dx2->random(); |
| 116 |
dx3->init(0.0); |
| 117 |
dx4->random(); |
| 118 |
|
| 119 |
Teuchos::RCP<NOX::Abstract::MultiVector> dx_all = |
| 120 |
dx1->clone(NOX::DeepCopy); |
| 121 |
dx_all->augment(*dx2); |
| 122 |
dx_all->augment(*dx3); |
| 123 |
dx_all->augment(*dx4); |
| 124 |
|
| 125 |
NOX::Abstract::MultiVector::DenseMatrix dp1(dx1->numVectors(), |
| 126 |
pVector.length()); |
| 127 |
NOX::Abstract::MultiVector::DenseMatrix dp2(dx2->numVectors(), |
| 128 |
pVector.length()); |
| 129 |
NOX::Abstract::MultiVector::DenseMatrix dp3(dx3->numVectors(), |
| 130 |
pVector.length()); |
| 131 |
NOX::Abstract::MultiVector::DenseMatrix dp4(dx4->numVectors(), |
| 132 |
pVector.length()); |
| 133 |
dp1.random(); |
| 134 |
dp2.random(); |
| 135 |
dp3.random(); |
| 136 |
dp4.random(); |
| 137 |
|
| 138 |
NOX::Abstract::MultiVector::DenseMatrix dp_all(dx_all->numVectors(), |
| 139 |
pVector.length()); |
| 140 |
for (int j=0; j<dp_all.numCols(); j++) { |
| 141 |
for (int i=0; i<dp1.numRows(); i++) |
| 142 |
dp_all(i,j) = dp1(i,j); |
| 143 |
for (int i=0; i<dp2.numRows(); i++) |
| 144 |
dp_all(dp1.numRows()+i,j) = dp2(i,j); |
| 145 |
for (int i=0; i<dp3.numRows(); i++) |
| 146 |
dp_all(dp1.numRows()+dp2.numRows()+i,j) = dp3(i,j); |
| 147 |
for (int i=0; i<dp4.numRows(); i++) |
| 148 |
dp_all(dp1.numRows()+dp2.numRows()+dp3.numRows()+i,j) = dp4(i,j); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
std::vector< Teuchos::RCP<LOCA::MultiContinuation::ConstraintInterface> > constraintObjs(4); |
| 153 |
Teuchos::RCP<LinearConstraint> linear_constraint; |
| 154 |
|
| 155 |
linear_constraint = Teuchos::rcp(new LinearConstraint(dx1->numVectors(), |
| 156 |
pVector, |
| 157 |
nox_clone_vec)); |
| 158 |
linear_constraint->setDgDx(*dx1); |
| 159 |
linear_constraint->setDgDp(dp1); |
| 160 |
linear_constraint->setIsZeroDX(false); |
| 161 |
constraintObjs[0] = linear_constraint; |
| 162 |
|
| 163 |
linear_constraint = Teuchos::rcp(new LinearConstraint(dx2->numVectors(), |
| 164 |
pVector, |
| 165 |
nox_clone_vec)); |
| 166 |
linear_constraint->setDgDx(*dx2); |
| 167 |
linear_constraint->setDgDp(dp2); |
| 168 |
linear_constraint->setIsZeroDX(false); |
| 169 |
constraintObjs[1] = linear_constraint; |
| 170 |
|
| 171 |
linear_constraint = Teuchos::rcp(new LinearConstraint(dx3->numVectors(), |
| 172 |
pVector, |
| 173 |
nox_clone_vec)); |
| 174 |
linear_constraint->setDgDx(*dx3); |
| 175 |
linear_constraint->setDgDp(dp3); |
| 176 |
linear_constraint->setIsZeroDX(true); |
| 177 |
constraintObjs[2] = linear_constraint; |
| 178 |
|
| 179 |
linear_constraint = Teuchos::rcp(new LinearConstraint(dx4->numVectors(), |
| 180 |
pVector, |
| 181 |
nox_clone_vec)); |
| 182 |
linear_constraint->setDgDx(*dx4); |
| 183 |
linear_constraint->setDgDp(dp4); |
| 184 |
linear_constraint->setIsZeroDX(false); |
| 185 |
constraintObjs[3] = linear_constraint; |
| 186 |
|
| 187 |
// Check some statistics on the solution |
| 188 |
NOX::TestCompare testCompare(globalData->locaUtils->out(), |
| 189 |
*(globalData->locaUtils)); |
| 190 |
|
| 191 |
LOCA::MultiContinuation::CompositeConstraint composite(globalData, |
| 192 |
constraintObjs); |
| 193 |
composite.setX(*x); |
| 194 |
|
| 195 |
LinearConstraint combined(dx_all->numVectors(), pVector, nox_clone_vec); |
| 196 |
combined.setDgDx(*dx_all); |
| 197 |
combined.setDgDp(dp_all); |
| 198 |
combined.setX(*x); |
| 199 |
|
| 200 |
// |
| 201 |
// test computeConstraints() |
| 202 |
// |
| 203 |
|
| 204 |
composite.computeConstraints(); |
| 205 |
combined.computeConstraints(); |
| 206 |
|
| 207 |
int numConstraints = dx_all->numVectors(); |
| 208 |
const NOX::Abstract::MultiVector::DenseMatrix& g_composite = |
| 209 |
composite.getConstraints(); |
| 210 |
const NOX::Abstract::MultiVector::DenseMatrix& g_combined = |
| 211 |
combined.getConstraints(); |
| 212 |
|
| 213 |
ierr += testCompare.testMatrix( |
| 214 |
g_composite, g_combined, reltol, abstol, |
| 215 |
"CompositeConstraint::computeConstraints()"); |
| 216 |
|
| 217 |
// |
| 218 |
// test computeDP() |
| 219 |
// |
| 220 |
|
| 221 |
std::vector<int> paramIDs(3); |
| 222 |
paramIDs[0] = 1; |
| 223 |
paramIDs[1] = 2; |
| 224 |
paramIDs[2] = 4; |
| 225 |
NOX::Abstract::MultiVector::DenseMatrix dgdp_composite( |
| 226 |
numConstraints, |
| 227 |
paramIDs.size()+1); |
| 228 |
NOX::Abstract::MultiVector::DenseMatrix dgdp_combined( |
| 229 |
numConstraints, |
| 230 |
paramIDs.size()+1); |
| 231 |
dgdp_composite.putScalar(0.0); |
| 232 |
dgdp_combined.putScalar(0.0); |
| 233 |
composite.computeDP(paramIDs, dgdp_composite, false); |
| 234 |
combined.computeDP(paramIDs, dgdp_combined, false); |
| 235 |
|
| 236 |
ierr += testCompare.testMatrix( |
| 237 |
dgdp_composite, dgdp_combined, reltol, abstol, |
| 238 |
"CompositeConstraint::computeDP()"); |
| 239 |
|
| 240 |
// |
| 241 |
// test multiplyDX() |
| 242 |
// |
| 243 |
|
| 244 |
composite.computeDX(); |
| 245 |
combined.computeDX(); |
| 246 |
|
| 247 |
int numMultiply = 5; |
| 248 |
Teuchos::RCP<NOX::Abstract::MultiVector> A = |
| 249 |
nox_clone_vec.createMultiVector(numMultiply); |
| 250 |
A->random(); |
| 251 |
NOX::Abstract::MultiVector::DenseMatrix composite_multiply(numConstraints, |
| 252 |
numMultiply); |
| 253 |
NOX::Abstract::MultiVector::DenseMatrix combined_multiply(numConstraints, |
| 254 |
numMultiply); |
| 255 |
composite.multiplyDX(2.65, *A, composite_multiply); |
| 256 |
combined.multiplyDX(2.65, *A, combined_multiply); |
| 257 |
|
| 258 |
ierr += testCompare.testMatrix(composite_multiply, combined_multiply, |
| 259 |
reltol, abstol, |
| 260 |
"CompositeConstraint::multiplyDX()"); |
| 261 |
|
| 262 |
// |
| 263 |
// test addDX() (No Trans) |
| 264 |
// |
| 265 |
|
| 266 |
int numAdd = 5; |
| 267 |
NOX::Abstract::MultiVector::DenseMatrix B1(numConstraints, numAdd); |
| 268 |
B1.random(); |
| 269 |
NOX::Abstract::MultiVector::DenseMatrix B2(numAdd, numConstraints); |
| 270 |
B2.random(); |
| 271 |
|
| 272 |
Teuchos::RCP<NOX::Abstract::MultiVector> composite_add1 = |
| 273 |
nox_clone_vec.createMultiVector(numAdd); |
| 274 |
composite_add1->random(); |
| 275 |
Teuchos::RCP<NOX::Abstract::MultiVector> composite_add2 = |
| 276 |
nox_clone_vec.createMultiVector(numAdd); |
| 277 |
composite_add2->random(); |
| 278 |
|
| 279 |
Teuchos::RCP<NOX::Abstract::MultiVector> combined_add1 = |
| 280 |
composite_add1->clone(NOX::DeepCopy); |
| 281 |
Teuchos::RCP<NOX::Abstract::MultiVector> combined_add2 = |
| 282 |
composite_add2->clone(NOX::DeepCopy); |
| 283 |
|
| 284 |
composite.addDX(Teuchos::NO_TRANS, 1.45, B1, 2.78, *composite_add1); |
| 285 |
combined.addDX(Teuchos::NO_TRANS, 1.45, B1, 2.78, *combined_add1); |
| 286 |
|
| 287 |
ierr += testCompare.testMultiVector( |
| 288 |
*composite_add1, *combined_add1, |
| 289 |
reltol, abstol, |
| 290 |
"CompositeConstraint::addDX() (No Trans)"); |
| 291 |
|
| 292 |
// |
| 293 |
// test addDX() (Trans) |
| 294 |
// |
| 295 |
|
| 296 |
composite.addDX(Teuchos::TRANS, 1.45, B2, 2.78, *composite_add2); |
| 297 |
combined.addDX(Teuchos::TRANS, 1.45, B2, 2.78, *combined_add2); |
| 298 |
|
| 299 |
ierr += testCompare.testMultiVector( |
| 300 |
*composite_add2, *combined_add2, |
| 301 |
reltol, abstol, |
| 302 |
"CompositeConstraint::addDX() (Trans)"); |
| 303 |
|
| 304 |
LOCA::destroyGlobalData(globalData); |
| 305 |
} |
| 306 |
|
| 307 |
catch (std::exception& e) { |
| 308 |
std::cout << e.what() << std::endl; |
| 309 |
ierr = 1; |
| 310 |
} |
| 311 |
catch (const char *s) { |
| 312 |
std::cout << s << std::endl; |
| 313 |
ierr = 1; |
| 314 |
} |
| 315 |
catch (...) { |
| 316 |
std::cout << "Caught unknown exception!" << std::endl; |
| 317 |
ierr = 1; |
| 318 |
} |
| 319 |
|
| 320 |
if (MyPID == 0) { |
| 321 |
if (ierr == 0) |
| 322 |
std::cout << "All tests passed!" << std::endl; |
| 323 |
else |
| 324 |
std::cout << ierr << " test(s) failed!" << std::endl; |
| 325 |
} |
| 326 |
|
| 327 |
#ifdef HAVE_MPI |
| 328 |
MPI_Finalize() ; |
| 329 |
#endif |
| 330 |
|
| 331 |
return ierr; |
| 332 |
} |