Skip to content

Commit

Permalink
Merge pull request #26 from INTO-CPS-Association/add-serializeFMUstat…
Browse files Browse the repository at this point in the history
…e-to-fmi2

added fmi2 serializeFMUstate
  • Loading branch information
lausdahl authored Jul 16, 2024
2 parents 2fe57e9 + ae74d49 commit 9a839a6
Show file tree
Hide file tree
Showing 6 changed files with 424 additions and 214 deletions.
4 changes: 4 additions & 0 deletions fmi2/src/main/java/org/intocps/fmi/IFmiComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ public abstract Fmi2Status setStrings(long[] scalarValueIndices,

public abstract Fmi2Status freeState(IFmiComponentState state) throws FmuInvocationException;

FmuResult<Long> getSerializedFMUstateSize(IFmiComponentState state) throws FmuInvocationException;
FmuResult<byte[]> serializeFMUstate(IFmiComponentState state, long size) throws FmuInvocationException;
FmuResult<IFmiComponentState> deSerializeFMUstate( byte[] bytes, long size) throws FmuInvocationException;

public abstract boolean isValid();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,50 @@ public Fmi2Status freeState(IFmiComponentState state)
throw new FmuInvocationException("Invalid state");
}

public FmuResult<Long> getSerializedFMUstateSize(IFmiComponentState state) throws FmuInvocationException{
if (state != null && state instanceof FmuComponentState)
{
FmuComponentState st = (FmuComponentState) state;
if (st.comp == this && st.allocated)
{
long[] byteSize= new long[1];
Fmi2Status res = Fmi2Status.valueOf(nSerializedFMUstateSize(fmuPtr, componentPtr, st.ptr,byteSize));

return new FmuResult<>(res,byteSize[0]);
}
}

throw new FmuInvocationException("Invalid state");

}
public FmuResult<byte[]> serializeFMUstate(IFmiComponentState state, long byteSize) throws FmuInvocationException{
if (state != null && state instanceof FmuComponentState)
{
FmuComponentState st = (FmuComponentState) state;
if (st.comp == this && st.allocated && byteSize>=0)
{
byte[] bytes= new byte[(int) byteSize];
Fmi2Status res = Fmi2Status.valueOf(nSerializeFMUstate(fmuPtr, componentPtr, st.ptr,bytes,byteSize));

return new FmuResult<>(res,bytes);
}
}

throw new FmuInvocationException("Invalid state");

}
public FmuResult<IFmiComponentState> deSerializeFMUstate( byte[] bytes, long size) throws FmuInvocationException{
long statePtr[] = new long[1];
Fmi2Status res = Fmi2Status.valueOf(nDeSerializeFMUstate(fmuPtr, componentPtr, bytes,size,statePtr));

if (res == Fmi2Status.OK)
{
return new FmuResult<IFmiComponentState>(res, new FmuComponentState(this, statePtr[0]));
}

return new FmuResult<IFmiComponentState>(res, null);
}

@Override
public FmuResult<Double> getMaxStepSize()
throws FmiInvalidNativeStateException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,12 @@ protected native byte nSetString(long fmu, long component, long[] vr,
// typedef fmi2Status fmi2FreeFMUstateTYPE (fmi2Component, fmi2FMUstate*);
protected native byte nFreeFmuState(long fmu, long component, long statePtr);

// We will not support serialization, it is too slow anyway
// typedef fmi2Status fmi2SerializedFMUstateSizeTYPE(fmi2Component, fmi2FMUstate, size_t*);
protected native byte nSerializedFMUstateSize(long fmu, long component, long statePtr, long[] size);
// typedef fmi2Status fmi2SerializeFMUstateTYPE (fmi2Component, fmi2FMUstate, fmi2Byte[], size_t);
protected native byte nSerializeFMUstate(long fmu, long component, long statePtr,byte[] bytes, long size);
// typedef fmi2Status fmi2DeSerializeFMUstateTYPE (fmi2Component, const fmi2Byte[], size_t, fmi2FMUstate*);
protected native byte nDeSerializeFMUstate(long fmu, long component, byte[] bytes, long size,long[] statePtr);

/* Getting partial derivatives */
// typedef fmi2Status fmi2GetDirectionalDerivativeTYPE(fmi2Component, const fmi2ValueReference[], size_t,
Expand Down
99 changes: 99 additions & 0 deletions jnifmuapi/src/main/native/src/comp_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,105 @@ Java_org_intocps_fmi_jnifmuapi_NativeFmuComponent_nFreeFmuState(
return res;
}


/*
* Class: org_intocps_fmi_jnifmuapi_NativeFmuComponent
* Method: nSerializedFMUstateSize
* Signature: (JJJ[J)B
*/
JNIEXPORT jbyte JNICALL Java_org_intocps_fmi_jnifmuapi_NativeFmuComponent_nSerializedFMUstateSize(
JNIEnv *env, jobject obj, jlong fmuPtr, jlong componentPtr, jlong statePtr, jlongArray sizeArray){

fmi2Component c = getCompPtr(componentPtr);

fmi2FMUstate state = ((fmi2FMUstate)statePtr);
size_t fmiStateSize;
fmi2Status res = getFmuPtr(fmuPtr)->serializedFMUstateSize(c, state, &fmiStateSize);
if( res == fmi2OK)
{
// Get a pointer to the elements of the array
jlong *elements = (*env)->GetLongArrayElements(env, sizeArray, NULL);

// Set the value at the specified index
elements[0] = fmiStateSize;

// Release the array elements and commit changes
(*env)->ReleaseLongArrayElements(env, sizeArray, elements, 0);
}

return res;
}

/*
* Class: org_intocps_fmi_jnifmuapi_NativeFmuComponent
* Method: nSerializeFMUstate
* Signature: (JJJ[BJ)B
*/
JNIEXPORT jbyte JNICALL Java_org_intocps_fmi_jnifmuapi_NativeFmuComponent_nSerializeFMUstate__JJJ_3BJ
(JNIEnv *env, jobject obj, jlong fmuPtr, jlong componentPtr, jlong statePtr, jbyteArray bytesArray, jlong bytesSize){

fmi2Component c = getCompPtr(componentPtr);

size_t len = bytesSize;

char bytes[len];

fmi2FMUstate state = ((fmi2FMUstate)statePtr);
fmi2Status status = getFmuPtr(fmuPtr)->serializeFMUstate(c,state, bytes, len);


if(status==fmi2OK)
{
jbyte *vbody = (*env)->GetByteArrayElements(env, bytesArray, 0);

int i;
for (i = 0; i < len; i++) {
vbody[i] = bytes[i];
}

(*env)->ReleaseByteArrayElements(env, bytesArray, vbody, 0);
}

return status;
}

/*
* Class: org_intocps_fmi_jnifmuapi_NativeFmuComponent
* Method: nDeSerializeFMUstate
* Signature: (JJ[BJ[J)B
*/
JNIEXPORT jbyte JNICALL Java_org_intocps_fmi_jnifmuapi_NativeFmuComponent_nDeSerializeFMUstate__JJ_3BJ_3J
(JNIEnv *env, jobject obj, jlong fmuPtr, jlong componentPtr, jbyteArray bytesArray, jlong bytesSize, jlongArray statePtrArr){

fmi2Component c = getCompPtr(componentPtr);

size_t len = bytesSize;

char bytes[len];
{
jbyte *vbody = (*env)->GetByteArrayElements(env, bytesArray, 0);

int i;
for (i = 0; i < len; i++) {
bytes[i] = vbody[i];
}

(*env)->ReleaseByteArrayElements(env, bytesArray, vbody, 0);
}
fmi2FMUstate state;
state = NULL;
fmi2Status status = getFmuPtr(fmuPtr)->deSerializeFMUstate(c, bytes, len,&state);
if(status==fmi2OK)
{
jlong *vbody = (*env)->GetLongArrayElements(env, statePtrArr, 0);

vbody[0] = (jlong) (state);

(*env)->ReleaseLongArrayElements(env, statePtrArr, vbody, 0);
}
return status;
}

// INTO CPS
JNIEXPORT jbyte JNICALL
Java_org_intocps_fmi_jnifmuapi_NativeFmuComponent_nGetMaxStepsize(
Expand Down
Loading

0 comments on commit 9a839a6

Please sign in to comment.