Page 1 of 1

NaN values when passing from a subroutine to another

Posted: Mon Jan 13, 2025 8:50 pm
by fracenvi
Hello,

I'm trying to run a C++ function within a cs_user_physical_properties.f90 subroutine. Everything has been implemented just fine following how this C++/Fortran coupling is done in other standard routines, but i don't get why when passing fields from the fortran to C++ NaN values will appear. The only additional processing that the data will be getting in the C++ routine is a stacking operation, to have a single input tensor instead of multiple ones. I posted the routines as well to give more context.

I tried on a separate testing case and this seems to not be happening.

What can possibly be the cause of this behavior? And eventually how can i fix it?

Thanks to everyone for the help,
Francesco

Re: NaN values when passing from a subroutine to another

Posted: Tue Jan 14, 2025 3:15 am
by Yvan Fournier
Hello,

Did you try running this on a debug build under Valgrind, or on a build with AddressSanitizer ? Since you have many arguments, a typo could easily hide in your code.

If this does not find any error, than printing outputs or checking for isnan() of values in the C code just before reading them back in Fortran can help determine whether the NaN"s are actually a product of the called code, or a problem with the iso-c bindings.

Also, using the C version of cs_user_physical_properties can remove the need for complex iso-C bindings, and simplify the whole thing.

You can even move cs_user_physical_properties from C to C++ by changing its extension to .cxx or .cpp, so as to be able to place the code in predict4fort.cpp directly in it. In this case, you simply need to use

Code: Select all

extern "C" void
cs_user_physical_properties
instead of just

Code: Select all

void
cs_user_physical_properties
To ensure the code links and is called correctly.

Best regards,

Yvan