CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (https://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   writing values to IOdictionary subDict not generating expected output? (https://www.cfd-online.com/Forums/openfoam-programming-development/214055-writing-values-iodictionary-subdict-not-generating-expected-output.html)

massive_turbulence January 17, 2019 13:32

writing values to IOdictionary subDict not generating expected output?
 
Hello All,

Below I have the code that tries to output values to a dict file ( nevermind that it doesn't make sense in terms of actual scientific use as it's only for a reading and writing example). Basically I have a mesh that is read and some math is used to find scalars and I want to write this to a dictionary file in /constant.

Right now it just writes out the dimensions but not the result of the calculation
for volScalarField DVals = cellCentroid & cellCentroid;

Code:

#include "fvCFD.H"

int main(int argc, char *argv[])
{
        #include "setRootCase.H"
        #include "createTime.H"
        #include "createMesh.H"       
       
        IOdictionary electrolyteProperties
        (
                IOobject
                (
                  "electrolyteProperties",
                  runTime.constant(),
                  mesh,
                  IOobject::MUST_READ_IF_MODIFIED,
                  IOobject::NO_WRITE
                )
        );

        dictionary& DDict = electrolyteProperties.subDict("D");

        volVectorField cellCentroid = mesh.C();               
        volScalarField DVals = cellCentroid & cellCentroid;

        DDict.set("values", DVals);
        electrolyteProperties.regIOobject::write();

        return 0;
}

blockMeshDict

Code:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  plus                                  |
|  \\  /    A nd          | Web:      www.OpenFOAM.com                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version    2.0;
    format      ascii;
    class      dictionary;
    object      blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

convertToMeters 1;

a 32;
b 32;
c 32;
yi -2;
yf 2;
zi -2;
zf 2;
xi -2;
xf 2;

vertices
(
    ($xi $yi $zi) //0
    ($xf $yi $zi) //1
    ($xf $yf $zi) //2
    ($xi $yf $zi) //3
    ($xi $yi $zf) //4
    ($xf $yi $zf) //5
    ($xf $yf $zf) //6
    ($xi $yf $zf) //7
);


blocks
(
    hex (0 1 2 3 4 5 6 7) ($a $b $c) simpleGrading (1 1 1)
);

edges
(
);

patches
(
);
boundaryField
{
        /// Patches should have same names as in
        /// polyMesh/boundary
        patch_1
        {
                type zeroGradient;
        }
/// other patches
}
mergePatchPairs
(
);
// ************************************************************************* //

/constant/electrolyteProperties


Code:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  v1806                                |
|  \\  /    A nd          | Web:      www.OpenFOAM.com                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/
FoamFile
{
    version    2.0;
    format      binary;
    class      dictionary;
    arch        "LSB;label=32;scalar=64";
    location    "constant";
    object      electrolyteProperties;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

D
{
    values          dimensions [ 0 2 0 0 0 0 0 ];
}


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


massive_turbulence January 19, 2019 12:13

I got this to work!

Code:

#include "fvCFD.H"

int main(int argc, char *argv[])
{
        #include "setRootCase.H"
        #include "createTime.H"
        #include "createMesh.H"       
       
        IOdictionary electrolyteProperties
        (
                IOobject
                (
                  "electrolyteProperties",
                  runTime.constant(),
                  mesh,
                  IOobject::MUST_READ_IF_MODIFIED,
                  IOobject::NO_WRITE
                )
        );       

        volVectorField cellCentroid = mesh.C();               
        volScalarField DVals = cellCentroid & cellCentroid;       
       
        fileName outputDir = mesh.time().path()/"postProcessing";
    mkDir(outputDir);
        autoPtr<OFstream> outputFilePtr;
    outputFilePtr.reset(new OFstream(outputDir/"customOutputFile.dat")); 
    outputFilePtr() << DVals << endl;

        return 0;
}

Now the problem I have is how to read it back into a variable,
for this type..
internalField nonuniform List<scalar>

massive_turbulence January 21, 2019 08:48

1 Attachment(s)
Now I get nulls in my output in the internalField?

Basically I'm reaeding the mesh into a volVectorField and trying to write
to disk (file /0/result) but for some reason it's turning into garbage?

Something like this

Code:

internalField  nonuniform List<vector>
32768
(      ÿ¿      ÿ .....

Any suggestions?

Code:

#include "fvCFD.H"

int main(int argc, char *argv[])
{
        #include "setRootCase.H"
        #include "createTime.H"
        #include "createMesh.H"       
       
        volVectorField cellCentroid = mesh.C();               
        volScalarField DVals = cellCentroid & cellCentroid;               
      Info << DVals << endl;
       
        volVectorField result
    (
        IOobject
        (
            "result",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
                mesh.C()     
    );
        result.write();
       
        return 0;
}


EDIT:NEVERMIND

I read the result IOobject into Info and it reproduced the list. I still don't see why nulls are created? Must be a type of openfoam "codex"?

Code:

#include "fvCFD.H"

int main(int argc, char *argv[])
{
        #include "setRootCase.H"
        #include "createTime.H"
        #include "createMesh.H"       
       
        volVectorField cellCentroid = mesh.C();       
       
        volVectorField result
    (
        IOobject
        (
            "result",
            runTime.timeName(),
            mesh,
            IOobject::NO_READ,
            IOobject::AUTO_WRITE
        ),
                cellCentroid     
    );
        result.write();       
       
        Info << result << endl;
       
        return 0;
}

Whole project Attachment 67872


All times are GMT -4. The time now is 08:41.