Skip to content

Commit

Permalink
- Made result-file interface pure C instead of C++
Browse files Browse the repository at this point in the history
- Matlab code still uses a C++ implementation but csv and plt now use only C-code


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@15182 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 15, 2013
1 parent 6c5da28 commit 55ffa29
Show file tree
Hide file tree
Showing 16 changed files with 766 additions and 936 deletions.
4 changes: 2 additions & 2 deletions SimulationRuntime/c/Makefile.common
Expand Up @@ -98,8 +98,8 @@ $(UTILOBJSPATH):%.o: %.c $(UTILHFILESPATH) $(COMMON_HEADERS)
$(MATHOBJSPATH):%.o: %.c $(MATHHFILESPATH) $(COMMON_HEADERS)
$(CC) -c $(CFLAGS) $(FPMATHFORTRAN) -o $@ $< -I$(MATHPATH) -I$(UTILPATH)

$(SOLVEROBJSPATH):%.o: %.c $(SOLVERHFILESPATH) $(INITIALIZATIONHFILESPATH) $(COMMON_HEADERS)
$(CC) -c $(CFLAGS) -o $@ $< -I$(SOLVERPATH) -I$(INITIALIZATIONPATH) -I$(SIMPATH) -I$(MATHPATH) -I$(UTILPATH)
$(SOLVEROBJSPATH):%.o: %.c $(SOLVERHFILESPATH) $(RESULTSHFILESPATH) $(INITIALIZATIONHFILESPATH) $(COMMON_HEADERS)
$(CC) -c $(CFLAGS) -o $@ $< -I$(SOLVERPATH) -I$(INITIALIZATIONPATH) -I$(RESULTSPATH) -I$(SIMPATH) -I$(MATHPATH) -I$(UTILPATH)

$(INITIALIZATIONOBJSPATH):%.o: %.c $(SOLVERHFILESPATH) $(INITIALIZATIONHFILESPATH) $(COMMON_HEADERS)
$(CC) -c $(CFLAGS) -o $@ $< -I$(SOLVERPATH) -I$(INITIALIZATIONPATH) -I$(SIMPATH) -I$(MATHPATH) -I$(UTILPATH)
Expand Down
47 changes: 47 additions & 0 deletions SimulationRuntime/c/simulation/results/simulation_result.c
@@ -0,0 +1,47 @@
/*
* 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 THE BSD NEW LICENSE OR THE
* GPL VERSION 3 LICENSE OR THE 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 OSMC (Open Source Modelica Consortium)
* Public License (OSMC-PL) are obtained from OSMC, either from the above
* address, from the URLs: http://www.openmodelica.org or
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
* distribution. GNU version 3 is obtained from:
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
* http://www.opensource.org/licenses/BSD-3-Clause.
*
* 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.
*
*/

#include "simulation_result.h"

static void sim_result_doNothing(simulation_result* self, DATA *data)
{
/* Do nothing */
}

simulation_result sim_result = {
NULL, /* filename */
0, /* numpoints */
0, /* cpuTime */
NULL, /* extra data */
sim_result_doNothing, /* init */
sim_result_doNothing, /* emit */
sim_result_doNothing, /* writeParam */
sim_result_doNothing, /* free */
};
95 changes: 39 additions & 56 deletions SimulationRuntime/c/simulation/results/simulation_result.h
@@ -1,39 +1,30 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, Linköping University,
* Department of Computer and Information Science,
* 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
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, 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.
*
*/

/*
* File: simulation_runtime.h
*
* Description: This file is a C++ header file for the simulation runtime.
* It contains a prototype for the simulation result interface.
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
* GPL VERSION 3 LICENSE OR THE 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 OSMC (Open Source Modelica Consortium)
* Public License (OSMC-PL) are obtained from OSMC, either from the above
* address, from the URLs: http://www.openmodelica.org or
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
* distribution. GNU version 3 is obtained from:
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
* http://www.opensource.org/licenses/BSD-3-Clause.
*
* 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.
*
*/

Expand All @@ -42,34 +33,26 @@

#include "simulation_data.h"

class SimulationResultBaseException {};
class SimulationResultFileOpenException : SimulationResultBaseException {};
class SimulationResultFileCloseException : SimulationResultBaseException {};
class SimulationResultMallocException : SimulationResultBaseException {};
class SimulationResultReallocException : SimulationResultBaseException {};
#ifdef __cplusplus
extern "C" {
#endif /* cplusplus */

/*
* numpoints, maximum number of points that can be stored.
* nx number of states
* ny number of variables
* np number of parameters (not used in this impl.)
*/
class simulation_result
{
protected:
/* A prototype for the simulation result interface. */
typedef struct simulation_result {
const char *filename;
const long numpoints;
const DATA *data;
const int cpuTime;

public:
simulation_result(const char* filename, long numpoints, const DATA* data, int cpuTime) : filename(filename), numpoints(numpoints), data(data), cpuTime(cpuTime) {};
virtual ~simulation_result() {};
virtual void emit() = 0;

/* write the parameter data after updateBoundParameters is called */
virtual void writeParameterData() = 0;
virtual const char* result_type() = 0;
};
long numpoints;
int cpuTime;
void *storage; /* Internal data used for each storage scheme */
void (*init)(struct simulation_result*,DATA*);
void (*emit)(struct simulation_result*,DATA*);
void (*writeParameterData)(struct simulation_result*,DATA*);
void (*free)(struct simulation_result*,DATA*);
} simulation_result;

extern simulation_result sim_result;

#ifdef __cplusplus
}
#endif /* cplusplus */

#endif
@@ -1,30 +1,30 @@
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2010, Linköpings University,
* Department of Computer and Information Science,
* 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 THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
* GPL VERSION 3 LICENSE OR THE 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 OSMC (Open Source Modelica Consortium)
* Public License (OSMC-PL) are obtained from OSMC, either from the above
* address, from the URLs: http://www.openmodelica.org or
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
* distribution. GNU version 3 is obtained from:
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
* http://www.opensource.org/licenses/BSD-3-Clause.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* 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.
* 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.
*
*/

Expand All @@ -45,38 +45,37 @@
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <limits> /* adrpo - for std::numeric_limits in MSVC */
#include <sstream>
#include <time.h>


void simulation_result_csv::emit()
void csv_emit(simulation_result *self, DATA *data)
{
FILE *fout = (FILE*) self->storage;
const char* format = "%.16g,";
const char* formatint = "%i,";
const char* formatbool = "%i,";
const char* formatstring = "\"%s\",";
int i;
modelica_real value=0;
rt_tick(SIM_TIMER_OUTPUT);

rt_accumulate(SIM_TIMER_TOTAL);
static double cpu_offset = rt_accumulated(SIM_TIMER_TOTAL); /* ??? */
double cpuTimeValue = rt_accumulated(SIM_TIMER_TOTAL) - cpu_offset;
double cpuTimeValue = rt_accumulated(SIM_TIMER_TOTAL);
rt_tick(SIM_TIMER_TOTAL);

fprintf(fout, format, data->localData[0]->timeValue);
if(cpuTime)
if (self->cpuTime)
fprintf(fout, format, cpuTimeValue);
for(int i = 0; i < data->modelData.nVariablesReal; i++) if(!data->modelData.realVarsData[i].filterOutput)
for(i = 0; i < data->modelData.nVariablesReal; i++) if(!data->modelData.realVarsData[i].filterOutput)
fprintf(fout, format, (data->localData[0])->realVars[i]);
for(int i = 0; i < data->modelData.nVariablesInteger; i++) if(!data->modelData.integerVarsData[i].filterOutput)
for(i = 0; i < data->modelData.nVariablesInteger; i++) if(!data->modelData.integerVarsData[i].filterOutput)
fprintf(fout, formatint, (data->localData[0])->integerVars[i]);
for(int i = 0; i < data->modelData.nVariablesBoolean; i++) if(!data->modelData.booleanVarsData[i].filterOutput)
for(i = 0; i < data->modelData.nVariablesBoolean; i++) if(!data->modelData.booleanVarsData[i].filterOutput)
fprintf(fout, formatbool, (data->localData[0])->booleanVars[i]);
for(int i = 0; i < data->modelData.nVariablesString; i++) if(!data->modelData.stringVarsData[i].filterOutput)
for(i = 0; i < data->modelData.nVariablesString; i++) if(!data->modelData.stringVarsData[i].filterOutput)
fprintf(fout, formatstring, (data->localData[0])->stringVars[i]);

for(int i = 0; i < data->modelData.nAliasReal; i++) if(!data->modelData.realAlias[i].filterOutput){
for(i = 0; i < data->modelData.nAliasReal; i++) if(!data->modelData.realAlias[i].filterOutput) {
if(data->modelData.realAlias[i].aliasType == 2)
value = (data->localData[0])->timeValue;
else
Expand All @@ -86,59 +85,63 @@ void simulation_result_csv::emit()
else
fprintf(fout, format, value);
}
for(int i = 0; i < data->modelData.nAliasInteger; i++) if(!data->modelData.integerAlias[i].filterOutput){
for(i = 0; i < data->modelData.nAliasInteger; i++) if(!data->modelData.integerAlias[i].filterOutput) {
if(data->modelData.integerAlias[i].negate)
fprintf(fout, formatint, -(data->localData[0])->integerVars[data->modelData.integerAlias[i].nameID]);
else
fprintf(fout, formatint, (data->localData[0])->integerVars[data->modelData.integerAlias[i].nameID]);
}
for(int i = 0; i < data->modelData.nAliasBoolean; i++) if(!data->modelData.booleanAlias[i].filterOutput){
for(i = 0; i < data->modelData.nAliasBoolean; i++) if(!data->modelData.booleanAlias[i].filterOutput) {
if(data->modelData.booleanAlias[i].negate)
fprintf(fout, formatbool, (data->localData[0])->booleanVars[data->modelData.booleanAlias[i].nameID]==1?0:1);
else
fprintf(fout, formatbool, (data->localData[0])->booleanVars[data->modelData.booleanAlias[i].nameID]);
}
for(int i = 0; i < data->modelData.nAliasString; i++) if(!data->modelData.stringAlias[i].filterOutput)
for(i = 0; i < data->modelData.nAliasString; i++) if(!data->modelData.stringAlias[i].filterOutput) {
/* there would no negation of a string happen */
fprintf(fout, formatstring, (data->localData[0])->stringVars[data->modelData.stringAlias[i].nameID]);
}
fprintf(fout, "\n");
rt_accumulate(SIM_TIMER_OUTPUT);
}

simulation_result_csv::simulation_result_csv(const char* filename, long numpoints, const DATA* data, int cpuTime) : simulation_result(filename, numpoints, data, cpuTime)
void csv_init(simulation_result *self, DATA *data)
{
int i;
const MODEL_DATA *mData = &(data->modelData);

const char* format = "\"%s\",";
fout = fopen(filename, "w");
FILE *fout = fopen(self->filename, "w");

ASSERT2(fout, "Error, couldn't create output file: [%s] because of %s", filename, strerror(errno));
ASSERT2(fout, "Error, couldn't create output file: [%s] because of %s", self->filename, strerror(errno));

fprintf(fout, format, "time");
if(cpuTime)
if (self->cpuTime)
fprintf(fout, format, "$cpuTime");
for(int i = 0; i < mData->nVariablesReal; i++) if(!mData->realVarsData[i].filterOutput)
for(i = 0; i < mData->nVariablesReal; i++) if(!mData->realVarsData[i].filterOutput)
fprintf(fout, format, mData->realVarsData[i].info.name);
for(int i = 0; i < mData->nVariablesInteger; i++) if(!mData->integerVarsData[i].filterOutput)
for(i = 0; i < mData->nVariablesInteger; i++) if(!mData->integerVarsData[i].filterOutput)
fprintf(fout, format, mData->integerVarsData[i].info.name);
for(int i = 0; i < mData->nVariablesBoolean; i++) if(!mData->booleanVarsData[i].filterOutput)
for(i = 0; i < mData->nVariablesBoolean; i++) if(!mData->booleanVarsData[i].filterOutput)
fprintf(fout, format, mData->booleanVarsData[i].info.name);
for(int i = 0; i < mData->nVariablesString; i++) if(!mData->stringVarsData[i].filterOutput)
for(i = 0; i < mData->nVariablesString; i++) if(!mData->stringVarsData[i].filterOutput)
fprintf(fout, format, mData->stringVarsData[i].info.name);

for(int i = 0; i < mData->nAliasReal; i++) if(!mData->realAlias[i].filterOutput)
for(i = 0; i < mData->nAliasReal; i++) if(!mData->realAlias[i].filterOutput)
fprintf(fout, format, mData->realAlias[i].info.name);
for(int i = 0; i < mData->nAliasInteger; i++) if(!mData->integerAlias[i].filterOutput)
for(i = 0; i < mData->nAliasInteger; i++) if(!mData->integerAlias[i].filterOutput)
fprintf(fout, format, mData->integerAlias[i].info.name);
for(int i = 0; i < mData->nAliasBoolean; i++) if(!mData->booleanAlias[i].filterOutput)
for(i = 0; i < mData->nAliasBoolean; i++) if(!mData->booleanAlias[i].filterOutput)
fprintf(fout, format, mData->booleanAlias[i].info.name);
for(int i = 0; i < mData->nAliasString; i++) if(!mData->stringAlias[i].filterOutput)
for(i = 0; i < mData->nAliasString; i++) if(!mData->stringAlias[i].filterOutput)
fprintf(fout, format, mData->stringAlias[i].info.name);
fprintf(fout,"\n");
self->storage = fout;
}

simulation_result_csv::~simulation_result_csv()
void csv_free(simulation_result *self, DATA *data)
{
FILE *fout = (FILE*) self->storage;
rt_tick(SIM_TIMER_OUTPUT);
fclose(fout);
rt_accumulate(SIM_TIMER_OUTPUT);
Expand Down

0 comments on commit 55ffa29

Please sign in to comment.