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/)
-   -   Define an array of "volVectroField" type (https://www.cfd-online.com/Forums/openfoam-programming-development/119642-define-array-volvectrofield-type.html)

haghgoo_reza June 20, 2013 18:15

Define an array of "volVectroField" type
 
Hi all, ;)

When I define a variable "Uold" as:

volVectorField Uold("Uold", U);
Uold = U;

it works. However, I would like to define an array of Uold. I mean, I want to have Uold1, Uold2, ..... So When I define the above expression inside a loop it does not work I.e.

for (int i=1; i < 100; i++) {
volVectorField Uold[i] ("Uold", U);
Uold[i] = U;
}

Could anybody please help me how to define an array of the object "volVectroField" type?:)

Thanks

Cyp June 21, 2013 02:55

Hi!

You can be inspired by what is done for multicomponent mass transfer where there is a table of volScalarField Y.

You first have to reserve the memory with a list of pointer :
Code:

PtrList <volScalarField> Y(speciesNames.size());
where speciesNames is a list of word defined elsewhere. For example,
Code:

wordList speciesNames
(
    transportProperties.lookup("speciesNames")
);

Then you create your speciesNames.size() objects on the heap:

Code:

forAll(speciesNames, s)
{
  Y.set
  (
        s,
        new volScalarField
        (
            IOobject
            (
                speciesNames[s],
                runTime.timeName(),
                mesh,
                IOobject::MUST_READ,
                IOobject::AUTO_WRITE
        ),
        mesh
  );
}

Best,
Cyp

haghgoo_reza June 24, 2013 19:53

Loop over volVectorField
 
Thanks a lot Cyp.
Your information was pretty helpful to me.:)

Regards
Reza

sonGoku June 3, 2018 21:02

what does wordList specieName contain?
is it used to input size of the array?
and how do I define it in transportProperties?

Thank You

i would hiGhly appreciate any sort of help/feedback

zhangyan June 4, 2018 03:34

Quote:

Originally Posted by sonGoku (Post 694462)
what does wordList specieName contain?
is it used to input size of the array?
and how do I define it in transportProperties?

For a scalarList, it can be read in a Dict like this:
Code:

Z
(
0
0.5
1
);

Similarly, a wordList should be like this:
Code:

speciesNames
(
H2
O2
H2O
);


ancolli May 9, 2020 15:37

What about the same procedure but for several regions?
for example:
Code:

forAll(regions, i)
{
 forAll(speciesNames, s)
 {
  Y[i].set
  (
        s,
        new volScalarField
        (
            IOobject
            (
                speciesNames[s],
                runTime.timeName(),
                mesh_region[i],
                IOobject::MUST_READ,
                IOobject::AUTO_WRITE
        ),
        mesh_region[i]
  );
 }
}

how to declare the pointer?

zhangyan May 9, 2020 15:54

Quote:

Originally Posted by ancolli (Post 769629)
What about the same procedure but for several regions?
for example:
Code:

forAll(regions, i)
{
 forAll(speciesNames, s)
 {
  Y[i].set
  (
        s,
        new volScalarField
        (
            IOobject
            (
                speciesNames[s],
                runTime.timeName(),
                mesh_region[i],
                IOobject::MUST_READ,
                IOobject::AUTO_WRITE
        ),
        mesh_region[i]
  );
 }
}

how to declare the pointer?


Code:

PtrList<PtrList<volScalarField>> Y(regions.size());

forAll(regions, i)
{
    Y.set
    (
        i,
        new PtrList<volScalarField>(mesh_region.size())
    );
    forAll(speciesNames, s)
    {
        Y[nsf].set
        (
            s,
            new volScalarField
            (
                IOobject
                (
                    speciesNames[s],,
                    runTime.timeName(),
                    mesh_region[i],
                    IOobject::MUST_READ,
                    IOobject::AUTO_WRITE
                ),
                mesh_region[i]
            )
        );
    }
}


ancolli May 10, 2020 21:09

Quote:

Originally Posted by zhangyan (Post 769633)
Code:

PtrList<PtrList<volScalarField>> Y(regions.size());

forAll(regions, i)
{
    Y.set
    (
        i,
        new PtrList<volScalarField>(mesh_region.size())
    );
    forAll(speciesNames, s)
    {
        Y[nsf].set
        (
            s,
            new volScalarField
            (
                IOobject
                (
                    speciesNames[s],,
                    runTime.timeName(),
                    mesh_region[i],
                    IOobject::MUST_READ,
                    IOobject::AUTO_WRITE
                ),
                mesh_region[i]
            )
        );
    }
}


Thanks a lot zhangyan

It compiles well and it works great when I have 1 species (speciesNames.size() = 1, s = 0)
However, when I run the code with 2 species (speciesNames.size() = 2, s = 0,1 ), I got the following error for s = 1:
Code:

#0  Foam::error::printStack(Foam::Ostream&) at ??:?
#1  Foam::sigSegv::sigHandler(int) at ??:?
#2  ? in "/lib/x86_64-linux-gnu/libc.so.6"
#3  ? in "/home/user/OpenFOAM/user-6/platforms/linux64GccDPInt32Opt/bin/solver"
#4  __libc_start_main in "/lib/x86_64-linux-gnu/libc.so.6"
#5  ? in "/home/user/OpenFOAM/user-6/platforms/linux64GccDPInt32Opt/bin/solver"
Segmentation fault (core dumped)

due to the following piece of code:

Code:

        Y[s].set
        (
            s,
            new volScalarField
            (
                IOobject
                (
                    speciesNames[s],
                    runTime.timeName(),
                    mesh_region[i],
                    IOobject::MUST_READ,
                    IOobject::AUTO_WRITE
                ),
                mesh_region[i]
            )
        );

Do you know what can it be?

ancolli May 10, 2020 21:45

Quote:

Originally Posted by ancolli (Post 769777)
Thanks a lot zhangyan

It compiles well and it works great when I have 1 species (speciesNames.size() = 1, s = 0)
However, when I run the code with 2 species (speciesNames.size() = 2, s = 0,1 ), I got the following error for s = 1:
Code:

#0  Foam::error::printStack(Foam::Ostream&) at ??:?
#1  Foam::sigSegv::sigHandler(int) at ??:?
#2  ? in "/lib/x86_64-linux-gnu/libc.so.6"
#3  ? in "/home/user/OpenFOAM/user-6/platforms/linux64GccDPInt32Opt/bin/solver"
#4  __libc_start_main in "/lib/x86_64-linux-gnu/libc.so.6"
#5  ? in "/home/user/OpenFOAM/user-6/platforms/linux64GccDPInt32Opt/bin/solver"
Segmentation fault (core dumped)

due to the following piece of code:

Code:

        Y[s].set
        (
            s,
            new volScalarField
            (
                IOobject
                (
                    speciesNames[s],
                    runTime.timeName(),
                    mesh_region[i],
                    IOobject::MUST_READ,
                    IOobject::AUTO_WRITE
                ),
                mesh_region[i]
            )
        );

Do you know what can it be?

I found the mistake, sorry!

the following lines:

Code:

    Y.set
    (
        i,
        new PtrList<volScalarField>(mesh_region.size())
    );
    forAll(speciesNames, s)
    {
      Y[s].set
        (
            s,
            new volScalarField
            (
                IOobject
                (
                    speciesNames[s],
                    runTime.timeName(),
                    mesh_region[i],
                    IOobject::MUST_READ,
                    IOobject::AUTO_WRITE
                ),
                mesh_region[i]
            )
        );
    }

should be:

Code:

    Y.set
    (
        i,
        new PtrList<volScalarField>(speciesNames.size())
    );
    forAll(speciesNames, s)
    {
      Y[i].set
        (
            s,
            new volScalarField
            (
                IOobject
                (
                    speciesNames[s],
                    runTime.timeName(),
                    mesh_region[i],
                    IOobject::MUST_READ,
                    IOobject::AUTO_WRITE
                ),
                mesh_region[i]
            )
        );
    }



All times are GMT -4. The time now is 01:35.