Skip to content

Commit

Permalink
-fix in cvode calcfunction for exception handling.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@12951 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
niklwors committed Sep 17, 2012
1 parent 59e3c47 commit a39f9bb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion SimulationRuntime/cpp/Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ SET(SUNDIALS_INLCUDE_HOME $ENV{OMDEV}/lib/3rdParty/Sundials/include)
if(MSVC)
SET(SUNDIALS_LIBRARY_RELEASE_HOME $ENV{OMDEV}/lib/3rdParty/Sundials/lib/release/vc100)
elseif(MINGW)
SET(SUNDIALS_LIBRARY_RELEASE_HOME $ENV{OMDEV}/lib/3rdParty/Sundials/lib/release/mingw)
SET(SUNDIALS_LIBRARY_RELEASE_HOME $ENV{OMDEV}/lib/3rdParty/Sundials/lib/release/mingw/dlls)
endif()

SET(INSTALL_OMDEV_LIBS ON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ double division (const double &a,const double &b,std::string text)
{
if(b != 0)
return a/b ;
else
throw std::invalid_argument(text);
else
{
std::string error_msg = "Division by zeror: ";
throw std::invalid_argument(error_msg+text);
}
}
24 changes: 9 additions & 15 deletions SimulationRuntime/cpp/Source/SimManager/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ int main(int argc, const char* argv[])
fs::path default_system_name(SYSTEM_LIB);
fs::path default_system_path = libraries_path;
default_system_path/=default_system_name;



default_system_path.make_preferred();
modelica_system_path.make_preferred();
Expand All @@ -134,10 +134,10 @@ int main(int argc, const char* argv[])
type_map types;
if(!load_single_library(types, default_system_path.string()))
throw std::invalid_argument("System default library could not be loaded");

shared_library math_lib(math_path.string());
if(!math_lib.open())
throw std::invalid_argument("Math library could not be loaded");
throw std::invalid_argument("Math library could not be loaded");

if(!load_single_library(types, modelica_system_path.string()))
throw std::invalid_argument("ModelicaSystem library could not be loaded");
Expand Down Expand Up @@ -169,29 +169,23 @@ int main(int argc, const char* argv[])
solver->setEndTime(global_settings->getEndTime());
solver->setInitStepSize(config.getSolverSettings()->gethInit());
// Call the solver
try
{


solver->solve(command);
}
catch(std::exception& ex)
{
std::string error = ex.what();
cerr << "Simulation error: "<< error ;
//return 1;
}

// Get the status of the solver (is the interation done sucessfully?)
IDAESolver::SOLVERSTATUS status = solver->getSolverStatus();
//Todo: use flags for simulation outputs
//solver->writeSimulationInfo(std::cout);
//solver->reportErrorMessage(std::cout);
return 0;
}

}
catch(std::exception& ex)
{
std::string error = ex.what();
cerr << "Simulation stopped: "<< error ;
return 1;
}

}
28 changes: 19 additions & 9 deletions SimulationRuntime/cpp/Source/Solver/CVode/Implementation/CVode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,21 +544,31 @@ void Cvode::writeCVodeOutput(const double &time,const double &h,const int &stp)



void Cvode::calcFunction(const double& time, const double* y, double* f)
int Cvode::calcFunction(const double& time, const double* y, double* f)
{
IContinous* continous_system = dynamic_cast<IContinous*>(_system);
IEvent* event_system = dynamic_cast<IEvent*>(_system);
continous_system->setTime(time);
continous_system->setVars(y,IContinous::ALL_STATES);
continous_system->update(IContinous::CONTINOUS);
continous_system->giveRHS(f,IContinous::ALL_STATES);
try
{
IContinous* continous_system = dynamic_cast<IContinous*>(_system);
IEvent* event_system = dynamic_cast<IEvent*>(_system);
continous_system->setTime(time);
continous_system->setVars(y,IContinous::ALL_STATES);
continous_system->update(IContinous::CONTINOUS);
continous_system->giveRHS(f,IContinous::ALL_STATES);
}//workaround until exception can be catch from c- libraries
catch(std::exception& ex)
{
std::string error = ex.what();
IDAESolver::SOLVERERROR;
cerr << "CVode integration error: "<< error ;
return -1;
}
}

int Cvode::CV_fCallback(double t, N_Vector y, N_Vector ydot, void *user_data)
{
((Cvode*) user_data)->calcFunction(t, NV_DATA_S(y),NV_DATA_S(ydot));
return ((Cvode*) user_data)->calcFunction(t, NV_DATA_S(y),NV_DATA_S(ydot));


return(0);
}

void Cvode::giveZeroVal(const double &t,const double *y,double *zeroValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class Cvode
void CVodeCore();

/// Kapselung der Berechnung der rechten Seite
void calcFunction(const double& time, const double* y, double* yd);
int calcFunction(const double& time, const double* y, double* yd);

// Callback für die rechte Seite
static int CV_fCallback(double t, N_Vector y, N_Vector ydot, void *user_data);
Expand Down

0 comments on commit a39f9bb

Please sign in to comment.