| 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_Matlab_Interface.H" |
| 11 |
|
| 12 |
#ifdef HAVE_MATLAB |
| 13 |
|
| 14 |
#include "EpetraExt_BlockMapOut.h" |
| 15 |
#include "EpetraExt_VectorOut.h" |
| 16 |
#include "EpetraExt_BlockMapIn.h" |
| 17 |
#include "EpetraExt_CrsMatrixIn.h" |
| 18 |
#include "EpetraExt_RowMatrixOut.h" |
| 19 |
|
| 20 |
// This is a new class that may evantually get moved into NOX. For now, |
| 21 |
// this is simply used as a testbed for driving NOX using Matlab |
| 22 |
|
| 23 |
// Declare and define static data member |
| 24 |
// Declare and define static data member |
| 25 |
map< NOX::Abstract::Group::ReturnType, std::string > Matlab_Interface::CommandBase::returnMsg; |
| 26 |
map< Matlab_Interface::CommandBase::CommandCategory, std::string > Matlab_Interface::CommandBase::categoryDescription; |
| 27 |
std::istream * Matlab_Interface::CMD_newMacro::is = &(std::cin); |
| 28 |
|
| 29 |
Matlab_Interface::Matlab_Interface(NOX::Solver::Manager & noxSolver) : |
| 30 |
enginePtr( new EpetraExt::EpetraExt_MatlabEngine(comm) ), |
| 31 |
engine(*enginePtr), |
| 32 |
solver(noxSolver) |
| 33 |
{ |
| 34 |
std::cout << "matlab started\n"; |
| 35 |
|
| 36 |
// Verify we are using an Epetra concrete implemntation |
| 37 |
const NOX::Epetra::Group * testGroup = &(dynamic_cast<const NOX::Epetra::Group &>(solver.getSolutionGroup())); |
| 38 |
if( NULL == testGroup ) |
| 39 |
{ |
| 40 |
throw "Matlab_Interface ERROR: NOX solver not using Epetra implementation."; |
| 41 |
} |
| 42 |
|
| 43 |
groupPtr = const_cast<NOX::Epetra::Group*>(testGroup); |
| 44 |
|
| 45 |
solnPtr = const_cast<Epetra_Vector*>(&(dynamic_cast<const NOX::Epetra::Vector&>(groupPtr->getX()).getEpetraVector())); |
| 46 |
|
| 47 |
// Pack our list of supported commands |
| 48 |
mapHandler = new CMD_map( engine, solver ) ; |
| 49 |
commands.push_back( mapHandler ); |
| 50 |
|
| 51 |
CMD_read * reader = new CMD_read ( engine, solver ); |
| 52 |
reader->registerDriver( this ); |
| 53 |
commands.push_back( reader ); |
| 54 |
|
| 55 |
CMD_write * writer = new CMD_write ( engine, solver ); |
| 56 |
writer->registerDriver( this ); |
| 57 |
commands.push_back( writer ); |
| 58 |
|
| 59 |
CMD_newMacro * newMacro = new CMD_newMacro ( engine, solver ); |
| 60 |
newMacro->registerDriver( this ); |
| 61 |
commands.push_back( newMacro ); |
| 62 |
|
| 63 |
CMD_showStack * showStack = new CMD_showStack ( engine, solver ); |
| 64 |
showStack->registerDriver( this ); |
| 65 |
commands.push_back( showStack ); |
| 66 |
|
| 67 |
CMD_clearStack * clearStack = new CMD_clearStack ( engine, solver ); |
| 68 |
clearStack->registerDriver( this ); |
| 69 |
commands.push_back( clearStack ); |
| 70 |
|
| 71 |
commands.push_back( new CMD_isF ( engine, solver ) ); |
| 72 |
commands.push_back( new CMD_isJacobian ( engine, solver ) ); |
| 73 |
commands.push_back( new CMD_isGradient ( engine, solver ) ); |
| 74 |
commands.push_back( new CMD_isNewton ( engine, solver ) ); |
| 75 |
commands.push_back( new CMD_isNormNewtonSolveResidual ( engine, solver ) ); |
| 76 |
commands.push_back( new CMD_isPreconditioner ( engine, solver ) ); |
| 77 |
commands.push_back( new CMD_isConditionNumber ( engine, solver ) ); |
| 78 |
commands.push_back( new CMD_showValid ( engine, solver ) ); |
| 79 |
commands.push_back( new CMD_getJacobianConditionNumber ( engine, solver ) ); |
| 80 |
commands.push_back( new CMD_getNormF ( engine, solver ) ); |
| 81 |
commands.push_back( new CMD_setX ( engine, solver ) ); |
| 82 |
commands.push_back( new CMD_computeF ( engine, solver ) ); |
| 83 |
commands.push_back( new CMD_computeJacobian ( engine, solver ) ); |
| 84 |
commands.push_back( new CMD_computeGradient ( engine, solver ) ); |
| 85 |
commands.push_back( new CMD_computeNewton ( engine, solver ) ); |
| 86 |
commands.push_back( new CMD_getX ( engine, solver ) ); |
| 87 |
commands.push_back( new CMD_getF ( engine, solver ) ); |
| 88 |
commands.push_back( new CMD_getGradient ( engine, solver ) ); |
| 89 |
commands.push_back( new CMD_getNewton ( engine, solver ) ); |
| 90 |
commands.push_back( new CMD_getJacobian ( engine, solver ) ); |
| 91 |
} |
| 92 |
|
| 93 |
//----------------------------------------------------------------------------- |
| 94 |
|
| 95 |
Matlab_Interface::~Matlab_Interface() |
| 96 |
{ |
| 97 |
delete enginePtr; |
| 98 |
} |
| 99 |
|
| 100 |
//----------------------------------------------------------------------------- |
| 101 |
|
| 102 |
void |
| 103 |
Matlab_Interface::interact() |
| 104 |
{ |
| 105 |
char s [BUFSIZE] ; |
| 106 |
|
| 107 |
bool status; |
| 108 |
|
| 109 |
while(1) |
| 110 |
{ |
| 111 |
// Prompt the user and get a std::string |
| 112 |
printf(">> "); |
| 113 |
if (fgets(s, BUFSIZE, stdin) == NULL) |
| 114 |
{ |
| 115 |
printf("Bye\n"); |
| 116 |
break ; |
| 117 |
} |
| 118 |
printf ("command :%s:\n", s) ; |
| 119 |
|
| 120 |
std::string inStr(s); |
| 121 |
status = doCommand( inStr ); |
| 122 |
|
| 123 |
if( status ) |
| 124 |
commandStack.push_back( inStr ); |
| 125 |
} |
| 126 |
|
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
//----------------------------------------------------------------------------- |
| 131 |
|
| 132 |
bool |
| 133 |
Matlab_Interface::doCommand( std::string command ) |
| 134 |
{ |
| 135 |
char matlabBuffer [MATLABBUF]; |
| 136 |
|
| 137 |
bool status; |
| 138 |
|
| 139 |
// Parse for NOX commands |
| 140 |
if( command[0] == '#' ) |
| 141 |
{ |
| 142 |
command.erase(0,1); |
| 143 |
// Remove any carriage returns |
| 144 |
if( command.find('\n') != std::string::npos ) |
| 145 |
command.replace( command.find('\n'), 1, ""); |
| 146 |
|
| 147 |
status = doNOXCommand( command ); |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
// Send the command to MATLAB - output goes to stdout |
| 152 |
char * c_comm = const_cast<char *>(command.c_str()); |
| 153 |
int err = engine.EvalString(c_comm, matlabBuffer, MATLABBUF); |
| 154 |
if (err != 0) |
| 155 |
{ |
| 156 |
printf("there was an error: %d", err); |
| 157 |
status = false; |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
printf("Matlab Output:\n%s", matlabBuffer); |
| 162 |
status = true; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return status; |
| 167 |
} |
| 168 |
|
| 169 |
//----------------------------------------------------------------------------- |
| 170 |
|
| 171 |
bool |
| 172 |
Matlab_Interface::doNOXCommand( std::string & command ) |
| 173 |
{ |
| 174 |
|
| 175 |
std::cout << "NOX : " << command << std::endl; |
| 176 |
|
| 177 |
// Allow for "#" no-op |
| 178 |
if( '#' == command[0] ) |
| 179 |
return true; |
| 180 |
|
| 181 |
// Give priority to mapped commands |
| 182 |
|
| 183 |
if( mapHandler->getUserMaps().end() != mapHandler->getUserMaps().find(command) ) |
| 184 |
command = mapHandler->getUserMaps()[command]; |
| 185 |
|
| 186 |
try |
| 187 |
{ |
| 188 |
// Convenience methods |
| 189 |
|
| 190 |
if( command.find("?") != std::string::npos ) |
| 191 |
{ |
| 192 |
std::cout << "\n\tCommand Summary:\n" << std::endl; |
| 193 |
|
| 194 |
std::map< CommandBase::CommandCategory, std::string >::iterator cIter = CommandBase::categoryDescription.begin() , |
| 195 |
cIter_end = CommandBase::categoryDescription.end() ; |
| 196 |
|
| 197 |
for( ; cIter_end != cIter; ++cIter ) |
| 198 |
{ |
| 199 |
std::cout << "\n\t" << (*cIter).second << std::endl << std::endl; |
| 200 |
|
| 201 |
std::list<CommandBase *>::const_iterator iter = commands.begin() , |
| 202 |
iter_end = commands.end() ; |
| 203 |
for( ; iter_end != iter; ++iter ) |
| 204 |
if( (*iter)->isCategory((*cIter).first) ) |
| 205 |
(*iter)->describe(); |
| 206 |
} |
| 207 |
|
| 208 |
if( !mapHandler->getUserMaps().empty() ) |
| 209 |
{ |
| 210 |
std::cout << "\n\tUser Defined (Mapped) Command Summary:\n" << std::endl; |
| 211 |
|
| 212 |
for( std::map<string, std::string>::const_iterator iter = |
| 213 |
mapHandler->getUserMaps().begin(); iter != mapHandler->getUserMaps().end(); ++iter ) |
| 214 |
std::cout << "\t\t" << (*iter).first << " --> " << (*iter).second << std::endl; |
| 215 |
} |
| 216 |
|
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
std::list<CommandBase *>::const_iterator iter = commands.begin() , |
| 221 |
iter_end = commands.end() ; |
| 222 |
for( ; iter_end != iter; ++iter ) |
| 223 |
if( std::string::npos != command.find((*iter)->getCommand()) ) |
| 224 |
return (*iter)->doCommand(command); |
| 225 |
|
| 226 |
std::cout << "\n" << command << " : Not found." << std::endl; |
| 227 |
return false; |
| 228 |
|
| 229 |
} |
| 230 |
catch( const char * msg ) |
| 231 |
{ |
| 232 |
std::cout << msg << std::endl; |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
//----------------------------------------------------------------------------- |
| 240 |
//---------------------- Commands for NOX solver ----------------------------- |
| 241 |
//----------------------------------------------------------------------------- |
| 242 |
|
| 243 |
Matlab_Interface::CommandBase::CommandBase( EpetraExt::EpetraExt_MatlabEngine & engine_, |
| 244 |
NOX::Solver::Manager & solver_ ) : |
| 245 |
engine(engine_), |
| 246 |
solver(solver_), |
| 247 |
driver(0) |
| 248 |
{ |
| 249 |
initialize(); |
| 250 |
} |
| 251 |
|
| 252 |
//----------------------------------------------------------------------------- |
| 253 |
|
| 254 |
void |
| 255 |
Matlab_Interface::CommandBase::initialize() |
| 256 |
{ |
| 257 |
// Verify we are using an Epetra concrete implemntation |
| 258 |
const NOX::Epetra::Group * testGroup = &(dynamic_cast<const NOX::Epetra::Group &>(solver.getSolutionGroup())); |
| 259 |
if( NULL == testGroup ) |
| 260 |
{ |
| 261 |
throw "Matlab_Interface ERROR: NOX solver not using Epetra implementation."; |
| 262 |
} |
| 263 |
|
| 264 |
groupPtr = const_cast<NOX::Epetra::Group*>(testGroup); |
| 265 |
|
| 266 |
solnPtr = const_cast<Epetra_Vector*>(&(dynamic_cast<const NOX::Epetra::Vector&>(groupPtr->getX()).getEpetraVector())); |
| 267 |
|
| 268 |
returnMsg[ NOX::Abstract::Group::Ok ] = "Ok" ; |
| 269 |
returnMsg[ NOX::Abstract::Group::NotDefined ] = "NotDefined" ; |
| 270 |
returnMsg[ NOX::Abstract::Group::BadDependency ] = "BadDependency" ; |
| 271 |
returnMsg[ NOX::Abstract::Group::NotConverged ] = "NotConverged" ; |
| 272 |
returnMsg[ NOX::Abstract::Group::Failed ] = "Failed" ; |
| 273 |
|
| 274 |
categoryDescription[ GROUP_GET ] = "Group 'Get' Methods" ; |
| 275 |
categoryDescription[ GROUP_SET ] = "Group 'Set' Methods" ; |
| 276 |
categoryDescription[ GROUP_COMPUTE ] = "Group 'Compute' Methods" ; |
| 277 |
categoryDescription[ CALLBACK_FUN ] = "Callback Functionality" ; |
| 278 |
categoryDescription[ QUERY ] = "Query Methods" ; |
| 279 |
categoryDescription[ INOUT ] = "Input/Output Methods" ; |
| 280 |
categoryDescription[ GENERIC ] = "Generic Methods" ; |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
//----------------------------------------------------------------------------- |
| 285 |
|
| 286 |
bool |
| 287 |
Matlab_Interface::CMD_map::doCommand( std::string commandLine ) |
| 288 |
{ |
| 289 |
commandLine.replace( commandLine.find("map "), 4, ""); |
| 290 |
std::string::size_type loc = commandLine.find(" "); |
| 291 |
if( std::string::npos == loc ) |
| 292 |
{ |
| 293 |
std::cout << "Could not get two valid arguments." << std::endl; |
| 294 |
return false; |
| 295 |
} |
| 296 |
std::string arg1 = commandLine.substr(0, loc); |
| 297 |
commandLine.replace( 0, loc+1, ""); |
| 298 |
std::string arg2 = commandLine; |
| 299 |
std::cout << "Mapping \"" << arg1 << "\" to \"" << arg2 << "\"" << std::endl; |
| 300 |
userMaps[ arg1 ] = arg2; |
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
//----------------------------------------------------------------------------- |
| 305 |
|
| 306 |
void |
| 307 |
Matlab_Interface::CMD_map::writeMaps( std::ofstream & os ) |
| 308 |
{ |
| 309 |
|
| 310 |
std::map< std::string, std::string >::const_iterator iter = userMaps.begin() , |
| 311 |
iter_end = userMaps.end() ; |
| 312 |
for( ; iter_end != iter; ++iter ) |
| 313 |
{ |
| 314 |
std::string cLine = "#map " + (*iter).first + " " + (*iter).second; |
| 315 |
os << cLine << std::endl; |
| 316 |
} |
| 317 |
|
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
//----------------------------------------------------------------------------- |
| 322 |
|
| 323 |
bool |
| 324 |
Matlab_Interface::CMD_showStack::doCommand( std::string commandLine ) |
| 325 |
{ |
| 326 |
|
| 327 |
if( 0 == driver ) |
| 328 |
{ |
| 329 |
std::cout << "ERROR: No valid driver registered with showStack." << std::endl; |
| 330 |
return false; |
| 331 |
} |
| 332 |
|
| 333 |
// Show the command stack |
| 334 |
std::cout << "Command Stack :\n" << std::endl; |
| 335 |
|
| 336 |
std::list< std::string >::const_iterator iter = driver->getCommandStack().begin() , |
| 337 |
iter_end = driver->getCommandStack().end() ; |
| 338 |
for( ; iter_end != iter; ++iter ) |
| 339 |
std::cout << (*iter) << std::endl; |
| 340 |
|
| 341 |
return true; |
| 342 |
} |
| 343 |
|
| 344 |
//----------------------------------------------------------------------------- |
| 345 |
|
| 346 |
bool |
| 347 |
Matlab_Interface::CMD_clearStack::doCommand( std::string commandLine ) |
| 348 |
{ |
| 349 |
|
| 350 |
if( 0 == driver ) |
| 351 |
{ |
| 352 |
std::cout << "ERROR: No valid driver registered with clearStack." << std::endl; |
| 353 |
return false; |
| 354 |
} |
| 355 |
|
| 356 |
// Show the command stack |
| 357 |
std::cout << "Command Stack Cleared." << std::endl; |
| 358 |
|
| 359 |
driver->getCommandStack().clear(); |
| 360 |
|
| 361 |
return false; |
| 362 |
} |
| 363 |
|
| 364 |
//----------------------------------------------------------------------------- |
| 365 |
|
| 366 |
bool |
| 367 |
Matlab_Interface::CMD_isF::doCommand( std::string commandLine ) |
| 368 |
{ |
| 369 |
std::string isValid = (groupPtr->isF() ? "True" : "False" ); |
| 370 |
std::cout << " --> " << isValid << std::endl; |
| 371 |
return true; |
| 372 |
} |
| 373 |
|
| 374 |
//----------------------------------------------------------------------------- |
| 375 |
|
| 376 |
bool |
| 377 |
Matlab_Interface::CMD_isJacobian::doCommand( std::string commandLine ) |
| 378 |
{ |
| 379 |
std::string isValid = (groupPtr->isJacobian() ? "True" : "False" ); |
| 380 |
std::cout << " --> " << isValid << std::endl; |
| 381 |
return true; |
| 382 |
} |
| 383 |
|
| 384 |
//----------------------------------------------------------------------------- |
| 385 |
|
| 386 |
bool |
| 387 |
Matlab_Interface::CMD_isGradient::doCommand( std::string commandLine ) |
| 388 |
{ |
| 389 |
std::string isValid = (groupPtr->isGradient() ? "True" : "False" ); |
| 390 |
std::cout << " --> " << isValid << std::endl; |
| 391 |
return true; |
| 392 |
} |
| 393 |
|
| 394 |
//----------------------------------------------------------------------------- |
| 395 |
|
| 396 |
bool |
| 397 |
Matlab_Interface::CMD_isNewton::doCommand( std::string commandLine ) |
| 398 |
{ |
| 399 |
std::string isValid = (groupPtr->isNewton() ? "True" : "False" ); |
| 400 |
std::cout << " --> " << isValid << std::endl; |
| 401 |
return true; |
| 402 |
} |
| 403 |
|
| 404 |
//----------------------------------------------------------------------------- |
| 405 |
|
| 406 |
bool |
| 407 |
Matlab_Interface::CMD_isNormNewtonSolveResidual::doCommand( std::string commandLine ) |
| 408 |
{ |
| 409 |
std::string isValid = (groupPtr->isNormNewtonSolveResidual() ? "True" : "False" ); |
| 410 |
std::cout << " --> " << isValid << std::endl; |
| 411 |
return true; |
| 412 |
} |
| 413 |
|
| 414 |
//----------------------------------------------------------------------------- |
| 415 |
|
| 416 |
bool |
| 417 |
Matlab_Interface::CMD_isPreconditioner::doCommand( std::string commandLine ) |
| 418 |
{ |
| 419 |
std::string isValid = (groupPtr->isPreconditioner() ? "True" : "False" ); |
| 420 |
std::cout << " --> " << isValid << std::endl; |
| 421 |
return true; |
| 422 |
} |
| 423 |
|
| 424 |
//----------------------------------------------------------------------------- |
| 425 |
|
| 426 |
bool |
| 427 |
Matlab_Interface::CMD_isConditionNumber::doCommand( std::string commandLine ) |
| 428 |
{ |
| 429 |
std::string isValid = (groupPtr->isConditionNumber() ? "True" : "False" ); |
| 430 |
std::cout << " --> " << isValid << std::endl; |
| 431 |
return true; |
| 432 |
} |
| 433 |
|
| 434 |
//----------------------------------------------------------------------------- |
| 435 |
|
| 436 |
bool |
| 437 |
Matlab_Interface::CMD_showValid::doCommand( std::string commandLine ) |
| 438 |
{ |
| 439 |
return showValid( groupPtr ); |
| 440 |
} |
| 441 |
|
| 442 |
//----------------------------------------------------------------------------- |
| 443 |
|
| 444 |
bool |
| 445 |
Matlab_Interface::CMD_showValid::showValid( const NOX::Epetra::Group * p_Grp ) |
| 446 |
{ |
| 447 |
std::string isValid = (p_Grp->isF() ? "True" : "False" ); |
| 448 |
std::cout << " isF --> " << isValid << std::endl; |
| 449 |
isValid = (p_Grp->isJacobian() ? "True" : "False" ); |
| 450 |
std::cout << " isJacobian --> " << isValid << std::endl; |
| 451 |
isValid = (p_Grp->isGradient() ? "True" : "False" ); |
| 452 |
std::cout << " isGradient --> " << isValid << std::endl; |
| 453 |
isValid = (p_Grp->isNewton() ? "True" : "False" ); |
| 454 |
std::cout << " isNewton --> " << isValid << std::endl; |
| 455 |
isValid = (p_Grp->isPreconditioner() ? "True" : "False" ); |
| 456 |
std::cout << " isPreconditioner --> " << isValid << std::endl; |
| 457 |
return true; |
| 458 |
} |
| 459 |
|
| 460 |
//----------------------------------------------------------------------------- |
| 461 |
|
| 462 |
bool |
| 463 |
Matlab_Interface::CMD_getJacobianConditionNumber::doCommand( std::string commandLine ) |
| 464 |
{ |
| 465 |
std::cout << groupPtr->getJacobianConditionNumber() << std::endl; |
| 466 |
return true; |
| 467 |
} |
| 468 |
|
| 469 |
//----------------------------------------------------------------------------- |
| 470 |
|
| 471 |
bool |
| 472 |
Matlab_Interface::CMD_getNormF::doCommand( std::string commandLine ) |
| 473 |
{ |
| 474 |
std::cout << groupPtr->getNormF() << std::endl; |
| 475 |
return true; |
| 476 |
} |
| 477 |
|
| 478 |
//----------------------------------------------------------------------------- |
| 479 |
|
| 480 |
bool |
| 481 |
Matlab_Interface::CMD_setX::doCommand( std::string commandLine ) |
| 482 |
{ |
| 483 |
commandLine.replace( commandLine.find("setX"), 5, ""); |
| 484 |
engine.GetMultiVector( commandLine.c_str(), *solnPtr ); |
| 485 |
groupPtr->setX(*solnPtr); |
| 486 |
return true; |
| 487 |
} |
| 488 |
|
| 489 |
//----------------------------------------------------------------------------- |
| 490 |
|
| 491 |
bool |
| 492 |
Matlab_Interface::CMD_computeF::doCommand( std::string commandLine ) |
| 493 |
{ |
| 494 |
NOX::Abstract::Group::ReturnType returnStatus = groupPtr->computeF(); |
| 495 |
std::cout << "Return Status = " << returnMsg[ returnStatus ] << std::endl; |
| 496 |
return true; |
| 497 |
} |
| 498 |
|
| 499 |
//----------------------------------------------------------------------------- |
| 500 |
|
| 501 |
bool |
| 502 |
Matlab_Interface::CMD_computeJacobian::doCommand( std::string commandLine ) |
| 503 |
{ |
| 504 |
NOX::Abstract::Group::ReturnType returnStatus = groupPtr->computeJacobian(); |
| 505 |
std::cout << "Return Status = " << returnMsg[ returnStatus ] << std::endl; |
| 506 |
return true; |
| 507 |
} |
| 508 |
|
| 509 |
//----------------------------------------------------------------------------- |
| 510 |
|
| 511 |
bool |
| 512 |
Matlab_Interface::CMD_computeGradient::doCommand( std::string commandLine ) |
| 513 |
{ |
| 514 |
NOX::Abstract::Group::ReturnType returnStatus = groupPtr->computeGradient(); |
| 515 |
std::cout << "Return Status = " << returnMsg[ returnStatus ] << std::endl; |
| 516 |
return true; |
| 517 |
} |
| 518 |
|
| 519 |
//----------------------------------------------------------------------------- |
| 520 |
|
| 521 |
bool |
| 522 |
Matlab_Interface::CMD_computeNewton::doCommand( std::string commandLine ) |
| 523 |
{ |
| 524 |
const Teuchos::ParameterList & const_lsParams = solver.getList(). |
| 525 |
sublist("Direction"). |
| 526 |
sublist("Newton"). |
| 527 |
sublist("Linear Solver"); |
| 528 |
Teuchos::ParameterList & lsParams = const_cast<Teuchos::ParameterList &>(const_lsParams); |
| 529 |
NOX::Abstract::Group::ReturnType returnStatus = groupPtr->computeNewton(lsParams); |
| 530 |
std::cout << "Return Status = " << returnMsg[ returnStatus ] << std::endl; |
| 531 |
return true; |
| 532 |
} |
| 533 |
|
| 534 |
//----------------------------------------------------------------------------- |
| 535 |
|
| 536 |
bool |
| 537 |
Matlab_Interface::CMD_getX::doCommand( std::string commandLine ) |
| 538 |
{ |
| 539 |
const Epetra_Vector * tmpVec = &(dynamic_cast<const NOX::Epetra::Vector&> |
| 540 |
(groupPtr->getX()).getEpetraVector()); |
| 541 |
engine.PutMultiVector( *tmpVec, "X" ); |
| 542 |
return true; |
| 543 |
} |
| 544 |
|
| 545 |
//----------------------------------------------------------------------------- |
| 546 |
|
| 547 |
bool |
| 548 |
Matlab_Interface::CMD_getF::doCommand( std::string commandLine ) |
| 549 |
{ |
| 550 |
const Epetra_Vector * tmpVec = &(dynamic_cast<const NOX::Epetra::Vector&> |
| 551 |
(groupPtr->getF()).getEpetraVector()); |
| 552 |
engine.PutMultiVector( *tmpVec, "F" ); |
| 553 |
return true; |
| 554 |
} |
| 555 |
|
| 556 |
//----------------------------------------------------------------------------- |
| 557 |
|
| 558 |
bool |
| 559 |
Matlab_Interface::CMD_getGradient::doCommand( std::string commandLine ) |
| 560 |
{ |
| 561 |
const Epetra_Vector * tmpVec = &(dynamic_cast<const NOX::Epetra::Vector&> |
| 562 |
(groupPtr->getGradient()).getEpetraVector()); |
| 563 |
engine.PutMultiVector( *tmpVec, "Gradient" ); |
| 564 |
return true; |
| 565 |
} |
| 566 |
|
| 567 |
//----------------------------------------------------------------------------- |
| 568 |
|
| 569 |
bool |
| 570 |
Matlab_Interface::CMD_getNewton::doCommand( std::string commandLine ) |
| 571 |
{ |
| 572 |
const Epetra_Vector * tmpVec = &(dynamic_cast<const NOX::Epetra::Vector&> |
| 573 |
(groupPtr->getNewton()).getEpetraVector()); |
| 574 |
engine.PutMultiVector( *tmpVec, "Newton" ); |
| 575 |
return true; |
| 576 |
} |
| 577 |
|
| 578 |
//----------------------------------------------------------------------------- |
| 579 |
|
| 580 |
bool |
| 581 |
Matlab_Interface::CMD_getJacobian::doCommand( std::string commandLine ) |
| 582 |
{ |
| 583 |
Epetra_Operator * jacOp = (groupPtr->getLinearSystem()->getJacobianOperator().get()); |
| 584 |
Epetra_RowMatrix * rowMatrix = dynamic_cast<Epetra_RowMatrix *>(jacOp); |
| 585 |
if( rowMatrix ) |
| 586 |
{ |
| 587 |
engine.PutRowMatrix( *rowMatrix, "Jacobian", false ); |
| 588 |
return true; |
| 589 |
} |
| 590 |
NOX::Epetra::FiniteDifference * fdOp = dynamic_cast<NOX::Epetra::FiniteDifference *>(jacOp); |
| 591 |
if( fdOp ) |
| 592 |
{ |
| 593 |
engine.PutRowMatrix( fdOp->getUnderlyingMatrix(), "Jacobian", false ); |
| 594 |
return true; |
| 595 |
} |
| 596 |
|
| 597 |
std::cout << "Could not get a valid matrix." << std::endl; |
| 598 |
return false; |
| 599 |
} |
| 600 |
|
| 601 |
//----------------------------------------------------------------------------- |
| 602 |
|
| 603 |
bool |
| 604 |
Matlab_Interface::CMD_read::doCommand( std::string commandLine ) |
| 605 |
{ |
| 606 |
|
| 607 |
if( 0 == driver ) |
| 608 |
{ |
| 609 |
std::cout << "ERROR: No valid driver registered with reader." << std::endl; |
| 610 |
return false; |
| 611 |
} |
| 612 |
|
| 613 |
commandLine.replace( commandLine.find(command), command.size()+1, ""); |
| 614 |
|
| 615 |
ifstream inFile(commandLine.c_str()); |
| 616 |
if( !inFile ) |
| 617 |
{ |
| 618 |
std::cout << "ERROR: Could not open file \"" << commandLine << "\"" << std::endl; |
| 619 |
return false; |
| 620 |
} |
| 621 |
|
| 622 |
Matlab_Interface::CMD_newMacro::is = &inFile; |
| 623 |
|
| 624 |
char cLine[256]; |
| 625 |
|
| 626 |
while( !inFile.eof() ) |
| 627 |
{ |
| 628 |
inFile.getline( cLine, 256 ); |
| 629 |
std::string sLine(cLine); |
| 630 |
driver->doCommand( sLine ); |
| 631 |
} |
| 632 |
|
| 633 |
Matlab_Interface::CMD_newMacro::is = &(std::cin); |
| 634 |
|
| 635 |
return true; |
| 636 |
} |
| 637 |
|
| 638 |
//----------------------------------------------------------------------------- |
| 639 |
|
| 640 |
bool |
| 641 |
Matlab_Interface::CMD_write::doCommand( std::string commandLine ) |
| 642 |
{ |
| 643 |
|
| 644 |
if( 0 == driver ) |
| 645 |
{ |
| 646 |
std::cout << "ERROR: No valid driver registered with reader." << std::endl; |
| 647 |
return false; |
| 648 |
} |
| 649 |
|
| 650 |
commandLine.replace( commandLine.find(command), command.size()+1, ""); |
| 651 |
|
| 652 |
std::ofstream outFile(commandLine.c_str()); |
| 653 |
if( !outFile ) |
| 654 |
{ |
| 655 |
std::cout << "ERROR: Could not open file \"" << commandLine << "\"" << std::endl; |
| 656 |
return false; |
| 657 |
} |
| 658 |
|
| 659 |
// First, we write out all maps |
| 660 |
outFile << "######################" << std::endl; |
| 661 |
outFile << "####### Maps #########" << std::endl; |
| 662 |
outFile << "######################" << std::endl; |
| 663 |
outFile << "##" << std::endl; |
| 664 |
driver->getMapHandler()->writeMaps( outFile ); |
| 665 |
outFile << "##" << std::endl; |
| 666 |
outFile << "##" << std::endl; |
| 667 |
|
| 668 |
// Then we write out all macros |
| 669 |
outFile << "######################" << std::endl; |
| 670 |
outFile << "###### Macros ########" << std::endl; |
| 671 |
outFile << "######################" << std::endl; |
| 672 |
outFile << "##" << std::endl; |
| 673 |
std::map< std::string, Matlab_Interface::CMD_macro * >::const_iterator miter = driver->getUserMacros().begin() , |
| 674 |
miter_end = driver->getUserMacros().end() ; |
| 675 |
for( ; miter_end != miter; ++miter ) |
| 676 |
(*miter).second->writeMacro( outFile ); |
| 677 |
|
| 678 |
outFile << "##" << std::endl; |
| 679 |
outFile << "##" << std::endl; |
| 680 |
|
| 681 |
// Now dump the command stack |
| 682 |
outFile << "######################" << std::endl; |
| 683 |
outFile << "### Command Stack ####" << std::endl; |
| 684 |
outFile << "######################" << std::endl; |
| 685 |
outFile << "##" << std::endl; |
| 686 |
std::list< std::string >::const_iterator siter = driver->getCommandStack().begin() , |
| 687 |
siter_end = driver->getCommandStack().end() ; |
| 688 |
for( ; siter_end != siter; ++siter ) |
| 689 |
outFile << (*siter) << std::endl; |
| 690 |
|
| 691 |
return true; |
| 692 |
} |
| 693 |
|
| 694 |
//----------------------------------------------------------------------------- |
| 695 |
|
| 696 |
bool |
| 697 |
Matlab_Interface::CMD_newMacro::doCommand( std::string commandLine ) |
| 698 |
{ |
| 699 |
if( 0 == driver ) |
| 700 |
{ |
| 701 |
std::cout << "ERROR: No valid driver registered with newMacro." << std::endl; |
| 702 |
return false; |
| 703 |
} |
| 704 |
|
| 705 |
commandLine.replace( commandLine.find(command), command.size()+1, ""); |
| 706 |
|
| 707 |
std::map< std::string, Matlab_Interface::CMD_macro * > & userMacros = driver->getUserMacros(); |
| 708 |
|
| 709 |
std::map< std::string, Matlab_Interface::CMD_macro * >::iterator iter = userMacros.find(commandLine); |
| 710 |
if( userMacros.end() != iter ) |
| 711 |
{ |
| 712 |
std::cout << "Replacing macro \"" << commandLine << "\"" << std::endl; |
| 713 |
delete (*iter).second; |
| 714 |
driver->removeCommand( (*iter).second ); |
| 715 |
} |
| 716 |
else |
| 717 |
std::cout << "Creating macro \"" << commandLine << "\"" << std::endl; |
| 718 |
|
| 719 |
Matlab_Interface::CMD_macro * p_macro = new Matlab_Interface::CMD_macro( engine, solver, commandLine ); |
| 720 |
Matlab_Interface::CMD_macro & macro = *p_macro; |
| 721 |
macro.registerDriver( driver ); |
| 722 |
|
| 723 |
std::string sLine; |
| 724 |
|
| 725 |
do |
| 726 |
{ |
| 727 |
getline( *is, sLine ); |
| 728 |
macro.addLineCommand( sLine ); |
| 729 |
} |
| 730 |
while( sLine != "##" ); |
| 731 |
|
| 732 |
userMacros[commandLine] = p_macro; |
| 733 |
driver->addCommand( p_macro ); |
| 734 |
|
| 735 |
return false; |
| 736 |
} |
| 737 |
|
| 738 |
//----------------------------------------------------------------------------- |
| 739 |
|
| 740 |
bool |
| 741 |
Matlab_Interface::CMD_macro::doCommand( std::string commandLine ) |
| 742 |
{ |
| 743 |
if( 0 == driver ) |
| 744 |
{ |
| 745 |
std::cout << "ERROR: No valid driver registered with macro." << std::endl; |
| 746 |
return false; |
| 747 |
} |
| 748 |
|
| 749 |
// I may be the wrong macro. Nevertheless, I get and invoke the correct one. |
| 750 |
|
| 751 |
commandLine.replace( commandLine.find(command), command.size()+1, ""); |
| 752 |
|
| 753 |
Matlab_Interface::CMD_macro * p_macro = driver->getUserMacros()[commandLine]; |
| 754 |
|
| 755 |
if( !p_macro ) |
| 756 |
{ |
| 757 |
std::cout << "Macro \"" << commandLine << "\" not found." << std::endl; |
| 758 |
return false; |
| 759 |
} |
| 760 |
|
| 761 |
std::cout << "Doing macro \"" << commandLine << "\"" << std::endl; |
| 762 |
|
| 763 |
std::list< std::string >::iterator iter = p_macro->macroCommands.begin() , |
| 764 |
iter_end = p_macro->macroCommands.end() ; |
| 765 |
|
| 766 |
for( ; iter_end != iter; ++iter ) |
| 767 |
driver->doCommand( *iter ); |
| 768 |
|
| 769 |
return true; |
| 770 |
} |
| 771 |
|
| 772 |
//----------------------------------------------------------------------------- |
| 773 |
|
| 774 |
void |
| 775 |
Matlab_Interface::CMD_macro::writeMacro( std::ofstream & os ) |
| 776 |
{ |
| 777 |
|
| 778 |
std::string cLine = "#newMacro " + macroName; |
| 779 |
os << cLine << std::endl; |
| 780 |
|
| 781 |
std::list< std::string >::const_iterator iter = macroCommands.begin() , |
| 782 |
iter_end = macroCommands.end() ; |
| 783 |
for( ; iter_end != iter; ++iter ) |
| 784 |
{ |
| 785 |
os << (*iter) << std::endl; |
| 786 |
} |
| 787 |
|
| 788 |
return; |
| 789 |
} |
| 790 |
|
| 791 |
//----------------------------------------------------------------------------- |
| 792 |
|
| 793 |
void |
| 794 |
Matlab_Interface::CMD_macro::describe() |
| 795 |
{ |
| 796 |
|
| 797 |
std::cout << "\n\tMacro \"" << macroName << "\" :" << std::endl; |
| 798 |
|
| 799 |
std::list< std::string >::const_iterator iter = macroCommands.begin() , |
| 800 |
iter_end = macroCommands.end() ; |
| 801 |
for( ; iter_end != iter; ++iter ) |
| 802 |
std::cout << "\t\t" << (*iter) << std::endl; |
| 803 |
|
| 804 |
return; |
| 805 |
} |
| 806 |
|
| 807 |
#endif |