diff --git a/src/OSPSuite.SimModelNative/src/Quantity.cpp b/src/OSPSuite.SimModelNative/src/Quantity.cpp index 3269754..3d88b04 100644 --- a/src/OSPSuite.SimModelNative/src/Quantity.cpp +++ b/src/OSPSuite.SimModelNative/src/Quantity.cpp @@ -47,6 +47,7 @@ void Quantity::DeleteFormula() } _originalValueFormula=NULL; + _originalFormulaID = INVALID_QUANTITY_ID; } string Quantity::GetName(void) diff --git a/tests/TestAppCpp/Include/TestAppCpp.h b/tests/TestAppCpp/Include/TestAppCpp.h new file mode 100644 index 0000000..23e1746 --- /dev/null +++ b/tests/TestAppCpp/Include/TestAppCpp.h @@ -0,0 +1,39 @@ +#ifndef __TESTSIMMODEL_H +#define __TESTSIMMODEL_H + +#include +#include + +#include "SimModel/Simulation.h" +#include "SimModel/PInvokeHelper.h" +#include "SimModel/PInvokeQuantity.h" +#include "SimModel/PInvokeSimulation.h" +#include "DynamicLibrary.h" + +using namespace std; +using namespace SimModelNative; + +void evalPInvokeErrorMsg(const bool success, char* errorMessage); +void ShowErrorMessage(const string& msg); +void ShowErrorMessage(const ErrorData& ED); + +string BasisDir(const string & exeName); +string TestFileFrom(const string& fileName); +void ShowTimeSpan(double tstart, double tend); + +Simulation* LoadSimulation(const string& fileName); +Simulation* LoadSimulation(const string& fileName); +void FinalizeSimulation(Simulation* sim); +void RunSimulation(Simulation* sim, bool showInfo=true); +void ShowFirstWarning(Simulation* sim); +vector* GetParameterProperties(Simulation* sim); +void SetVariableParameters(Simulation* sim, vector* parameterProperties, vector variableParameterIndices); +void SetParameterValues(Simulation* sim, vector* parameterProperties, vector variableParameterIndices); + +void TestLeaks(void); + +void Test1(const string& simName); +void TestSetTablePoints(); + +void ClearDynamicLibrary(); +#endif diff --git a/tests/TestAppCpp/TestAppCpp.sln b/tests/TestAppCpp/TestAppCpp.sln new file mode 100644 index 0000000..6a1afe6 --- /dev/null +++ b/tests/TestAppCpp/TestAppCpp.sln @@ -0,0 +1,27 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29123.88 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestAppCpp", "TestAppCpp.vcxproj", "{9D2C1009-2090-44E5-B57D-97B68D01C40A}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{465ED1B2-2A89-46A7-B967-5E021E3C6873}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9D2C1009-2090-44E5-B57D-97B68D01C40A}.Debug|x64.ActiveCfg = Debug|x64 + {9D2C1009-2090-44E5-B57D-97B68D01C40A}.Debug|x64.Build.0 = Debug|x64 + {9D2C1009-2090-44E5-B57D-97B68D01C40A}.Release|x64.ActiveCfg = Release|x64 + {9D2C1009-2090-44E5-B57D-97B68D01C40A}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {400A4F13-3471-45B3-B557-363342A4504B} + EndGlobalSection +EndGlobal diff --git a/tests/TestAppCpp/TestAppCpp.vcxproj b/tests/TestAppCpp/TestAppCpp.vcxproj new file mode 100644 index 0000000..1be21b7 --- /dev/null +++ b/tests/TestAppCpp/TestAppCpp.vcxproj @@ -0,0 +1,115 @@ + + + + + Debug + x64 + + + Release + x64 + + + + + + + + + + + 16.0 + {9D2C1009-2090-44E5-B57D-97B68D01C40A} + Win32Proj + TestAppCpp + 10.0.17763.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + false + $(Platform)\$(Configuration)\ + Intermediate\$(Platform)\$(Configuration)\ + C:\Program Files (x86)\Visual Leak Detector\include;$(IncludePath) + C:\Program Files (x86)\Visual Leak Detector\lib\Win64;$(LibraryPath) + + + false + $(Platform)\$(Configuration)\ + Intermediate\$(Platform)\$(Configuration)\ + + + + NotUsing + Level3 + Disabled + true + _DEBUG;_WINDOWS;_USRDLL;SIMMODEL_EXPORTS;%(PreprocessorDefinitions) + true + $(ProjectDir)Include;$(FuncParserPath)include;$(SolverPath)include;$(ProjectDir)..\..\src\OSPSuite.SysTool\Include;$(ProjectDir)..\..\src\OSPSuite.XMLWrapper\Include;$(ProjectDir)..\..\src\OSPSuite.SimModelSolverBase\src\OSPSuite.SimModelSolverBase\include;$(ProjectDir)..\..\src\OSPSuite.SimModelNative\include;%(AdditionalIncludeDirectories) + OldStyle + false + true + false + true + + + Console + true + OSPSuite.FuncParserNative.lib;OSPSuite.SimModelNative.lib;%(AdditionalDependencies) + $(OutDir);%(AdditionalLibraryDirectories) + /fixed:no %(AdditionalOptions) + false + false + + + + + + + Level3 + MaxSpeed + true + true + true + NDEBUG;_WINDOWS;_USRDLL;SIMMODEL_EXPORTS;%(PreprocessorDefinitions) + true + $(ProjectDir)Include;$(FuncParserPath)include;$(SolverPath)include;$(ProjectDir)..\OSPSuite.SysTool\Include;$(ProjectDir)..\OSPSuite.XMLWrapper\Include;$(ProjectDir)..\OSPSuite.SimModelSolverBase\src\OSPSuite.SimModelSolverBase\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + $(OutDir);%(AdditionalLibraryDirectories) + OSPSuite.FuncParserNative.lib;OSPSuite.SimModelNative.lib;%(AdditionalDependencies) + + + + + + \ No newline at end of file diff --git a/tests/TestAppCpp/TestAppCpp.vcxproj.filters b/tests/TestAppCpp/TestAppCpp.vcxproj.filters new file mode 100644 index 0000000..bc631a1 --- /dev/null +++ b/tests/TestAppCpp/TestAppCpp.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/tests/TestAppCpp/src/HelpFunctions.cpp b/tests/TestAppCpp/src/HelpFunctions.cpp new file mode 100644 index 0000000..09bbc54 --- /dev/null +++ b/tests/TestAppCpp/src/HelpFunctions.cpp @@ -0,0 +1,222 @@ +#include "TestAppCpp.h" +#include +#include + +void evalPInvokeErrorMsg(const bool success, char* errorMessage) +{ + if (success) + { + if (errorMessage != NULL) + CoTaskMemFree(errorMessage); + return; + } + + string error = errorMessage; + CoTaskMemFree(errorMessage); + + throw(error); +} + +Simulation* LoadSimulation(const string& fileName) +{ + bool success; + char *errorMsg = NULL; + + Simulation* sim = CreateSimulation(); + + SimulationOptionsStructure options; + FillSimulationOptions(sim, &options); + options.AutoReduceTolerances = false; + SetSimulationOptions(sim, options); + + double t1, t2; + cout << "loading " << fileName.c_str() << " ... "; fflush(stdout); + + t1 = GetTickCount(); + LoadSimulationFromXMLFile(sim, TestFileFrom(fileName).c_str(), success, &errorMsg); + evalPInvokeErrorMsg(success, errorMsg); + t2 = GetTickCount(); + + ShowTimeSpan(t1, t2); + + return sim; +} + +void FinalizeSimulation(Simulation* sim) +{ + bool success; + char* errorMsg = NULL; + + double t1, t2; + cout << "finalizing " << " ... "; fflush(stdout); + + t1 = GetTickCount(); + FinalizeSimulation(sim, success, &errorMsg); + evalPInvokeErrorMsg(success, errorMsg); + t2 = GetTickCount(); + + ShowTimeSpan(t1, t2); +} + +void RunSimulation(Simulation* sim, bool showInfo) +{ + bool success; + char* errorMsg = NULL; + bool toleranceWasReduced; + double newAbsTol, newRelTol; + + double t1, t2; + if (showInfo) + { + cout << "running ... "; + fflush(stdout); + } + + t1 = GetTickCount(); + RunSimulation(sim, toleranceWasReduced, newAbsTol, newRelTol, success, &errorMsg); + evalPInvokeErrorMsg(success, errorMsg); + t2 = GetTickCount(); + + if (showInfo) + ShowTimeSpan(t1, t2); + + ShowFirstWarning(sim); +} + +void ShowFirstWarning(Simulation* sim) +{ + bool success; + char* errorMsg = NULL; + double* times = NULL; + char** warnings = NULL; + + auto noOfWarnings = GetNumberOfSolverWarnings(sim); + if (noOfWarnings == 0) + return; + + times = new double[noOfWarnings]; + warnings = new char* [noOfWarnings]; + + FillSolverWarnings(sim, noOfWarnings, times, warnings, success, &errorMsg); + evalPInvokeErrorMsg(success, errorMsg); + + cout << noOfWarnings << "solver warnings. 1st warning: " << warnings[0] << endl; + + delete[] times; + for (auto i = 0; i < noOfWarnings; i++) + CoTaskMemFree(warnings[i]); + + delete[] warnings; +} + +vector* GetParameterProperties(Simulation* sim) +{ + bool success; + char* errorMsg = NULL; + + vector* parameterInfos = CreateParameterInfoVector(); + FillParameterProperties(sim, parameterInfos, success, &errorMsg); + evalPInvokeErrorMsg(success, errorMsg); + + return parameterInfos; +} + +void SetVariableParameters(Simulation* sim, vector* parameterProperties, vector variableParameterIndices) +{ + bool success; + char* errorMsg = NULL; + int* indices = NULL; + + try + { + auto noOfVariableParameters = (int)variableParameterIndices.size(); + if (noOfVariableParameters == 0) + return; + + indices = new int[noOfVariableParameters]; + for (auto i = 0; i < noOfVariableParameters; i++) + indices[i] = variableParameterIndices[i]; + + SetVariableParameters(sim, parameterProperties, indices, noOfVariableParameters, success, &errorMsg); + evalPInvokeErrorMsg(success, errorMsg); + + delete[] indices; + } + catch (...) + { + if (indices != NULL) + delete[] indices; + throw; + } +} + +void SetParameterValues(Simulation* sim, vector* parameterProperties, vector variableParameterIndices) +{ + bool success; + char* errorMsg = NULL; + int* indices = NULL; + + try + { + auto noOfVariableParameters = (int)variableParameterIndices.size(); + if (noOfVariableParameters == 0) + return; + + indices = new int[noOfVariableParameters]; + for (auto i = 0; i < noOfVariableParameters; i++) + indices[i] = variableParameterIndices[i]; + + SetParameterValues(sim, parameterProperties, indices, noOfVariableParameters, success, &errorMsg); + evalPInvokeErrorMsg(success, errorMsg); + + delete[] indices; + } + catch (...) + { + if (indices != NULL) + delete[] indices; + throw; + } +} + +void ShowErrorMessage(const string & msg) +{ + cout<Clear(); +} diff --git a/tests/TestAppCpp/src/TestAppCpp.cpp b/tests/TestAppCpp/src/TestAppCpp.cpp new file mode 100644 index 0000000..d9b848c --- /dev/null +++ b/tests/TestAppCpp/src/TestAppCpp.cpp @@ -0,0 +1,135 @@ +#include "TestAppCpp.h" +//#include + +int main() +{ + + try + { + CoInitialize(NULL); + + //TestLeaks(); + TestSetTablePoints(); + + string simName; + simName = "SimModel4_ExampleInput06_Modified"; + //simName = "SolverError01"; + + //Test1(simName); + } + catch (ErrorData& ED) + { + ShowErrorMessage(ED); + } + catch (const string& str) + { + ShowErrorMessage(str); + } + catch (SimModelSolverErrorData sED) + { + ShowErrorMessage(ErrorData(ErrorData::errNumber(sED.GetNumber()), sED.GetSource(), sED.GetDescription())); + } + catch (...) + { + ShowErrorMessage("Unknown Error"); + } + + ClearDynamicLibrary(); +// cout << "Enter anything"; +// _getch(); + + return 0; +} + +void TestSetTablePoints() +{ + bool success; + char* errorMsg = NULL; + + const string simName = "SimModel4_ExampleInput06_Modified2";//"SimModel4_ExampleInput06_Modified"; + + Simulation* sim = NULL; + vector* parameterInfos = NULL; + + try + { + sim = LoadSimulation(simName); + + parameterInfos = GetParameterProperties(sim); + vector variableParameterIndices; + + for (auto i = 0; i < parameterInfos->size(); i++) + { + if ((*parameterInfos)[i].GetFullName() == "TopContainer/Subcontainer1/P5") + { + variableParameterIndices.push_back(i); + break; + } + } + if (variableParameterIndices.size() == 0) + throw "TopContainer/Subcontainer1/P5 not found"; + + SetVariableParameters(sim, parameterInfos, variableParameterIndices); + + FinalizeSimulation(sim); + RunSimulation(sim); + + double xValues[3] = { 1,2,3 }; + double yValues[3] = { 4,5,6 }; + bool restart[3] = { false,false,false }; + SetParameterTablePoints(parameterInfos, variableParameterIndices[0], xValues, yValues, restart,3, success, &errorMsg); + evalPInvokeErrorMsg(success, errorMsg); + + SetParameterValues(sim, parameterInfos, variableParameterIndices); + RunSimulation(sim); + + double xValues2[10] = {0, 1,2,3,4,5,6,7,8,9 }; + double yValues2[10] = { 4,5,6,6,6,6,6,6,6,6 }; + bool restart2[10] = { false,false,false,false,false,false,false,false,false,false }; + SetParameterTablePoints(parameterInfos, variableParameterIndices[0], xValues2, yValues2, restart2, 10, success, &errorMsg); + evalPInvokeErrorMsg(success, errorMsg); + + SetParameterValues(sim, parameterInfos, variableParameterIndices); + RunSimulation(sim); + + SetParameterTablePoints(parameterInfos, variableParameterIndices[0], NULL, NULL, NULL, 0, success, &errorMsg); + evalPInvokeErrorMsg(success, errorMsg); + + SetParameterValues(sim, parameterInfos, variableParameterIndices); + RunSimulation(sim); + + DisposeParameterInfoVector(parameterInfos); + parameterInfos = NULL; + + DisposeSimulation(sim); + sim = NULL; + } + catch (...) + { + if (sim != NULL) + DisposeSimulation(sim); + if (parameterInfos != NULL) + DisposeParameterInfoVector(parameterInfos); + throw; + } +} + +void Test1(const string& simName) +{ + Simulation* sim = NULL; + try + { + sim = LoadSimulation(simName); + FinalizeSimulation(sim); + RunSimulation(sim); + + DisposeSimulation(sim); + sim = NULL; + } + catch (...) + { + if (sim != NULL) + DisposeSimulation(sim); + throw; + } +} \ No newline at end of file diff --git a/tests/TestData/SimModel4_ExampleInput06_Modified2.xml b/tests/TestData/SimModel4_ExampleInput06_Modified2.xml new file mode 100644 index 0000000..ddf2637 --- /dev/null +++ b/tests/TestData/SimModel4_ExampleInput06_Modified2.xml @@ -0,0 +1,152 @@ + + + + + 1 + + + + + + + + 1 + + + + + + + 1 + + + + 1 + + + + + + +

+

+

+

+

+

+

+

+

+

+

+

+ + + + P1+P2-1 + + + + + + + y1 + + + + + + sin(y3)^2+4 + + + + + + cos(y3)^2+5 + + + + + + Time + + + + + + y3-2 + + + + + + + + + + + + + + (P1+P2)*y2 + + + + + + + + (P3-Time)*y1 + + + + + + + + y3-2 + + + + + + y1 + + + + + + P4 + + + + + + P5 + + + + + + + + + + + + + + + + + + 0 + 10 + 11 + + + + 0 + + +