CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums

Memory protection in OpenFOAM / combinig with FORTRAN

Register Blogs Community New Posts Updated Threads Search

Rate this Entry

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

Quote:
Originally Posted by botp View Post
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)


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 &quot;mixture&quot; 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 &quot;C&quot; void external_solver_t0_setup_(); // Setting up external solver
extern &quot;C&quot; void external_solver_take_a_timestep_(); // Take a time step in external solver

#include &quot;fvCFD.H&quot;
#include &quot;MULES.H&quot;
#include &quot;subCycle.H&quot;
#include &quot;interfaceProperties.H&quot;
#include &quot;twoPhaseMixture.H&quot;
#include &quot;turbulenceModel.H&quot;
#include &quot;interpolationTable.H&quot;
#include &quot;pimpleControl.H&quot;
#include &quot;IObasicSourceList.H&quot;

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

int main(int argc, char *argv[])
{
// Calling external FORTRAN function
    external_solver_t0_setup_(); 

    #include &quot;setRootCase.H&quot;
    #include &quot;createTime.H&quot;
    #include &quot;createMesh.H&quot;

    pimpleControl pimple(mesh);

    #include &quot;initContinuityErrs.H&quot;
    #include &quot;createFields.H&quot;
    #include &quot;readTimeControls.H&quot;
    #include &quot;correctPhi.H&quot;
    #include &quot;CourantNo.H&quot;
    #include &quot;setInitialDeltaT.H&quot;


    //// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

    Info<< &quot;\nStarting time loop\n&quot; << endl;

    while (runTime.run())
    {
        #include &quot;readTimeControls.H&quot;
        #include &quot;CourantNo.H&quot;
        #include &quot;alphaCourantNo.H&quot;
        #include &quot;setDeltaT.H&quot;

        runTime++;

// Calling external FORTRAN function
       external_solver_take_a_timestep_();

        Info<< &quot;Time = &quot; << 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 &quot;alphaEqnSubCycle.H&quot;

        //// --- Pressure-velocity PIMPLE corrector loop
        //while (pimple.loop())
        //{
            //#include &quot;UEqn.H&quot;

            //// --- Pressure corrector loop
            //while (pimple.correct())
            //{
                //#include &quot;pEqn.H&quot;
            //}

            //if (pimple.turbCorr())
            //{
                //turbulence->correct();
            //}
        //}

        runTime.write();

        Info<< &quot;ExecutionTime = &quot; << runTime.elapsedCpuTime() << &quot; s&quot;
        << &quot;  ClockTime = &quot; << runTime.elapsedClockTime() << &quot; s&quot;
        << nl << endl;
    }

    Info<< &quot;End\n&quot; << endl;

    return 0;
}


// ************************************************************************* //
where the Make/options file looks like:
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
All help and suggestions on how to successfully combining external code with OpenFOAM is highly appreciated.

Kind Regards,
Bo Terp
Posted in Uncategorized
Views 1496 Comments 0 Edit Tags Email Blog Entry
« Prev     Main     Next »
Total Comments 0

Comments

 

All times are GMT -4. The time now is 07:27.