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/)
-   -   How to exchange information within coded fvpatchfield between different fields (https://www.cfd-online.com/Forums/openfoam-programming-development/242854-how-exchange-information-within-coded-fvpatchfield-between-different-fields.html)

ruanyg968tf May 16, 2022 05:25

How to exchange information within coded fvpatchfield between different fields
 
Dear foamers,
I want to define a field within a volScalarField called psi and access it within another volVectorField named psiVector. I have followed the thread https://www.cfd-online.com/Forums/op...-scalar-3.html. I defined an IOdictionary within createField.H, and tried to modify it within psi field. However, when I tried to access the IOdictionary object within psiVector, the IOdictionary I accessed from the registry was empty.
In order to illustrate it more clearly, there are the codes and output.
1. Define an IOobject within createFields.H file.
Code:

IOdictionary regedDictionary
(
    IOobject
    (
        "regedDictionary",
        runTime.constant(),
        mesh,
        IOobject::NO_READ,
        IOobject::NO_WRITE
    )
);

2. Modify the IOobject within volScalarField psi.
Code:

left
    {
        type            codedFixedValue;
        name            leftCodedVal;
        value          uniform 0.2;
        code
        #{
            const Time &runTime = this->db().time();
            const fvMesh &mesh = this->patch().boundaryMesh().mesh();
            Info << "time = " << runTime.value() << endl;

            IOdictionary &regedDict = mesh.lookupObjectRef<IOdictionary>("regedDictionary");
            dictionary scalardict;
            scalardict.set("value",2 * runTime.value());
            regedDict.set("scalardict",scalardict);
            dictionary vectordict;
            vectordict.set("value",3 * runTime.value());
            regedDict.set("vectordict", vectordict);
            const IOdictionary &searchRegDict = this->db().lookupObject<IOdictionary>("regedDictionary");
            Info << "IOdictionary regedDictionary from psi:" << endl << searchRegDict << endl;
        #};
    }

3. Access the IOobject within volVectorField psiVector.
Code:

left
    {
        type            codedFixedValue;
        value          uniform (0.2 0.2 0.2);
        name            leftVecCodedVal;
        code
        #{
            const IOdictionary &regedDict = this->db().lookupObject<IOobject>("regedDictionary");
            Info << "IOdictionary regedDictionary from psiVector: " << endl << regedDict << endl;
        #};
    }

There are the outputs.
Code:

IOdictionary regedDictionary from psi:

{
    scalardict
    {
        value          4;
    }
    vectordict
    {
        value          6;
    }
}

IOdictionary regedDictionary from psiVector:

{
}

It can be seen that the IOdictionary regedDictionary was truly modified within psi boundary. But it seemed that the modification vanished whthin psiVector. Do you know what's the reason. And do you know how to access the value (or variables) from psiVector defined within the variable psi? Thanks in advance.

ruanyg968tf May 16, 2022 06:05

The problem has been solved. Sorry for my foolish error. The error came from the definition of the IOdictionary within createFields.H. The definition should be like this:
Code:

IOdictionary regedDictionary
(
    IOobject
    (
        "regedDictionary",
        runTime.constant(),
        mesh,
        IOobject::READ_IF_PRESENT,
        IOobject::NO_WRITE
    )
);

.
So the value of dictionary could be updated within psiVector.

By the way, is it possible to define a new variable or dictionary within psi that can be accessed within psiVector. So the modification of the original solver can be avoided. Do you have any ideals?
I have tried the following:
psi:
Code:

left
    {
        type            codedFixedValue;
        name            leftCodedVal;
        value          uniform 0.2;
        code
        #{
            this->operator == (0.2);
            const Time &runTime = this->db().time();
            const fvMesh &mesh = this->patch().boundaryMesh().mesh();
            Info << "time = " << runTime.value() << endl;

            vectorIOField regVectorField
            (
                IOobject
                (
                    "regVectorField",
                    runTime.timeName(),
                    mesh,
                    IOobject::NO_READ,
                    IOobject::NO_WRITE
                ),
                vectorField(this->size(),vector(0,0,0))
            );

            IOdictionary testDict
            (
                IOobject
                (
                    "testDict",
                    runTime.timeName(),
                    mesh,
                    IOobject::NO_READ,
                    IOobject::NO_WRITE
                )
            );

            Info << "db().name() = " << this->db().names() << endl;
        #};
    }

.
psiVector:
Code:

left
    {
        type            codedFixedValue;
        value          uniform (0.2 0.2 0.2);
        name            leftVecCodedVal;
        code
        #{
            Info << "psiVector Fields " << endl << this->db().names() << endl;
        #};
    }

.
I got the following output:
Code:

time = 2
db().name() =
18
(
solutionControl
points
neighbour
psi
faces
psiVector
fvSchemes
fvOptions
faceZones
fvSolution
owner
data
regedDictionary
cellZones
boundary
regVectorField
testDict
pointZones
)

psiVector Fields

16
(
solutionControl
points
neighbour
psi
faces
psiVector
fvSchemes
fvOptions
faceZones
fvSolution
owner
data
regedDictionary
cellZones
boundary
pointZones
)

.
Neither the IOdictionary testDict nor the vectorIOField regVectorField could be accessed from psiVectoer. Any suggestions would be appreciated.


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