| 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 "LinearSystem.H" |
| 11 |
|
| 12 |
// Paracont headers |
| 13 |
#include "IOContFileUtils.H" |
| 14 |
|
| 15 |
// Trilinos headers |
| 16 |
#include "EpetraExt_MultiVectorOut.h" |
| 17 |
|
| 18 |
#ifdef HAVE_MPI |
| 19 |
LinearSystem:: |
| 20 |
LinearSystem( |
| 21 |
const Teuchos::RCP <Epetra_MpiComm> & aComm): |
| 22 |
#else |
| 23 |
LinearSystem:: |
| 24 |
LinearSystem( |
| 25 |
const Teuchos::RCP <Epetra_SerialComm> & aComm): |
| 26 |
#endif |
| 27 |
comm(aComm), |
| 28 |
continuableParams(LOCA::ParameterVector()), |
| 29 |
continuationFileParams(Teuchos::null), |
| 30 |
initialGuess(Teuchos::null), |
| 31 |
jacobian(Teuchos::null), |
| 32 |
myGlobalElements(NULL), |
| 33 |
numMyElements(0), |
| 34 |
vectorMap(Teuchos::null) |
| 35 |
{ |
| 36 |
|
| 37 |
// Check if we are running with too many processors |
| 38 |
TEUCHOS_TEST_FOR_EXCEPTION( comm->NumProc() > 3, |
| 39 |
std::logic_error, |
| 40 |
"Run this code with 3 processors at most!"); |
| 41 |
|
| 42 |
// Parameters ------------------------------------------- |
| 43 |
// |
| 44 |
// Building the list of continuable parameters |
| 45 |
// (only one parameter: p) |
| 46 |
continuableParams.addParameter("p",1.0); |
| 47 |
|
| 48 |
// In the continuation file, we want p and the three |
| 49 |
// components of x, |
| 50 |
continuationFileParams = |
| 51 |
Teuchos::rcp (new Teuchos::ParameterList()); |
| 52 |
continuationFileParams->set<double>("p", |
| 53 |
continuableParams.getValue("p")); |
| 54 |
continuationFileParams->set<double>("x1",0.0); |
| 55 |
continuationFileParams->set<double>("x2",0.0); |
| 56 |
continuationFileParams->set<double>("x3",0.0); |
| 57 |
|
| 58 |
// Vector distribution --------------------------------- |
| 59 |
// |
| 60 |
// Vector map for three components |
| 61 |
vectorMap = Teuchos::rcp (new Epetra_Map(3,0,*comm)); |
| 62 |
|
| 63 |
// Number of local elements |
| 64 |
numMyElements = vectorMap->NumMyElements(); |
| 65 |
|
| 66 |
// Quering local to global indexing for vectors |
| 67 |
myGlobalElements = vectorMap->MyGlobalElements(); |
| 68 |
|
| 69 |
// Initial guess --------------------------------------- |
| 70 |
initialGuess = |
| 71 |
Teuchos::rcp (new Epetra_Vector(*vectorMap)); |
| 72 |
initialGuess->PutScalar(0.0); |
| 73 |
|
| 74 |
// Jacobian -------------------------------------------- |
| 75 |
// Number of nonzero elements |
| 76 |
int * numNonzeros = new int[3]; |
| 77 |
for ( int i = 0; i < numMyElements; i++ ) |
| 78 |
if ( myGlobalElements[i] == 0 ) |
| 79 |
numNonzeros[i] = 3; |
| 80 |
else |
| 81 |
numNonzeros[i] = 2; |
| 82 |
|
| 83 |
// Create jacobian |
| 84 |
jacobian = Teuchos::rcp (new Epetra_CrsMatrix(Copy,*vectorMap,numNonzeros)); |
| 85 |
|
| 86 |
// Filling the jacobian |
| 87 |
int * indices = new int[3]; |
| 88 |
double * values = new double[3]; |
| 89 |
for ( int i = 0; i < numMyElements; i++ ) |
| 90 |
{ |
| 91 |
switch (myGlobalElements[i]) { |
| 92 |
case 0: |
| 93 |
indices[0] = 0; |
| 94 |
values[0] = 1.0; |
| 95 |
indices[1] = 1; |
| 96 |
values[1] = 1.0; |
| 97 |
indices[2] = 2; |
| 98 |
values[2] = 1.0; |
| 99 |
break; |
| 100 |
case 1: |
| 101 |
indices[0] = 0; |
| 102 |
values[0] = -2.0; |
| 103 |
indices[1] = 1; |
| 104 |
values[1] = -2.0; |
| 105 |
break; |
| 106 |
case 2: |
| 107 |
indices[0] = 0; |
| 108 |
values[0] = 2.0; |
| 109 |
indices[1] = 1; |
| 110 |
values[1] = 1.0; |
| 111 |
break; |
| 112 |
default: |
| 113 |
throw "Thrown exception in LinearSystem: myGlobalElements[i] must be between 0 to 2"; |
| 114 |
} |
| 115 |
jacobian->InsertGlobalValues(myGlobalElements[i],numNonzeros[i],values,indices); |
| 116 |
} |
| 117 |
|
| 118 |
// Optimise storage |
| 119 |
jacobian->FillComplete(); |
| 120 |
jacobian->OptimizeStorage(); |
| 121 |
|
| 122 |
// Clean |
| 123 |
delete [] numNonzeros; |
| 124 |
delete [] indices; |
| 125 |
delete [] values; |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
LinearSystem:: |
| 130 |
~LinearSystem() |
| 131 |
{ |
| 132 |
} |
| 133 |
|
| 134 |
bool LinearSystem:: |
| 135 |
ComputeF(const Epetra_Vector & x, Epetra_Vector & f) |
| 136 |
{ |
| 137 |
// Matrix-vector multiplication |
| 138 |
jacobian->Multiply(false,x,f); |
| 139 |
|
| 140 |
// Getting the continuable parameter |
| 141 |
double p = continuableParams.getValue("p"); |
| 142 |
|
| 143 |
// Filling in the forcing terms |
| 144 |
for ( int i = 0; i < numMyElements; i++ ) |
| 145 |
if (myGlobalElements[i] == 0 || myGlobalElements[i] == 1) |
| 146 |
f[i] -= p; |
| 147 |
|
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
bool LinearSystem:: |
| 152 |
ComputeJacF(const Epetra_Vector & x) |
| 153 |
{ |
| 154 |
return true; |
| 155 |
} |
| 156 |
|
| 157 |
Teuchos::RCP <Epetra_CrsMatrix> LinearSystem:: |
| 158 |
GetJacF() const |
| 159 |
{ |
| 160 |
return jacobian; |
| 161 |
} |
| 162 |
|
| 163 |
Teuchos::RCP <Epetra_Vector> LinearSystem:: |
| 164 |
GetInitialGuess() const |
| 165 |
{ |
| 166 |
return initialGuess; |
| 167 |
} |
| 168 |
|
| 169 |
LOCA::ParameterVector LinearSystem:: |
| 170 |
GetContinuableParams() const |
| 171 |
{ |
| 172 |
return continuableParams; |
| 173 |
} |
| 174 |
|
| 175 |
bool LinearSystem:: |
| 176 |
SetContinuableParameter(std::string label,double value) |
| 177 |
{ |
| 178 |
|
| 179 |
// These are the continuable parameters |
| 180 |
if (label == "p") |
| 181 |
continuableParams.setValue("p",value); |
| 182 |
else |
| 183 |
throw "Thrown exception in SetParameter(): label not known"; |
| 184 |
|
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
bool LinearSystem:: |
| 189 |
UpdateContinuationFile( const std::string & fileName, |
| 190 |
const int & idStep, |
| 191 |
const Teuchos::ParameterList & continuationFileParams) |
| 192 |
{ |
| 193 |
|
| 194 |
// Here we are using the coninuation file utilities |
| 195 |
if (comm->MyPID() == 0) |
| 196 |
UpdateContFile(fileName,idStep,continuationFileParams); |
| 197 |
|
| 198 |
return true; |
| 199 |
} |
| 200 |
|
| 201 |
bool LinearSystem:: |
| 202 |
SetContinuationFileParameters(const Epetra_Vector & x) |
| 203 |
{ |
| 204 |
|
| 205 |
// Parameter p |
| 206 |
continuationFileParams->set<double>("p",continuableParams.getValue("p")); |
| 207 |
|
| 208 |
// Three components of the solution |
| 209 |
for ( int i = 0; i < numMyElements; i++ ) |
| 210 |
switch (myGlobalElements[i]) { |
| 211 |
case 0: |
| 212 |
continuationFileParams->set<double>("x1",x[i]); |
| 213 |
break; |
| 214 |
case 1: |
| 215 |
continuationFileParams->set<double>("x2",x[i]); |
| 216 |
break; |
| 217 |
case 2: |
| 218 |
continuationFileParams->set<double>("x3",x[i]); |
| 219 |
break; |
| 220 |
default: |
| 221 |
throw "Thrown exception in LinearSystem: myGlobalElements[i] must be between 0 to 2"; |
| 222 |
} |
| 223 |
|
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
Teuchos::RCP <Teuchos::ParameterList> LinearSystem:: |
| 228 |
GetContinuationFileParameters() |
| 229 |
{ |
| 230 |
return continuationFileParams; |
| 231 |
} |
| 232 |
|
| 233 |
bool LinearSystem:: |
| 234 |
PrintSolutionFile(const std::string & fileName, const Epetra_Vector & x, |
| 235 |
const Teuchos::ParameterList & xParams) |
| 236 |
{ |
| 237 |
|
| 238 |
// Here we are using Trilinos output to Matlab file |
| 239 |
EpetraExt::MultiVectorToMatlabFile(fileName.c_str(),x); |
| 240 |
|
| 241 |
return true; |
| 242 |
} |