| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// NOX: An Object-Oriented Nonlinear Solver Package |
| 4 |
// |
| 5 |
// Copyright 2002 NTESS and the NOX contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
|
| 10 |
#include "NOX_Multiphysics_Matlab_Interface.H" |
| 11 |
|
| 12 |
#ifdef HAVE_MATLAB |
| 13 |
|
| 14 |
#include "Problem_Manager.H" |
| 15 |
|
| 16 |
// This is a new class that may evantually get moved into NOX. For now, |
| 17 |
// this is simply used as a testbed for driving NOX using Matlab |
| 18 |
|
| 19 |
Coupling_Matlab_Interface::Coupling_Matlab_Interface(Problem_Manager & manager_) : |
| 20 |
Matlab_Interface( *(manager_.getCompositeSolver()) ), |
| 21 |
problemManager(manager_) |
| 22 |
{ |
| 23 |
std::cout << "coupling matlab started\n"; |
| 24 |
|
| 25 |
commands.push_back( new CMD_problemSummary ( engine, problemManager ) ); |
| 26 |
commands.push_back( new CMD_showAllValid ( engine, problemManager ) ); |
| 27 |
commands.push_back( new CMD_getAllX ( engine, problemManager ) ); |
| 28 |
commands.push_back( new CMD_setXvec ( engine, problemManager ) ); |
| 29 |
commands.push_back( new CMD_compJac ( engine, problemManager ) ); |
| 30 |
commands.push_back( new CMD_compPreconditioner ( engine, problemManager ) ); |
| 31 |
commands.push_back( new CMD_compRes ( engine, problemManager ) ); |
| 32 |
commands.push_back( new CMD_syncAllGroupX ( engine, problemManager ) ); |
| 33 |
commands.push_back( new CMD_doXfers ( engine, problemManager ) ); |
| 34 |
commands.push_back( new CMD_getJac ( engine, problemManager ) ); |
| 35 |
commands.push_back( new CMD_getRes ( engine, problemManager ) ); |
| 36 |
commands.push_back( new CMD_getAllJac ( engine, problemManager ) ); |
| 37 |
commands.push_back( new CMD_getAllRes ( engine, problemManager ) ); |
| 38 |
commands.push_back( new CMD_getPrecMatrix ( engine, problemManager ) ); |
| 39 |
} |
| 40 |
|
| 41 |
//----------------------------------------------------------------------------- |
| 42 |
//---------------------- Commands for Coupling solver ------------------------ |
| 43 |
//----------------------------------------------------------------------------- |
| 44 |
|
| 45 |
Coupling_Matlab_Interface::CommandBase::CommandBase( EpetraExt::EpetraExt_MatlabEngine & engine_, |
| 46 |
Problem_Manager & problemManager_) : |
| 47 |
Matlab_Interface::CommandBase( engine_, *problemManager_.getCompositeSolver() ), |
| 48 |
problemManager(problemManager_) |
| 49 |
{ |
| 50 |
initialize(); |
| 51 |
} |
| 52 |
|
| 53 |
//----------------------------------------------------------------------------- |
| 54 |
//---------------- Matlab Coupling Interface Commands --------------------- |
| 55 |
//----------------------------------------------------------------------------- |
| 56 |
|
| 57 |
bool |
| 58 |
Coupling_Matlab_Interface::CMD_problemSummary::doCommand( std::string commandLine ) |
| 59 |
{ |
| 60 |
problemManager.outputStatus(std::cout); |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
//----------------------------------------------------------------------------- |
| 65 |
|
| 66 |
bool |
| 67 |
Coupling_Matlab_Interface::CMD_showAllValid::doCommand( std::string commandLine ) |
| 68 |
{ |
| 69 |
|
| 70 |
std::cout << std::endl; |
| 71 |
std::cout << "\tCoupling Group status " << std::endl; |
| 72 |
std::cout << "\t--------------------- " << std::endl; |
| 73 |
Matlab_Interface::CMD_showValid::showValid( groupPtr ); |
| 74 |
std::cout << std::endl; |
| 75 |
|
| 76 |
for( int probId = 1; probId <= problemManager.getProblemCount(); ++probId ) |
| 77 |
{ |
| 78 |
std::cout << "\tGroup status for problem \"" << problemManager.getNames()[probId] << "\"" << std::endl; |
| 79 |
std::cout << "\t------------------------ " << std::endl; |
| 80 |
|
| 81 |
const NOX::Epetra::Group * p_probGrp = &(problemManager.getSolutionGroup(probId)); |
| 82 |
|
| 83 |
Matlab_Interface::CMD_showValid::showValid( p_probGrp ); |
| 84 |
std::cout << std::endl; |
| 85 |
} |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
//----------------------------------------------------------------------------- |
| 91 |
|
| 92 |
bool |
| 93 |
Coupling_Matlab_Interface::CMD_compPreconditioner::doCommand( std::string commandLine ) |
| 94 |
{ |
| 95 |
//Epetra_Operator * dummyOp = NULL; |
| 96 |
|
| 97 |
std::cout << "Command currently unspupported." << std::endl; |
| 98 |
return false; |
| 99 |
//cout << "Computing coupling matrix preconditioner." << std::endl; |
| 100 |
//return problemManager.computeJacobian( *solnPtr, *dummyOp ); |
| 101 |
} |
| 102 |
|
| 103 |
//----------------------------------------------------------------------------- |
| 104 |
|
| 105 |
bool |
| 106 |
Coupling_Matlab_Interface::CMD_compJac::doCommand( std::string commandLine ) |
| 107 |
{ |
| 108 |
commandLine.replace( commandLine.find(command), command.size(), ""); |
| 109 |
std::string::size_type loc = commandLine.find(" "); |
| 110 |
if( std::string::npos == loc ) |
| 111 |
{ |
| 112 |
std::cout << "Could not get two valid arguments." << std::endl; |
| 113 |
return false; |
| 114 |
} |
| 115 |
std::string arg1 = commandLine.substr(0, loc); |
| 116 |
commandLine.replace( 0, loc+1, ""); |
| 117 |
loc = commandLine.find("]"); |
| 118 |
std::string arg2 = commandLine.substr(0, loc); |
| 119 |
int probId = atoi( arg1.c_str()) , |
| 120 |
depID = atoi( arg2.c_str()) ; |
| 121 |
|
| 122 |
std::cout << "Computing Jacobian Block " << probId << "," << depID << std::endl; |
| 123 |
|
| 124 |
problemManager.computeBlockJacobian( probId, depID ); |
| 125 |
|
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
//----------------------------------------------------------------------------- |
| 130 |
|
| 131 |
bool |
| 132 |
Coupling_Matlab_Interface::CMD_setXvec::doCommand( std::string commandLine ) |
| 133 |
{ |
| 134 |
commandLine.replace( commandLine.find(command), command.size(), ""); |
| 135 |
std::string::size_type loc = commandLine.find("]"); |
| 136 |
if( std::string::npos == loc ) |
| 137 |
{ |
| 138 |
std::cout << "Could not get a valid argument." << std::endl; |
| 139 |
return false; |
| 140 |
} |
| 141 |
std::string arg1 = commandLine.substr(0, loc); |
| 142 |
int probId = atoi( arg1.c_str()) ; |
| 143 |
|
| 144 |
commandLine.replace( 0, loc+2, ""); |
| 145 |
|
| 146 |
std::cout << "Set solution vector for Problem " << probId << " using \"" |
| 147 |
<< commandLine << "\"" << std::endl; |
| 148 |
|
| 149 |
// Note we set both vectors, the one one in the problem, and the one in |
| 150 |
// the corresponding group |
| 151 |
|
| 152 |
Epetra_Vector * p_soln = problemManager.getProblem(probId).getSolution().get(); |
| 153 |
|
| 154 |
engine.GetMultiVector( commandLine.c_str(), *p_soln ); |
| 155 |
const_cast<NOX::Epetra::Group&>(problemManager.getSolutionGroup(probId)).setX(*p_soln); |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
//----------------------------------------------------------------------------- |
| 161 |
|
| 162 |
bool |
| 163 |
Coupling_Matlab_Interface::CMD_compRes::doCommand( std::string commandLine ) |
| 164 |
{ |
| 165 |
commandLine.replace( commandLine.find(command), command.size(), ""); |
| 166 |
std::string::size_type loc = commandLine.find("]"); |
| 167 |
if( std::string::npos == loc ) |
| 168 |
{ |
| 169 |
std::cout << "Could not get a valid argument." << std::endl; |
| 170 |
return false; |
| 171 |
} |
| 172 |
std::string arg1 = commandLine.substr(0, loc); |
| 173 |
int probId = atoi( arg1.c_str()) ; |
| 174 |
|
| 175 |
std::cout << "Computing Residual for Problem " << probId << std::endl; |
| 176 |
|
| 177 |
problemManager.computeGroupF( probId ); |
| 178 |
|
| 179 |
return true; |
| 180 |
} |
| 181 |
|
| 182 |
//----------------------------------------------------------------------------- |
| 183 |
|
| 184 |
bool |
| 185 |
Coupling_Matlab_Interface::CMD_syncAllGroupX::doCommand( std::string command ) |
| 186 |
{ |
| 187 |
|
| 188 |
const Epetra_Vector * compositeSoln = &(dynamic_cast<const NOX::Epetra::Vector&> |
| 189 |
(groupPtr->getX()).getEpetraVector()); |
| 190 |
|
| 191 |
for( int probId = 1; probId <= problemManager.getProblemCount(); ++probId ) |
| 192 |
{ |
| 193 |
Epetra_Vector * tempSoln = new Epetra_Vector( problemManager.getSolutionVec(probId) ); |
| 194 |
|
| 195 |
problemManager.copyCompositeToVector( *compositeSoln, probId, *tempSoln ); |
| 196 |
|
| 197 |
NOX::Epetra::Group & grp = const_cast<NOX::Epetra::Group&>( problemManager.getSolutionGroup(probId) ); |
| 198 |
grp.setX(*tempSoln); |
| 199 |
|
| 200 |
delete tempSoln; tempSoln = 0; |
| 201 |
|
| 202 |
std::cout << "Copied composite solution into problem group # " << probId << std::endl; |
| 203 |
} |
| 204 |
|
| 205 |
return true; |
| 206 |
} |
| 207 |
|
| 208 |
//----------------------------------------------------------------------------- |
| 209 |
|
| 210 |
bool |
| 211 |
Coupling_Matlab_Interface::CMD_doXfers::doCommand( std::string command ) |
| 212 |
{ |
| 213 |
problemManager.syncAllProblems(); |
| 214 |
problemManager.setAllGroupX(); |
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
//----------------------------------------------------------------------------- |
| 219 |
|
| 220 |
bool |
| 221 |
Coupling_Matlab_Interface::CMD_getAllX::doCommand( std::string command ) |
| 222 |
{ |
| 223 |
map<int, Teuchos::RCP<GenericEpetraProblem> >::iterator problemIter = problemManager.getProblems().begin(); |
| 224 |
map<int, Teuchos::RCP<GenericEpetraProblem> >::iterator problemLast = problemManager.getProblems().end(); |
| 225 |
|
| 226 |
// Do diagonal blocks |
| 227 |
for( ; problemLast != problemIter; ++problemIter ) |
| 228 |
{ |
| 229 |
int probId = (*problemIter).first; |
| 230 |
|
| 231 |
const Epetra_Vector * tempVec = &( problemManager.getSolutionVec(probId) ); |
| 232 |
|
| 233 |
if( tempVec ) |
| 234 |
{ |
| 235 |
ostringstream sval1; |
| 236 |
sval1 << probId << flush; |
| 237 |
std::string name = "X_" + sval1.str(); |
| 238 |
engine.PutMultiVector( *tempVec, name.c_str() ); |
| 239 |
std::cout << "Stored Solution (" << probId << ") in \"" << name << "\"" << std::endl; |
| 240 |
} |
| 241 |
} |
| 242 |
return true; |
| 243 |
} |
| 244 |
|
| 245 |
//----------------------------------------------------------------------------- |
| 246 |
|
| 247 |
bool |
| 248 |
Coupling_Matlab_Interface::CMD_getJac::doCommand( std::string commandLine ) |
| 249 |
{ |
| 250 |
commandLine.replace( commandLine.find(command), command.size(), ""); |
| 251 |
std::string::size_type loc = commandLine.find(" "); |
| 252 |
if( std::string::npos == loc ) |
| 253 |
{ |
| 254 |
std::cout << "Could not get two valid arguments." << std::endl; |
| 255 |
return false; |
| 256 |
} |
| 257 |
std::string arg1 = commandLine.substr(0, loc); |
| 258 |
commandLine.replace( 0, loc+1, ""); |
| 259 |
loc = commandLine.find("]"); |
| 260 |
std::string arg2 = commandLine.substr(0, loc); |
| 261 |
int probId = atoi( arg1.c_str()) , |
| 262 |
depID = atoi( arg2.c_str()) ; |
| 263 |
|
| 264 |
Epetra_RowMatrix * rowMatrix = problemManager.getBlockJacobianMatrix( probId, depID ).get(); |
| 265 |
if( rowMatrix ) |
| 266 |
{ |
| 267 |
std::string name = "BJac_" + arg1 + "_" + arg2; |
| 268 |
engine.PutRowMatrix( *rowMatrix, name.c_str(), false ); |
| 269 |
std::cout << "Stored Block Jacobian (" << probId << "," << depID << ") in \"" |
| 270 |
<< name << "\"" << std::endl; |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
std::cout << "Could not get a valid matrix." << std::endl; |
| 275 |
return false; |
| 276 |
} |
| 277 |
|
| 278 |
//----------------------------------------------------------------------------- |
| 279 |
|
| 280 |
bool |
| 281 |
Coupling_Matlab_Interface::CMD_getAllJac::doCommand( std::string commandLine ) |
| 282 |
{ |
| 283 |
map<int, Teuchos::RCP<GenericEpetraProblem> >::iterator problemIter = problemManager.getProblems().begin(); |
| 284 |
map<int, Teuchos::RCP<GenericEpetraProblem> >::iterator problemLast = problemManager.getProblems().end(); |
| 285 |
|
| 286 |
// Do diagonal blocks |
| 287 |
for( ; problemLast != problemIter; ++problemIter ) |
| 288 |
{ |
| 289 |
|
| 290 |
GenericEpetraProblem & problem = *(*problemIter).second; |
| 291 |
int probId = (*problemIter).first; |
| 292 |
|
| 293 |
Epetra_RowMatrix * rowMatrix = problemManager.getBlockJacobianMatrix( probId ).get(); |
| 294 |
|
| 295 |
if( rowMatrix ) |
| 296 |
{ |
| 297 |
ostringstream sval1, sval2; |
| 298 |
sval1 << probId << flush; |
| 299 |
std::string name = "BJac_" + sval1.str() + "_" + sval1.str(); |
| 300 |
engine.PutRowMatrix( *rowMatrix, name.c_str(), false ); |
| 301 |
std::cout << "Stored Block Jacobian (" << probId << "," << probId << ") in \"" |
| 302 |
<< name << "\"" << std::endl; |
| 303 |
} |
| 304 |
|
| 305 |
// Do off-diagoanl blocks if appropriate |
| 306 |
if( problemManager.useOffBlocks() ) |
| 307 |
{ |
| 308 |
#ifdef HAVE_NOX_EPETRAEXT |
| 309 |
for( unsigned int k = 0; k < problem.getDependentProblems().size(); ++k) |
| 310 |
{ |
| 311 |
int depId = problem.getDependentProblems()[k]; |
| 312 |
|
| 313 |
Epetra_RowMatrix * rowMatrix = problemManager.getBlockJacobianMatrix( probId, depId ).get(); |
| 314 |
|
| 315 |
if( rowMatrix ) |
| 316 |
{ |
| 317 |
ostringstream sval1, sval2; |
| 318 |
sval1 << probId << flush; |
| 319 |
sval2 << depId << flush; |
| 320 |
std::string name = "BJac_" + sval1.str() + "_" + sval2.str(); |
| 321 |
engine.PutRowMatrix( *rowMatrix, name.c_str(), false ); |
| 322 |
std::cout << "Stored Block Jacobian (" << probId << "," << depId << ") in \"" |
| 323 |
<< name << "\"" << std::endl; |
| 324 |
} |
| 325 |
} |
| 326 |
#endif |
| 327 |
} |
| 328 |
} |
| 329 |
return true; |
| 330 |
} |
| 331 |
|
| 332 |
//----------------------------------------------------------------------------- |
| 333 |
|
| 334 |
bool |
| 335 |
Coupling_Matlab_Interface::CMD_getRes::doCommand( std::string commandLine ) |
| 336 |
{ |
| 337 |
commandLine.replace( commandLine.find(command), command.size(), ""); |
| 338 |
std::string::size_type loc = commandLine.find("]"); |
| 339 |
if( std::string::npos == loc ) |
| 340 |
{ |
| 341 |
std::cout << "Could not get a valid argument." << std::endl; |
| 342 |
return false; |
| 343 |
} |
| 344 |
std::string arg1 = commandLine.substr(0, loc); |
| 345 |
int probId = atoi( arg1.c_str()) ; |
| 346 |
|
| 347 |
const Epetra_Vector * resVec = &(problemManager.getResidualVec( probId )); |
| 348 |
|
| 349 |
ostringstream sval1; |
| 350 |
sval1 << probId << flush; |
| 351 |
std::string name = "F_" + sval1.str(); |
| 352 |
engine.PutMultiVector( *resVec, name.c_str() ); |
| 353 |
std::cout << "Stored Residual (" << probId << ") in \"" << name << "\"" << std::endl; |
| 354 |
|
| 355 |
return true; |
| 356 |
} |
| 357 |
|
| 358 |
//----------------------------------------------------------------------------- |
| 359 |
|
| 360 |
bool |
| 361 |
Coupling_Matlab_Interface::CMD_getAllRes::doCommand( std::string command ) |
| 362 |
{ |
| 363 |
map<int, Teuchos::RCP<GenericEpetraProblem> >::iterator problemIter = problemManager.getProblems().begin(); |
| 364 |
map<int, Teuchos::RCP<GenericEpetraProblem> >::iterator problemLast = problemManager.getProblems().end(); |
| 365 |
|
| 366 |
// Do diagonal blocks |
| 367 |
for( ; problemLast != problemIter; ++problemIter ) |
| 368 |
{ |
| 369 |
//GenericEpetraProblem & problem = *(*problemIter).second; |
| 370 |
int probId = (*problemIter).first; |
| 371 |
|
| 372 |
const Epetra_Vector * resVec = &(problemManager.getResidualVec( probId )); |
| 373 |
|
| 374 |
if( resVec ) |
| 375 |
{ |
| 376 |
ostringstream sval1; |
| 377 |
sval1 << probId << flush; |
| 378 |
std::string name = "F_" + sval1.str(); |
| 379 |
engine.PutMultiVector( *resVec, name.c_str() ); |
| 380 |
std::cout << "Stored Residual (" << probId << ") in \"" << name << "\"" << std::endl; |
| 381 |
} |
| 382 |
} |
| 383 |
return true; |
| 384 |
} |
| 385 |
|
| 386 |
//----------------------------------------------------------------------------- |
| 387 |
|
| 388 |
bool |
| 389 |
Coupling_Matlab_Interface::CMD_getPrecMatrix::doCommand( std::string command ) |
| 390 |
{ |
| 391 |
|
| 392 |
std::cout << "Command currently unspupported." << std::endl; |
| 393 |
return false; |
| 394 |
//Epetra_RowMatrix & rowMatrix = *(problemManager.get_matrix()); |
| 395 |
//engine.PutRowMatrix( rowMatrix, "PrecMatrix", false ); |
| 396 |
return true; |
| 397 |
|
| 398 |
} |
| 399 |
|
| 400 |
#endif |