Skip to content

Commit

Permalink
- fix for fmu with nonlinear systems, call allocateNonlinearSystem be…
Browse files Browse the repository at this point in the history
…fore simulate

- fixes to compile fmu with visual studio

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13861 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Nov 11, 2012
1 parent 09dcbcf commit b0a564a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 22 deletions.
83 changes: 63 additions & 20 deletions SimulationRuntime/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,27 @@ ENDMACRO(BUILDMODELMOS)
# MACRO BUILDMODEL
MACRO(BUILDMODELFMU model dir Flags CSRC)

IF(WIN32)
SET(COPY copy)
else(WIN32)
SET(COPY cp)
endif(WIN32)

IF(SUNDIALS_INCLUDE_DIR AND
SUNDIALS_LIBRARY_CVODE AND
SUNDIALS_LIBRARY_IDA AND
SUNDIALS_LIBRARY_NVEC AND
SUNDIALS_KINSOL)

INCLUDE_DIRECTORIES(${SUNDIALS_INCLUDE_DIR})
SET(SUNDIALS_LIBRARIES ${SUNDIALS_LIBRARY_CVODE} ${SUNDIALS_LIBRARY_IDA} ${SUNDIALS_LIBRARY_NVEC} ${SUNDIALS_KINSOL})

ENDIF(SUNDIALS_INCLUDE_DIR AND
SUNDIALS_LIBRARY_CVODE AND
SUNDIALS_LIBRARY_IDA AND
SUNDIALS_LIBRARY_NVEC AND
SUNDIALS_KINSOL)

# includes
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c/linearization)
Expand Down Expand Up @@ -327,9 +348,9 @@ MACRO(BUILDMODELFMU model dir Flags CSRC)
# target fuer OM_OUTPUT
ADD_CUSTOM_TARGET(${model}codegen ALL DEPENDS ${OMC_OUTPUT})

SET(OMC_FMU_CODE ${OMCTRUNCHOME}/c_runtime/fmu_model_interface.h
${OMCTRUNCHOME}/c_runtime/fmiModelFunctions.h
${OMCTRUNCHOME}/c_runtime/fmiModelTypes.h)
SET(OMC_FMU_CODE ${OMCTRUNCHOME}/SimulationRuntime/fmi/export/fmu_model_interface.h
${OMCTRUNCHOME}/SimulationRuntime/fmi/export/fmiModelFunctions.h
${OMCTRUNCHOME}/SimulationRuntime/fmi/export/fmiModelTypes.h)

ADD_LIBRARY(${model} SHARED ${OMC_OUTPUT} ${CSRC} ${OMC_FMU_CODE})
TARGET_LINK_LIBRARIES(${model} simulation util math-support results solver f2c meta ModelicaExternalC libexpat initialization)
Expand All @@ -347,6 +368,27 @@ ENDMACRO(BUILDMODELFMU)
# MACRO BUILDMODEL
MACRO(BUILDMODELFMUMOS model mos Flags CSRC)

IF(WIN32)
SET(COPY copy)
else(WIN32)
SET(COPY cp)
endif(WIN32)

IF(SUNDIALS_INCLUDE_DIR AND
SUNDIALS_LIBRARY_CVODE AND
SUNDIALS_LIBRARY_IDA AND
SUNDIALS_LIBRARY_NVEC AND
SUNDIALS_KINSOL)

INCLUDE_DIRECTORIES(${SUNDIALS_INCLUDE_DIR})
SET(SUNDIALS_LIBRARIES ${SUNDIALS_LIBRARY_CVODE} ${SUNDIALS_LIBRARY_IDA} ${SUNDIALS_LIBRARY_NVEC} ${SUNDIALS_KINSOL})

ENDIF(SUNDIALS_INCLUDE_DIR AND
SUNDIALS_LIBRARY_CVODE AND
SUNDIALS_LIBRARY_IDA AND
SUNDIALS_LIBRARY_NVEC AND
SUNDIALS_KINSOL)

# includes
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c/linearization)
Expand All @@ -358,8 +400,8 @@ MACRO(BUILDMODELFMUMOS model mos Flags CSRC)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c/simulation/libf2c)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c/simulation/results)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c/simulation/solver)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/simulation/solver/initialization)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c/util)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c/simulation/solver/initialization)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/c/util)
INCLUDE_DIRECTORIES(${OMCTRUNCHOME}/SimulationRuntime/fmi/export)

# OMDEV PATH
Expand All @@ -370,33 +412,34 @@ MACRO(BUILDMODELFMUMOS model mos Flags CSRC)
INCLUDE_DIRECTORIES(${OMDEV}/lib/expat-win32-msvc)
link_directories(${OMDEV}/lib/expat-win32-msvc)

SET(OMC_MODELNAME ${model})
STRING(REPLACE "." "_" FMU_MODELNAME ${model})
# generate model.mos

# Variablen fuer openmodelica2sarturis
SET(OMC_CODE ${CMAKE_CURRENT_BINARY_DIR}/${model}.c
${CMAKE_CURRENT_BINARY_DIR}/${model}_functions.c
${CMAKE_CURRENT_BINARY_DIR}/${model}_init.txt)
SET(OMC_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${model}.c
${CMAKE_CURRENT_BINARY_DIR}/${model}_FMU.c
# ${CMAKE_CURRENT_BINARY_DIR}/${model}_functions.cpp
${CMAKE_CURRENT_BINARY_DIR}/${model}_functions.h
${CMAKE_CURRENT_BINARY_DIR}/${model}_records.c)
SET(OMC_CODE ${CMAKE_CURRENT_BINARY_DIR}/${FMU_MODELNAME}.c
${CMAKE_CURRENT_BINARY_DIR}/${FMU_MODELNAME}_functions.c
${CMAKE_CURRENT_BINARY_DIR}/${FMU_MODELNAME}_init.txt)
SET(OMC_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${FMU_MODELNAME}.c
${CMAKE_CURRENT_BINARY_DIR}/${FMU_MODELNAME}_FMU.c
# ${CMAKE_CURRENT_BINARY_DIR}/${FMU_MODELNAME}_functions.cpp
${CMAKE_CURRENT_BINARY_DIR}/${FMU_MODELNAME}_functions.h
${CMAKE_CURRENT_BINARY_DIR}/${FMU_MODELNAME}_records.c)
# custom command fuer openmodelicacompiler
ADD_CUSTOM_COMMAND(OUTPUT ${OMC_OUTPUT}
COMMAND ${OMC_DEBUG} ${Flags} ${mos}
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
COMMENT "Erzeuge Code fuer ${model} with ${OMC_DEBUG}")
COMMENT "Erzeuge Code fuer ${model} with ${OMC_DEBUG} in ${CMAKE_CURRENT_BINARY_DIR}")
# target fuer OM_OUTPUT
ADD_CUSTOM_TARGET(${model}codegen ALL DEPENDS ${OMC_OUTPUT})

SET(OMC_FMU_CODE ${OMCTRUNCHOME}/c_runtime/fmu_model_interface.h
${OMCTRUNCHOME}/c_runtime/fmiModelFunctions.h
${OMCTRUNCHOME}/c_runtime/fmiModelTypes.h)
SET(OMC_FMU_CODE ${OMCTRUNCHOME}/SimulationRuntime/fmi/export/fmu_model_interface.h
${OMCTRUNCHOME}/SimulationRuntime/fmi/export/fmiModelFunctions.h
${OMCTRUNCHOME}/SimulationRuntime/fmi/export/fmiModelTypes.h)

ADD_DEFINITIONS(/TP ${model}.c)
ADD_DEFINITIONS(/TP ${FMU_MODELNAME}.c)
set_source_files_properties(${OMC_OUTPUT} PROPERTIES LANGUAGE CXX)
ADD_LIBRARY(${model} SHARED ${OMC_OUTPUT} ${CSRC} ${OMC_FMU_CODE})
TARGET_LINK_LIBRARIES(${model} simulation util math-support results solver f2c meta ModelicaExternalC libexpat initialization)
TARGET_LINK_LIBRARIES(${model} simulation util math-support results solver meta f2c ModelicaExternalC libexpat initialization ${SUNDIALS_LIBRARIES})

# Dependencies
ADD_DEPENDENCIES(${model} ${model}codegen)
Expand Down
1 change: 1 addition & 0 deletions SimulationRuntime/c/openmodelica_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ extern "C" {
#include "nonlinearSystem.h"

/* DATA* initializeDataStruc(); */ /*create in model code */
extern void setupDataStruc(DATA *data);
extern void setupDataStruc2(DATA *data);

/* Function for calling external object constructors */
Expand Down
6 changes: 6 additions & 0 deletions SimulationRuntime/c/simulation/solver/events.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
#include <stdlib.h>
#include <string.h>

#ifdef __cplusplus
extern "C" {
#endif

double bisection(DATA* data, double*, double*, double*, double*, LIST*, LIST*);
modelica_boolean checkZeroCrossings(DATA *data, LIST *list, LIST*);
Expand Down Expand Up @@ -768,3 +771,6 @@ void saveZeroCrossingsAfterEvent(DATA* data)
}
}

#ifdef __cplusplus
}
#endif
8 changes: 8 additions & 0 deletions SimulationRuntime/c/simulation/solver/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#include "solver_main.h"
#include "list.h"

#ifdef __cplusplus
extern "C" {
#endif

modelica_boolean sample(DATA *data, double start, double interval, int hindex);
void initSample(DATA *data, double start, double stop);
modelica_boolean activateSampleEvents(DATA *data);
Expand All @@ -54,4 +58,8 @@ int handleSampleEvent(DATA* data);

void findRoot(DATA *data, LIST *eventList, double*);

#ifdef __cplusplus
}
#endif

#endif
2 changes: 1 addition & 1 deletion SimulationRuntime/c/util/real_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ void usub_real_array(real_array_t* a)
}
}

void usub_alloc_real_array(real_array_t* a, real_array_t* dest)
void usub_alloc_real_array(const real_array_t* a, real_array_t* dest)
{
size_t nr_of_elements, i;
clone_real_array_spec(a,dest);
Expand Down
2 changes: 1 addition & 1 deletion SimulationRuntime/c/util/real_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ extern void add_real_array(const real_array_t * a, const real_array_t * b, real_

/* Unary subtraction */
extern void usub_real_array(real_array_t* a);
extern void usub_alloc_real_array(real_array_t* a, real_array_t* dest);
extern void usub_alloc_real_array(const real_array_t* a, real_array_t* dest);
extern void sub_real_array(const real_array_t * a, const real_array_t * b, real_array_t* dest);
extern void sub_alloc_real_array(const real_array_t * a, const real_array_t * b, real_array_t* dest);

Expand Down
4 changes: 4 additions & 0 deletions SimulationRuntime/fmi/export/fmu_model_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ fmiStatus fmiInitialize(fmiComponent c, fmiBoolean toleranceControlled, fmiReal
//input_function(comp->fmuData);
/* initial sample and delay before initial the system */
callExternalObjectConstructors(comp->fmuData);
/* allocate memory for non-linear system solvers */
allocateNonlinearSystem(comp->fmuData);
/*TODO: Simulation stop time is need to calculate in before hand all sample events
We shouldn't generate them all in beforehand */
initSample(comp->fmuData, comp->fmuData->localData[0]->timeValue, 100 /*should be stopTime*/);
Expand Down Expand Up @@ -647,6 +649,8 @@ fmiStatus fmiTerminate(fmiComponent c){

/* deinitDelay(comp->fmuData); */
callExternalObjectDestructors(comp->fmuData);
/* free nonlinear system data */
freeNonlinearSystem(comp->fmuData);
deInitializeDataStruc(comp->fmuData);
comp->functions.freeMemory(comp->fmuData);

Expand Down

0 comments on commit b0a564a

Please sign in to comment.