Skip to content

Commit

Permalink
New implementation for writing mat files
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - OpenModelica/OMCompiler#1952
  • Loading branch information
lochel authored and OpenModelica-Hudson committed Oct 30, 2017
1 parent bf1eaeb commit a981b37
Show file tree
Hide file tree
Showing 9 changed files with 1,152 additions and 8 deletions.
15 changes: 12 additions & 3 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -656,7 +656,7 @@ template simulationFile_bnd(SimCode simCode)
<%functionUpdateBoundVariableAttributes(simCode, startValueEquations, nominalValueEquations, minValueEquations, maxValueEquations, modelNamePrefix(simCode))%>
<%functionUpdateBoundParameters(selectScalarLiteralAssignments(parameterEquations), filterScalarLiteralAssignments(parameterEquations), simCode.fileNamePrefix, simCode.fullPathPrefix, modelNamePrefix(simCode))%>
<%functionUpdateBoundParameters(selectScalarLiteralAssignments(parameterEquations), filterScalarLiteralAssignments(parameterEquations), simCode.fileNamePrefix, simCode.fullPathPrefix, modelNamePrefix(simCode), simCode)%>
#if defined(__cplusplus)
}
Expand Down Expand Up @@ -2917,10 +2917,11 @@ template functionUpdateBoundVariableAttributes(SimCode simCode, list<SimEqSystem
>>
end functionUpdateBoundVariableAttributes;
template functionUpdateBoundParameters(list<SimEqSystem> simpleParameterEquations, list<SimEqSystem> parameterEquations, String fileNamePrefix, String fullPathPrefix, String modelNamePrefix)
template functionUpdateBoundParameters(list<SimEqSystem> simpleParameterEquations, list<SimEqSystem> parameterEquations, String fileNamePrefix, String fullPathPrefix, String modelNamePrefix, SimCode simCode)
"Generates function in simulation file."
::=
let &eqFuncs = buffer ""
let &auxFunction = buffer ""
let fncalls = functionEquationsMultiFiles(parameterEquations, listLength(parameterEquations),
Flags.getConfigInt(Flags.EQUATIONS_PER_FILE), fileNamePrefix, fullPathPrefix, modelNamePrefix,
"updateBoundParameters", "08bnd", &eqFuncs, /* Static? */ true, true /* No optimization */)
Expand All @@ -2930,7 +2931,15 @@ template functionUpdateBoundParameters(list<SimEqSystem> simpleParameterEquation
int <%symbolName(modelNamePrefix,"updateBoundParameters")%>(DATA *data, threadData_t *threadData)
{
TRACE_PUSH
<%sortSimpleAssignmentBasedOnLhs(simpleParameterEquations) |> eq as SES_SIMPLE_ASSIGN(__) => '<%cref(cref)%> = <%daeExpSimpleLiteral(exp)%>;' ; separator="\n" %>
<%sortSimpleAssignmentBasedOnLhs(simpleParameterEquations) |> eq as SES_SIMPLE_ASSIGN(__) =>
<<
<%cref(cref)%> = <%daeExpSimpleLiteral(exp)%>;
<%match cref2simvar(cref, simCode)
case SIMVAR(varKind=PARAM()) then
'data->modelData-><%expTypeShort(type_)%>ParameterData[<%index%>].time_unvarying = 1;'
case SIMVAR(__) then
'data->modelData-><%expTypeShort(type_)%>VarsData[<%index%>].time_unvarying = 1;'%>
>> ; separator="\n" %>
<%fncalls%>
TRACE_POP
return 0;
Expand Down
6 changes: 3 additions & 3 deletions SimulationRuntime/c/Makefile.objs
Expand Up @@ -84,14 +84,14 @@ OPTIMIZATION_OBJS=
OPTIMIZATION_HFILES=
endif

RESULTS_OBJS_MINIMAL=simulation_result$(OBJ_EXT) simulation_result_csv$(OBJ_EXT) simulation_result_mat$(OBJ_EXT)
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)
ifeq ($(OMC_MINIMAL_RUNTIME),)
RESULTS_OBJS=$(RESULTS_OBJS_MINIMAL) simulation_result_ia$(OBJ_EXT) simulation_result_plt$(OBJ_EXT) simulation_result_wall$(OBJ_EXT)
else
RESULTS_OBJS=$(RESULTS_OBJS_MINIMAL)
endif
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
RESULTS_FILES = simulation_result_ia.cpp simulation_result_csv.cpp simulation_result_mat.cpp simulation_result_plt.cpp simulation_result_wall.cpp
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
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

SIM_OBJS = simulation_runtime$(OBJ_EXT) ../linearization/linearize$(OBJ_EXT) socket$(OBJ_EXT)
ifeq ($(OMC_FMI_RUNTIME),)
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/results/CMakeLists.txt
Expand Up @@ -7,7 +7,7 @@ simulation_result.cpp simulation_result_ia.cpp simulation_result_plt.cpp
simulation_result_csv.cpp simulation_result_mat.cpp simulation_result_wall.cpp
)

SET(results_headers ../../util/read_csv.h
SET(results_headers ../../util/read_csv.h
simulation_result.h simulation_result_ia.h simulation_result_plt.h
simulation_result_csv.h simulation_result_mat.h simulation_result_wall.h
)
Expand Down
278 changes: 278 additions & 0 deletions SimulationRuntime/c/simulation/results/MatVer4.cpp
@@ -0,0 +1,278 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/

#include "MatVer4.h"

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

const char isBigEndian()
{
union
{
uint32_t i32;
uint8_t i8[4];
} test = { 0x01020304 };
return (1 == test.i8[0]);
}

int writeMatVer4Matrix_4(FILE* file, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type)
{
struct MatVer4Header
{
unsigned int type;
unsigned int mrows;
unsigned int ncols;
unsigned int imagf;
unsigned int namelen;
} header;

size_t size;
switch (type)
{
case MatVer4Type_DOUBLE:
size = sizeof(double);
break;
case MatVer4Type_SINGLE:
size = sizeof(float);
break;
case MatVer4Type_INT32:
size = sizeof(int32_t);
break;
case MatVer4Type_CHAR:
size = sizeof(uint8_t);
break;
default:
return -1;
}

header.type = (isBigEndian() ? 1000 : 0) + type;
header.mrows = (unsigned int) rows;
header.ncols = (unsigned int) cols;
header.imagf = 0;
header.namelen = (unsigned int) strlen(name) + 1;

fwrite(&header, sizeof(MatVer4Header), 1, file);
fwrite(name, sizeof(char), header.namelen, file);
fwrite(matrixData, size, rows * cols, file);

return 0;
}

int appendMatVer4Matrix_4(FILE* file, long position, const char* name, size_t rows, size_t cols, const void* matrixData, MatVer4Type_t type)
{
struct MatVer4Header
{
unsigned int type;
unsigned int mrows;
unsigned int ncols;
unsigned int imagf;
unsigned int namelen;
} header;

size_t size;
switch (type)
{
case MatVer4Type_DOUBLE:
size = sizeof(double);
break;
case MatVer4Type_SINGLE:
size = sizeof(float);
break;
case MatVer4Type_INT32:
size = sizeof(int32_t);
break;
case MatVer4Type_CHAR:
size = sizeof(uint8_t);
break;
default:
return -1;
}

long eof = ftell(file);
fseek(file, position, SEEK_SET);
fread(&header, sizeof(MatVer4Header), 1, file);
if (header.type != (isBigEndian() ? 1000 : 0) + type)
return -1;
if (header.mrows != rows)
return -1;
header.ncols += (unsigned int) cols;
if (header.imagf != 0)
return -1;
if (header.namelen != strlen(name) + 1)
return -1;

fseek(file, position, SEEK_SET);
fwrite(&header, sizeof(MatVer4Header), 1, file);
fseek(file, eof, SEEK_SET);
fwrite(matrixData, size, rows * cols, file);
return 0;
}

int writeMatVer4Header_4(FILE* file, long position, const char* name, size_t rows, size_t cols, MatVer4Type_t type)
{
struct MatVer4Header
{
unsigned int type;
unsigned int mrows;
unsigned int ncols;
unsigned int imagf;
unsigned int namelen;
} header;

size_t size;
switch (type)
{
case MatVer4Type_DOUBLE:
size = sizeof(double);
break;
case MatVer4Type_SINGLE:
size = sizeof(float);
break;
case MatVer4Type_INT32:
size = sizeof(int32_t);
break;
case MatVer4Type_CHAR:
size = sizeof(uint8_t);
break;
default:
return -1;
}

long eof = ftell(file);
fseek(file, position, SEEK_SET);
fread(&header, sizeof(MatVer4Header), 1, file);
if (header.type != (isBigEndian() ? 1000 : 0) + type)
return -1;
if (header.mrows != rows)
return -1;
header.ncols = (unsigned int) cols;
if (header.imagf != 0)
return -1;
if (header.namelen != strlen(name) + 1)
return -1;

fseek(file, position, SEEK_SET);
fwrite(&header, sizeof(MatVer4Header), 1, file);
fseek(file, eof, SEEK_SET);
return 0;
}

MatVer4Matrix* readMatVer4Matrix_4(FILE* file)
{
MatVer4Matrix *matrix = (MatVer4Matrix*) malloc(sizeof(MatVer4Matrix));
if (!matrix)
return NULL;

fread(&matrix->header, sizeof(MatVer4Header), 1, file);

// skip name
fseek(file, matrix->header.namelen, SEEK_CUR);

size_t size;
switch (matrix->header.type % 100)
{
case MatVer4Type_DOUBLE:
size = sizeof(double);
break;
case MatVer4Type_SINGLE:
size = sizeof(float);
break;
case MatVer4Type_INT32:
size = sizeof(int32_t);
break;
case MatVer4Type_CHAR:
size = sizeof(uint8_t);
break;
default:
free(matrix);
return NULL;
}

matrix->data = malloc(matrix->header.mrows * matrix->header.ncols * size);
fread(matrix->data, size, matrix->header.mrows*matrix->header.ncols, file);

return matrix;
}

void freeMatVer4Matrix_4(MatVer4Matrix** matrix)
{
if (*matrix)
{
if ((*matrix)->data)
free((*matrix)->data);
free(*matrix);
*matrix = NULL;
}
}

int skipMatVer4Matrix_4(FILE* file)
{
MatVer4Header header;
fread(&header, sizeof(MatVer4Header), 1, file);

// skip name
fseek(file, header.namelen, SEEK_CUR);

size_t size;
switch (header.type % 100)
{
case MatVer4Type_DOUBLE:
size = sizeof(double);
break;
case MatVer4Type_SINGLE:
size = sizeof(float);
break;
case MatVer4Type_INT32:
size = sizeof(int32_t);
break;
case MatVer4Type_CHAR:
size = sizeof(uint8_t);
break;
default:
return -1;
}

// skip data
fseek(file, header.mrows*header.ncols*size, SEEK_CUR);
return 0;
}

#ifdef __cplusplus
}
#endif

0 comments on commit a981b37

Please sign in to comment.