How to add source code written in c++

Questions and remarks about code_saturne usage
Forum rules
Please read the forum usage recommendations before posting.
Post Reply
lehongduc
Posts: 23
Joined: Thu Aug 20, 2015 3:05 pm

How to add source code written in c++

Post by lehongduc »

Hello all,

I'm using CSv4.0.

I has a vey basic question i think. I would like to use the cs_user_postprocess.c instead of GUI. When I copy the source code cs_user_postprocess.c from /SRC/REFERENCE/ to /SRC, should I also looking for all the "local headers" in cs_user_postprocess.c and put it in the same repertory, i.e. /SRC? Is there another way simpler to use the souce code written in c++?

I hope that my question is clear.

Thanks in advance,
Best regard,
LE Hong Duc
leguichet
Posts: 34
Joined: Wed Sep 03, 2014 1:00 pm

Re: How to add source code written in c++

Post by leguichet »

You just need to copy the .c file in the /SRC folder. The makefile ensures that all the headers will be found automatically and correctly. What you need to do is just modifier the .c file, unless you include new libraries.

The Code is written in C and Fortran. I am not sure that you can use c++ in a C code.
User avatar
jcharris
Posts: 48
Joined: Tue Apr 16, 2019 7:28 pm

Re: How to add source code written in c++

Post by jcharris »

Has the status of C++ (or other languages) changed in recent years?

I see that it's possible to include a C++ file with suffix .cxx, for example, which the Python bits will recognize and on my setup get compiled by g++, but gcc will still be used as the linker and then crash.

Of course the missing part is just the C++ library, so one could test to show that there is no problem tying the two together, such as :

Code: Select all

code_saturne compile  --libs=-lstdc++
but there appears to be no corresponding option for adding libraries in the GUI or with the "code_saturne run" command.

I'm guessing that it could be possible to adjust the options with the initial ./configure command before compiling Code_Saturne at all, but while this is fine for small problems, could be difficult for dealing with existing configurations on clusters and such, I presume?

(Alternatively, of course there exist things to translate C++ into C code, but it would be nice to find a more natural solution if possible.)
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: How to add source code written in c++

Post by Yvan Fournier »

Hello,

Actually the choice of the liner also depends on some optional libraries. For example, for a build using MED or another library requiring C++, the linker defaults to C++.

For other cases, you can try adding CS_LD=cxx or CS_LD=mpicxx to your configure line when installing. It should in theory do the trick.

To add things in cs_run, you can use the cs_user_scripts.py (copying it from DATA/REFERENCE to DATA to make activate it), and setting the domain.compile* fields (remove the "if domain.param == None" test).

Best regards,

Yvan
User avatar
jcharris
Posts: 48
Joined: Tue Apr 16, 2019 7:28 pm

Re: How to add source code written in c++

Post by jcharris »

Ah, interesting, thanks ! I can confirm that it works.

To say more precisely for those who may be interested, I was able to use C++ code without recompiling Code_Saturne -- first by using a ".cxx" suffix in the user routines in place of a ".c" suffix (for my test, cs_user_extra_operations.cxx), and copying cs_user_scripts.py as mentioned out of the REFERENCE folder into DATA/, then adding the line:

Code: Select all

domain.compile_libs = '-lstdc++'
on line 161 of the script.

After that, the simulation runs without any problem, even from the GUI (though I didn't test it for anything terribly exotic yet ; one can always imagine ways to break the code...).

Presumably this is also the "correct" way if we would like to access existing libraries from the user routines?
Yvan Fournier
Posts: 4070
Joined: Mon Feb 20, 2012 3:25 pm

Re: How to add source code written in c++

Post by Yvan Fournier »

Hello,

Yes, this was designed so users can link to additional libraries (most commonly used for post-processing/data extraction but could also be used for fluid property libraries, advanced source term or boundary conditions, ...)

Best regards,

Yvan
Post Reply