CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Post-Processing (http://www.cfd-online.com/Forums/openfoam-post-processing/)
-   -   How to sum up a volScalarField (http://www.cfd-online.com/Forums/openfoam-post-processing/115143-how-sum-up-volscalarfield.html)

RugbyGandalf March 25, 2013 03:39

How to sum up a volScalarField
 
Dear Foamers,

i build a small postprocessing program, which calculates the volume of each cell and multiplies it by the a concentration of this cell. (It is a case, where I simulate the drug-transport through a coronary artery).
So I know about the mass of drug in each cell, but i would like to sum all values up, so that I get the overall value...

Does anyone have a clue, how to do this?

Thank you very much

Regards,
Martin

Lieven March 25, 2013 03:53

You can simply use
Code:

scalar sum = gSum(C);
where C is the mass scalarField.

Cheers,

Lieven

RugbyGandalf March 25, 2013 05:05

Dear Lieven,

thank you very much for your answer.
Unfortunately wmake gives out the following errors:

wirkstoffgehalt.C: In function ‘int main(int, char**)’:
wirkstoffgehalt.C:158: error: ‘sum’ cannot be used as a function
wirkstoffgehalt.C:141: warning: unused variable ‘Gesamtwirkstoffgehalt’
make: *** [Make/linux64GccDPOpt/wirkstoffgehalt.o] Fehler 1






I added this line at the end of my program.C - Code, which now looks as follows:

Code:

/*---------------------------------------------------------------------------*\
  =========                |
  \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox
  \\    /  O peration    |
    \\  /    A nd          | Copyright (C) 1991-2010 OpenCFD Ltd.
    \\/    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/>.

Description
    Write the three components of the cell centres as volScalarFields so
    they can be used in postprocessing in thresholding.

\*---------------------------------------------------------------------------*/

#include "argList.H"
#include "timeSelector.H"
#include "Time.H"
#include "fvMesh.H"
#include "vectorIOField.H"
#include "volFields.H"
#include "fvCFD.H"

using namespace Foam;

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

// Main program:

int main(int argc, char *argv[])
{

    timeSelector::addOptions();

#  include "setRootCase.H"
#  include "createTime.H"

    instantList timeDirs = timeSelector::select0(runTime, args);

#  include "createMesh.H"

    forAll(timeDirs, timeI)
    {
        runTime.setTime(timeDirs[timeI], timeI);

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

        // Check for new mesh
        mesh.readUpdate();

    volScalarField C
    (
        IOobject
        (
            "C",
            runTime.timeName(),
            mesh,
            IOobject::MUST_READ,
            IOobject::AUTO_WRITE
        ),
        mesh
    );


    volScalarField cv
    (
            IOobject
            (
                    "cellVolumes",
                    runTime.timeName(),
                    mesh,
                    IOobject::NO_READ,
                    IOobject::AUTO_WRITE
            ),
            mesh,
            dimensionedScalar("zero",dimVolume,0.0)
    );
   
    volScalarField wirkstoffgehalt
    (
            IOobject
            (
                    "Wirkstoffgehalt",
                    runTime.timeName(),
                    mesh,
                    IOobject::NO_READ,
                    IOobject::AUTO_WRITE
            ),
            mesh,
            dimensionedScalar("zero",dimMass,0.0)
    );

    cv.internalField() = mesh.V();

    Info<< "Schreibe den Wert " << cv.name() << " in "
            << runTime.timeName() << endl;

    wirkstoffgehalt.internalField() = mesh.V()*C.internalField();


        Info<< "Schreibe den Wirkstoffgehalt je Zelle in kg " << cv.name() << " in "
            << runTime.timeName() << endl;
       
        cv.write();
    wirkstoffgehalt.write();



    IOdictionary transportProperties
    (
        IOobject
        (
            "transportProperties",
            runTime.constant(),
            mesh,
            IOobject::MUST_READ_IF_MODIFIED,
            IOobject::NO_WRITE
        )
    );


scalar sum = sum(wirkstoffgehalt);

   
    Info<< "Gesamtwirkstoffgehalt in kg beträgt " << sum << " in "
            << runTime.timeName() << endl;

  }


    Info<< "\nEnd" << endl;

    return 0;
}


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

Could you tell me, what is wrong?

Regards,
Martin

Lieven March 25, 2013 05:24

Ok, but you should copy the code a bit more carefully (it is gSum and not simply sum) and it is also bit tricky what you try to do in your code.
You try to create a variable with the same name as the function you are calling. I'm certainly not an C++ experiment but I can imagine that could cause problems...

Cheers,

L

RugbyGandalf March 25, 2013 12:20

Hey Lieven,

I tried another way. The program from above gives out a volScalarField (let's call it "Values")
with another program, I read in Values, and tried your "gsum" lines again, but it did nor work.
# gsum is not defined in this scope
Is a header missing?

Regards,
Martin

Code:

/*---------------------------------------------------------------------------*\
  =========                |
  \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox
  \\    /  O peration    |
    \\  /    A nd          | Copyright (C) 1991-2010 OpenCFD Ltd.
    \\/    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/>.

Description
    Write the three components of the cell centres as volScalarFields so
    they can be used in postprocessing in thresholding.

\*---------------------------------------------------------------------------*/

#include "argList.H"
#include "timeSelector.H"
#include "Time.H"
#include "fvMesh.H"
#include "vectorIOField.H"
#include "volFields.H"
#include "fvCFD.H"

using namespace Foam;

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

// Main program:

int main(int argc, char *argv[])
{

    timeSelector::addOptions();

#  include "setRootCase.H"
#  include "createTime.H"

    instantList timeDirs = timeSelector::select0(runTime, args);

#  include "createMesh.H"

    forAll(timeDirs, timeI)
    {
        runTime.setTime(timeDirs[timeI], timeI);

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

        // Check for new mesh
        mesh.readUpdate();

    volScalarField Wirkstoffgehalt
    (
        IOobject
        (
            "Wirkstoffgehalt",
            runTime.timeName(),
            mesh,
            IOobject::MUST_READ,
            IOobject::NO_WRITE
        ),
        mesh
    );

scalar sum = gsum(Wirkstoffgehalt);


    Info<< "Schreibe den Wert " << sum << " in "
            << runTime.timeName() << endl;

  }


    Info<< "\nEnd" << endl;

    return 0;
  }
}


lin March 25, 2013 21:04

gSum!=gsum

RugbyGandalf March 26, 2013 11:40

Dear Leaven and Lin,

thank you very much for your help!
It was a spelling problem of mine.

Now it works fine :)


All times are GMT -4. The time now is 04:28.