Skip to content

Commit 0000343

Browse files
committed
added new exception class for all simulation errors in cpp runtime
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@24400 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent f800645 commit 0000343

37 files changed

+352
-294
lines changed

Compiler/Template/CodegenCpp.tpl

Lines changed: 73 additions & 48 deletions
Large diffs are not rendered by default.

SimulationRuntime/cpp/Core/DataExchange/SimData.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ISimVar* SimData::Get(string key)
2626
return obj.get();
2727
}
2828
else
29-
throw std::invalid_argument("There is no such sim variable " + key);
29+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(DATASTORAGE) << error_message("There is no such sim variable " + key));
3030
}
3131

3232
void SimData::addOutputResults(string name,uBlas::vector<double> v)
@@ -72,7 +72,7 @@ void SimData::getOutputResults(string name,uBlas::vector<double>& v)
7272
v = boost::ref(iter->second);
7373
}
7474
else
75-
throw std::invalid_argument("There is no such output variable " + name);
75+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(DATASTORAGE) << error_message("There is no such output variable " + name));
7676
}
7777

7878
extern "C" ISimData* createSimDataAnalyzation()

SimulationRuntime/cpp/Core/Math/ArrayOperations.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,26 @@ void cat_array (int k,BaseArray<T>& a, vector<BaseArray<T>* >& x )
2525
unsigned int n = x.size();
2626
/* check dim sizes of all inputs */
2727
if(n<1)
28-
throw std::invalid_argument("No input arrays");
28+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_ARRAY_FUNCTION) << error_message("No input arrays"));
2929

3030
if(x[0]->getDims().size() < k)
31-
throw std::invalid_argument("Wrong dimension for input array");
31+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_ARRAY_FUNCTION) << error_message("Wrong dimension for input array"));
3232

3333
new_k_dim_size = x[0]->getDims()[k-1];
3434
for(int i = 1; i < n; i++)
3535
{
3636
if(x[0]->getDims().size() != x[i]->getDims().size())
37-
throw std::invalid_argument("Wrong dimension for input array");
37+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_ARRAY_FUNCTION) << error_message("Wrong dimension for input array"));
3838
for(int j = 0; j < (k - 1); j++)
3939
{
4040
if (x[0]->getDims()[j] != x[i]->getDims()[j])
41-
throw std::invalid_argument("Wrong size for input array");
41+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_ARRAY_FUNCTION) << error_message("Wrong size for input array"));
4242
}
4343
new_k_dim_size += x[i]->getDims()[k-1];
4444
for(int j = k; j < x[0]->getDims().size(); j++)
4545
{
4646
if (x[0]->getDims()[j] != x[i]->getDims()[j])
47-
throw std::invalid_argument("Wrong size for input array");
47+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_ARRAY_FUNCTION) << error_message("Wrong size for input array"));
4848
}
4949
}
5050
/* calculate size of sub and super structure in 1-dim data representation */
@@ -62,7 +62,7 @@ void cat_array (int k,BaseArray<T>& a, vector<BaseArray<T>* >& x )
6262
vector<size_t> ex = x[0]->getDims();
6363
ex[k-1] = new_k_dim_size;
6464
if(ex.size()<k)
65-
throw std::invalid_argument("Error resizing concatenate array");
65+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_ARRAY_FUNCTION) << error_message("Error resizing concatenate array"));
6666
a.setDims( ex );
6767

6868
/* concatenation along k-th dimension */
@@ -106,7 +106,7 @@ void create_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d
106106

107107
//Check if the dimension of passed indices match the dimension of target array
108108
if(sp.second.size()!=s.getNumDims())
109-
throw std::invalid_argument("Erro in create array from shape, number of dimensions does not match");
109+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_ARRAY_FUNCTION) << error_message("Erro in create array from shape, number of dimensions does not match"));
110110

111111
T* data = new T[d.getNumElems()];
112112

@@ -135,7 +135,7 @@ void create_array_from_shape(const spec_type& sp,BaseArray<T>& s,BaseArray<T>& d
135135
}
136136
if(index>(d.getNumElems()-1))
137137
{
138-
throw std::invalid_argument("Erro in create array from shape, number of dimensions does not match");
138+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_ARRAY_FUNCTION) << error_message("Erro in create array from shape, number of dimensions does not match"));
139139
}
140140
data[index] = s(idx);
141141
idx.clear();
@@ -165,7 +165,7 @@ void transpose_array (BaseArray< T >& a, BaseArray< T >& x )
165165

166166
{
167167
if(a.getNumDims()!=2 || x.getNumDims()!=2)
168-
throw std::invalid_argument("Erro in transpose_array, number of dimensions does not match");
168+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_ARRAY_FUNCTION) << error_message("Erro in transpose_array, number of dimensions does not match"));
169169

170170
vector<size_t> ex = x.getDims();
171171
std::swap( ex[0], ex[1] );
@@ -257,7 +257,7 @@ template <typename T>
257257
T dot_array(BaseArray<T> & a, BaseArray<T> & b)
258258
{
259259
if(a.getNumDims()!=1 || b.getNumDims()!=1)
260-
throw std::invalid_argument("error in dot array function. Wrong dimension");
260+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_ARRAY_FUNCTION) << error_message("error in dot array function. Wrong dimension"));
261261

262262
T* data1 = a.getData();
263263
unsigned int nelems = a.getNumElems();

SimulationRuntime/cpp/Core/Math/Functions.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ double division (const double &a,const double &b, const char* text)
3030
else
3131
{
3232
std::string error_msg = "Division by zeror: ";
33-
throw std::invalid_argument(error_msg+string(text));
33+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(UTILITY) << error_message(error_msg+string(text)));
3434
}
3535
}
3636

@@ -117,7 +117,7 @@ int pivot(double *A, int n_rows, int n_cols, int *rowInd, int *colInd)
117117
pivot = get_pivot_matrix_elt(A,row,row);
118118
/* internal error, pivot element should never be zero if maxsearch succeeded */
119119
if(pivot == 0)
120-
throw std::invalid_argument("pivot element is zero ");
120+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(UTILITY) << error_message("pivot element is zero "));
121121

122122
/* perform one step of Gaussian Elimination */
123123
for(i=row+1;i<n_rows;i++)

SimulationRuntime/cpp/Core/Math/SparseMatrix.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ void sparse_matrix::build(sparse_inserter& ins) {
66
n=ins.content.rbegin()->first.first+1;
77
} else {
88
if(n-1!=ins.content.rbegin()->first.first) {
9-
throw std::runtime_error("size doesn't match");
9+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MATH_FUNCTION) << error_message("size doesn't match"));
1010
}
1111
}
1212
size_t n=ins.content.size();
@@ -42,11 +42,11 @@ int sparse_matrix::solve(const double* b, double * x) {
4242
}
4343
#else
4444
void sparse_matrix::build(sparse_inserter& ins) {
45-
throw std::runtime_error("no umfpack");
45+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MATH_FUNCTION) << error_message("no umfpack"));
4646
}
4747

4848
int sparse_matrix::solve(const double* b, double * x) {
49-
throw std::runtime_error("no umfpack");
49+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MATH_FUNCTION) << error_message("no umfpack"));
5050
}
5151

5252
#endif

SimulationRuntime/cpp/Core/Modelica/ModelicaSystem.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,22 +172,22 @@ bool Modelica::checkForDiscreteEvents()
172172

173173
bool Modelica::stepCompleted(double time)
174174
{
175-
throw std::runtime_error("stepCompleted is not yet implemented");
175+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_EQ_SYSTEM) << error_message("stepCompleted is not yet implemented"));
176176
}
177177

178178
bool Modelica::checkConditions()
179179
{
180-
throw std::runtime_error("checkConditions is not yet implemented");
180+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_EQ_SYSTEM) << error_message("checkConditions is not yet implemented"));
181181
}
182182

183183
void Modelica::getJacobian(SparseMatrix& matrix)
184184
{
185-
throw std::runtime_error("giveJacobian is not yet implemented");
185+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_EQ_SYSTEM) << error_message("giveJacobian is not yet implemented"));
186186
}
187187

188188
void Modelica::getStateSetJacobian(SparseMatrix& matrix)
189189
{
190-
throw std::runtime_error("giveStateJacobian is not yet implemented");
190+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_EQ_SYSTEM) << error_message("giveStateJacobian is not yet implemented"));
191191
}
192192

193193
bool Modelica::isODE()
@@ -207,7 +207,7 @@ int Modelica::getDimZeroFunc()
207207

208208
bool Modelica::provideSymbolicJacobian()
209209
{
210-
throw std::runtime_error("provideSymbolicJacobian is not yet implemented");
210+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MODEL_EQ_SYSTEM) << error_message("provideSymbolicJacobian is not yet implemented"));
211211
}
212212

213213
void Modelica::saveAll()

SimulationRuntime/cpp/Core/SimController/SimController.cpp

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ std::pair<boost::shared_ptr<IMixedSystem>, boost::shared_ptr<ISimData> > SimCont
6969
return system;
7070
}
7171
else
72-
throw std::invalid_argument("No Modelica Compiler configured");
72+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(SIMMANAGER) << error_message("No Modelica Compiler configured"));
7373
}
7474

7575
boost::shared_ptr<ISimData> SimController::getSimData(string modelname)
@@ -108,13 +108,11 @@ void SimController::StartVxWorks(boost::shared_ptr<IMixedSystem> mixedsystem, Si
108108

109109
_simMgr->initialize();
110110
}
111-
catch(std::exception& ex)
111+
catch(boost::exception & ex)
112112
{
113-
std::string error =string("Simulation failed for ") + simsettings.outputfile_name + string(" with error ")+ ex.what();
114-
115-
printf("Fehler %s\n", ex.what());
116-
117-
throw std::runtime_error(error);
113+
ex << error_id(SIMMANAGER) << error_message(string("Simulation failed for ") + simsettings.outputfile_name);
114+
printf("Fehler %s\n",diagnostic_information(ex));
115+
throw;
118116
}
119117
}
120118

@@ -156,35 +154,37 @@ void SimController::Start(boost::shared_ptr<IMixedSystem> mixedsystem, SimSettin
156154
_simMgr->initialize();
157155

158156
_simMgr->runSimulation();
159-
160-
/* if(boost::shared_ptr<IMixedSystem> history_system = mixedsystem.lock())
161-
{
162-
//get history object to query simulation results
163-
IHistory* history = history_system->getHistory();
164-
//simulation results (output variables)
165-
ublas::matrix<double> Ro;
166-
//query simulation result otuputs
167-
history->getOutputResults(Ro);
168-
vector<string> output_names;
169-
history->getOutputNames(output_names);
170-
string name;
171-
int j=0;
172-
BOOST_FOREACH(name,output_names)
173-
{
174-
ublas::vector<double> o_j;
175-
o_j =ublas::row(Ro,j);
176-
simData->addOutputResults(name,o_j);
177-
j++;
178-
}
179-
vector<double> time_values = history->getTimeEntries();
180-
simData->addTimeEntries(time_values);
181-
}*/
157+
158+
boost::shared_ptr<IWriteOutput> writeoutput_system = boost::dynamic_pointer_cast<IWriteOutput>(mixedsystem);
159+
160+
if((global_settings->getOutputFormat()==BUFFER) && writeoutput_system)
161+
{
162+
//get history object to query simulation results
163+
IHistory* history = writeoutput_system->getHistory();
164+
//simulation results (output variables)
165+
ublas::matrix<double> Ro;
166+
//query simulation result otuputs
167+
history->getOutputResults(Ro);
168+
vector<string> output_names;
169+
history->getOutputNames(output_names);
170+
string name;
171+
int j=0;
172+
BOOST_FOREACH(name,output_names)
173+
{
174+
ublas::vector<double> o_j;
175+
o_j =ublas::row(Ro,j);
176+
_systems[modelKey].second->addOutputResults(name,o_j);
177+
j++;
178+
}
179+
vector<double> time_values = history->getTimeEntries();
180+
_systems[modelKey].second->addTimeEntries(time_values);
181+
}
182182

183183
}
184-
catch(std::exception& ex)
184+
catch(boost::exception & ex)
185185
{
186-
std::string error = string("Simulation failed for ") + simsettings.outputfile_name + string(" with error ")+ ex.what();
187-
throw std::runtime_error(error);
186+
ex << error_id(SIMMANAGER) << error_message(string("Simulation failed for ") + simsettings.outputfile_name);
187+
throw;
188188
}
189189
}
190190

SimulationRuntime/cpp/Core/SimController/SimManager.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ void SimManager::initialize()
124124
// System zusammenbauen und einmal updaten
125125
_initialization->initializeSystem();
126126
}
127-
catch (std::exception& ex)
127+
catch (boost::exception& ex)
128128
{
129-
std::string msg = ex.what();
130-
throw std::invalid_argument(msg);
129+
ex << error_id(SIMMANAGER);
130+
throw;
131131
}
132132

133133
_totStps = 0;
@@ -203,7 +203,7 @@ void SimManager::runSingleStep(double cycletime)
203203
{
204204

205205
if (_lastCycleTime && cycletime != _lastCycleTime)
206-
throw std::runtime_error("Cycle time can not be changed, if time events (samples) are present!");
206+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(SIMMANAGER) << error_message("Cycle time can not be changed, if time events (samples) are present!"));
207207
else
208208
_lastCycleTime = cycletime;
209209

@@ -245,7 +245,7 @@ void SimManager::computeSampleCycles()
245245
{
246246
if (iter->first != 0.0 || iter->second == 0.0)
247247
{
248-
throw std::runtime_error("Time event not starting at t=0.0 or not cyclic!");
248+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(SIMMANAGER) << error_message("Time event not starting at t=0.0 or not cyclic!"));
249249
}
250250
else
251251
{
@@ -256,7 +256,7 @@ void SimManager::computeSampleCycles()
256256
}
257257
else
258258
{
259-
throw std::runtime_error("Sample time is not a multiple of the cycle time!");
259+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(SIMMANAGER) << error_message("Sample time is not a multiple of the cycle time!"));
260260
}
261261

262262
}
@@ -291,18 +291,19 @@ void SimManager::runSimulation()
291291
writeProperties();
292292
}
293293
}
294-
catch (std::exception& ex)
294+
catch (boost::exception & ex)
295295
{
296296
/* Logs temporarily disabled
297297
BOOST_LOG_SEV(simmgr_lg::get(), simmgr_normal) << "Simulation finish with errors at t= " << _tEnd;
298298
BOOST_LOG_SEV(simmgr_lg::get(), simmgr_normal) << "Number of steps: " << _totStps.at(0);
299299
*/
300300
writeProperties();
301-
std::string simmgr_error_simmgr_info = ex.what();
301+
302302
/* Logs temporarily disabled
303303
BOOST_LOG_SEV(simmgr_lg::get(), simmgr_critical) << "SimManger simmgr_error: " + simmgr_error_simmgr_info;
304304
*/
305-
throw;
305+
ex << error_id(SIMMANAGER);
306+
throw;
306307
}
307308
#ifdef RUNTIME_PROFILING
308309
if (MeasureTime::getInstance() != NULL)

SimulationRuntime/cpp/Core/Solver/SystemStateSelection.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ SystemStateSelection::SystemStateSelection(IMixedSystem* system)
1414

1515
_state_selection = dynamic_cast<IStateSelection*>(system);
1616
if ( !_state_selection)
17-
throw std::invalid_argument("No state selection system");
17+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MATH_FUNCTION) << error_message("No state selection system"));
1818

1919
}
2020

@@ -93,7 +93,7 @@ return true;
9393

9494
if((pivot(jac, _dimDummyStates[i], _dimStateCanditates[i], _rowPivot[i].get(), _colPivot[i].get()) != 0))
9595
{
96-
throw std::invalid_argument("Error, singular Jacobian for dynamic state selection at time");
96+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MATH_FUNCTION) << error_message("Error, singular Jacobian for dynamic state selection at time"));
9797
}
9898

9999
/* if we have a new set throw event for reinitialization
@@ -167,7 +167,7 @@ void SystemStateSelection::setAMatrix(int* newEnable, unsigned int index)
167167
_state_selection->setAMatrix(index,A1);
168168
}
169169
else
170-
throw std::invalid_argument("No A matrix availibale for state selection");
170+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(MATH_FUNCTION) << error_message("No A matrix availibale for state selection"));
171171
_state_selection->setStates(index,states);
172172
delete [] states ;
173173
delete [] states2 ;

SimulationRuntime/cpp/Core/System/AlgLoopDefaultImplementation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ void AlgLoopDefaultImplementation::initialize()
7979
}
8080
}
8181
else
82-
throw std::invalid_argument("AlgLoopDefaultImplementation::initialize(): Unknown _constraintType.");
82+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(ALGLOOP_EQ_SYSTEM) << error_message("AlgLoopDefaultImplementation::initialize(): Unknown _constraintType."));
8383

8484
//nach default algloop verschieben
8585
// Prüfen ob min. eine Bindungsgleichung vorhanden
8686
if ( _dimAEq == 0 )
87-
throw std::invalid_argument("AlgLoop::initialize(): No constraint defined.");
87+
BOOST_THROW_EXCEPTION(ModelicaSimulationError() << error_id(ALGLOOP_EQ_SYSTEM) << error_message("AlgLoop::initialize(): No constraint defined."));
8888

8989
};
9090

0 commit comments

Comments
 (0)