Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 83d7b4a

Browse files
qichenghuaOpenModelica-Hudson
authored andcommitted
deleted unnecessary comments and dead code in Nox.
1 parent b3f0ed0 commit 83d7b4a

File tree

5 files changed

+77
-320
lines changed

5 files changed

+77
-320
lines changed

SimulationRuntime/cpp/Include/Solver/Nox/Nox.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class Nox : public IAlgLoopSolver
3232
void createSolverParameters();
3333
void LocaHomotopySolve(int numberofhomotopytries);
3434
NOX::StatusTest::StatusType BasicNLSsolve();
35-
NOX::StatusTest::StatusType secondBasicNLSsolve();
3635

3736
//void check4EventRetry(double* y)
3837

@@ -74,9 +73,10 @@ class Nox : public IAlgLoopSolver
7473
//solver
7574
Teuchos::RCP<NOX::Solver::Generic> _solver;
7675

76+
Teuchos::RCP<std::ostream> _output;
77+
7778
bool _firstCall;
7879
bool _generateoutput;
7980
bool _useDomainScaling;
80-
8181
};
8282
/** @} */ // end of solverNox

SimulationRuntime/cpp/Include/Solver/Nox/NoxLapackInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class NoxLapackInterface : public LOCA::LAPACK::Interface {
3434
//! Initial guess
3535
Teuchos::RCP<NOX::LAPACK::Vector> _initialGuess;
3636
INonLinearAlgLoop *_algLoop;///< Algebraic loop to be solved, required to obtain value of f
37-
double *_yScale, *_fScale;
37+
double *_yScale, *_fScale, *_hugeabsolutevalues, *_xtemp, *_rhs;
3838
int _dimSys;
3939
bool _generateoutput;
4040
bool _useDomainScaling;

SimulationRuntime/cpp/SimCoreFactory/OMCFactory/OMCFactory.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ OMCFactory::OMCFactory(PATH library_path, PATH modelicasystem_path)
100100
#else
101101
, _defaultLinSolver("linearSolver")
102102
#endif
103-
, _defaultNonLinSolver("kinsol")
103+
, _defaultNonLinSolver("nox")
104104
{
105105
fillArgumentsToIgnore();
106106
fillArgumentsToReplace();
@@ -114,7 +114,7 @@ OMCFactory::OMCFactory()
114114
#else
115115
, _defaultLinSolver("linearSolver")
116116
#endif
117-
, _defaultNonLinSolver("kinsol")
117+
, _defaultNonLinSolver("nox")
118118
{
119119
fillArgumentsToIgnore();
120120
fillArgumentsToReplace();

SimulationRuntime/cpp/Solver/Nox/Nox.cpp

Lines changed: 30 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ void Nox::initialize()
4949
{
5050
if (_generateoutput) std::cout << "starting init" << std::endl;
5151
_firstCall = false;
52-
_algLoop->initialize();
52+
_algLoop->initialize();//this sets values in the real variable
53+
54+
5355
if(_y) delete [] _y;
5456
if(_y0) delete [] _y0;
5557
if(_y_old) delete [] _y_old;
@@ -82,17 +84,15 @@ void Nox::initialize()
8284

8385

8486
void Nox::solve()
85-
{
87+
{//if(_algLoop->getSimTime()>0.088421) _generateoutput=true;
8688
if (_generateoutput) std::cout << "starting solving algloop " << _algLoop->getEquationIndex() << std::endl;
87-
8889
int iter=-1; //Iterationcount
8990
NOX::StatusTest::StatusType status;
9091
NOX::LAPACK::Vector Lapacksolution;//temporary variable used to convert the solution from NOX::LAPACK::Vector to a double array.
9192
double * rhs = new double[_dimSys];
9293
double sum;
9394

94-
Teuchos::RCP<std::ostream> output = Teuchos::rcp(new std::stringstream);
95-
95+
_output = Teuchos::rcp(new std::stringstream);
9696
if (_firstCall) initialize();
9797

9898
// Set up the status tests
@@ -105,59 +105,44 @@ void Nox::solve()
105105
_statusTestsCombo->addStatusTest(_statusTestStagnation);
106106
_statusTestsCombo->addStatusTest(_statusTestDivergence);
107107

108-
// Create the list of solver parameters
108+
// Create the list of solver parameters. For detailed calibration, check https://trilinos.org/docs/dev/packages/nox/doc/html/parameters.html
109109
_solverParametersPtr = Teuchos::rcp(new Teuchos::ParameterList);
110-
//resetting method to default (Line Search). For detailed calibration, check https://trilinos.org/docs/dev/packages/nox/doc/html/parameters.html
110+
111111
_solverParametersPtr->set("Nonlinear Solver", "Line Search Based");
112112

113-
_solverParametersPtr->sublist("Printing").set("Output Precision", 15);
114-
_solverParametersPtr->sublist("Printing").set("Output Stream", output);
115-
_solverParametersPtr->sublist("Printing").set("Error Stream", output);
113+
// Set the level of output
114+
if(!_generateoutput){
115+
_solverParametersPtr->sublist("Printing").set("Output Precision", 15);
116+
_solverParametersPtr->sublist("Printing").set("Output Stream", _output);
117+
_solverParametersPtr->sublist("Printing").set("Error Stream", _output);
118+
}
116119

117120
//_solverParametersPtr->sublist("Direction").set("Method", "Steepest Descent");
118121

119122
//resetting Line search method to default (Full Step, ie. Standard Newton with lambda=1)
120123
_solverParametersPtr->sublist("Line Search").set("Method", "Full Step");
121-
//_solverParametersPtr->sublist("Line Search").sublist("Full Step").set("Full Step", 0.5);
122-
//_solverParametersPtr->sublist("Line Search").set("Method", "Polynomial");
123-
// Set the level of output
124+
124125
if (_generateoutput){
125126
_solverParametersPtr->sublist("Printing").set("Output Information", NOX::Utils::Error + NOX::Utils::Warning + NOX::Utils::OuterIteration + NOX::Utils::Details + NOX::Utils::Debug); //(there are also more options, but error and outer iteration are the ones that I commonly use.
126127
}else{
127128
_solverParametersPtr->sublist("Printing").set("Output Information", NOX::Utils::Error);
128129
}
129-
// _solverParametersPtr->sublist("Printing").set("Output Information", NOX::Utils::Error + NOX::Utils::Warning + NOX::Utils::OuterIteration + NOX::Utils::InnerIteration + NOX::Utils::Details + NOX::Utils::Debug); //(there are also more options, but error and outer iteration are the ones that I commonly use.
130-
131130

132131
if (_generateoutput) std::cout << "creating noxLapackInterface" << std::endl;
133132

134133
_noxLapackInterface = Teuchos::rcp(new NoxLapackInterface (_algLoop,-1));//this also gets the nominal values
135134

136-
137-
138135
_iterationStatus=CONTINUE;
139136

140137
if (_generateoutput) std::cout << "starting while loop" << std::endl;
141138

142139
while(_iterationStatus==CONTINUE){
143140
iter++;
144141

145-
if ((false && (iter != 1))){
146-
std::cout << "Entering next iteration of while loop" << std::endl;
147-
_solverParametersPtr->print();
148-
std::cout << std::endl;
149-
}
150-
151142
// Reset initial guess
152143
memcpy(_y,_y0,_dimSys*sizeof(double));
153144
_algLoop->setReal(_y);
154145

155-
if ((false && (iter != 1))){
156-
std::cout << "building grp" << std::endl;
157-
_solverParametersPtr->print();
158-
std::cout << std::endl;
159-
}
160-
161146
try{
162147
_grp = Teuchos::rcp(new NOX::LAPACK::Group(*_noxLapackInterface));//this also calls the getInitialGuess-function in the NoxLapackInterface and sets the initial guess in the NOX::LAPACK::Group
163148
}
@@ -167,20 +152,6 @@ void Nox::solve()
167152
std::string errorstring(ex.what());
168153
//we only do the error handling by variation of initial guess in case of division by zero at evaluation of initial guess
169154
if(errorstring.find(divbyzero)!=std::string::npos){
170-
//some magic
171-
172-
//alle Richtungen durchzuprobieren ist doof, denn das kann ich nur durch 2^_dimSys Aufrufe von der Lösungsroutine beschränken. Allerdings besteht Grund zur Hoffnung, dass es relativ schnell gelöst wird.
173-
//Dann sollte ich allerdings nicht mehr allzu viele Methoden durchprobieren, ie. einfach Linesearch und das wars.
174-
//Oder ich versuche herauszufinden, welche Koordinaten die Division durch Null hervorrufen. Heuristisch sind das die Koordinaten, die sowieso schon Null sind.
175-
//anstatt vectors von arrays kann ich auch einfach mehrere vectors erstellen. Oder halt gleich eine Matrix mit Zweifachzeigern.
176-
177-
//iterate through all possibilities.
178-
//bool triednew_yindirection p = true;
179-
//algLoop->setReal(new_y);
180-
//try{_algLoop->evaluate}
181-
//_grp = ...
182-
//simplelinesearchsolve(_grp);
183-
184155
NOX::StatusTest::StatusType stat = NOX::StatusTest::Unevaluated;
185156
int initialguessdividebyzerofailurecounter = 0;
186157

@@ -246,35 +217,9 @@ void Nox::solve()
246217
}
247218
}
248219

249-
if ((false && (iter != 1))){
250-
std::cout << "grp built" << std::endl;
251-
_solverParametersPtr->print();
252-
std::cout << std::endl;
253-
}
254-
255220
if (_generateoutput) std::cout << "building solver" << std::endl;
256221

257-
258-
259-
try{
260-
//std::cout << "building solver..." << std::endl;
261-
// Create the solver
262222
_solver = NOX::Solver::buildSolver(_grp, _statusTestsCombo, _solverParametersPtr);
263-
//std::cout << "done!" << std::endl;
264-
}
265-
catch(const std::exception &ex)
266-
{
267-
std::cout << std::endl << "sth went wrong during solver building, with error message" << ex.what() << std::endl;
268-
std::cout << "the group is given by" << std::endl;
269-
_grp->print();
270-
std::cout << "the status test is given by" << std::endl;
271-
_statusTestsCombo->print(std::cout);
272-
std::cout << "the solver parameters are given by" << std::endl;
273-
_solverParametersPtr->print();
274-
//std::cout << add_error_info("building solver with grp, status tests and solverparameters", ex.what(), ex.getErrorID(), time) << std::endl << std::endl;
275-
throw ModelicaSimulationError(ALGLOOP_SOLVER,"solver building error");
276-
}
277-
278223

279224
if (_generateoutput) {
280225
double* rhsssss = new double[_dimSys];//stores f(x)
@@ -298,28 +243,18 @@ void Nox::solve()
298243
std::cout << "solving..." << std::endl;
299244
}
300245

301-
if (false && (iter != 1)){
302-
std::cout << "we are solving with the following options:" <<std::endl;
303-
_solverParametersPtr->print();
304-
std::cout << std::endl;
305-
}
306-
307-
308246
try{
309-
//std::cout << "solving..." << std::endl;
310247
status = _solver->solve();
311-
if(!((dynamic_cast<const std::stringstream &>(*output)).str().empty())){
248+
if(!((dynamic_cast<const std::stringstream &>(*_output)).str().empty())){
312249
LOGGER_WRITE_BEGIN("NOX: ",LC_NLS,LL_DEBUG);
313-
LOGGER_WRITE((dynamic_cast<const std::stringstream &>(*output)).str(),LC_NLS,LL_DEBUG);
250+
LOGGER_WRITE((dynamic_cast<const std::stringstream &>(*_output)).str(),LC_NLS,LL_DEBUG);
314251
LOGGER_WRITE_END(LC_NLS,LL_DEBUG);
315-
//(dynamic_cast<const std::stringstream &>(*output)).str().clear();//this does nothing. Also, using std::cout somehow miraculously disables the logging.
252+
//(dynamic_cast<const std::stringstream &>(*_output)).str().clear();//this does nothing. Also, using std::cout somehow miraculously disables the logging.
316253
}
317-
//std::cout << "done!" << std::endl;
318254
}
319255
catch(const std::exception &ex)
320256
{
321257
std::cout << std::endl << "sth went wrong during solving, with error message" << ex.what() << std::endl;
322-
//std::cout << add_error_info("building solver with grp, status tests and solverparameters", ex.what(), ex.getErrorID(), time) << std::endl << std::endl;
323258
throw ModelicaSimulationError(ALGLOOP_SOLVER,"solving error");
324259
}
325260

@@ -509,31 +444,13 @@ void Nox::solve()
509444

510445
std::cout << "simtime=" << std::setprecision (std::numeric_limits<double>::digits10 + 1) << _algLoop->getSimTime() << std::endl;
511446
std::cout << "Some error occured when solving algloop " << _algLoop->getEquationIndex() << ". Trying to solve with different method. iter=" << iter << std::endl;
512-
//_solverParametersPtr->print();
513-
//_solverParametersPtr->sublist("Printing").set("Output Information", NOX::Utils::Error + NOX::Utils::Warning + NOX::Utils::OuterIteration + NOX::Utils::InnerIteration);
514-
//_solverParametersPtr->print();
515447
}
516448

517449
if(_generateoutput){
518450
std::cout << "Solverparameters and StatusTest at iter " << iter << ", with simtime " << _algLoop->getSimTime() << std::endl;
519-
_solverParametersPtr->print();
520-
_statusTestsCombo->print(std::cout);
521451
}
522452
}
523-
if (false && (iter==1)&&(_iterationStatus==CONTINUE)){
524-
std::cout << "deleting _noxLapackInterface..." << std::endl;
525-
_solverParametersPtr->print();
526-
std::cout << std::endl;
527-
}
528-
529-
530-
if (false && (iter==1)&&(_iterationStatus==CONTINUE)){
531-
std::cout << "finishing an iteration of the while loop" << std::endl;
532-
_solverParametersPtr->print();
533-
std::cout << std::endl;
534-
}
535453
}
536-
//if (_noxLapackInterface) delete _noxLapackInterface;
537454
if (_generateoutput) std::cout << "ending solve" << std::endl;
538455
if (rhs) delete rhs;
539456
}
@@ -584,8 +501,6 @@ void Nox::LocaHomotopySolve(int numberofhomotopytries)
584501
//Since we are doing an equilibrium continuation, we set the bifurcation method to "None".
585502
//We use a secant predictor and adaptive step size control with an initial step size of 0.1, maximum of 1.0 and minimum of 0.001.
586503

587-
Teuchos::RCP<std::ostream> output = Teuchos::rcp(new std::stringstream);
588-
589504
// Create parameter list. For detailed calibration, check https://trilinos.org/docs/dev/packages/nox/doc/html/parameters.html
590505
Teuchos::RCP<Teuchos::ParameterList> paramList = Teuchos::rcp(new Teuchos::ParameterList);
591506
// Create LOCA sublist
@@ -627,8 +542,8 @@ void Nox::LocaHomotopySolve(int numberofhomotopytries)
627542

628543
Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing");
629544
nlPrintParams.set("Output Precision", 15);
630-
nlPrintParams.set("Output Stream", output);
631-
nlPrintParams.set("Error Stream", output);
545+
nlPrintParams.set("Output Stream", _output);
546+
nlPrintParams.set("Error Stream", _output);
632547
//Set the level of output
633548
if (_generateoutput){
634549
nlPrintParams.set("Output Information", NOX::Utils::Details + NOX::Utils::OuterIteration + NOX::Utils::Warning + NOX::Utils::StepperIteration + NOX::Utils::StepperDetails + NOX::Utils::StepperParameters); // Should set
@@ -667,9 +582,9 @@ void Nox::LocaHomotopySolve(int numberofhomotopytries)
667582
try{
668583
// Perform continuation run
669584
LOCA::Abstract::Iterator::IteratorStatus status = stepper.run();
670-
if(!((dynamic_cast<const std::stringstream &>(*output)).str().empty())){
585+
if(!((dynamic_cast<const std::stringstream &>(*_output)).str().empty())){
671586
LOGGER_WRITE_BEGIN("LOCA: ",LC_NLS,LL_DEBUG);
672-
LOGGER_WRITE((dynamic_cast<const std::stringstream &>(*output)).str(),LC_NLS,LL_DEBUG);
587+
LOGGER_WRITE((dynamic_cast<const std::stringstream &>(*_output)).str(),LC_NLS,LL_DEBUG);
673588
LOGGER_WRITE_END(LC_NLS,LL_DEBUG);
674589
}
675590
// Check for convergence
@@ -758,7 +673,9 @@ NOX::StatusTest::StatusType Nox::BasicNLSsolve(){
758673
statusTestsCombo->addStatusTest(_statusTestStagnation);
759674
statusTestsCombo->addStatusTest(_statusTestDivergence);
760675
Teuchos::RCP<Teuchos::ParameterList> solverParametersPtr = Teuchos::rcp(new Teuchos::ParameterList);
761-
solverParametersPtr->sublist("Printing").set("Output Information", NOX::Utils::Error + NOX::Utils::OuterIteration);
676+
solverParametersPtr->sublist("Printing").set("Output Information", NOX::Utils::Error);// + NOX::Utils::OuterIteration);
677+
solverParametersPtr->sublist("Printing").set("Output Stream", _output);
678+
solverParametersPtr->sublist("Printing").set("Error Stream", _output);
762679
solverParametersPtr->sublist("Line Search").set("Method","Backtrack");
763680
solverParametersPtr->sublist("Line Search").sublist("Backtrack").set("Recovery Step", 0.0);
764681
Teuchos::RCP<NOX::Solver::Generic> solver = NOX::Solver::buildSolver(grp, statusTestsCombo, solverParametersPtr);
@@ -780,6 +697,11 @@ NOX::StatusTest::StatusType Nox::BasicNLSsolve(){
780697
}else{
781698
_iterationStatus=SOLVERERROR;
782699
}*/
700+
if(!((dynamic_cast<const std::stringstream &>(*_output)).str().empty())){
701+
LOGGER_WRITE_BEGIN("NOX: ",LC_NLS,LL_DEBUG);
702+
LOGGER_WRITE((dynamic_cast<const std::stringstream &>(*_output)).str(),LC_NLS,LL_DEBUG);
703+
LOGGER_WRITE_END(LC_NLS,LL_DEBUG);
704+
}
783705
_iterationStatus=DONE;
784706
NOX::LAPACK::Group solnGrp = dynamic_cast<const NOX::LAPACK::Group&>(solver->getSolutionGroup());
785707
const NOX::LAPACK::Vector& finalSolution = dynamic_cast<const NOX::LAPACK::Vector&>(solnGrp.getX());
@@ -803,8 +725,7 @@ NOX::StatusTest::StatusType Nox::BasicNLSsolve(){
803725
}
804726
catch(const std::exception &ex)
805727
{
806-
std::cout << "sth went wrong when solving AlgLoop " << _algLoop->getEquationIndex() << " at simtime " << _algLoop->getSimTime() << ", with error message:" << std::endl << ex.what() << std::endl;
807-
throw ModelicaSimulationError(ALGLOOP_SOLVER,"solving error");
728+
throw ModelicaSimulationError(ALGLOOP_SOLVER,"solving error in Algloop " + std::to_string(_algLoop->getEquationIndex()) + " at simtime " + std::to_string(_algLoop->getSimTime()) + ", with error message: " + ex.what());
808729
}
809730
return status;
810731
}

0 commit comments

Comments
 (0)