CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Running, Solving & CFD (https://www.cfd-online.com/Forums/openfoam-solving/)
-   -   Array of fields (https://www.cfd-online.com/Forums/openfoam-solving/59186-array-fields.html)

r2d2 July 26, 2005 10:32

Hi, How can I create an arra
 
Hi,
How can I create an array of volScalFields in one CreatFields.H, say of oodles? Say field[i], i = 0,10...Is there an example somewhere?
Cheers,
Radu

henry July 26, 2005 10:38

Try creating a ptrList of them
 
Try creating a ptrList of them.

r2d2 July 26, 2005 11:17

Thanks for the message, Henry.
 
Thanks for the message, Henry.
I thought of something like this in CreateFields.H.

const int nVarmax = 10;
for (int i =0; i<nVarmax; i++)
{

word fieldName = "name_of_field"[i]

volScalarField field[i]
(
IOobject
(
fieldName,
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);
}
Still have to sort out the "name_of_field"[i] and field[i] bits...
Cheers,
Radu

mattijs July 26, 2005 14:08

Try something like ptrList<
 
Try something like

ptrList<volscalarfield> fields(10);

for (i=0 ...)
{
word fieldName = "field_" + Foam::name(i);

fields.hook(new volScalarField(IOobject(...))
}

See e.g foamToVTK.C, call to readFields (though is templated)

r2d2 July 27, 2005 08:52

OK, thanks. I did the followin
 
OK, thanks. I did the following:

ptrList<volscalarfield> fields(10);
for (int i =0; i<10; i++)
{
word fieldName = "field_" + Foam::name(i);
Info<< "Reading field " << fieldName<< endl;
fields.hook(
volScalarField
(
IOobject
(
fieldName,
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
)
);

}
So now if I want to solve an, say, convection diffusion eq + source term with one such field (actually all of them) what do I refer to in the ddt, div and laplacian operators? This is kind of what I want:

solve
(
fvm::ddt(fields(i))
+ fvm::div(phi,fields(i))
- fvm::laplacian(sgsModel->nuEff(),fields(i))
);

Many thanks,
Radu

henry July 27, 2005 08:58

you should loop over the field
 
you should loop over the fields and use fields[i] rather than fields(i).

r2d2 September 12, 2005 07:48

Hi again, I´m back with my lit
 
Hi again, I´m back with my little problems.
Having done what I was taught to do above, and the things work fine with no source term I would like now to add some source for each field and then to calculate at run time a "mean field" (mean wrt the number of fields) and write it out.
Thought of doing in the body of oodles.C:
volScalarField meanFields;
for (int i =0; i<10; i++){

meanFields +=fields[i];


}
meanFields /=10;

...but doesn´t seem to work. Should I do something like in createAverages.H and calculateAverages.H?
Thanks,
Radu

mattijs September 12, 2005 13:47

doesn't it compile? Where? Fai
 
doesn't it compile? Where? Fail during running? Where?

You will need to define your volScalarField properly (so with an IOobject and mesh etc.).

diegon January 31, 2007 16:50

Hi everybody, is it possibl
 
Hi everybody,

is it possible to use PtrList with Matrix<scalar>?

Thanks in advance

Diego

evan January 29, 2008 14:28

Sorry for the stupid question,
 
Sorry for the stupid question, but I am trying to use a ptrlist (parameter list?) in createfields.H. I have:

PtrList<volscalarfield> nu(Y);
for (int i=0, i<Y.size(); i++)
{
word nui = "nu_" + Foam::name(i);
Info << "Reading Field << nui << endl;
nu.set
(
volScalarField
(
IOobject
(
nui,
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
)
);
}

And when I compile I get:

error: no matching function for call to "Foam::PtrList< ... etc ..."

Would somebody mind explaining to me what I'm doing wrong here?

Evan

hjasak January 29, 2008 14:46

Eeeeasy! http://www.cfd-online
 
Eeeeasy! http://www.cfd-online.com/OpenFOAM_D...part/happy.gif Do:

nu.set
(
i,
new volScalarField
(
...
);


See it - you need and index and a new in front of volScalarField.

Enjoy,

Hrv

evan January 30, 2008 12:40

Neat. Thanks Hrv! I hope you
 
Neat. Thanks Hrv! I hope you can forgive my programming ignorance ... you should have seen me a year ago http://www.cfd-online.com/OpenFOAM_D...part/happy.gif! Seems I spend a lot of time enjoying your text, most recently Numerical Solution Algorithms ..., and am excited to take a closer look at this new coupledFvScalarMatrix class!

I posted the below question yesterday in "Running / Solving / CFD: Pollutant dispersion in the environment" too, though it seems related to this thread, and maybe this is a more appropriate venue for the question, so let me copy it here, and sorry for double posting.

----------------------
Hi all,

So to revisit the question raised by Alberto, does anyone know (or be
willing to discuss) how to make an indexed list of constants that is
read from a dictionary. So, the diffusivity example Alberto brought up
would work great. That is, say you have n diffusion constants listed
in your constants dictionary as:

dimensionedScalar D1 (dictionary.lookup("D1")); . . . dimensionedScalar Dn (dictionary.lookup("DY"));

but you want to index them in your solver (along with other indexed
fields) as:

for(label i=0; i<n; i++)
{
volScalarField& Yi = Y[i];

// call D[i] list here

solve
(
fvm::ddt(Yi)
+ fvm::D[i]*fvm::laplacian(Yi)

);

etc.

Or is there a better way about going about this? Mattijs' suggestion
from before is currently over my head http://www.cfd-online.com/OpenFOAM_D...part/happy.gif.

Best,
Evan


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