Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Limiting range of arguments supplied to external function

Limiting range of arguments supplied to external function

I'm using an external thermodynamic properties library, Cantera.  I have a whole bunch of external functions that are used to interface Cantera to Modelica.  The model is crashing because there's a negative temperature being supplied.  All of the temperatures that I can see in the output are well within the acceptable range, so I suspect a negative temperature is being passed as a result of a linear solver, non-linear solver, or a numerical jacobian evaluation.

Any ideas for how to track down the precise cause of the bad call?  I suppose I could try to figure out how to run the executable with gdb or something and get a backtrace.  Is that a good way?

I've been thinking about how I can code defensively so that negative temperatures don't get the the external library and cause it to throw and error and crash the simulation. 

This is what an example wrapper looks like now:

Code:

extern "C" void rho_TPY(void* obj,double T, double P, double* Y,

    double* rho)
{
  CanteraGas* cantera = reinterpret_cast<CanteraGas*>(obj);
  cantera->gas.setState_TPY(T,P,Y);
  *rho = cantera->gas.density(); //kg/kmol
  return;
}

I might try to add logic so that negative temperatures don't get sent to Cantera and add a error value the call signature.  Can modelica do anything with the error return variable in this proposed revision? 

Code:

extern "C" void rho_TPY(void* obj,double T, double P, double* Y,

    double* rho, int* error)
{
  CanteraGas* cantera = reinterpret_cast<CanteraGas*>(obj);
  if (*T >= T_min)
  {
      cantera->gas.setState_TPY(T,P,Y);
      *rho = cantera->gas.density(); //kg/kmol
      *error = 0;
  }
  else{*error=1;}
  return;
}

Thanks for your help

Adam

Edited by: adamLange - May-18-17 20:37:18

Re: Limiting range of arguments supplied to external function

I clamped the range of the temperature in my external c code and retained the original function call signature.  It seems to work now.   

There are 0 guests and 0 other users also viewing this topic
You are here: