Skip to content

Commit 18a7424

Browse files
committed
Improved support for FMI 2.0
1 parent b754153 commit 18a7424

38 files changed

+255
-230
lines changed

SourceCode/socket/utilSocket.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int save_appendFMU(char* *buffer, const char *toAdd, int *bufLen){
6060
// reallocate memory if needed
6161
if ( *bufLen < nNewCha + nBufCha + 1){
6262
*bufLen = *bufLen + size * (((nNewCha + nBufCha) / size)+1);
63-
*buffer = realloc(*buffer, *bufLen);
63+
*buffer = (char*)realloc(*buffer, *bufLen);
6464
if (*buffer == NULL) {
6565
perror("Realloc failed in save_appendFMU.");
6666
#ifdef NDEBUG
@@ -393,7 +393,7 @@ int writetosocketFMU(const int *sockfd,
393393
fprintf(f1, "Assembling buffer.\n", *sockfd);
394394
#endif
395395

396-
buffer = malloc(bufLen);
396+
buffer = (char *)malloc(bufLen);
397397
if (buffer == NULL) {
398398
perror("malloc failed in writetosocketFMU.");
399399
#ifdef NDEBUG

SourceCode/v20/EnergyPlus/defines.h

+4-5
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
#define RESOURCES "resources"
5252
#define XML_FILE "modelDescription.xml"
5353

54-
#include "fmi2TypesPlatform.h"
55-
#include "fmi2Functions.h"
56-
#include "XmlParserCApi.h"
54+
#include "../fmusdk-shared/include/fmi2TypesPlatform.h"
55+
#include "../fmusdk-shared/include/fmi2Functions.h"
56+
#include "../fmusdk-shared/parser/XmlParserCApi.h"
5757

5858

5959
/* Export fmi functions on Windows */
@@ -62,11 +62,10 @@
6262
#else
6363
#define DllExport
6464
#endif
65-
//#include "xml_parser_cosim.h"
6665

6766
typedef struct ModelInstance {
6867
int index;
69-
fmi2CallbackFunctions* functions;
68+
const fmi2CallbackFunctions* functions;
7069
char instanceName[MAX_VARNAME_LEN];
7170
char cwd[256];
7271
char in_file_name[100];

SourceCode/v20/EnergyPlus/main.c

+143-121
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,22 @@
2323
#define MAXHOSTNAME 10000
2424
#define MAX_MSG_SIZE 1000
2525
#define MAXBUFFSIZE 1000
26+
//#define LIBXML_STATIC 1
27+
2628

2729
#include <stdio.h>
2830
#include <stdlib.h>
2931
#include <string.h>
30-
#include "util.h"
31-
#include "utilSocket.h"
32+
#include "../../utility/util.h"
33+
#include "../../socket/utilSocket.h"
3234
#include "defines.h"
33-
//#include "reader.h"
3435
#include <errno.h>
3536
#include <sys/stat.h>
3637

3738
// Addition for FMI 2.0 support
38-
#include "fmi2.h"
39-
#include "sim_support.h"
40-
#include "xmlVersionParser.h"
39+
#include "../fmusdk-shared/fmi2.h"
40+
#include "../fmusdk-shared/sim_support.h"
41+
#include "../fmusdk-shared/xmlVersionParser.h"
4142

4243
#ifdef _MSC_VER
4344
#pragma comment(lib, "legacy_stdio_definitions.lib")
@@ -69,7 +70,6 @@ ModelInstance **fmuInstances;
6970
int fmuLocCoun=0;
7071
#define DELTA 10
7172

72-
7373
///////////////////////////////////////////////////////////////
7474
/// This method is used to get the number of outputs in the FMU
7575
///
@@ -501,14 +501,14 @@ void fmuLogger(fmi2Component c, fmi2String instanceName, fmi2Status status,
501501
printf("%s %s (%s): %s\n", fmi2StatusToString(status), instanceName, category, msg);
502502
}
503503

504-
////////////////////////////////////////////////////////////////
504+
//////////////////////////////////////////////////////////////
505505
/// This method is used to get the fmi types of platform
506-
///\return fmiPlatform.
507-
////////////////////////////////////////////////////////////////
508-
//DllExport char* fmi2GetTypesPlatform()
509-
//{
510-
// return "default";
511-
//}
506+
/// \return fmiPlatform.
507+
//////////////////////////////////////////////////////////////
508+
DllExport const char* fmi2GetTypesPlatform()
509+
{
510+
return "default";
511+
}
512512

513513
////////////////////////////////////////////////////////////////
514514
/// This method is used to get the fmi version
@@ -698,8 +698,8 @@ int getResourceLocation(ModelInstance *_c, fmi2String fmuLocation)
698698

699699
// allocate memory for fmuResourceLocation
700700
_c->fmuResourceLocation=(char *)_c->functions->allocateMemory(strlen (_c->fmuUnzipLocation)
701-
+ strlen (RESOURCES) + strlen(PATH_SEP) + 1, sizeof(char));
702-
sprintf(_c->fmuResourceLocation, "%s%s%s", _c->fmuUnzipLocation, RESOURCES, PATH_SEP);
701+
+ strlen(PATH_SEP) + 1, sizeof(char));
702+
sprintf(_c->fmuResourceLocation, "%s%s", _c->fmuUnzipLocation, PATH_SEP);
703703
return 0;
704704
}
705705

@@ -759,6 +759,13 @@ DllExport fmi2Component fmi2Instantiate(fmi2String instanceName,
759759
// write instanceName to the struct
760760
strcpy(_c->instanceName, instanceName);
761761

762+
// check fmu location
763+
functions->logger(NULL, instanceName, fmi2OK, "ok",
764+
"fmi2Instantiate: The Resource location of FMU with instance name %s is %s.\n",
765+
instanceName, fmuLocation);
766+
767+
768+
762769
// assign FMU parameters
763770
//fixme
764771
_c->functions=functions;
@@ -767,15 +774,15 @@ DllExport fmi2Component fmi2Instantiate(fmi2String instanceName,
767774
if (visible == fmi2True) {
768775
_c->functions->logger(NULL, instanceName, fmi2Warning, "warning",
769776
"fmi2Instantiate: Argument visible is set to %d\n."
770-
" This is not supported. visible will default to fmi2False\n", visible);
777+
" This is not supported. visible will default to '0'.\n", visible);
771778
}
772779

773780
_c->visible=fmi2False;
774781

775782
if (loggingOn == fmi2True) {
776783
_c->functions->logger(NULL, instanceName, fmi2Warning, "warning",
777784
"fmi2Instantiate: Argument loggingOn is set to %d\n."
778-
" This is not supported. loggingOn will default to fmi2False\n", loggingOn);
785+
" This is not supported. loggingOn will default to '0'.\n", loggingOn);
779786
}
780787

781788
_c->visible = fmi2False;
@@ -866,6 +873,7 @@ DllExport fmi2Component fmi2Instantiate(fmi2String instanceName,
866873
_c->functions->logger(NULL, _c->instanceName, fmi2Error, "error", "fmi2Instantiate: Could not copy"
867874
" variables.cfg to the output directory folder %s. Instantiation of %s failed.\n", _c->cwd, _c->instanceName);
868875
// Free resources allocated to instance.
876+
//return NULL;
869877
freeInstanceResources (_c);
870878
return NULL;
871879
}
@@ -1956,6 +1964,20 @@ DllExport fmi2Status fmi2CancelStep(fmi2Component c)
19561964
return fmi2Warning;
19571965
}
19581966

1967+
////////////////////////////////////////////////////////////////
1968+
/// This method is used to reset the FMU
1969+
///
1970+
///\param c The FMU instance.
1971+
///\return fmi2Warning if no error occurred.
1972+
////////////////////////////////////////////////////////////////
1973+
DllExport fmi2Status fmi2Reset(fmi2Component c)
1974+
{
1975+
ModelInstance* _c = (ModelInstance *)c;
1976+
_c->functions->logger(NULL, _c->instanceName, fmi2Warning,
1977+
"Warning", "fmi2Reset: The function fmi2Reset: is not provided.\n");
1978+
return fmi2Warning;
1979+
}
1980+
19591981
////////////////////////////////////////////////////////////////
19601982
/// This method is used to get FMU status
19611983
///
@@ -2035,109 +2057,109 @@ DllExport fmi2Status fmi2GetStringStatus (fmi2Component c, const fmi2StatusKind
20352057
return fmi2Warning;
20362058
}
20372059

2038-
int main(){
2039-
double time;
2040-
double tStart=0; // start time
2041-
double tStop=86400; // start time
2042-
const char* guid="{19751346-37ce-4422-86bf-bd1609f0d579}"; // global unique id of the fmu
2043-
fmi2Component c; // instance of the fmu
2044-
//fmi2Status fmiFlag; // return code of the fmu functions
2045-
const char* fmuResourceLocation ="file:///D:\\proj\\lbnl\\eplustofmu\\_fmu_export_schedule"; // path to the unzipped fmu location as URL
2046-
const char* modelDescriptionPath = "file:///D:\\proj\\lbnl\\eplustofmu\\_fmu_export_schedule\\modelDescription.xml"; // path to the unzipped fmu location as URL
2047-
const char* mimeType="application/x-fmu-sharedlibrary"; // denotes tool in case of tool coupling
2048-
fmi2Real timeout=1000; // wait period in milli seconds, 0 for unlimited wait period"
2049-
fmi2Boolean visible=fmi2False; // no simulator user interface
2050-
fmi2Boolean interactive=fmi2False; // simulation run without user interaction
2051-
fmi2CallbackFunctions callbacks= {fmuLogger, calloc, free, NULL, NULL}; // called by the model during simulation
2052-
ModelDescription* md; // handle to the parsed XML file
2053-
fmi2String instanceName; // instance name of the FMU
2054-
int nSteps=0;
2055-
int loggingOn=0;
2056-
int retVal;
2057-
//FILE* file;
2058-
const fmi2ValueReference valRefIn[]={1};
2059-
const fmi2ValueReference valRefOut[]={100001};
2060-
fmi2Real valIn[1]={50000};
2061-
fmi2Real valOut[1];
2062-
char* fmiVersionStr;
2063-
Element *defaultExp;
2064-
fmi2Status fmi2Flag; // return code of the fmu functions
2065-
fmi2Boolean toleranceDefined = fmi2False; // true if model description define tolerance
2066-
//int nCategories;
2067-
//char **categories;
2068-
fmi2Real tolerance = 0; // used in setting up the experiment
2069-
ValueStatus vs = valueMissing;
2070-
double tEnd=86400;
2071-
2072-
// allocate memory for md
2073-
//md = (ModelDescription*)malloc(sizeof(ModelDescription*));
2074-
2075-
//md = parse(modelDescriptionPath);
2076-
2077-
// get the GUID
2078-
//guid = getAttributeValue((Element *)md, att_guid);
2079-
2080-
// get the instance name
2081-
//instanceName = getAttributeValue((Element *)getCoSimulation(md), att_modelIdentifier);
2082-
2083-
// instantiate the fmu
2084-
c=fmi2Instantiate("_fmu_export_schedule", fmi2CoSimulation,
2085-
guid, fmuResourceLocation, &callbacks, visible, loggingOn);
2086-
2087-
//if (nCategories > 0) {
2088-
// fmi2Flag = fmu->setDebugLogging(c, fmi2True, nCategories, categories);
2089-
// if (fmi2Flag > fmi2Warning) {
2090-
// return error("could not initialize model; failed FMI set debug logging");
2091-
// }
2092-
//}
2093-
2094-
//defaultExp = getDefaultExperiment(md);
2095-
//if (defaultExp) tolerance = getAttributeDouble(defaultExp, att_tolerance, &vs);
2096-
//if (vs == valueDefined) {
2097-
// toleranceDefined = fmi2True;
2098-
//}
2099-
2100-
// setup experiment parameters in the FMU
2101-
fmi2Flag = fmi2SetupExperiment(c, toleranceDefined, tolerance, tStart, fmi2True, tEnd);
2102-
if (fmi2Flag > fmi2Warning) {
2103-
printf("could not initialize model; failed FMI setup experiment");
2104-
return;
2105-
}
2106-
2107-
// enter initialization in the FMU
2108-
fmi2Flag = fmi2EnterInitializationMode(c);
2109-
if (fmi2Flag > fmi2Warning) {
2110-
printf("could not initialize model; failed FMI enter initialization mode");
2111-
return;
2112-
}
2113-
2114-
// exit initialization in the FMU
2115-
fmi2Flag = fmi2ExitInitializationMode(c);
2116-
if (fmi2Flag > fmi2Warning) {
2117-
printf("could not initialize model; failed FMI exit initialization mode");
2118-
return;
2119-
}
2120-
2121-
time=0;
2122-
2123-
fmiVersionStr = extractVersion(modelDescriptionPath);
2124-
printf("This is the fmi version %s\n", fmiVersionStr);
2125-
while (time < tStop) {
2126-
// set the inputs
2127-
retVal = fmi2SetReal(c, valRefIn, 1, valIn);
2128-
// do step
2129-
retVal= fmi2DoStep(c, time, 900, 1);
2130-
// get the outputs
2131-
retVal = fmi2GetReal(c, valRefOut, 1, valOut);
2132-
printf ("This is the value of output %f\n", valOut);
2133-
time=time+900;
2134-
}
2135-
// terminate the FMU
2136-
retVal=fmi2Terminate(c);
2137-
// free te FMU
2138-
fmi2FreeInstance(c);
2139-
printf ("Simulation successfully terminated\n");
2140-
}
2060+
//void main(){
2061+
// double time;
2062+
// double tStart=0; // start time
2063+
// double tStop=86400; // start time
2064+
// const char* guid="{19751346-37ce-4422-86bf-bd1609f0d579}"; // global unique id of the fmu
2065+
// fmi2Component c; // instance of the fmu
2066+
// //fmi2Status fmiFlag; // return code of the fmu functions
2067+
// const char* fmuResourceLocation ="file:///D:\\proj\\lbnl\\eplustofmu\\_fmu_export_schedule"; // path to the unzipped fmu location as URL
2068+
// const char* modelDescriptionPath = "file:///D:\\proj\\lbnl\\eplustofmu\\_fmu_export_schedule\\modelDescription.xml"; // path to the unzipped fmu location as URL
2069+
// const char* mimeType="application/x-fmu-sharedlibrary"; // denotes tool in case of tool coupling
2070+
// fmi2Real timeout=1000; // wait period in milli seconds, 0 for unlimited wait period"
2071+
// fmi2Boolean visible=fmi2False; // no simulator user interface
2072+
// fmi2Boolean interactive=fmi2False; // simulation run without user interaction
2073+
// fmi2CallbackFunctions callbacks= {fmuLogger, calloc, free, NULL, NULL}; // called by the model during simulation
2074+
// ModelDescription* md; // handle to the parsed XML file
2075+
// fmi2String instanceName; // instance name of the FMU
2076+
// int nSteps=0;
2077+
// int loggingOn=0;
2078+
// int retVal;
2079+
// //FILE* file;
2080+
// const fmi2ValueReference valRefIn[]={1};
2081+
// const fmi2ValueReference valRefOut[]={100001};
2082+
// fmi2Real valIn[1]={50000};
2083+
// fmi2Real valOut[1];
2084+
// char* fmiVersionStr;
2085+
// Element *defaultExp;
2086+
// fmi2Status fmi2Flag; // return code of the fmu functions
2087+
// fmi2Boolean toleranceDefined = fmi2False; // true if model description define tolerance
2088+
// //int nCategories;
2089+
// //char **categories;
2090+
// fmi2Real tolerance = 0; // used in setting up the experiment
2091+
// ValueStatus vs = valueMissing;
2092+
// double tEnd=86400;
2093+
//
2094+
// // allocate memory for md
2095+
// //md = (ModelDescription*)malloc(sizeof(ModelDescription*));
2096+
//
2097+
// //md = parse(modelDescriptionPath);
2098+
//
2099+
// // get the GUID
2100+
// //guid = getAttributeValue((Element *)md, att_guid);
2101+
//
2102+
// // get the instance name
2103+
// //instanceName = getAttributeValue((Element *)getCoSimulation(md), att_modelIdentifier);
2104+
//
2105+
// // instantiate the fmu
2106+
// c=fmi2Instantiate("_fmu_export_schedule", fmi2CoSimulation,
2107+
// guid, fmuResourceLocation, &callbacks, visible, loggingOn);
2108+
//
2109+
// //if (nCategories > 0) {
2110+
// // fmi2Flag = fmu->setDebugLogging(c, fmi2True, nCategories, categories);
2111+
// // if (fmi2Flag > fmi2Warning) {
2112+
// // return error("could not initialize model; failed FMI set debug logging");
2113+
// // }
2114+
// //}
2115+
//
2116+
// //defaultExp = getDefaultExperiment(md);
2117+
// //if (defaultExp) tolerance = getAttributeDouble(defaultExp, att_tolerance, &vs);
2118+
// //if (vs == valueDefined) {
2119+
// // toleranceDefined = fmi2True;
2120+
// //}
2121+
//
2122+
// // setup experiment parameters in the FMU
2123+
// fmi2Flag = fmi2SetupExperiment(c, toleranceDefined, tolerance, tStart, fmi2True, tEnd);
2124+
// if (fmi2Flag > fmi2Warning) {
2125+
// printf("could not initialize model; failed FMI setup experiment");
2126+
// return;
2127+
// }
2128+
//
2129+
// // enter initialization in the FMU
2130+
// fmi2Flag = fmi2EnterInitializationMode(c);
2131+
// if (fmi2Flag > fmi2Warning) {
2132+
// printf("could not initialize model; failed FMI enter initialization mode");
2133+
// return;
2134+
// }
2135+
//
2136+
// // exit initialization in the FMU
2137+
// fmi2Flag = fmi2ExitInitializationMode(c);
2138+
// if (fmi2Flag > fmi2Warning) {
2139+
// printf("could not initialize model; failed FMI exit initialization mode");
2140+
// return;
2141+
// }
2142+
//
2143+
// time=0;
2144+
//
2145+
// fmiVersionStr = extractVersion(modelDescriptionPath);
2146+
// printf("This is the fmi version %s\n", fmiVersionStr);
2147+
// while (time < tStop) {
2148+
// // set the inputs
2149+
// retVal = fmi2SetReal(c, valRefIn, 1, valIn);
2150+
// // do step
2151+
// retVal= fmi2DoStep(c, time, 900, 1);
2152+
// // get the outputs
2153+
// retVal = fmi2GetReal(c, valRefOut, 1, valOut);
2154+
// printf ("This is the value of output %f\n", valOut);
2155+
// time=time+900;
2156+
// }
2157+
// // terminate the FMU
2158+
// retVal=fmi2Terminate(c);
2159+
// // free te FMU
2160+
// fmi2FreeInstance(c);
2161+
// printf ("Simulation successfully terminated\n");
2162+
//}
21412163

21422164
/*
21432165

SourceCode/v20/fmu-export-prep/fmu-export-idf-data.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using std::endl;
2222

2323
#include "fmu-export-idf-data.h"
2424

25-
#include "../utility/string-help.h"
25+
#include "../../utility/string-help.h"
2626

2727
//--- Microsoft doesn't implement the modern standard.
2828
//

0 commit comments

Comments
 (0)