How to interface a Fortran thermodynamic tool with OpenFOAM?
Hi everybody!
I am looking to interface a specific thermodynamic tool with OpenFOAM. This program is written in Fortran. From a Pressure, a temperature and a composition it can provide a density : rho=f(P,T,x) I wonder how to bound these different programs. I would like to keep the OpenFOAM structure and evaluate the density at each time step with a command as : thermo.rho(); Is anybody already performed such an interfacing ? Can you give some hints to start ? from which thermophysicalModels can I be inspired? Regards, Cyp |
Hi,
compile the fortran programm as a dynamic library. Then, use that library in OpenFoam. Is the programm you want the use in OpenFoam Refprop (NIST)? Regards, Christian |
Hi Chris!
Thank you for your answer!! Indeed it is exactly the NIST REFPROP I want to use with OpenFOAM! Did you already experienced to call REFPROP from OpenFOAM ? For the moment, I compiled the Fortran subroutine as a shared object (.so). How can I call it from OF ?? Best regards, Cyp |
Hi,
yes, I have implemented Refprop in OpenFoam. However, I noticed that Refprop is far too slow to be used in OpenFOAM. The simulation time will increase in order of many magnitudes. Regards, Christian |
Hi
another way to do this (we use this with REFPROP) is to create a lookup table using REFPROP with rho as a function of (P,T,x). Then write a class called, say, thermoprops. when you call thermo.rho(P,T,x) (where thermo is of type thermoprops) , it would interpolate in this table and return the required rho. Hope this helps Kshitij |
Hi!
Quote:
Quote:
I think my main problem is either I chose a direct call of REFPROP or an interpolation from a table, is the creation of the thermoprops class... From which existed class can I start ?? Regards, Cyp |
yes. so you would have something like
#P T x rho 1000 273 0.01 900 1200 273 0.01 890 ..... and you would interpolate to get rho=f(P,T,x). another idea you could try ( I dont know if this is feasible in your case), instead of using P, T and x, you could use P,H. This way the interpolation would be simplified. I dont know of any existing class that does this interpolation. We had to write our own. |
Thank you for your answers! For the moment, I would like to test to implement a direct call from REFPROP instead of a reading in a table.
As a first test, and to become familiar with the thermodynamic class programmation in OpenFOAM, I try to create my own class from basicRhoThermo : rho=f(T,P). However, when I read the .C and .H files, I can't where rho is calculated... Do you have any idea ? Regards, Cyp |
basicRhoThermo is a base class. since rho() is a virtual function, it would be calculated in some class that is derived from basicRhoThermo. If you look at some class that is derived from basicRhoThermo, you might get an idea.
I am still using 1.5-dev so I dont have basicRhoThermo. Sorry that I cant be of more help |
Hi,
might I ask, what exactly you want to simulated? Ok, here is what you have to do to implement refprop in OpenFoam. This is not the best way to do it, but it works. Start from the file hPsiThermo (or hRhoThermo). In this file, you have to call the Refprop functions. In the constuctor, you have to create the Refprop object. You also need a second "rho" field. This is needed because rho not directly connected to psi anymore (as it is for perfect gases). In this class, all the functions have to be changed so that the Refprop functions are called. Now comes the tricky part. Firstly, you have to change the enthalpy BC's. The "normal" BC are programmed for perfect gases, so we have h(T) instead of h(T,p). You also need to change the functions in basicThermo called by the enthalpy BC. As I said, Refprop is very slow and you have to solve the fundamental equations for each grid point (without changing refprop even more than once). About using tables, it is a possibility. But one problem is that you get an error and I don't mean the "normal" interpolation error. The problem is that thermo physical properties are connected to each other e.g. c_p=dh/dT. So, the interpolated value of c_p might not correspond correctly with you enthalpy. Regards, Christian |
Hi Christian!
Thank you for your extended answer! In my investigation fields, the final step will be to simulate a two-phase multicomponent flow. I already have developed models for the mass fractions in each phase and I need to obtain the density of each phase from REFPROP. For each phase, density depends on Temperature, Pressure and Composition. It is my final objectives. I will consider a rough mesh. So I hope that the use of REFPROP will not have a consequent impact on my CPU time... For the moment, to become familiar with OpenFoam/REFPROP interfacing, I tried the implementation on a simpler model : just one phase is considered. There is no question of enthalpy, Cp and other thermodynamic properties. Just the density (I call the TPRHO() Refprop subroutine). As you advised me, I started from hPsiThermo. I saw enthalpy, Cp and Cv variables. Since I not consider such variables, I suppose I can rid of them ? And so I can rid of the part where I should change the enthalpy BC, can't I ? I am not sure to well understand the part with the second density. Do you mean I have to create a rho2 variable in hPsiThermo.H file ? Moreover, if I understand well, it is in the hPsiThermo.C file that I call the REFPROP function ? Thank you very much for your help. |
Hi,
I'm also interested in multiphase flow. So you have phase change in your application? How did you get your pressure equation? But back to your question: "As you advised me, I started from hPsiThermo. I saw enthalpy, Cp and Cv variable. Since I not consider such variables, I suppose I can rid of them ?" For now, yes. But for a real compressible flow, no, you need this functions like "template<class MixtureType> Foam::tmp<Foam::scalarField> Foam::hPsiThermo<MixtureType>::Cp" even so the variables given to the function have to be changed. Within, this function, you must call the refprop routines. "I am not sure to well understand the part with the second density. Do you mean I have to create a rho2 variable in hPsiThermo.H file?" About the second density field. If you look at the rho function in basicPsiThermo.C ( virtual tmp<volScalarField> rho() const { return p_*psi(); }) You see that rho is connected to psi for perfect gases, so that no rho field has to be defined to save the results for rho. For real fluids, there is no relationship as the one above between rho and psi, so you have the save the results for rho separately. "And so I can rid of the part where I should change the enthalpy BC, can't I ?" If you don't have a energy equation, yes, you bypass the BC problem. However this is not correct for compressible flows This BC functions are in different classes (within a subsubfolder of the basic folder). This classes call functions in basicThermo, which are later overwriten by the functions in your new "hPsiThermo". "(I call the TPRHO() Refprop subroutine)". Unless you rewrite the energy equations (I would not use energy equations based on temperature for real fluids), you need a function like rho(p,h) and rho(p,T) (for BC's). "Moreover, if I understand well, it is in the hPsiThermo.C file that I call the REFPROP function?" Yes, you can. Regards, Christian |
Yes I have phase change in my application. For the pressure equation, since I work in a porous medium, I developed an IMPES method (Implicit Pressure, Explicit Saturation). Pressures within each phase are related by a capillary pressure which depends on the saturation.
I was thinking about the creation of a second rho to rid of the perfect gas law. I tried to define a new base class which is a copy of basicPsiThermo (I called it testThermo1). In the equivalent of the makeBasicPsiThermo.H file, I could precised only two function : the Cthermo and the Mixture. So I could have only one rho variable. What do you think about that ? In a second step, I defined a new class deriving from the previous class and MixtureType. (exactly what have been done for the hPsiThermo class, I called it testThermo2). Then I modify the equivalent of the hPsiThermos.C file in order to define only Cthermo and pureMixture.... but it doesn't compile..... I get : Code:
cyp@cyp-laptop:~/OpenFOAM/cyp-1.7.1/applications/lib$ wmake libso I think it should come from my makeTestThermo.H file : Quote:
Best Regards, Cyp |
how can i make dynamic file from a fortan code and use it in openFoam ?
|
Basicthermo.H
hello everyone,
I am new to the OpenFoam software. Can anyone tell me how I can make use of Basicthermo.H while using Interfoam as solver?? |
may be its good to open new thread for ur issue, however you should look at user guide chapter 3 but a brief description can be like that:
in interfoam.C u should include ur new class: # include "Basicthermo.H" then in case directory use "wmake" to compile it. |
hey nimasam,
Thank you for your reply, i tried that. Any idea how to vary density with pressure change?? |
look in compressible solver for example "compressibleInterFoam" and compare it with interFoam
|
Hi,
I have couple of fortran.90 subroutines which I would like to call from OpenFOAM1.6. What I know direct call of fortran subroutine from c++ is not good. Thus, one has to have C interface to routines and some more technical difficulties as, fortran stores rowwise C stores columnwise :confused: Whatever, these are general problems of C++ and Fortran mixing. My questions are more OpenFOAM specific: 1) What should be the compiled fortran source codes, dynamic library, shared objects (.so), objects (.o) ... ? If more than one is good, which one performs better? 2) How should be this "executables" (compiled files) linked to OpenFOAM, let's say scalarFoam? How should the <file> and <options> in Make folder be modified? Please no suggestion like "why don't you rewrite your subroutines in C++" ;-) All the best, @ Cyp: You see, I am chasing you ;-) |
Hi!
To link a library with OpenFOAM you can follow this 2 steps : - First ,you create a *.so file using "wmake libso" - Then you link this *.so file to OpenFOAM by including the following command in your controlDict file : Code:
libs Cyp |
mixing Fortran with OpenFOAM, Towards to a unified solution
:rolleyes:
So, I came up with a solution to my prototype problem. Calling an external Fortran90 subroutine from an OpenFoam application. STEPS 1) Declare an interface C function in the main file or in .H file and include it extern "C" { void fortfunc_(int *ii, double *ff); // underscore is important (gfortran linux)! } When mixing Fortran with C++, name mangling must be prevented. You may use "objdump -t <file>" file: object or library files. In this example the subroutine named as fortfunc in the Fortran 90 code. But it's referred as fortfunc_ 2) Compile your Fortran code and obtain object files "*.o". You may link the object files or you may create a static library form object files as follows, ar rc myPbeLib.a fortfunc.o other.o object.o files.o // specific to Linux system 3) In your solver folder Make/options append -lgfortran (runtime fortran libraries) and your object or library files. If your fortran subroutine call some other routines from other libraries, add them as well. (Last case is not tested. :rolleyes: ) EXE_LIBS = -lfiniteVolume\ -lgfortran\ $(FOAM_USER_LIBBIN)/fortfunc.o 4) Build your executable with wmake in solver folder. This approach worked for my prototype problem. However I don't know if this is the best way, most probably NOT. Cyp's suggestion sounds really cool. But, I had a problem with obtaining the shared object files (*.so). I had done it before for a C++ code for boundary conditions. There it had worked very nicely. But with Fortran code I had problems. It doesn't compile the fortran code. If someone can describe how to obtain shared object files for Fortran codes, it will be really cool. Then, we can close this thread with a happy end ;-) All the best; Evren |
Dear all,
I have posted in this thread because it is the closest to the topic of my problem. I want to code a Hookstep-Newton-Krylov algorithm to find peculiar solutions of the flow with some symmetry properties in a rotating annulus. This algorithm involves OpenFOAM for solving Navier-Stokes equations and additional routines in FORTRAN 90, especially from linear algebra libraries. I have created a main program in C++ which calls the fortran fsubroutines (here a test case nothing.h. Some of these fortran subroutines call OpenFOAM. I am trying to make interact the main program with the fortran subroutines and OpenFOAM. I have made a test case where: -the main program is "icoDHRAFoamEssai.C" -it calls the fortran function "nothing in "nothing.f90" -which calls at its turn "integration" in "integration.C" that gathers all the OpenFOAM functions that are dedicated to solve Navier-Stokes equations in my case. Here you can find some parts of my code. First the main function in "icoDHRAFoamEssai.C" Code:
#include "fvCFD.H" Code:
subroutine nothing(AA, lengthAA, argvLength, argc, argvChar, argv1StrLength) Code:
void Integration_(double* AA, int* lengthAA,int& argc) Code:
gfortran -c nothing.f90 -o nothing.o Code:
EXE_INC = \ I compile with "wmake"; if I don't call the function "integration" in "nothing.f90", everything goes well. When I call it, I get the error message: Code:
In function `nothing_': I would be very grateful to anyone who may supply any suggestion. Nicolas Périnet |
Differences in Library Linking
Dear All,
I am also going to call a whole list of FORTRAN 90 subroutines from OF (in my case 1.7.1 though). I do this by adding the *.o files to the "objectFiles". Apparently, as I am reading through this really helpful thread, it seems like there are different methods to do this: static library, dynamic library, static objects, dynamic objects, shared objects, objects, ... :confused: I would appreciate, if someone could comment on the differences of these strategies (efficiency, ease of use, ...). This question came up earlier in the thread but has not been answered. Thanks guys. Best regards, Andreas |
link OF to fortran reading an input file
Hi All,
did anyone try to link OF with fortran to read from an input file, something like: integer temp open(unit=10,file='input.dat') read(10,*,end=100)temp 100 continue close(10) where the input file simply contains an integer, e.g.: "1", and calling this from an OF application? If I do this, I am getting a random output for "temp". It does work however, if I link a separate c++ main program and a fortran subroutine... so not sure why OF is causing trouble there... Any help is appreciated. Thanks, Andreas |
please tell me ???
Hi Dear Nima again: I want to add surface tension(sigma) in one solver,for this reason I added : #include ''fvCFD.H'' fvc::interpolate(interface.sigma()) in this code: Foam::tmp<Foam::volScalarField> Foam:haseChangeTwoPhaseMixtures::SchnerrSauer: Coeff ( const volScalarField& p ) const { volScalarField limitedAlpha1(min(max(alpha1_, scalar(0)), scalar(1))); volScalarField rho ( limitedAlpha1*rho1() + (scalar(1) - limitedAlpha1)*rho2() ); return //......I want to change it( <<sigma>> surface tension multiple in it): (3*rho1()*rho2())*sqrt(2/(3*rho1()))* (fvc::interpolate(interface.sigma())) *rRb(limitedAlpha1)/(rho*sqrt(mag(p - pSat()) + 0.01*pSat())); //.................................................. ...... } dont successful wmake, and seen(was not declared ): phaseChangeTwoPhaseMixtures/SchnerrSauer/SchnerrSauer.C:113: error: 'interface' was not declared in this scope make: *** [Make/linux64GccDPOpt/SchnerrSauer.o] Error 1 please help me,and tell me ,How to correct this problem??? |
dear vahid, instead of multiple posts of the same question, in different place, open new thread! with appropriate title,ask your question there!
it is an inappropriate to ask non-related issue under a post! and about your problem, i told you here (http://www.cfd-online.com/Forums/ope...interfoam.html). you need a background of C++, look at here: "http://cplus.about.com/od/learning1/ss/cppobjects.htm" |
Thanks
ok.mr nima.
Thanks |
Hi pge_cfd,
I am reading your thread and but this solution work well for my testing. But I did not run the following command as you mentioned: ar rc myPbeLib.a fortfunc.o other.o object.o files.o // specific to Linux system The coupled code can run well (but this is my simple test and so there is no complex in the fortran code). Could you please tell me what is the role for the creation of the library? Thanks. Quote:
|
Hi
The role of creating the library is: you can call the functions/subroutines in the created library once you include the library in your make file before compiling your code. When one needs to use the functions/subroutines of the created library in other applications, she/he includes the library and compile it, and there you are. Beside this, it was convenient for my purposes. I hope that I could explain it. All the best, Evren |
Hello everybody,
When you use your own thermodynamic library, instead of OF's, do you provide the density from your own thermodynamic library? Or still use the openfoam's default method to update the density ---thermo.rho(). Because I also developed my own fortran thermodyanmic library to provide the density for the OF solver, however, I also have the numerical instabilisty problem. Does anybody have the same problem when you implement your thermodyanmic models? Thank you very much. OFer Quote:
|
HI,
to get an idea how I use the external library, have a look at my water properties library in OpenFOAM ext. (which uses an external library to calculate the fluid propertiess) http://www.cfd-online.com/Forums/ope...perties-3.html Kind Regards, Christian |
Coupling interFoam/interDyMFoam with FORTRAN 90 code
Dear All,
I would like to couple mooring cable dynamic code written in FORTRAN 90 to interFoam & interDyMFoam. For every time step 1.InterFoam/InterDyMFoam solves for numerical wave tank.Based on the current wave field,the solver should provide velocities and accelerations at particular grid points (at mooring cable positions)as an input to the mooring cable code. 2.Based on these velocities and accelerations, the mooring cable fortran code solves for the tensions in the mooring cable and provides the tension at the fairlead point(particular grid point) as an input to the solver. Kindly can somebody please explain how I could perform this dynamic coupling between OpenFOAM and FORTRAN 90. Thank you. Regards, Gautami. |
All times are GMT -4. The time now is 23:43. |