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/)
-   -   create volTensorField out of volScalarFields (https://www.cfd-online.com/Forums/openfoam-programming-development/112276-create-voltensorfield-out-volscalarfields.html)

anton_lias January 25, 2013 04:48

create volTensorField out of volScalarFields
 
Hello everybody,
I want to create a volTensorField out of a couple of volScalarFields. The tensor should look (for example) like this JacobiMatrix=(m1, m2+m1, 0, m5*m3, m4, 0, m0, m2/m1, m2).

The Moments m0-m5 are defined like this
Code:

        Info<< "Reading field m0" << endl;
        volScalarField m0
        (
                IOobject
                (
                        "m0",
                        runTime.timeName(),
                        mesh,
                        IOobject::MUST_READ,
                        IOobject::AUTO_WRITE
                ),
                mesh
        );

I have tried a lot of things, but I do not manage to create the Tensor.
For example:

Code:

        Info<< "Define tensorfield jacobiMatrix" << endl;
        volTensorField jacobiMatrix
        (
                IOobject
                (
                        "jacobiMatrix",
                        runTime.timeName(),
                        mesh,
                        IOobject::NO_READ,
                        IOobject::NO_WRITE
                ),
                mesh,
                dimensionedTensor
                (
                        "zero",
                        dimensionSet(0, 0, 0, 0, 0, 0, 0),
                        tensor::zero
                )
        );

and
Code:

jacobiMatrix.component(0) = m1;
jacobiMatrix.component(1) = m2+m1;

The Solver is compiling, but when I use it, an error message appears.

Quote:

--> FOAM FATAL ERROR:
attempted to assign to a const reference to constant object
Does anyone know, how I could create a volTensorField out of the volScalarFields?

JimKnopf January 25, 2013 05:50

You could do a loop over all cells and then compute your components like:

Code:

// const Foam::cellList cells = mesh.cells()
forAll(cells,cellI)
{
JacobiMatrix[cellI].component(0) = m1[cellI]+m2[cellI];
// same for all the others
}

This probably no good style but it should work.

Best Regards
Jim

anton_lias January 25, 2013 08:40

Thanks a lot, it is working.
But I have to write
forAll(mesh.cells(),cellI)
{...

I already read that it's not a good style. But I did not find an answer on the question, how a good style would look like.

JackW December 21, 2014 00:10

I know this is an old post, but did you (or anyone else) find a more compact solution to this problem? Would be much appreciated!

deepsterblue December 22, 2014 10:13

What you're looking for here is the replace() member function:

Code:

jacobiMatrix.replace(0, m0);
jacobiMatrix.replace(1, m1);
jacobiMatrix.replace(2, m2);
...
...
jacobiMatrix.replace(8, m8);

No need to loop over all cells.

JackW December 22, 2014 11:44

Brilliant, thanks!


All times are GMT -4. The time now is 13:31.