Skip to content

Commit

Permalink
all the missing functions added to Fmi3Interpreter (except setBinary …
Browse files Browse the repository at this point in the history
…for now)

Signed-off-by: Nestor <[email protected]>
  • Loading branch information
TheRealNestor committed Nov 2, 2023
1 parent 80bd6bb commit 671246b
Show file tree
Hide file tree
Showing 9 changed files with 931 additions and 55 deletions.
29 changes: 0 additions & 29 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions .idea/saveactions_settings.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,7 @@ void enterInitializationMode(Scope<T> scope,boolean toleranceDefined, double tol
}

/**
* Interface for an fmi compoennt.
* Interface for an fmi component.
* <p>
* Note that all methods that do not take a scope uses the builders dynamic scope and adds the underlying instructions int he active scope.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ private ArrayVariableFmi2Api<Object> createBuffer(PType type, String prefix, int

return new ArrayVariableFmi2Api<>(var, type, scope, builder.getDynamicScope(), newAIdentifierStateDesignator(newAIdentifier(ioBufName)),
newAIdentifierExp(ioBufName), items);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class Fmi3Interpreter {

static final ExternalReflectCallHelper.ArgMapping intInArgMapper = new ExternalReflectCallHelper.ArgMapping(TP.Int, 1,
ExternalReflectCallHelper.ArgMapping.InOut.Input, null);

static final ExternalReflectCallHelper.ArgMapping longInArgMapper = new ExternalReflectCallHelper.ArgMapping(TP.Long, 1,
ExternalReflectCallHelper.ArgMapping.InOut.Input, null);

static final ExternalReflectCallHelper.ArgMapping doubleInArgMapper = new ExternalReflectCallHelper.ArgMapping(TP.Real, 1,
ExternalReflectCallHelper.ArgMapping.InOut.Input, null);

Expand All @@ -55,6 +59,12 @@ public class Fmi3Interpreter {
static final ExternalReflectCallHelper.ArgMapping doubleArrayInArgMapper = new ExternalReflectCallHelper.ArgMapping(TP.Real, 2,
ExternalReflectCallHelper.ArgMapping.InOut.Input, null);

static final ExternalReflectCallHelper.ArgMapping boolArrayInArgMapper = new ExternalReflectCallHelper.ArgMapping(TP.Bool, 2,
ExternalReflectCallHelper.ArgMapping.InOut.Input, null);

static final ExternalReflectCallHelper.ArgMapping intArrayInArgMapper = new ExternalReflectCallHelper.ArgMapping(TP.Int, 2,
ExternalReflectCallHelper.ArgMapping.InOut.Input, null);

static final ExternalReflectCallHelper.ArgMapping longArrayInArgMapper = new ExternalReflectCallHelper.ArgMapping(TP.Long, 2,
ExternalReflectCallHelper.ArgMapping.InOut.Input, null);

Expand Down Expand Up @@ -301,7 +311,7 @@ public static Function<ExternalReflectCallHelper.ArgMappingContext, IArgMapping>


private static Value getFmuInstanceValue(BufferedOutputStream fmuLogOutputStream, IFmi3Instance instance,
Function<String, AModuleDeclaration> resolver) throws NoSuchMethodException {
Function<String, AModuleDeclaration> resolver) throws NoSuchMethodException {

//populate component functions
var module = resolver.apply("FMI3Instance");
Expand Down Expand Up @@ -393,6 +403,144 @@ private static Value getFmuInstanceValue(BufferedOutputStream fmuLogOutputStream

}));


functions.put("getOutputDerivatives", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int getOutputDerivatives(uint valueReferences[], int nValueReferences,
// int orders[], out real values[], out int nValues);

checkArgLength(fcargs, 5);
try {
FmuResult<double[]> res = instance.getOutputDerivatives((long[]) longArrayInArgMapper.map(fcargs.get(0)),
(int[]) intArrayInArgMapper.map(fcargs.get(2)));
doubleArrayOutArgMapper.mapOut(fcargs.get(3), res.result);
return status2IntValue(res.status);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}));

functions.put("getShiftDecimal", new FunctionValue.ExternalFunctionValue(fcargs->{
// int getShiftDecimal(uint valueReferences[], int nValueReferences, real shifts[]);

checkArgLength(fcargs, 3);
try {
FmuResult<double[]> res = instance.getShiftDecimal((long[]) longArrayInArgMapper.map(fcargs.get(0)));
doubleArrayOutArgMapper.mapOut(fcargs.get(2), res.result);
return status2IntValue(res.status);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}));

functions.put("getShiftFraction", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int getShiftFraction(uint valueReferences[], int nValueReferences, out uint shiftCounters[], out uint resolutions[]);

checkArgLength(fcargs, 4);
try {
FmuResult<IFmi3Instance.GetShiftFractionResponse> res = instance.getShiftFraction((long[]) longArrayInArgMapper.map(fcargs.get(0)));
uintArrayOutArgMapper.mapOut(fcargs.get(2), res.result.getShiftCounters());
uintArrayOutArgMapper.mapOut(fcargs.get(3), res.result.getResolutions());
return status2IntValue(res.status);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}));

functions.put("getVariableDependencies", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int getVariableDependencies(uint dependent, out int elementIndicesOfDependent[], out uint independents[],
// out uint elementIndicesOfIndependents[], out int dependencyKinds[], int nDependencies);
checkArgLength(fcargs, 6);
try {
FmuResult<IFmi3Instance.VariableDependency> res = instance.getVariableDependencies((long) longInArgMapper.map(fcargs.get(0)), (long) intInArgMapper.map(fcargs.get(5)));
intArrayOutArgMapper.mapOut(fcargs.get(1), res.result.getElementIndicesOfDependent());
longArrayOutArgMapper.mapOut(fcargs.get(2), res.result.getIndependents());
longArrayOutArgMapper.mapOut(fcargs.get(3), res.result.getElementIndicesOfIndependents());
intArrayOutArgMapper.mapOut(fcargs.get(4), res.result.getDependencyKinds());
return status2IntValue(res.status);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}));

functions.put("setClock", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int setClock(uint valueReferences[], int nValueReferences, bool values[]);
checkArgLength(fcargs, 3);
try {
Fmi3Status res = instance.setClock((long[]) longArrayInArgMapper.map(fcargs.get(0)), (boolean[]) boolArrayInArgMapper.map(fcargs.get(2)));
return new IntegerValue(res.value);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}));

functions.put("setContinuousStates", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int setContinuousStates(real continuousStates[], int nContinuousStates);
checkArgLength(fcargs, 2);
try {
Fmi3Status res = instance.setContinuousStates((double[]) doubleArrayInArgMapper.map(fcargs.get(0)));
return new IntegerValue(res.value);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}));

functions.put("setIntervalDecimal", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int setIntervalDecimal(uint valueReferences[], int nValueReferences, real interval[]);
checkArgLength(fcargs, 3);
try {
Fmi3Status res = instance.setIntervalDecimal((long[]) longArrayInArgMapper.map(fcargs.get(0)), (double[]) doubleArrayInArgMapper.map(fcargs.get(2)));
return new IntegerValue(res.value);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}));

functions.put("setIntervalFraction", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int setIntervalFraction(uint valueReferences[], int nValueReferences, uint intervalCounter[], uint resolution[]);
checkArgLength(fcargs, 4);
try {
Fmi3Status res = instance.setIntervalFraction((long[]) longArrayInArgMapper.map(fcargs.get(0)),
(long[]) longArrayInArgMapper.map(fcargs.get(2)), (long[]) longArrayInArgMapper.map(fcargs.get(3)));
return new IntegerValue(res.value);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}));

functions.put("setShiftDecimal", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int setShiftDecimal(uint valueReferences[], int nValueReferences, real shifts[]);
checkArgLength(fcargs, 3);
Fmi3Status res = instance.setShiftDecimal((long[]) longArrayInArgMapper.map(fcargs.get(0)), (double[]) doubleArrayInArgMapper.map(fcargs.get(2)));
return new IntegerValue(res.value);
}));

functions.put("setShiftFraction", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int setShiftFraction(uint valueReferences[], int nValueReferences, uint counters[], uint resolutions[]);
checkArgLength(fcargs, 4);
Fmi3Status res = instance.setShiftFraction((long[]) longArrayInArgMapper.map(fcargs.get(0)),
(long[]) longArrayInArgMapper.map(fcargs.get(2)), (long[]) longArrayInArgMapper.map(fcargs.get(3)));
return new IntegerValue(res.value);
}));

functions.put("updateDiscreteStates", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int updateDiscreteStates(bool[] discreteStatesNeedUpdate, bool[] terminateSimulation,
// bool[] nominalsOfContinuousStatesChanged, bool[] valuesOfContinuousStatesChanged, bool[] nextEventTimeDefined,
// real[] nextEventTime);
checkArgLength(fcargs, 6);
try {
FmuResult<IFmi3Instance.UpdateDiscreteStates> res = instance.updateDiscreteStates();
boolArrayOutArgMapper.mapOut(fcargs.get(0), new boolean[]{res.result.isDiscreteStatesNeedUpdate()});
boolArrayOutArgMapper.mapOut(fcargs.get(1), new boolean[]{res.result.isTerminateSimulation()});
boolArrayOutArgMapper.mapOut(fcargs.get(2), new boolean[]{res.result.isNominalsOfContinuousStatesChanged()});
boolArrayOutArgMapper.mapOut(fcargs.get(3), new boolean[]{res.result.isValuesOfContinuousStatesChanged()});
boolArrayOutArgMapper.mapOut(fcargs.get(4), new boolean[]{res.result.isNextEventTimeDefined()});
doubleArrayOutArgMapper.mapOut(fcargs.get(5), new double[]{res.result.getNextEventTime()});
return status2IntValue(res.status);
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}
}));

functions.put("getClock", new FunctionValue.ExternalFunctionValue(fcargs -> {
// int getClock(uint valueReferences[], int nValueReferences, bool values[]);

Expand All @@ -404,10 +552,10 @@ private static Value getFmuInstanceValue(BufferedOutputStream fmuLogOutputStream
} catch (FmuInvocationException e) {
throw new InterpreterException(e);
}

}));



functions.put("getContinuousStateDerivatives", new FunctionValue.ExternalFunctionValue(fcargs -> {
//int getContinuousStateDerivatives(real derivatives[], int nContinuousStates);

Expand Down
102 changes: 102 additions & 0 deletions maestro/outputs.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
time,{x1}.controller.valve,{x2}.tank.level
0.0,false,0.0
0.1,false,0.1099999999999999
0.2,false,0.19999999999999996
0.30000000000000004,false,0.30000000000000004
0.4,false,0.40000000000000013
0.5,false,0.5000000000000002
0.6,false,0.6000000000000003
0.7,false,0.7000000000000004
0.7999999999999999,false,0.8000000000000005
0.8999999999999999,false,0.9000000000000006
0.9999999999999999,false,1.0000000000000007
1.0999999999999999,false,1.100000000000003
1.2,false,1.2000000000000053
1.3,false,1.3000000000000076
1.4000000000000001,false,1.40000000000001
1.5000000000000002,false,1.5000000000000122
1.6000000000000003,false,1.6000000000000145
1.7000000000000004,false,1.7000000000000168
1.8000000000000005,false,1.8000000000000191
1.9000000000000006,false,1.9000000000000214
2.0000000000000004,false,2.0000000000000235
2.1000000000000005,true,2.110000000000021
2.2000000000000006,true,1.9902812339436071
2.3000000000000007,true,1.879488458105753
2.400000000000001,true,1.7801372059716003
2.500000000000001,true,1.6910459161652158
2.600000000000001,true,1.6111550466606475
2.700000000000001,true,1.5395144738945912
2.800000000000001,true,1.4752721931674422
2.9000000000000012,true,1.4176641859492016
3.0000000000000013,true,1.3660053335844786
3.1000000000000014,true,1.319681269335382
3.2000000000000015,true,1.2781410718605366
3.3000000000000016,true,1.2408907132354772
3.4000000000000017,true,1.2074871835932366
3.5000000000000018,true,1.1775332225108484
3.600000000000002,true,1.1506725944833651
3.700000000000002,true,1.1265858522977028
3.800000000000002,true,1.1049865379211017
3.900000000000002,true,1.085617775722246
4.000000000000002,true,1.0682492175090177
4.100000000000001,true,1.0526743030509254
4.200000000000001,true,1.0387078035062411
4.300000000000001,true,1.0261836185383848
4.4,true,1.014952800923189
4.5,true,1.0048817851541365
4.6,true,0.9958507989788068
4.699999999999999,false,0.9877524389752776
4.799999999999999,false,1.0859580220444744
4.899999999999999,false,1.1859580220444768
4.999999999999998,false,1.285958022044479
5.099999999999998,false,1.3859580220444814
5.1999999999999975,false,1.4859580220444837
5.299999999999997,false,1.585958022044486
5.399999999999997,false,1.6859580220444883
5.4999999999999964,false,1.7859580220444906
5.599999999999996,false,1.885958022044493
5.699999999999996,false,1.9859580220444952
5.799999999999995,false,2.0859580220444935
5.899999999999995,true,2.1859580220444914
5.999999999999995,true,2.058518842946217
6.099999999999994,true,1.940679197716537
6.199999999999994,true,1.8350088034505139
6.299999999999994,true,1.7402509466819145
6.399999999999993,true,1.6552786942194706
6.499999999999993,true,1.5790814907920725
6.5999999999999925,true,1.5107531407495396
6.699999999999992,true,1.4494810308881407
6.799999999999992,true,1.3945364662304194
6.8999999999999915,true,1.3452660038250044
6.999999999999991,true,1.3010836815012947
7.099999999999991,true,1.2614640491574003
7.19999999999999,true,1.2259359197040731
7.29999999999999,true,1.194076765346047
7.39999999999999,true,1.1655076925570644
7.499999999999989,true,1.1398889359871307
7.599999999999989,true,1.1169158177120915
7.699999999999989,true,1.0963151237698119
7.799999999999988,true,1.077841854889959
7.899999999999988,true,1.0612763127745573
7.999999999999988,true,1.0464214872771362
8.099999999999987,true,1.0331007134067987
8.199999999999987,true,1.0211555702924957
8.299999999999986,true,1.0104439971203927
8.399999999999986,true,1.0008386036375856
8.499999999999986,true,0.9922251551294017
8.599999999999985,false,0.9845012138524587
8.699999999999985,false,1.0827127033139619
8.799999999999985,false,1.1827127033139642
8.899999999999984,false,1.2827127033139665
8.999999999999984,false,1.3827127033139688
9.099999999999984,false,1.482712703313971
9.199999999999983,false,1.5827127033139734
9.299999999999983,false,1.6827127033139757
9.399999999999983,false,1.782712703313978
9.499999999999982,false,1.8827127033139803
9.599999999999982,false,1.9827127033139826
9.699999999999982,false,2.082712703313981
9.799999999999981,true,2.182712703313979
9.89999999999998,true,2.0556033801068625
9.99999999999998,true,1.9380648135073668
Loading

0 comments on commit 671246b

Please sign in to comment.