Memory protection in OpenFOAM / combinig with FORTRAN
Posted September 20, 2014 at 11:27 by pfhan
combine the interFoam solver with an external solver written in FORTRAN
openfoam call fortran subroutines example
openfoam call fortran subroutines example
Quote:
I am trying to combine the interFoam solver with an external solver written in FORTRAN. I had no problem combining the FORTRAN code with C++, but there seems to be something "OpenFOAM specific" corrupting the data of the FORTRAN solver. My best guess is that the automatic memory handling in OpenFOAM, unintended is freeing the memory of the FORTRAN solver and hereby causing memory corruption.
The implementation is made in the following way:
1. Compiled a shared library from the FORTRAN code; libexternalSolver.so
2. Linked to the shared library in Make/options
3. Declared necessary function form the external solver in the c++
code by extern "C" (see code below)
4. Compiled the OpenFOAM solver with wmake.
If the lines related to solving equations in interFoam is comment out (see below), the solver is running seamlessly and the external solver returns correct results. If the lines are uncomment, the fields of the external solver are corrupted and the code crashes with a floating point exception after a few time steps.
Notice that the simulation is crashing with data corruption even in the very simple "coupling", where no data are exchanged between interFoam and the external solver.
A minimal (non)working example is shown below. (interFoam from OpenFOAM-2.1.x)
where the Make/options file looks like:
All help and suggestions on how to successfully combining external code with OpenFOAM is highly appreciated.
Kind Regards,
Bo Terp
The implementation is made in the following way:
1. Compiled a shared library from the FORTRAN code; libexternalSolver.so
2. Linked to the shared library in Make/options
3. Declared necessary function form the external solver in the c++
code by extern "C" (see code below)
4. Compiled the OpenFOAM solver with wmake.
If the lines related to solving equations in interFoam is comment out (see below), the solver is running seamlessly and the external solver returns correct results. If the lines are uncomment, the fields of the external solver are corrupted and the code crashes with a floating point exception after a few time steps.
Notice that the simulation is crashing with data corruption even in the very simple "coupling", where no data are exchanged between interFoam and the external solver.
A minimal (non)working example is shown below. (interFoam from OpenFOAM-2.1.x)
Code:
/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License This file is part of OpenFOAM. OpenFOAM is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. OpenFOAM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. Application interFoam Description Solver for 2 incompressible, isothermal immiscible fluids using a VOF (volume of fluid) phase-fraction based interface capturing approach. The momentum and other fluid properties are of the "mixture" and a single momentum equation is solved. Turbulence modelling is generic, i.e. laminar, RAS or LES may be selected. For a two-fluid approach see twoPhaseEulerFoam. \*---------------------------------------------------------------------------*/ // Declaration of external functions extern "C" void external_solver_t0_setup_(); // Setting up external solver extern "C" void external_solver_take_a_timestep_(); // Take a time step in external solver #include "fvCFD.H" #include "MULES.H" #include "subCycle.H" #include "interfaceProperties.H" #include "twoPhaseMixture.H" #include "turbulenceModel.H" #include "interpolationTable.H" #include "pimpleControl.H" #include "IObasicSourceList.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // int main(int argc, char *argv[]) { // Calling external FORTRAN function external_solver_t0_setup_(); #include "setRootCase.H" #include "createTime.H" #include "createMesh.H" pimpleControl pimple(mesh); #include "initContinuityErrs.H" #include "createFields.H" #include "readTimeControls.H" #include "correctPhi.H" #include "CourantNo.H" #include "setInitialDeltaT.H" //// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // Info<< "\nStarting time loop\n" << endl; while (runTime.run()) { #include "readTimeControls.H" #include "CourantNo.H" #include "alphaCourantNo.H" #include "setDeltaT.H" runTime++; // Calling external FORTRAN function external_solver_take_a_timestep_(); Info<< "Time = " << runTime.timeName() << nl << endl; twoPhaseProperties.correct(); // If the following line is uncommented the data of the external solver is corrupted and the solver crashes! #include "alphaEqnSubCycle.H" //// --- Pressure-velocity PIMPLE corrector loop //while (pimple.loop()) //{ //#include "UEqn.H" //// --- Pressure corrector loop //while (pimple.correct()) //{ //#include "pEqn.H" //} //if (pimple.turbCorr()) //{ //turbulence->correct(); //} //} runTime.write(); Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s" << " ClockTime = " << runTime.elapsedClockTime() << " s" << nl << endl; } Info<< "End\n" << endl; return 0; } // ************************************************************************* //
Code:
EXE_INC = \ -I$(LIB_SRC)/transportModels \ -I$(LIB_SRC)/transportModels/incompressible/lnInclude \ -I$(LIB_SRC)/transportModels/interfaceProperties/lnInclude \ -I$(LIB_SRC)/turbulenceModels/incompressible/turbulenceModel \ -I$(LIB_SRC)/finiteVolume/lnInclude \ -I$(LIB_SRC)/meshTools/lnInclude \ -I$(HOME)/lib/include \ EXE_LIBS = \ -L ~/lib/ \ -lgfortran \ -ltwoPhaseInterfaceProperties \ -lincompressibleTransportModels \ -lincompressibleTurbulenceModel \ -lincompressibleRASModels \ -lincompressibleLESModels \ -lfiniteVolume \ -lmeshTools \ -L$(FOAM_USER_LIBBIN) \ -lexternalSolver
Kind Regards,
Bo Terp
Total Comments 0