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 update volVectorField from files (https://www.cfd-online.com/Forums/openfoam-programming-development/238558-how-update-volvectorfield-files.html)

newOpenfoamUser September 20, 2021 06:44

How to update volVectorField from files
 
Hi,

I am new to OpenFOAM and new to C++ as well. I have a basic question. My question is what is the proper way to update volScalarField or volVectorField from files (the files come from previous calculation)?

My problem is that I want to use scalarTransportFoam with prescribed time-varying velocity and phi field (calculated from pisoFoam). You might ask why don't I customise this into the time loop in pisoFoam. The thing is I know that my solution is periodic (with period of around 1s) so I only have to run pisoFoam for 3 periods for the solution to converge. Then, I can run scalarTransportFoam for 100 periods with the periodic velocity and phi field calculated earlier. By doing this, I think I can save a lot of time because I suppose pisoFoam is more computationally expensive than scalarTransportFoam.

The problem is that scalarTransportFoam only works for time-independent velocity and it will overwrite all the subsequent velocity with the initial velocity. My solution is to first change IOobject::AUTO_WRITE to IOobject::NO_WRITE in the initialisation of U and phi in "createFields.H" and "createPhi.H". While this will prevent it to overwrite the prescribed U and phi field, it does not read the U and phi field for each time step.

My second step is to include the following code inside the time loop of scalarTransportFoam:
Code:

U = volVectorField
(
    IOobject
    (
        "U",
        runTime.timeName(),
        mesh,
        IOobject::MUST_READ,
        IOobject::NO_WRITE
    ),
    mesh
);

phi = surfaceScalarField
(
    IOobject
    (
        "phi",
        runTime.timeName(),
        mesh,
        IOobject::READ_IF_PRESENT,
        IOobject::NO_WRITE
    ),
    fvc::flux(U)
);

This seems to work as I intended. However, I wonder whether this will create a new copy of volVectorField and surfaceScalarField while the previous copies will still live in the memory? So will this cause some memory and performance issues? I tried calling the destructor but that doesn't seem to work. This might be a basic C++ question but I am not very familiar with how memory allocation work in C++. Another question is that whether there are some member functions in the volVectorField and surfaceScalarField class that allow us to update the field values from files directly without having to create a new copy?


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