Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 0.0% 0 / 0 / 122
Functions: 0.0% 0 / 1 / 15
Branches: 0.0% 0 / 0 / 190

OMCompiler/SimulationRuntime/cpp/Core/DataExchange/Policies/BufferReaderWriter.h
Line Branch Exec Source
1 /*
2 * This file belongs to the OpenModelica Run-Time System
3 *
4 * Copyright (c) 1998-2026, Open Source Modelica Consortium (OSMC), c/o Linköpings
5 * universitet, Department of Computer and Information Science, SE-58183 Linköping, Sweden. All rights
6 * reserved.
7 *
8 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
9 * AGPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.8. ANY
10 * USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
11 * ACCEPTANCE OF THE BSD NEW LICENSE OR THE OSMC PUBLIC LICENSE OR THE AGPL
12 * VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
13 *
14 * The OpenModelica software and the OSMC (Open Source Modelica Consortium) Public License
15 * (OSMC-PL) are obtained from OSMC, either from the above address, from the URLs:
16 * http://www.openmodelica.org or https://github.com/OpenModelica/ or
17 * http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica distribution. GNU
18 * AGPL version 3 is obtained from: https://www.gnu.org/licenses/licenses.html#GPL. The BSD NEW
19 * License is obtained from: http://www.opensource.org/licenses/BSD-3-Clause.
20 *
21 * This program is distributed WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY
23 * SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF
24 * OSMC-PL.
25 *
26 */
27
28 #pragma once
29 /** @defgroup dataexchangePolicies Core.DataExchange.Policy
30 * Module for storing different file output formats ( csv- ,mat- files and buffer storage)
31 * @{
32 */
33 #include "TextfileWriter.h"
34
35 #include <deque>
36
37 /** Keeps the last capacity() elements pushed, dropping the oldest. */
38 template <class T>
39 ✗ class circular_buffer
40 {
41 public:
42 ✗ circular_buffer() : _capacity(0) {}
43 ✗ void set_capacity(size_t n)
44 {
45 ✗ _capacity = n;
46 ✗ while (_data.size() > _capacity)
47 ✗ _data.pop_front();
48 ✗ }
49 size_t capacity() const { return _capacity; }
50 ✗ void push_back(const T& v)
51 {
52 ✗ if (_capacity == 0)
53 return;
54 ✗ if (_data.size() == _capacity)
55 ✗ _data.pop_front();
56 ✗ _data.push_back(v);
57 }
58 ✗ void pop_back() { _data.pop_back(); }
59 ✗ void clear() { _data.clear(); }
60 size_t size() const { return _data.size(); }
61 T& operator[](size_t i) { return _data[i]; }
62 const T& operator[](size_t i) const { return _data[i]; }
63
64 private:
65 size_t _capacity;
66 std::deque<T> _data;
67 };
68 typedef std::vector<double> real_values_t;
69 typedef std::vector<int> int_values_t;
70 typedef std::vector<bool> bool_values_t;
71 typedef std::vector<double> der_values_t;
72 typedef std::vector<double> res_values_t;
73 struct CopyVar {
74 template<class T>
75 T operator()(T* var) const {
76 ✗ return *var;
77 }
78 };
79 class BufferReaderWriter : public ContainerManager
80 {
81 //typedef TextFileWriter<dim_1,dim_2> TextwriterType;
82 public:
83 ✗ BufferReaderWriter(unsigned long size, string file_name)
84 ✗ : ContainerManager(),
85 ✗ _buffer_pos(0)
86 {
87 try
88 {
89
90 ✗ _real_variables_buffer.set_capacity(size+size/5);
91 ✗ _bool_variables_buffer.set_capacity(size+size/5);
92 ✗ _int_variables_buffer.set_capacity(size+size/5);
93 ✗ _der_variables_buffer.set_capacity(size+size/5);
94 ✗ _res_variables_buffer.set_capacity(size+size/5);
95
96 // cout<<"size+size/5 "<< size+size/5 << std::endl;
97
98
99 /*ToDo: use correct size for corresponding type*/
100 }
101 catch(std::exception& ex)
102 {
103 throw ModelicaSimulationError(DATASTORAGE,string("allocating buffers failed")+ex.what());
104 }
105 ✗ }
106
107 void init(std::string file_name, size_t dim)
108 {
109 }
110 /**
111 Reads all Simulation results (algebraic and state variables in R, derivatives in dR)
112 Rij i variable index
113 j number of time index
114 */
115 void read(ublas::matrix<double>& R,ublas::matrix<double>& dR)
116 {
117 /*
118 try
119 {
120 ublas::matrix<double>::size_type m = size();
121 R.resize(dim_1,m);
122 for(int i=0;i<m;++i)
123 ublas::column(R,i)=_variables_buffer[i];
124 dR.resize(dim_2,m);
125 for(int i=0;i<m;++i)
126 ublas::column(dR,i)=_derivatives_buffer[i];
127 }
128 catch(std::exception& ex)
129 {
130 cout<<"read from buffer faild" << std::endl;
131 throw ex;
132 }
133 */
134
135 }
136
137 ✗ void read(ublas::matrix<double>& R,ublas::matrix<double>& dR,ublas::matrix<double>& Re)
138 {
139 /*
140 try
141 {
142 ublas::matrix<double>::size_type m = size();
143 R.resize(dim_1,m);
144 for(int i=0;i<m;++i)
145 ublas::column(R,i)=_variables_buffer[i];
146 dR.resize(dim_2,m);
147 for(int i=0;i<m;++i)
148 ublas::column(dR,i)=_derivatives_buffer[i];
149 Re.resize(dim_3,m);
150 for(int i=0;i<m;++i)
151 ublas::column(Re,i)=_residues_buffer[i];
152 }
153 catch(std::exception& ex)
154 {
155 cout<<"read from buffer faild" << std::endl;
156 throw ex;
157 }
158 */
159
160 ublas::matrix<double>::size_type m = size();
161 cout<<"time in buffer "<< m << std::endl;
162 ✗ ublas::matrix<double>::size_type n =_dim_der;/*get<0>(_var_outputs).size() + get<1>(_var_outputs).size() + get<2>(_var_outputs).size()*/;
163 // cout<<"_dim_der "<< n << std::endl;
164
165 ublas::matrix<double>::size_type i,i2=0,j;
166 try
167 {
168 dR.resize(n,m);
169 }
170 ✗ catch(std::exception& ex)
171 {
172 cout<<"read from derivative buffer faild alloc R with sizes "<<m <<"," << n << std::endl;
173 ✗ throw ModelicaSimulationError(DATASTORAGE,string("read from derivatives buffer failed alloc dR matrix")+ex.what());
174
175 ✗ }
176 try
177 {
178 /*FOREACH(i,indices)*/
179 ✗ for(i=0;i<n;i++)
180 {
181 //cout<<"Var "<<i;
182 ✗ for(j=0;j<m;++j)
183 {
184 // cout<<" t "<<j;
185 ✗ dR(i2,j)= (_der_variables_buffer[j][i]);
186 // cout<<" val "<<(_der_variables_buffer[j][i]) << std::endl;
187 }
188
189 ✗ i2++;
190 }
191
192 /* ublas::vector<double> o_j;
193 for(i=0;i<n;i++)
194 {
195 o_j = ublas::row(dR,i);
196 cout << "derivatives on read buffer " << o_j << std::endl;
197 } */
198 }
199 catch(std::exception& ex)
200 {
201
202 throw ModelicaSimulationError(DATASTORAGE,string("read from derivative buffer failed")+ex.what());
203
204 }
205
206 ✗ n =_dim_real;
207 i2=0;
208 // cout<<"_dim_real "<< n << std::endl;
209 try
210 {
211
212
213 R.resize(n,m);
214 }
215 ✗ catch(std::exception& ex)
216 {
217
218 ✗ throw ModelicaSimulationError(DATASTORAGE,string("read from real Variables buffer failed alloc R matrix")+ex.what());
219
220 ✗ }
221 try
222 {
223
224 //FOREACH(i,indices)
225 ✗ for(i=0;i<n;i++)
226 {
227 ✗ for(j=0;j<m;++j)
228
229 ✗ R(i2,j)= (_real_variables_buffer[j][i]);
230
231 //ToDo: add int and bool variables
232 ✗ i2++;
233 }
234
235
236 }
237 catch(std::exception& ex)
238 {
239
240 throw ModelicaSimulationError(DATASTORAGE,string("read from real buffer failed")+ex.what());
241
242 }
243 ✗ n =_dim_res;
244
245 i2=0;
246 // cout<<"_dim_res "<< n << std::endl;
247 try
248 {
249
250
251 Re.resize(n,m);
252 }
253 ✗ catch(std::exception& ex)
254 {
255
256 ✗ throw ModelicaSimulationError(DATASTORAGE,string("read from residues Variables buffer failed alloc R matrix")+ex.what());
257
258 ✗ }
259 try
260 {
261
262
263 ✗ for(i=0;i<n;i++)
264 {
265 ✗ for(j=0;j<m;++j)
266
267 ✗ Re(i2,j)= (_res_variables_buffer[j][i]);
268
269
270 ✗ i2++;
271 }
272 }
273 catch(std::exception& ex)
274 {
275
276 throw ModelicaSimulationError(DATASTORAGE,string("read from residues Variables buffer failed")+ex.what());
277
278 }
279
280
281 ✗ }
282
283 ✗ void read(ublas::matrix<double>& R)
284 {
285
286 ublas::matrix<double>::size_type m = size();
287
288 ublas::matrix<double>::size_type n ;
289
290 ✗ if(_var_outputs.size()!=0)
291
292 n= _var_outputs.size();/*get<0>(_var_outputs).size() + get<1>(_var_outputs).size() + get<2>(_var_outputs).size()*/
293
294 else
295 ✗ n=_dim_real;
296
297 // cout<<"_dim_outputR "<< n << std::endl;
298 try
299 {
300
301
302 R.resize(n,m);
303 }
304 ✗ catch(std::exception& ex)
305 {
306 // cout<<"read from variables buffer faild alloc R with sizes "<<m <<"," << n << std::endl;
307 ✗ throw ModelicaSimulationError(DATASTORAGE,string("read from variables buffer failed alloc R matrix")+ex.what());
308
309 ✗ }
310 ublas::matrix<double>::size_type i,i2=0,j;
311 try
312 {
313 /*FOREACH(i,indices)*/
314 ✗ for(i=0;i<n;i++)
315 {
316 ✗ for(j=0;j<m;++j)
317 {
318 ✗ R(i2,j)= (_real_variables_buffer[j][i]);
319
320 }
321
322 ✗ i2++;
323 }
324 }
325 catch(std::exception& ex)
326 {
327
328 throw ModelicaSimulationError(DATASTORAGE,string("read from variables buffer failed")+ex.what());
329
330 }
331
332
333 ✗ }
334
335 void read(const double& time,ublas::vector<double>& dv,ublas::vector<double>& v)
336 {
337 /*
338 std::map<double,unsigned long>::iterator iter;
339 iter = find_if( _time_entries.begin(), _time_entries.end(), floatCompare<double>(time, 1e-10) );
340 if(iter==_time_entries.end())
341 throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,string("getVariables: time parameters");
342 v=_variables_buffer[iter->second];
343 dv = _derivatives_buffer[iter->second];
344 */
345 }
346
347 void read(const double& time,ublas::vector<double>& r,ublas::vector<double>& dv,ublas::vector<double>& v)
348 {
349 /*
350 std::map<double,unsigned long>::iterator iter;
351 iter = find_if( _time_entries.begin(), _time_entries.end(), floatCompare<double>(time, 1e-10) );
352 if(iter==_time_entries.end())
353 throw ModelicaSimulationError(MODEL_ARRAY_FUNCTION,string("getVariables: time parameters");
354 v=_variables_buffer[iter->second];
355 dv = _derivatives_buffer[iter->second];
356 // r = _residues_buffer[iter->second];
357 */
358 }
359
360 void write(const vector<string>& s)
361 {
362
363 }
364
365 void write(const char c)
366 {
367
368 }
369 /*writes pramater values to results file
370 @v_list values of real,int,bool parameter
371 @start_time
372 @end_time
373 */
374 ✗ virtual void write(const all_vars_t& v_list, double start_time, double end_time)
375 {
376 //not supported for buffer
377 ✗ }
378 /*
379 writes header of results file with the variable names
380 @s_list name of real,int,bool variables
381 @s_desc_list description of real,int,bool variables
382 @s_parameter_list name ofreal,int,bool parameter
383 @s_desc_parameter_list description of real,int,bool parameter
384 */
385 ✗ virtual void write(const all_names_t& s_list,const all_description_t& s_desc_list,const all_names_t& s_parameter_list,const all_description_t& s_desc_parameter_list)
386 {
387
388 ✗ _dim_real = get<0>(s_list).size();
389 ✗ _dim_int = get<1>(s_list).size();
390 ✗ _dim_bool = get<2>(s_list).size();
391 ✗ _dim_der = get<3>(s_list).size();
392
393 ✗ _real_values = real_values_t(_dim_real);
394 ✗ _int_values = int_values_t(_dim_int);
395 ✗ _bool_values = bool_values_t(_dim_bool);
396 ✗ _der_values = der_values_t(_dim_der);
397
398 //_var_outputs = s_list;
399 ✗ _var_outputs.clear();
400 ✗ for (var_names_t::const_iterator it = get<0>(s_list).begin(); it != get<0>(s_list).end(); ++it)
401 {
402 ✗ _var_outputs.push_back(*it);
403 }
404 ✗ }
405
406 /*
407 writes simulation results for a time step
408 @v_list variables and state vars
409 @v2_list derivatives vars
410 @time
411 */
412 ✗ virtual void write(const all_vars_time_t& v_list,const neg_all_vars_t& neg_v_list)
413 {
414
415
416 try
417 {
418
419 std::pair<std::map<double, unsigned long>::iterator,bool> p;
420 ✗ p = _time_entries.insert(make_pair(get<3>(v_list),_buffer_pos));
421
422 ✗ if(!p.second)//if variable and derivatives for time are already inserted, erase old values
423 {
424 //cout<< "size real "<< _real_variables_buffer.size() <<std::endl;
425 _real_variables_buffer.pop_back();
426 //cout << "size int " << _int_variables_buffer.size()<<std::endl;
427 _int_variables_buffer.pop_back();
428 //cout << "size bool " << _bool_variables_buffer.size()<<std::endl;
429 _bool_variables_buffer.pop_back();
430 _der_variables_buffer.pop_back();
431 _res_variables_buffer.pop_back();
432
433 }
434 else
435 {
436 ✗ _buffer_pos++;
437
438 }
439
440 ✗ _dim_res = get<5>(v_list).size();
441 ✗ _res_values = res_values_t(_dim_res);
442
443 /* for (real_vars_t::const_iterator it = get<0>(v_list).begin(); it != get<0>(v_list).end(); ++it)
444 {
445
446 std::cout << *(*it)<<",";
447 }
448 std::cout<<"\n"; */
449
450 std::transform(get<0>(v_list).begin(),get<0>(v_list).end(),_real_values.begin(),CopyVar());
451 std::transform(get<1>(v_list).begin(),get<1>(v_list).end(),_int_values.begin(),CopyVar());
452 ✗ std::transform(get<2>(v_list).begin(),get<2>(v_list).end(),_bool_values.begin(),CopyVar());
453 std::transform(get<4>(v_list).begin(),get<4>(v_list).end(),_der_values.begin(),CopyVar());
454 std::transform(get<5>(v_list).begin(),get<5>(v_list).end(),_res_values.begin(),CopyVar());
455
456
457 ✗ _real_variables_buffer.push_back(_real_values);
458 ✗ _int_variables_buffer.push_back(_int_values);
459 ✗ _bool_variables_buffer.push_back(_bool_values);
460 ✗ _der_variables_buffer.push_back(_der_values);
461 ✗ _res_variables_buffer.push_back(_res_values);
462
463 // cout<<"output values on write buffer "<< std::endl;
464
465 /* for (real_values_t::const_iterator it = _real_values.begin(); it != _real_values.end(); ++it)
466 {
467
468 std::cout << *it<<",";
469 }
470 std::cout<<"\n"; */
471
472 //_all_var_size=get<0>(v_list).size();// now just real variables are supported
473
474 }
475 ✗ catch(std::exception& ex)
476 {
477
478 ✗ throw ModelicaSimulationError(DATASTORAGE,string("write to buffer failed")+ex.what());
479
480 ✗ }
481
482 // textwriter->write(v,v2, time);
483 ✗ }
484
485
486 ✗ void getTime(vector<double>& time)
487 {
488 try
489 {
490 typedef std::pair<double, unsigned long> pair_double_ulong;
491 ✗ FOREACH(pair_double_ulong i, _time_entries)
492 {
493 ✗ time.push_back(i.first);
494 }
495 }
496 ✗ catch(std::exception& ex)
497 {
498
499 ✗ throw ModelicaSimulationError(DATASTORAGE,string("read from time buffer failed")+ex.what());
500
501 ✗ }
502 ✗ }
503 unsigned long size()
504 {
505 try
506 {
507 return _time_entries.size();
508 }
509 catch(std::exception& ex)
510 {
511
512 throw ModelicaSimulationError(DATASTORAGE,string("time entries size failed")+ex.what());
513
514 }
515 }
516 ✗ void eraseAll()
517 {
518 try{
519 _real_variables_buffer.clear();
520 _int_variables_buffer.clear();
521 _bool_variables_buffer.clear();
522 _der_variables_buffer.clear();
523 _res_variables_buffer.clear();
524 //_residues_buffer.clear();
525 _time_entries.clear();
526 ✗ _buffer_pos=0;
527 //textwriter->eraseAll();
528 }
529
530 catch(std::exception& ex)
531 {
532
533 throw ModelicaSimulationError(DATASTORAGE,string("can not erase buffer of variables")+ex.what());
534
535 }
536
537 ✗ }
538
539
540 protected:
541
542 typedef circular_buffer< real_values_t > real_buffer_type;
543 typedef circular_buffer< int_values_t > int_buffer_type;
544 typedef circular_buffer< bool_values_t > bool_buffer_type;
545 typedef circular_buffer< der_values_t> der_buffer_type;
546 typedef circular_buffer< res_values_t> res_buffer_type;
547
548 typedef std::map<double,unsigned long> _time_entries_type;
549
550 real_buffer_type _real_variables_buffer;
551 int_buffer_type _int_variables_buffer;
552 bool_buffer_type _bool_variables_buffer;
553 der_buffer_type _der_variables_buffer;
554 res_buffer_type _res_variables_buffer;
555 //buffer_type buffer_var;
556
557 //buffer_type_r _residues_buffer;
558 _time_entries_type _time_entries;
559 unsigned long _buffer_pos;
560 vector<string> _var_outputs;
561
562 size_t _dim_real;
563 size_t _dim_int;
564 size_t _dim_bool;
565 size_t _dim_der;
566 size_t _dim_res;
567
568 real_values_t _real_values;
569 int_values_t _int_values;
570 bool_values_t _bool_values;
571 der_values_t _der_values;
572 res_values_t _res_values;
573 };
574 /** @} */ // end of dataexchangePolicies
575