modelon-community / fmi-library

C library for importing FMUs
Other
116 stars 34 forks source link

fmi2_import_get_real_variable_start(fmi2_import_real_variable_t*) API is returning trimmed value #124

Closed SudeepGhoshIN closed 5 months ago

SudeepGhoshIN commented 5 months ago

Hi Team,

I have a requirement of fetching start value for signal variables in FMI2.0. As part of this , I am trying to utilize fmi2_import_get_real_variable_start(fmi2_import_real_variable_t*) API to get the variable value start. But unfortunately, above mentioned API is returning trimmed value, so I could not proceed further for retrieving the proper start value.

My question is, is it expected behavior for fmi2_import_get_real_variable_start(fmi2_import_real_variable_t) API not to read start value and return trimmed value as bellow ? If so then what is the pattern ? We are using this code-> _fmi2_import_real_variable_t rv = fmi2_import_get_variable_as_real(var); fmi2_real_t StartValue = fmi2_import_get_real_variablestart(rv); We are getting this value: 1) 0.997497 55859375 --> 0.997498 2) 0.996662 327892311 --> 0.996662 3) 1.08699500201411 --> 1.087

Thanks, Sudeep

PeterMeisrimelModelon commented 5 months ago

Hi SudeepGhoshIN,

I did a small test with the values you provided and do not observe any truncation issues.

However, the truncation you describe would be consistent with a default accuracy you would get with printf("start=%f", StartValue).

Please verify this is not a printing issue? If not, can you please provide a minimal reproducer (complete call sequence + modelDescription)?

/Peter

SudeepGhoshIN commented 5 months ago

ModelDescription:

  <ScalarVariable name="Parameters.AmbientHumConvert_PartialPressure_y[38]" valueReference="611" causality="parameter" variability="tunable">
      <Real start="0.876435000755719"/>
    </ScalarVariable>
    <ScalarVariable name="Parameters.AmbientHumConvert_PartialPressure_y[39]" valueReference="612" causality="parameter" variability="tunable">
      <Real start="0.942482643066795"/>
    </ScalarVariable>
    <ScalarVariable name="Parameters.AmbientHumConvert_PartialPressure_y[40]" valueReference="613" causality="parameter" variability="tunable">
      <Real start="1.01260562980966"/>
    </ScalarVariable>
    <ScalarVariable name="Parameters.AmbientHumConvert_PartialPressure_y[41]" valueReference="614" causality="parameter" variability="tunable">
      <Real start="1.08699500201411"/>
    </ScalarVariable>
    <ScalarVariable name="Parameters.AmbientHumConvert_PartialPressure_y[42]" valueReference="615" causality="parameter" variability="tunable">
      <Real start="1.16584749044042"/>
    </ScalarVariable>
    <ScalarVariable name="Parameters.AmbientHumConvert_PartialPressure_y[43]" valueReference="616" causality="parameter" variability="tunable">
      <Real start="1.24936555283518"/>
    </ScalarVariable>
    <ScalarVariable name="Parameters.AmbientHumConvert_PartialPressure_y[44]" valueReference="617" causality="parameter" variability="tunable">
      <Real start="1.33775740784234"/>
    </ScalarVariable>
    <ScalarVariable name="Parameters.AmbientHumConvert_PartialPressure_y[45]" valueReference="618" causality="parameter" variability="tunable">
      <Real start="1.43123706561278"/>
    </ScalarVariable>

Code Sequence:

fmi2_import_variable_t* var = fmi2_import_get_variable(varList, i);
baseType = fmi2_import_get_variable_base_type(var);
VariableType = fmi2_base_type_to_string(baseType);
if (VariableType == "Real")
{
  fmi2_import_real_variable_t* rv = fmi2_import_get_variable_as_real(var);                    
  fmi2_real_t StartValue = fmi2_import_get_real_variable_start(rv);  
}
PeterMeisrimelModelon commented 5 months ago

Hi SudeepGhoshIN,

I took your modelDescription.xml and the following code

#include <stdio.h>
#include "fmilib.h"

int main(int argc, char **argv) {
    fmi_import_context_t *ctx = fmi_import_allocate_context(jm_get_default_callbacks());
    fmi2_import_t* xml = fmi2_import_parse_xml(ctx, ".", NULL);

    fmi2_import_variable_list_t* varList = fmi2_import_get_variable_list(xml, 0);
    for (int i = 0; i < fmi2_import_get_variable_list_size(varList); i++) {
        fmi2_import_variable_t* var = fmi2_import_get_variable(varList, i);
        printf("Variable, %s\n", fmi2_import_get_variable_name(var));
        int hasStartValue = fmi2_import_get_variable_has_start(var);
        printf("hasStartValue ? %i \n", hasStartValue);
        if (hasStartValue) {
            fmi2_import_real_variable_t *rv = fmi2_import_get_variable_as_real(var);
            printf("start = %2.15f\n", fmi2_import_get_real_variable_start(rv));
        }
    }

    fmi2_import_free(xml);
}

does produce the expected output

Variable, Parameters.AmbientHumConvert_PartialPressure_y[38]
hasStartValue ? 1 
start = 0.876435000755719
Variable, Parameters.AmbientHumConvert_PartialPressure_y[39]
hasStartValue ? 1 
start = 0.942482643066795
Variable, Parameters.AmbientHumConvert_PartialPressure_y[40]
hasStartValue ? 1 
start = 1.012605629809660
Variable, Parameters.AmbientHumConvert_PartialPressure_y[41]
hasStartValue ? 1 
start = 1.086995002014110
Variable, Parameters.AmbientHumConvert_PartialPressure_y[42]
hasStartValue ? 1 
start = 1.165847490440420
Variable, Parameters.AmbientHumConvert_PartialPressure_y[43]
hasStartValue ? 1 
start = 1.249365552835180
Variable, Parameters.AmbientHumConvert_PartialPressure_y[44]
hasStartValue ? 1 
start = 1.337757407842340
Variable, Parameters.AmbientHumConvert_PartialPressure_y[45]
hasStartValue ? 1 
start = 1.431237065612780

If this does not help, please provide a complete reproducer that produces the unexpected output.

/Peter

SudeepGhoshIN commented 5 months ago

Hi Peter, Thanks for your help. I have one more question if modelDescription.xml having `

</ScalarVariable>
<ScalarVariable name="Parameters.PartialWaterPressure_Wet_P3_Siz[1]" valueReference="572" causality="parameter" variability="tunable">
  <Real start="102.0"/>
</ScalarVariable>`

fmi2_import_real_variable_t *rv = fmi2_import_get_variable_as_real(var); printf("start = %2.15f\n", fmi2_import_get_real_variable_start(rv));

        it is returning 1.00000000 inspite of 1.0 , how to fix this?
PeterMeisrimelModelon commented 5 months ago

Hi SudeepGhoshIN,

1.00000000 is identical to 1.0, so I don't see the issue? I suppose this could be made prettier with appropriate formatting for printf, but this is a general C question and not specific to FMILibrary.

/Peter