Skip to content

Commit

Permalink
updated initialization to work with fmi3 all types
Browse files Browse the repository at this point in the history
  • Loading branch information
lausdahl committed Nov 15, 2023
1 parent 3bd84c1 commit b5d763c
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ public boolean has(Fmi3Causality scalarAttributeType) {


public Optional<Fmi2ModelDescription.ScalarVariable> getFmi2ScalarVariable() {
return Optional.of(getScalarVariable(Fmi2ModelDescription.ScalarVariable.class));
Fmi2ModelDescription.ScalarVariable sv = getScalarVariable(Fmi2ModelDescription.ScalarVariable.class);
return sv == null ? Optional.empty() : Optional.of(sv);
}

public Optional<Fmi3ModelDescription.Fmi3ScalarVariable> getFmi3ScalarVariable() {
return Optional.of(getScalarVariable(Fmi3ModelDescription.Fmi3ScalarVariable.class));
Fmi3ModelDescription.Fmi3ScalarVariable sv = getScalarVariable(Fmi3ModelDescription.Fmi3ScalarVariable.class);
return sv == null ? Optional.empty() : Optional.of(sv);
}

public SCALAR_TYPE getScalarVariable() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.intocps.maestro.framework.fmi2.api.mabl;

import org.intocps.maestro.ast.LexIdentifier;
import org.intocps.maestro.ast.node.*;
import org.intocps.maestro.fmi.fmi3.Fmi3Causality;
import org.intocps.maestro.fmi.fmi3.Fmi3ModelDescription;
Expand All @@ -12,7 +13,7 @@

import static org.intocps.maestro.ast.MableAstFactory.*;

public class PortFmi3Api implements FmiBuilder.Port<Fmi3ModelDescription.Fmi3ScalarVariable,PStm> {
public class PortFmi3Api implements FmiBuilder.Port<Fmi3ModelDescription.Fmi3ScalarVariable, PStm> {

public final InstanceVariableFmi3Api aMablFmi3InstanceAPI;
public final Fmi3ModelDescription.Fmi3ScalarVariable scalarVariable;
Expand Down Expand Up @@ -75,26 +76,26 @@ public PType getType() {
case ClockType:
return new AFloatNumericPrimitiveType();
case Int8Type:
return new AShortNumericPrimitiveType();
case UInt8Type:
return new AByteNumericPrimitiveType();
case Int16Type:
case UInt16Type:
return new AShortNumericPrimitiveType();
case Int32Type:
return newIntType();
case UInt32Type:
return newIntType();
case Int64Type:
case UInt64Type:
new ALongNumericPrimitiveType();
case EnumerationType:
return new ALongNumericPrimitiveType();
case BooleanType:
return newBoleanType();
case StringType:
return newStringType();
case BinaryType:
return null;
case EnumerationType:
new ALongNumericPrimitiveType();
return new ANameType(new LexIdentifier("ByteArrayArray", null));
default:
return null;
throw new RuntimeException("Unknown port type mapping. Port type: " + scalarVariable.getVariable().getTypeIdentifier());
}
}

Expand Down Expand Up @@ -125,7 +126,7 @@ public Long getPortReferenceValue() {


@Override
public void linkTo(FmiBuilder.Port<Fmi3ModelDescription.Fmi3ScalarVariable,PStm>... receivers) throws PortLinkException {
public void linkTo(FmiBuilder.Port<Fmi3ModelDescription.Fmi3ScalarVariable, PStm>... receivers) throws PortLinkException {

if (receivers == null || receivers.length == 0) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public class InstanceVariableFmi3Api extends VariableFmi2Api<FmiBuilder.NamedVar
private List<String> variabesToLog;

public InstanceVariableFmi3Api(PStm declaration, FmuVariableFmi3Api parent, String name, ModelDescriptionContext3 modelDescriptionContext,
MablApiBuilder builder, IMablScope declaringScope, PStateDesignator designator, PExp referenceExp) {
MablApiBuilder builder, IMablScope declaringScope, PStateDesignator designator, PExp referenceExp) {
this(declaration, parent, name, modelDescriptionContext, builder, declaringScope, designator, referenceExp, name);
}

public InstanceVariableFmi3Api(PStm declaration, FmuVariableFmi3Api parent, String name, ModelDescriptionContext3 modelDescriptionContext,
MablApiBuilder builder, IMablScope declaringScope, PStateDesignator designator, PExp referenceExp, String environmentName) {
MablApiBuilder builder, IMablScope declaringScope, PStateDesignator designator, PExp referenceExp, String environmentName) {
super(declaration, newANameType("FMI3Instance"), declaringScope, builder.getDynamicScope(), designator, referenceExp);
this.owner = parent;
this.name = name;
Expand Down Expand Up @@ -135,7 +135,7 @@ private ArrayVariableFmi2Api<Object> getValueReferenceBuffer() {
}

private ArrayVariableFmi2Api<Object> getBuffer(Map<PType, ArrayVariableFmi2Api<Object>> buffer, PType type, String prefix, int size,
IMablScope scope) {
IMablScope scope) {
Optional<PType> first = buffer.keySet().stream().filter(x -> x.toString().equals(type.toString())).findFirst();
if (first.isEmpty()) {
ArrayVariableFmi2Api<Object> value = createBuffer(type, prefix, size, scope);
Expand Down Expand Up @@ -170,7 +170,7 @@ private ArrayVariableFmi2Api<Object> getSharedBuffer(PType type) {

@SuppressWarnings("unchecked")
private ArrayVariableFmi2Api createMDArrayRecursively(List<Integer> arraySizes, PStm declaringStm, PStateDesignatorBase stateDesignator,
PExpBase indexExp) {
PExpBase indexExp) {

if (arraySizes.size() > 1) {
List<VariableFmi2Api> arrays = new ArrayList<>();
Expand Down Expand Up @@ -252,7 +252,7 @@ public void setDebugLogging(List<String> categories, boolean enableLogging) {

@Override
public void setupExperiment(FmiBuilder.DoubleVariable<PStm> startTime, FmiBuilder.DoubleVariable<PStm> endTime,
FmiBuilder.BoolVariable<PStm> endTimeDefined, Double tolerance) {
FmiBuilder.BoolVariable<PStm> endTimeDefined, Double tolerance) {
this.setupExperiment(((DoubleVariableFmi2Api) startTime).getReferenceExp().clone(),
((DoubleVariableFmi2Api) endTime).getReferenceExp().clone(), ((BooleanVariableFmi2Api) endTimeDefined).getReferenceExp().clone(),
tolerance);
Expand Down Expand Up @@ -287,22 +287,22 @@ public void enterInitializationMode(boolean toleranceDefined, double tolerance,

@Override
public void enterInitializationMode(FmiBuilder.Scope<PStm> scope, boolean toleranceDefined, double tolerance, double startTime,
boolean stopTimeDefined, double stopTime) {
boolean stopTimeDefined, double stopTime) {
this.enterInitializationMode(builder.getDynamicScope(), VarWrap.wrap(toleranceDefined), VarWrap.wrap(tolerance), VarWrap.wrap(startTime),
VarWrap.wrap(stopTimeDefined), VarWrap.wrap(stopTime));

}

@Override
public void enterInitializationMode(FmiBuilder.BoolVariable<PStm> toleranceDefined, FmiBuilder.DoubleVariable<PStm> tolerance,
FmiBuilder.DoubleVariable<PStm> startTime, FmiBuilder.BoolVariable<PStm> stopTimeDefined, FmiBuilder.DoubleVariable<PStm> stopTime) {
FmiBuilder.DoubleVariable<PStm> startTime, FmiBuilder.BoolVariable<PStm> stopTimeDefined, FmiBuilder.DoubleVariable<PStm> stopTime) {
enterInitializationMode(builder.getDynamicScope(), toleranceDefined, tolerance, startTime, stopTimeDefined, stopTime);
}

@Override
public void enterInitializationMode(FmiBuilder.Scope<PStm> scope, FmiBuilder.BoolVariable<PStm> toleranceDefined,
FmiBuilder.DoubleVariable<PStm> tolerance, FmiBuilder.DoubleVariable<PStm> startTime, FmiBuilder.BoolVariable<PStm> stopTimeDefined,
FmiBuilder.DoubleVariable<PStm> stopTime) {
FmiBuilder.DoubleVariable<PStm> tolerance, FmiBuilder.DoubleVariable<PStm> startTime, FmiBuilder.BoolVariable<PStm> stopTimeDefined,
FmiBuilder.DoubleVariable<PStm> stopTime) {

final String method = "enterInitializationMode";

Expand Down Expand Up @@ -332,7 +332,7 @@ public void exitInitializationMode() {

@Override
public void setupExperiment(FmiBuilder.Scope<PStm> scope, FmiBuilder.DoubleVariable<PStm> startTime, FmiBuilder.DoubleVariable<PStm> endTime,
FmiBuilder.BoolVariable<PStm> endTimeDefined, Double tolerance) {
FmiBuilder.BoolVariable<PStm> endTimeDefined, Double tolerance) {
this.setupExperiment(scope, ((DoubleVariableFmi2Api) startTime).getReferenceExp().clone(),
((DoubleVariableFmi2Api) endTime).getReferenceExp().clone(), endTimeDefined.getExp().clone(), tolerance);
}
Expand All @@ -358,32 +358,32 @@ public void exitInitializationMode(FmiBuilder.Scope<PStm> scope) {

@Override
public Map.Entry<FmiBuilder.BoolVariable<PStm>, FmiBuilder.DoubleVariable<PStm>> step(FmiBuilder.Scope<PStm> scope,
FmiBuilder.DoubleVariable<PStm> currentCommunicationPoint, FmiBuilder.DoubleVariable<PStm> communicationStepSize,
FmiBuilder.BoolVariable<PStm> noSetFMUStatePriorToCurrentPoint) {
FmiBuilder.DoubleVariable<PStm> currentCommunicationPoint, FmiBuilder.DoubleVariable<PStm> communicationStepSize,
FmiBuilder.BoolVariable<PStm> noSetFMUStatePriorToCurrentPoint) {
return step(scope, currentCommunicationPoint, communicationStepSize, ((VariableFmi2Api) noSetFMUStatePriorToCurrentPoint).getReferenceExp());
}

@Override
public Map.Entry<FmiBuilder.BoolVariable<PStm>, FmiBuilder.DoubleVariable<PStm>> step(FmiBuilder.Scope<PStm> scope,
FmiBuilder.DoubleVariable<PStm> currentCommunicationPoint, FmiBuilder.DoubleVariable<PStm> communicationStepSize) {
FmiBuilder.DoubleVariable<PStm> currentCommunicationPoint, FmiBuilder.DoubleVariable<PStm> communicationStepSize) {
return step(scope, currentCommunicationPoint, communicationStepSize, newABoolLiteralExp(false));
}

@Override
public Map.Entry<FmiBuilder.BoolVariable<PStm>, FmiBuilder.DoubleVariable<PStm>> step(FmiBuilder.DoubleVariable<PStm> currentCommunicationPoint,
FmiBuilder.DoubleVariable<PStm> communicationStepSize, FmiBuilder.BoolVariable<PStm> noSetFMUStatePriorToCurrentPoint) {
FmiBuilder.DoubleVariable<PStm> communicationStepSize, FmiBuilder.BoolVariable<PStm> noSetFMUStatePriorToCurrentPoint) {
return step(dynamicScope, currentCommunicationPoint, communicationStepSize, noSetFMUStatePriorToCurrentPoint);
}

@Override
public Map.Entry<FmiBuilder.BoolVariable<PStm>, FmiBuilder.DoubleVariable<PStm>> step(FmiBuilder.DoubleVariable<PStm> currentCommunicationPoint,
FmiBuilder.DoubleVariable<PStm> communicationStepSize) {
FmiBuilder.DoubleVariable<PStm> communicationStepSize) {
return step(dynamicScope, currentCommunicationPoint, communicationStepSize, newABoolLiteralExp(false));
}

private Map.Entry<FmiBuilder.BoolVariable<PStm>, FmiBuilder.DoubleVariable<PStm>> step(FmiBuilder.Scope<PStm> scope,
FmiBuilder.DoubleVariable<PStm> currentCommunicationPoint, FmiBuilder.DoubleVariable<PStm> communicationStepSize,
PExp noSetFMUStatePriorToCurrentPoint) {
FmiBuilder.DoubleVariable<PStm> currentCommunicationPoint, FmiBuilder.DoubleVariable<PStm> communicationStepSize,
PExp noSetFMUStatePriorToCurrentPoint) {

scope.add(newAAssignmentStm(((IMablScope) scope).getFmiStatusVariable().getDesignator().clone(),
newACallExp(this.getReferenceExp().clone(), newAIdentifier("doStep"),
Expand Down Expand Up @@ -493,7 +493,7 @@ public <V> Map<PortFmi3Api, VariableFmi2Api<V>> get(FmiBuilder.Port<Fmi3ModelDes
@SuppressWarnings("unchecked")
@Override
public <V> Map<PortFmi3Api, VariableFmi2Api<V>> get(FmiBuilder.Scope<PStm> scope,
FmiBuilder.Port<Fmi3ModelDescription.Fmi3ScalarVariable, PStm>... ports) {
FmiBuilder.Port<Fmi3ModelDescription.Fmi3ScalarVariable, PStm>... ports) {

List<PortFmi3Api> selectedPorts;
if (ports == null || ports.length == 0) {
Expand All @@ -519,53 +519,14 @@ public <V> Map<PortFmi3Api, VariableFmi2Api<V>> get(FmiBuilder.Scope<PStm> scope
scope.add(newAAssignmentStm(designator, newAIntLiteralExp(p.getPortReferenceValue().intValue())));
}


PType type;
switch (e.getKey()) {
case Float64Type:
type = new ARealNumericPrimitiveType();
break;
case Float32Type:
type = new AFloatNumericPrimitiveType();
break;
case Int8Type:
type = new AByteNumericPrimitiveType();
break;
case Int16Type:
type = new AShortNumericPrimitiveType();
break;
case Int32Type:
type = new AIntNumericPrimitiveType();
break;
case Int64Type:
type = new ALongNumericPrimitiveType();
break;

// TODO add the rest

// case Boolean:
// type = new ABooleanPrimitiveType();
// break;
// case Real:
// type = new ARealNumericPrimitiveType();
// break;
// case Integer:
// type = new AIntNumericPrimitiveType();
// break;
// case String:
// type = new AStringPrimitiveType();
// break;
// case Enumeration:
// throw new RuntimeException("Cannot assign enumeration port type.");
default:
throw new RuntimeException("Cannot match port types.");
}
//all ports should have the same type so lets use the first port to define the io array type
PType type = e.getValue().get(0).getType();

ArrayVariableFmi2Api<Object> valBuf = getIOBuffer(type);

AAssigmentStm stm = newAAssignmentStm(((IMablScope) scope).getFmiStatusVariable().getDesignator().clone(),
call(this.getReferenceExp().clone(), createFunctionName(FmiFunctionType.GET, e.getValue().get(0)),
vrefBuf.getReferenceExp().clone(), newAUIntLiteralExp((long) e.getValue().size()), valBuf.getReferenceExp().clone()));
vrefBuf.getReferenceExp().clone(), newAUIntLiteralExp((long) e.getValue().size()), newARefExp(valBuf.getReferenceExp().clone())));
scope.add(stm);

handleError(scope, createFunctionName(FmiFunctionType.GET, e.getValue().get(0)));
Expand Down Expand Up @@ -748,6 +709,10 @@ private String createFunctionName(FmiFunctionType f, Fmi3TypeEnum type) {
throw new RuntimeException("Attempting to call non type-dependant function with type: " + type);
}
functionName += type.name().substring(0, type.name().length() - "Type".length());

if (functionName.endsWith("Enumeration")) {
functionName = functionName.replace("Enumeration", "UInt64");
}
return functionName;
}

Expand Down Expand Up @@ -962,7 +927,7 @@ public void set(FmiBuilder.Scope<PStm> scope, List<PortFmi3Api> selectedPorts, F
* @param scope the builder scope
*/
public void setDerivatives(ArrayVariableFmi2Api derValInBuf, ArrayVariableFmi2Api derOrderInBuf, ArrayVariableFmi2Api derRefInBuf,
FmiBuilder.Scope<PStm> scope) {
FmiBuilder.Scope<PStm> scope) {
final String method = "setRealInputDerivatives";
int arraySize = derValInBuf.size();

Expand Down Expand Up @@ -990,7 +955,7 @@ public <V> void set(FmiBuilder.Port<Fmi3ModelDescription.Fmi3ScalarVariable, PSt
@SuppressWarnings("unchecked")
@Override
public <V> void set(FmiBuilder.Scope<PStm> scope, FmiBuilder.Port<Fmi3ModelDescription.Fmi3ScalarVariable, PStm> port,
FmiBuilder.Variable<PStm, V> value) {
FmiBuilder.Variable<PStm, V> value) {
this.set(scope, new PortVariableMapImpl(Map.of(port, value)));
}

Expand Down Expand Up @@ -1336,7 +1301,7 @@ public enum FmiFunctionType {
*/
static class FmiStatusErrorHandlingBuilder {
static void generate(MablApiBuilder builder, String method, InstanceVariableFmi3Api instance, IMablScope scope,
MablApiBuilder.Fmi3Status... statusesToFail) {
MablApiBuilder.Fmi3Status... statusesToFail) {
if (statusesToFail == null || statusesToFail.length == 0) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ private static Value getFmuInstanceValue(BufferedOutputStream fmuLogOutputStream


try {
FmuResult<byte[][]> res = instance.getBinary(scalarValueIndices);
FmuResult<byte[][]> res = instance.getBinary(scalarValueIndices, scalarValueIndices.length);

buffer.getModule().clear();
for (int i = 0; i < res.result.length; i++) {
Expand Down
Loading

0 comments on commit b5d763c

Please sign in to comment.