CFD Online Logo CFD Online URL
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Programming & Development

Memory protection in OpenFOAM / combinig with FORTRAN

Register Blogs Members List Search Today's Posts Mark Forums Read

LinkBack Thread Tools Search this Thread Display Modes
Old   February 5, 2013, 05:43
Default Memory protection in OpenFOAM / combinig with FORTRAN
New Member
Bo Terp Paulsen
Join Date: Oct 2010
Posts: 13
Rep Power: 15
botp is on a distinguished road
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;
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)

  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
    \\  /    A nd           | Copyright (C) 2011 OpenFOAM Foundation
     \\/     M anipulation  |
    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 <>.


    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

    #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 (
        #include "readTimeControls.H"
        #include "CourantNo.H"
        #include "alphaCourantNo.H"
        #include "setDeltaT.H"


// Calling external FORTRAN function

        Info<< "Time = " << runTime.timeName() << nl << endl;


// 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())


        Info<< "ExecutionTime = " << runTime.elapsedCpuTime() << " s"
        << "  ClockTime = " << runTime.elapsedClockTime() << " s"
        << nl << endl;

    Info<< "End\n" << endl;

    return 0;

// ************************************************************************* //
where the Make/options file looks like:
    -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 \

    -L ~/lib/ \
    -lgfortran \
    -ltwoPhaseInterfaceProperties \
    -lincompressibleTransportModels \
    -lincompressibleTurbulenceModel \
    -lincompressibleRASModels \
    -lincompressibleLESModels \
    -lfiniteVolume \
    -lmeshTools \
All help and suggestions on how to successfully combining external code with OpenFOAM is highly appreciated.

Kind Regards,
Bo Terp
botp is offline   Reply With Quote

Old   February 6, 2013, 04:26
Senior Member
Christian Lucas
Join Date: Aug 2009
Location: Braunschweig, Germany
Posts: 202
Rep Power: 17
Chris Lucas is on a distinguished road

the method you explain above works fine for me.

Have a look at the my iapws 97 library for OpenFOAM

Chris Lucas is offline   Reply With Quote

Old   February 15, 2016, 12:25
New Member
Join Date: Nov 2014
Location: Germany
Posts: 14
Rep Power: 11
mkiewat is on a distinguished road
I'm having a similar problem, did you ever resolve your issue?
OpenFOAM seems to free the memory for an array that I want to access using a pointer in an "external" C-function.
Any help is appreciated.
mkiewat is offline   Reply With Quote


coupled solver, fortran, memory corruption, open foam

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison between C/C++ and Fortran? rick Main CFD Forum 45 September 6, 2011 00:52
Memory leak in OpenFOAM? marupio OpenFOAM Bugs 8 October 14, 2010 12:49
Critical errors during OpenFoam installation in OpenSuse 11.0 amscosta OpenFOAM 5 May 1, 2009 14:06
Summer School on Numerical Modelling and OpenFOAM hjasak OpenFOAM 5 October 12, 2008 13:14
OpenFOAM Training and Workshop Hrvoje Jasak Main CFD Forum 0 October 7, 2005 07:14

All times are GMT -4. The time now is 20:26.