Skip to content

Commit

Permalink
- changed solver_main to c
Browse files Browse the repository at this point in the history
- added a lot of bugfixes


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@10473 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Nov 12, 2011
1 parent 59171cd commit 5c9bf5a
Show file tree
Hide file tree
Showing 17 changed files with 1,539 additions and 1,469 deletions.
2 changes: 1 addition & 1 deletion Compiler/susan_codegen/SimCode/CodegenC.tpl
Expand Up @@ -944,7 +944,7 @@ template functionStoreDelayed(DelayedExpression delayed)
&preExp /*BUFC*/, &varDecls /*BUFD*/)
<<
<%preExp%>
storeDelayedExpression(<%id%>, <%eRes%>);<%\n%>
storeDelayedExpression(<%id%>, <%eRes%>, time);<%\n%>
>>
))
<<
Expand Down
5 changes: 3 additions & 2 deletions SimulationRuntime/c/Makefile.common
Expand Up @@ -14,7 +14,7 @@ FFLAGS = -O -fexceptions

include Makefile.objs

LIBS = libsim.a libsolver.a libmath-support.a libutil.a libmeta.a libf2c.a libresult.a libModelicaExternalC.a libsendData.a
LIBS = libsim.a libsolver.a libmath-support.a libutil.a libmeta.a libf2c.a libresult.a libModelicaExternalC.a libsendData.a libModelicaExternalC.a

LIBSPATH= simulation/libsim.a math-support/libmath-support.a simulation/solver/libsolver.a simulation/results/libresult.a meta/libmeta.a util/libutil.a

Expand Down Expand Up @@ -53,11 +53,12 @@ libModelicaExternalC.a:
$(MAKE) -C ModelicaExternalC -f ${LIBMAKEFILE}

install: libSimulationRuntimeC.a $(LIBS) omdevinstall
cp libSimulationRuntimeC.a $(builddir_lib)
cp ./libSimulationRuntimeC.a $(builddir_lib)
cp ./ModelicaExternalC/libModelicaExternalC.a $(builddir_lib)
cp ./ModelicaExternalC/ModelicaUtilities.h $(builddir_inc)
cp ./openmodelica.h $(builddir_inc)
cp ./openmodelica_func.h $(builddir_inc)
cp ./inline.h $(builddir_inc)
cp ../interactive/libinteractive.a $(builddir_lib)
cp ./meta/*.h $(builddir_inc)
cp ./util/*.h $(builddir_inc)
Expand Down
10 changes: 6 additions & 4 deletions SimulationRuntime/c/ModelicaExternalC/Makefile.common
Expand Up @@ -4,14 +4,16 @@
# If you need to add files, etc, you modify Makefile.common - a common file
# for both UNIX/Linux and Windows platforms.

OBJS = ModelicaInternal.o ModelicaStrings.o ModelicaTablesImpl.o ModelicaUtilities.o tables.o
AR = ar -ru

include ../Makefile.objs

all: libModelicaExternalC.a

libModelicaExternalC.a : $(OBJS)
$(AR) $@ $(OBJS)
libModelicaExternalC.a : $(MOEXTC_OBJS)
$(AR) $@ $(MOEXTC_OBJS)
ranlib $@

clean:
rm -f libModelicaExternalC.a $(OBJS)
rm -f libModelicaExternalC.a $(MOEXTC_OBJS)

1 change: 0 additions & 1 deletion SimulationRuntime/c/ModelicaExternalC/Makefile.in
Expand Up @@ -8,7 +8,6 @@ CC = @CC@
CXX = @CXX@
CFLAGS := @CFLAGS@ -I../util
CPPFLAGS := @CPPFLAGS@ -I.. -Dlinux -Dstatic=
AR = ar -ru

include Makefile.common

Expand Down
29 changes: 10 additions & 19 deletions SimulationRuntime/c/math-support/delay.c
Expand Up @@ -54,7 +54,7 @@ typedef struct _ExpressionDelayBuffer
}t_ExpressionDelayBuffer;

/* the delayStructure looks like a matrix (rows = expressionNumber+currentColumnIndex, columns={time, value}) */
RINGBUFFER **delayStructure;
static RINGBUFFER **delayStructure;

extern const int numDelayExpressionIndex;

Expand All @@ -68,9 +68,10 @@ void initDelay(double startTime)
/* allocate the memory for rows */
delayStructure = (RINGBUFFER**)calloc(numDelayExpressionIndex, sizeof(RINGBUFFER*));
ASSERT(delayStructure, "out of memory");
for(i=0; i<numDelayExpressionIndex; i++){
delayStructure[i] = allocRingBuffer(1024, sizeof(t_TimeAndValue));
}

for(i=0; i<numDelayExpressionIndex; i++)
delayStructure[i] = allocRingBuffer(1024, sizeof(t_TimeAndValue));

DEBUG_INFO(LV_SOLVER, "initDelay called with startTime = %f\n", startTime);
}
Expand Down Expand Up @@ -112,23 +113,13 @@ void storeDelayedExpression(int exprNumber, double exprValue, double time)
{
t_TimeAndValue tpl;

if(exprNumber < 0)
{
/* ERROR("storeDelayedExpression: Invalid expression number %d", exprNumber); */
return;
}


if(time < tStart)
{
/* ERROR("storeDelayedExpression: Time is smaller than starting time. Value ignored"); */
return;
}

/* ERROR("storeDelayed[%d] %g:%g", exprNumber, time, exprValue); */
/* INFO("storeDelayed[%d] %g:%g", exprNumber, time, exprValue); */

/* Allocate more space for expressions */
ASSERT(exprNumber < numDelayExpressionIndex, "exprNumber < numDelayExpressionIndex");
ASSERT(exprNumber < numDelayExpressionIndex, "storeDelayedExpression: Invalid expression number %d", exprNumber);
ASSERT(exprNumber >= 0,"storeDelayedExpression: Invalid expression number %d", exprNumber);

ASSERT(time >= tStart,"storeDelayedExpression: Time is smaller than starting time. Value ignored");

tpl.time = time;
tpl.value = exprValue;
Expand All @@ -143,7 +134,7 @@ double delayImpl(int exprNumber, double exprValue, double time, double delayTime
/* ERROR("delayImpl: exprNumber = %d, exprValue = %lf, time = %lf, delayTime = %lf", exprNumber, exprValue, time, delayTime); */

/* Check for errors */
ASSERT(0 <= exprNumber, "0 <= exprNumber");
ASSERT(0 <= exprNumber, "0 > exprNumber = %d ",exprNumber);
ASSERT(exprNumber < numDelayExpressionIndex, "exprNumber < numDelayExpressionIndex");

if(time <= tStart)
Expand Down
11 changes: 11 additions & 0 deletions SimulationRuntime/c/math-support/ringbuffer.c
Expand Up @@ -48,13 +48,16 @@ struct RINGBUFFER
RINGBUFFER *allocRingBuffer(int bufferSize, int itemSize)
{
RINGBUFFER *rb = (RINGBUFFER*)malloc(sizeof(RINGBUFFER));
ASSERT(rb, "out of memory");

rb->firstElement = 0;
rb->nElements = 0;
rb->bufferSize = bufferSize > 0 ? bufferSize : 1;
rb->itemSize = itemSize;
rb->buffer = calloc(rb->bufferSize, rb->itemSize);
ASSERT(rb->buffer, "out of memory");

return rb;
}

void freeRingBuffer(RINGBUFFER *rb)
Expand Down Expand Up @@ -115,3 +118,11 @@ void rotateRingBuffer(RINGBUFFER *rb, int n)
ASSERT(0 <= n, "index out of range");
rb->firstElement = (rb->firstElement+n)%rb->bufferSize;
}

void infoRingBuffer(RINGBUFFER *rb)
{
INFO("size of one item in bytes: %d",rb->itemSize);
INFO("position of first element in buffer: %d",rb->firstElement);
INFO("number of elements in buffer: %d",rb->nElements);
INFO("number of elements which could be stored in buffer %d",rb->bufferSize);
}
2 changes: 2 additions & 0 deletions SimulationRuntime/c/math-support/ringbuffer.h
Expand Up @@ -56,6 +56,8 @@ extern "C" {

void rotateRingBuffer(RINGBUFFER *rb, int n);

void infoRingBuffer(RINGBUFFER *rb);

#ifdef __cplusplus
}
#endif
Expand Down
39 changes: 27 additions & 12 deletions SimulationRuntime/c/simulation/simulation_runtime.cpp
Expand Up @@ -100,6 +100,7 @@ int modelErrorCode = 0; // set by model calculations. Can be transferred to num.

const std::string *init_method = NULL; // method for initialization.


/* this is the globalData that is used in all the functions */
DATA *globalData = 0;

Expand Down Expand Up @@ -157,7 +158,6 @@ newTime(double t, double step, double stop)
globalData->forceEmit = 1;
}
}
else
{
newTime = (floor((t + 1e-10) / step) + 1.0) * step;
globalData->lastEmittedTime = newTime;
Expand Down Expand Up @@ -664,7 +664,7 @@ startNonInteractiveSimulation(int argc, char**argv)
result_file_cstr = *result_file;
}

retVal = callSolver(argc, argv, method, outputFormat, result_file_cstr, start, stop, stepSize,
retVal = callSolver(method, outputFormat, result_file_cstr, start, stop, stepSize,
outputSteps, tolerance);

if (retVal == 0 && create_linearmodel) {
Expand Down Expand Up @@ -698,7 +698,7 @@ startNonInteractiveSimulation(int argc, char**argv)
* "dopri5" calls an embedded DOPRI5(4)-solver with stepsize control
*/
int
callSolver(int argc, char**argv, string method, string outputFormat,
callSolver(string method, string outputFormat,
string result_file_cstr,
double start, double stop, double stepSize, long outputSteps,
double tolerance)
Expand Down Expand Up @@ -728,39 +728,39 @@ callSolver(int argc, char**argv, string method, string outputFormat,
if (sim_verbose >= LOG_SOLVER) {
cout << "No solver is set, using dassl." << endl; fflush(NULL);
}
retVal = solver_main(argc,argv,start,stop,stepSize,outputSteps,tolerance,3);
retVal = solver_main(start, stop, stepSize, outputSteps, tolerance, 3);
} else if (method == std::string("euler")) {
if (sim_verbose >= LOG_SOLVER) {
cout << "Recognized solver: " << method << "." << endl; fflush(NULL);
}
retVal = solver_main(argc, argv, start, stop, stepSize, outputSteps, tolerance, 1);
retVal = solver_main(start, stop, stepSize, outputSteps, tolerance, 1);
} else if (method == std::string("rungekutta")) {
if (sim_verbose >= LOG_SOLVER) {
cout << "Recognized solver: " << method << "." << endl; fflush(NULL);
}
retVal = solver_main(argc, argv, start, stop, stepSize, outputSteps, tolerance, 2);
retVal = solver_main(start, stop, stepSize, outputSteps, tolerance, 2);
} else if (method == std::string("dassl") || method == std::string("dassl2")) {
if (sim_verbose >= LOG_SOLVER) {
cout << "Recognized solver: " << method << "." << endl; fflush(NULL);
}
retVal = solver_main(argc, argv, start, stop, stepSize, outputSteps, tolerance, 3);
retVal = solver_main(start, stop, stepSize, outputSteps, tolerance, 3);
} else if (method == std::string("dassljac")) {
if (sim_verbose >= LOG_SOLVER) {
cout << "Recognized solver: " << method << "." << endl; fflush(NULL);
}
jac_flag = 1;
retVal = solver_main(argc, argv, start, stop, stepSize, outputSteps, tolerance, 3);
retVal = solver_main(start, stop, stepSize, outputSteps, tolerance, 3);
} else if (method == std::string("dasslnum")) {
if (sim_verbose >= LOG_SOLVER) {
cout << "Recognized solver: " << method << "." << endl; fflush(NULL);
}
num_jac_flag = 1;
retVal = solver_main(argc, argv, start, stop, stepSize, outputSteps, tolerance, 3);
retVal = solver_main(start, stop, stepSize, outputSteps, tolerance, 3);
} else if (method == std::string("dopri5")) {
if (sim_verbose >= LOG_SOLVER) {
cout << "Recognized solver: " << method << "." << endl; fflush(NULL);
}
retVal = solver_main(argc, argv, start, stop, stepSize, outputSteps, tolerance, 6);
retVal = solver_main(start, stop, stepSize, outputSteps, tolerance, 6);
} else if (method == std::string("inline-euler")) {
if (!_omc_force_solver || std::string(_omc_force_solver) != std::string("inline-euler")) {
cout << "Recognized solver: " << method
Expand All @@ -771,7 +771,7 @@ callSolver(int argc, char**argv, string method, string outputFormat,
if (sim_verbose >= LOG_SOLVER) {
cout << "Recognized solver: " << method << "." << endl; fflush(NULL);
}
retVal = solver_main(argc, argv, start, stop, stepSize, outputSteps, tolerance, 4);
retVal = solver_main(start, stop, stepSize, outputSteps, tolerance, 4);
}
} else if (method == std::string("inline-rungekutta")) {
if (!_omc_force_solver || std::string(_omc_force_solver) != std::string("inline-rungekutta")) {
Expand All @@ -783,7 +783,7 @@ callSolver(int argc, char**argv, string method, string outputFormat,
if (sim_verbose >= LOG_SOLVER) {
cout << "Recognized solver: " << method << "." << endl; fflush(NULL);
}
retVal = solver_main(argc, argv, start, stop, stepSize, outputSteps, tolerance, 4);
retVal = solver_main(start, stop, stepSize, outputSteps, tolerance, 4);
}
#ifdef _OMC_QSS_LIB
} else if (method == std::string("qss")) {
Expand Down Expand Up @@ -1060,6 +1060,21 @@ initRuntimeAndSimulation(int argc, char**argv, _X_DATA *data)
num_jac_flag = flagSet("numjac", argc, argv);


const std::string* init_method = getFlagValue("im", argc, argv); /* get the old initialization-flag */
const std::string* init_initMethod = getFlagValue("iim", argc, argv); /* get the initialization method */
const std::string* init_optiMethod = getFlagValue("iom", argc, argv); /* get the optimization method for the initialization */

if (init_method) {
fprintf(stdout,
"Error: old flag: initialization-method [im] is rejected\n");
fprintf(stdout,
" new flag: init-initialization-method [iim] current options are: state or old\n");
fprintf(stdout,
" new flag: init-optimization-method [iom] current options are: simplex or newuoa\n");
return -1;
}


// ppriv - NO_INTERACTIVE_DEPENDENCY - for simpler debugging in Visual Studio
#ifndef NO_INTERACTIVE_DEPENDENCY
interactiveSimulation = flagSet("interactive", argc, argv);
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/simulation/simulation_runtime.h
Expand Up @@ -83,7 +83,7 @@ class TerminateSimulationException {

extern simulation_result *sim_result;
/* function with template for linear model */
int callSolver(int, char**, string, string, string, double, double, double, long, double);
int callSolver(string, string, string, double, double, double, long, double);

#ifndef NO_INTERACTIVE_DEPENDENCY
#include "../../../interactive/socket.h"
Expand Down
20 changes: 20 additions & 0 deletions SimulationRuntime/c/simulation/solver/Makefile.common
@@ -0,0 +1,20 @@
include ../../Makefile.objs

HFILES = \
simulation_inline_solver.h \
solver_main.h \
dopri45.h \

LIBS = libsolver.a

.PHONY : libsolver.a clean

all : libsolver.a

libsolver.a : $(SOLVER_OBJS) $(HFILES)
$(AR) $@ $(SOLVER_OBJS)
ranlib $@

clean :
rm -f libsolver.a $(SOLVER_OBJS)

20 changes: 1 addition & 19 deletions SimulationRuntime/c/simulation/solver/Makefile.in
Expand Up @@ -12,25 +12,7 @@ CPPFLAGS := @CPPFLAGS@ -Wall $(ANSI) $(EXTRA_CFLAGS) -I. -I.. -I../../ -I../../u
AR = ar -ru
FFLAGS = -O -fexceptions

OBJS = daux.o ddasrt.o ddassl.o dlamch.o dlinpk.o solver_main.o

HFILES = \
simulation_inline_solver.h \
solver_main.h \

LIBS = libsolver.a

.PHONY : libsolver.a clean

all : libsolver.a

libsolver.a : $(OBJS) $(HFILES)
$(AR) $@ $(OBJS)
ranlib $@

clean :
rm -f libsolver.a
rm -f $(OBJS)
include Makefile.common

Makefile: Makefile.in
cd ../../../../; ./config.status
Expand Down

0 comments on commit 5c9bf5a

Please sign in to comment.