Linux GNU 11.4.0 Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 85.4% 35 / 0 / 41
Functions: 83.3% 5 / 0 / 6
Branches: 34.4% 11 / 0 / 32

OMCompiler/SimulationRuntime/cpp/Core/DataExchange/Policies/TextfileWriter.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 /** @addtogroup dataexchangePolicies
30 *
31 * @{
32 */
33
34 #include <Core/DataExchange/FactoryPolicy.h>
35 #include <fstream>
36
37 /**
38 Policy class to write simulation results in a text file
39 */
40 const char SEPERATOR = ',';
41 const char EXTENSION = ',';
42
43 class TextFileWriter : public ContainerManager
44 {
45 public:
46 2 TextFileWriter(unsigned long size, string file_name)
47 2 : ContainerManager(),
48 2 _output_stream(),
49 2 _curser_position(0),
50
2/2
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
2 _file_name(file_name)
51 {
52 2 }
53
54 2 ~TextFileWriter()
55 2 {
56
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (_output_stream.is_open())
57 2 _output_stream.close();
58 4 }
59
60 2 void init(std::string file_name, size_t dim)
61 {
62 2 _file_name = file_name;
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (_output_stream.is_open())
64 ✗ _output_stream.close();
65
66 2 _output_stream.open(file_name.c_str(), ios::out);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (_output_stream.fail())
68 ✗ throw ModelicaSimulationError(DATASTORAGE, string("Failed to open results file ") + file_name);
69 2 }
70 void read(ublas::matrix<double>& R, ublas::matrix<double>& dR)
71 {
72 //not supported for file output
73
74 }
75
76 void read(ublas::matrix<double>& R, ublas::matrix<double>& dR, ublas::matrix<double>& Re)
77 {
78 //not supported for file output
79
80 }
81
82 void read(const double& time, ublas::vector<double>& dv, ublas::vector<double>& v)
83 {
84 //not supported for file output
85
86 }
87
88 void read(ublas::matrix<double>& R)
89 {
90 //not supported for file output
91
92 }
93
94 /*writes pramater values to results file
95 @v_list values of parameter
96 @start_time
97 @end_time
98 */
99 ✗ virtual void write(const all_vars_t& v_list, double start_time, double end_time)
100 {
101
102 //not supported for file output
103 ✗ }
104
105 /*
106 writes header of results file with the variable names
107 @s_list name of variables
108 @s_desc_list description of variables
109 @s_parameter_list name of parameter
110 @s_desc_parameter_list description of parameter
111 */
112
1/1
✓ Branch 1 taken 2 times.
2 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)
113 {
114 std::string s;
115
1/1
✓ Branch 1 taken 2 times.
4 _output_stream << "\"time\"" << SEPERATOR;
116
117
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 for (var_names_t::const_iterator it = get<0>(s_list).begin(); it != get<0>(s_list).end(); ++it)
118 7 _output_stream << "\"" << (*it) << "\"" << SEPERATOR;
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 for (var_names_t::const_iterator it = get<1>(s_list).begin(); it != get<1>(s_list).end(); ++it)
120 ✗ _output_stream << "\"" << (*it) << "\"" << SEPERATOR;
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 for (var_names_t::const_iterator it = get<2>(s_list).begin(); it != get<2>(s_list).end(); ++it)
122 ✗ _output_stream << "\"" << (*it) << "\"" << SEPERATOR;
123 _output_stream << std::endl;
124 2 }
125
126 void write(const char c)
127 {
128 _output_stream << c;
129 }
130
131 /*
132 writes simulation results for a time step
133 @v_list variables and state vars
134 @v2_list derivatives vars
135 @time
136 */
137 950 virtual void write(const all_vars_time_t& v_list,const neg_all_vars_t& neg_v_list)
138 {
139 1900 _output_stream << get<3>(v_list) << SEPERATOR;
140
141
142 950 std::transform(get<0>(v_list).begin(), get<0>(v_list).end(), get<0>(neg_v_list).begin(),
143 950 std::ostream_iterator<double>(_output_stream,","), WriteOutputVar<double>());
144
145
146 950 std::transform(get<1>(v_list).begin(), get<1>(v_list).end(), get<1>(neg_v_list).begin(),
147 950 std::ostream_iterator<int>(_output_stream,","), WriteOutputVar<int>());
148
149
150 950 std::transform(get<2>(v_list).begin(), get<2>(v_list).end(), get<2>(neg_v_list).begin(),
151 950 std::ostream_iterator<bool>(_output_stream,","), WriteOutputVar<bool>());
152
153 _output_stream << std::endl;
154 950 }
155
156 void getTime(std::vector<double>& time)
157 {
158 //not supported for file output
159 }
160
161 unsigned long size()
162 {
163 //not supported for file output
164 return 0;
165 }
166
167 void eraseAll()
168 {
169 2 _curser_position = 0;
170 2 _output_stream.seekp(_curser_position);
171 }
172
173 protected:
174
175
176 std::fstream _output_stream;
177 unsigned int _curser_position; ///< Controls current Curser-Position
178 std::string _file_name;
179 vector<string> _var_outputs;
180 };
181 /** @} */ // end of dataexchangePolicies
182