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

Commit a981b37

Browse files
lochelOpenModelica-Hudson
authored andcommitted
New implementation for writing mat files
Belonging to [master]: - #1952
1 parent bf1eaeb commit a981b37

File tree

9 files changed

+1152
-8
lines changed

9 files changed

+1152
-8
lines changed

Compiler/Template/CodegenC.tpl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ template simulationFile_bnd(SimCode simCode)
656656
657657
<%functionUpdateBoundVariableAttributes(simCode, startValueEquations, nominalValueEquations, minValueEquations, maxValueEquations, modelNamePrefix(simCode))%>
658658
659-
<%functionUpdateBoundParameters(selectScalarLiteralAssignments(parameterEquations), filterScalarLiteralAssignments(parameterEquations), simCode.fileNamePrefix, simCode.fullPathPrefix, modelNamePrefix(simCode))%>
659+
<%functionUpdateBoundParameters(selectScalarLiteralAssignments(parameterEquations), filterScalarLiteralAssignments(parameterEquations), simCode.fileNamePrefix, simCode.fullPathPrefix, modelNamePrefix(simCode), simCode)%>
660660
661661
#if defined(__cplusplus)
662662
}
@@ -2917,10 +2917,11 @@ template functionUpdateBoundVariableAttributes(SimCode simCode, list<SimEqSystem
29172917
>>
29182918
end functionUpdateBoundVariableAttributes;
29192919
2920-
template functionUpdateBoundParameters(list<SimEqSystem> simpleParameterEquations, list<SimEqSystem> parameterEquations, String fileNamePrefix, String fullPathPrefix, String modelNamePrefix)
2920+
template functionUpdateBoundParameters(list<SimEqSystem> simpleParameterEquations, list<SimEqSystem> parameterEquations, String fileNamePrefix, String fullPathPrefix, String modelNamePrefix, SimCode simCode)
29212921
"Generates function in simulation file."
29222922
::=
29232923
let &eqFuncs = buffer ""
2924+
let &auxFunction = buffer ""
29242925
let fncalls = functionEquationsMultiFiles(parameterEquations, listLength(parameterEquations),
29252926
Flags.getConfigInt(Flags.EQUATIONS_PER_FILE), fileNamePrefix, fullPathPrefix, modelNamePrefix,
29262927
"updateBoundParameters", "08bnd", &eqFuncs, /* Static? */ true, true /* No optimization */)
@@ -2930,7 +2931,15 @@ template functionUpdateBoundParameters(list<SimEqSystem> simpleParameterEquation
29302931
int <%symbolName(modelNamePrefix,"updateBoundParameters")%>(DATA *data, threadData_t *threadData)
29312932
{
29322933
TRACE_PUSH
2933-
<%sortSimpleAssignmentBasedOnLhs(simpleParameterEquations) |> eq as SES_SIMPLE_ASSIGN(__) => '<%cref(cref)%> = <%daeExpSimpleLiteral(exp)%>;' ; separator="\n" %>
2934+
<%sortSimpleAssignmentBasedOnLhs(simpleParameterEquations) |> eq as SES_SIMPLE_ASSIGN(__) =>
2935+
<<
2936+
<%cref(cref)%> = <%daeExpSimpleLiteral(exp)%>;
2937+
<%match cref2simvar(cref, simCode)
2938+
case SIMVAR(varKind=PARAM()) then
2939+
'data->modelData-><%expTypeShort(type_)%>ParameterData[<%index%>].time_unvarying = 1;'
2940+
case SIMVAR(__) then
2941+
'data->modelData-><%expTypeShort(type_)%>VarsData[<%index%>].time_unvarying = 1;'%>
2942+
>> ; separator="\n" %>
29342943
<%fncalls%>
29352944
TRACE_POP
29362945
return 0;

SimulationRuntime/c/Makefile.objs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ OPTIMIZATION_OBJS=
8484
OPTIMIZATION_HFILES=
8585
endif
8686

87-
RESULTS_OBJS_MINIMAL=simulation_result$(OBJ_EXT) simulation_result_csv$(OBJ_EXT) simulation_result_mat$(OBJ_EXT)
87+
RESULTS_OBJS_MINIMAL=simulation_result$(OBJ_EXT) simulation_result_csv$(OBJ_EXT) simulation_result_mat$(OBJ_EXT) simulation_result_mat4$(OBJ_EXT) MatVer4$(OBJ_EXT)
8888
ifeq ($(OMC_MINIMAL_RUNTIME),)
8989
RESULTS_OBJS=$(RESULTS_OBJS_MINIMAL) simulation_result_ia$(OBJ_EXT) simulation_result_plt$(OBJ_EXT) simulation_result_wall$(OBJ_EXT)
9090
else
9191
RESULTS_OBJS=$(RESULTS_OBJS_MINIMAL)
9292
endif
93-
RESULTS_HFILES = simulation_result_ia.h simulation_result.h simulation_result_csv.h simulation_result_mat.h simulation_result_plt.h simulation_result_wall.h
94-
RESULTS_FILES = simulation_result_ia.cpp simulation_result_csv.cpp simulation_result_mat.cpp simulation_result_plt.cpp simulation_result_wall.cpp
93+
RESULTS_HFILES = simulation_result_ia.h simulation_result.h simulation_result_csv.h simulation_result_mat.h simulation_result_mat4.h MatVer4.h simulation_result_plt.h simulation_result_wall.h
94+
RESULTS_FILES = simulation_result_ia.cpp simulation_result_csv.cpp simulation_result_mat.cpp simulation_result_mat4.cpp MatVer4.cpp simulation_result_plt.cpp simulation_result_wall.cpp
9595

9696
SIM_OBJS = simulation_runtime$(OBJ_EXT) ../linearization/linearize$(OBJ_EXT) socket$(OBJ_EXT)
9797
ifeq ($(OMC_FMI_RUNTIME),)

SimulationRuntime/c/simulation/results/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ simulation_result.cpp simulation_result_ia.cpp simulation_result_plt.cpp
77
simulation_result_csv.cpp simulation_result_mat.cpp simulation_result_wall.cpp
88
)
99

10-
SET(results_headers ../../util/read_csv.h
10+
SET(results_headers ../../util/read_csv.h
1111
simulation_result.h simulation_result_ia.h simulation_result_plt.h
1212
simulation_result_csv.h simulation_result_mat.h simulation_result_wall.h
1313
)
Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
/*
2+
* This file is part of OpenModelica.
3+
*
4+
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
5+
* c/o Linköpings universitet, Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
7+
*
8+
* All rights reserved.
9+
*
10+
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
11+
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
12+
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
13+
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
14+
* ACCORDING TO RECIPIENTS CHOICE.
15+
*
16+
* The OpenModelica software and the Open Source Modelica
17+
* Consortium (OSMC) Public License (OSMC-PL) are obtained
18+
* from OSMC, either from the above address,
19+
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20+
* http://www.openmodelica.org, and in the OpenModelica distribution.
21+
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22+
*
23+
* This program is distributed WITHOUT ANY WARRANTY; without
24+
* even the implied warranty of MERCHANTABILITY or FITNESS
25+
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26+
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27+
*
28+
* See the full OSMC Public License conditions for more details.
29+
*
30+
*/
31+
32+
#include "MatVer4.h"
33+
34+
#include <stddef.h>
35+
#include <stdint.h>
36+
#include <stdio.h>
37+
#include <stdlib.h>
38+
#include <string.h>
39+
40+
#ifdef __cplusplus
41+
extern "C" {
42+
#endif
43+
44+
const char isBigEndian()
45+
{
46+
union
47+
{
48+
uint32_t i32;
49+
uint8_t i8[4];
50+
} test = { 0x01020304 };
51+
return (1 == test.i8[0]);
52+
}
53+
54+
int writeMatVer4Matrix_4(FILE* file, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type)
55+
{
56+
struct MatVer4Header
57+
{
58+
unsigned int type;
59+
unsigned int mrows;
60+
unsigned int ncols;
61+
unsigned int imagf;
62+
unsigned int namelen;
63+
} header;
64+
65+
size_t size;
66+
switch (type)
67+
{
68+
case MatVer4Type_DOUBLE:
69+
size = sizeof(double);
70+
break;
71+
case MatVer4Type_SINGLE:
72+
size = sizeof(float);
73+
break;
74+
case MatVer4Type_INT32:
75+
size = sizeof(int32_t);
76+
break;
77+
case MatVer4Type_CHAR:
78+
size = sizeof(uint8_t);
79+
break;
80+
default:
81+
return -1;
82+
}
83+
84+
header.type = (isBigEndian() ? 1000 : 0) + type;
85+
header.mrows = (unsigned int) rows;
86+
header.ncols = (unsigned int) cols;
87+
header.imagf = 0;
88+
header.namelen = (unsigned int) strlen(name) + 1;
89+
90+
fwrite(&header, sizeof(MatVer4Header), 1, file);
91+
fwrite(name, sizeof(char), header.namelen, file);
92+
fwrite(matrixData, size, rows * cols, file);
93+
94+
return 0;
95+
}
96+
97+
int appendMatVer4Matrix_4(FILE* file, long position, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type)
98+
{
99+
struct MatVer4Header
100+
{
101+
unsigned int type;
102+
unsigned int mrows;
103+
unsigned int ncols;
104+
unsigned int imagf;
105+
unsigned int namelen;
106+
} header;
107+
108+
size_t size;
109+
switch (type)
110+
{
111+
case MatVer4Type_DOUBLE:
112+
size = sizeof(double);
113+
break;
114+
case MatVer4Type_SINGLE:
115+
size = sizeof(float);
116+
break;
117+
case MatVer4Type_INT32:
118+
size = sizeof(int32_t);
119+
break;
120+
case MatVer4Type_CHAR:
121+
size = sizeof(uint8_t);
122+
break;
123+
default:
124+
return -1;
125+
}
126+
127+
long eof = ftell(file);
128+
fseek(file, position, SEEK_SET);
129+
fread(&header, sizeof(MatVer4Header), 1, file);
130+
if (header.type != (isBigEndian() ? 1000 : 0) + type)
131+
return -1;
132+
if (header.mrows != rows)
133+
return -1;
134+
header.ncols += (unsigned int) cols;
135+
if (header.imagf != 0)
136+
return -1;
137+
if (header.namelen != strlen(name) + 1)
138+
return -1;
139+
140+
fseek(file, position, SEEK_SET);
141+
fwrite(&header, sizeof(MatVer4Header), 1, file);
142+
fseek(file, eof, SEEK_SET);
143+
fwrite(matrixData, size, rows * cols, file);
144+
return 0;
145+
}
146+
147+
int writeMatVer4Header_4(FILE* file, long position, const char* name, size_t rows, size_t cols, MatVer4Type_t type)
148+
{
149+
struct MatVer4Header
150+
{
151+
unsigned int type;
152+
unsigned int mrows;
153+
unsigned int ncols;
154+
unsigned int imagf;
155+
unsigned int namelen;
156+
} header;
157+
158+
size_t size;
159+
switch (type)
160+
{
161+
case MatVer4Type_DOUBLE:
162+
size = sizeof(double);
163+
break;
164+
case MatVer4Type_SINGLE:
165+
size = sizeof(float);
166+
break;
167+
case MatVer4Type_INT32:
168+
size = sizeof(int32_t);
169+
break;
170+
case MatVer4Type_CHAR:
171+
size = sizeof(uint8_t);
172+
break;
173+
default:
174+
return -1;
175+
}
176+
177+
long eof = ftell(file);
178+
fseek(file, position, SEEK_SET);
179+
fread(&header, sizeof(MatVer4Header), 1, file);
180+
if (header.type != (isBigEndian() ? 1000 : 0) + type)
181+
return -1;
182+
if (header.mrows != rows)
183+
return -1;
184+
header.ncols = (unsigned int) cols;
185+
if (header.imagf != 0)
186+
return -1;
187+
if (header.namelen != strlen(name) + 1)
188+
return -1;
189+
190+
fseek(file, position, SEEK_SET);
191+
fwrite(&header, sizeof(MatVer4Header), 1, file);
192+
fseek(file, eof, SEEK_SET);
193+
return 0;
194+
}
195+
196+
MatVer4Matrix* readMatVer4Matrix_4(FILE* file)
197+
{
198+
MatVer4Matrix *matrix = (MatVer4Matrix*) malloc(sizeof(MatVer4Matrix));
199+
if (!matrix)
200+
return NULL;
201+
202+
fread(&matrix->header, sizeof(MatVer4Header), 1, file);
203+
204+
// skip name
205+
fseek(file, matrix->header.namelen, SEEK_CUR);
206+
207+
size_t size;
208+
switch (matrix->header.type % 100)
209+
{
210+
case MatVer4Type_DOUBLE:
211+
size = sizeof(double);
212+
break;
213+
case MatVer4Type_SINGLE:
214+
size = sizeof(float);
215+
break;
216+
case MatVer4Type_INT32:
217+
size = sizeof(int32_t);
218+
break;
219+
case MatVer4Type_CHAR:
220+
size = sizeof(uint8_t);
221+
break;
222+
default:
223+
free(matrix);
224+
return NULL;
225+
}
226+
227+
matrix->data = malloc(matrix->header.mrows * matrix->header.ncols * size);
228+
fread(matrix->data, size, matrix->header.mrows*matrix->header.ncols, file);
229+
230+
return matrix;
231+
}
232+
233+
void freeMatVer4Matrix_4(MatVer4Matrix** matrix)
234+
{
235+
if (*matrix)
236+
{
237+
if ((*matrix)->data)
238+
free((*matrix)->data);
239+
free(*matrix);
240+
*matrix = NULL;
241+
}
242+
}
243+
244+
int skipMatVer4Matrix_4(FILE* file)
245+
{
246+
MatVer4Header header;
247+
fread(&header, sizeof(MatVer4Header), 1, file);
248+
249+
// skip name
250+
fseek(file, header.namelen, SEEK_CUR);
251+
252+
size_t size;
253+
switch (header.type % 100)
254+
{
255+
case MatVer4Type_DOUBLE:
256+
size = sizeof(double);
257+
break;
258+
case MatVer4Type_SINGLE:
259+
size = sizeof(float);
260+
break;
261+
case MatVer4Type_INT32:
262+
size = sizeof(int32_t);
263+
break;
264+
case MatVer4Type_CHAR:
265+
size = sizeof(uint8_t);
266+
break;
267+
default:
268+
return -1;
269+
}
270+
271+
// skip data
272+
fseek(file, header.mrows*header.ncols*size, SEEK_CUR);
273+
return 0;
274+
}
275+
276+
#ifdef __cplusplus
277+
}
278+
#endif

0 commit comments

Comments
 (0)