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/)
-   -   Reading field only at the first time folder (https://www.cfd-online.com/Forums/openfoam-programming-development/177476-reading-field-only-first-time-folder.html)

yongxiang September 14, 2016 11:29

Reading field only at the first time folder
 
Hello everyone,
I am implementing a new solver with openfoam, I need to subtract a constant source term from the momentum equation. Since this source term is constant and is pre-obtained, it is better to only save it in the first time step folder. The following is the code (in red) I am using now, but this would end up with saving this term in every time step folder. If anyone hows how to implement it ?

Info<< "Reading field dU0dt\n" << endl;
volVectorField dU0dt
(
IOobject
(
"dU0dt",
runTime.timeName(),
mesh,
IOobject::MUST_READ,
IOobject::AUTO_WRITE
),
mesh
);

ps: Saving this constant term in /constant folder is not an option, since it would work with parallel computing. Therefore, it is better to be saved in the time folder.

AnthonyP September 14, 2016 15:50

Hi yongxiang,

I'm quite new at this, but this looks like it works.

In createFields.H
I wrote the following code snippet:
Code:

Info<< "Reading field dU0dt\n" << endl;
volVectorField dU0dt
(
IOobject
(
"dU0dt",
runTime.constant(),
mesh,
IOobject::MUST_READ,
IOobject::NO_WRITE
),
mesh
);

in the solver, I added the following lines:

Before the while (runTime.run()) loop:
Code:

bool write_dUdT = true;
After the first round of calculations (or wherever it is feasible for your code):
I placed it after runTime.write();
Code:

if(write_dUdT){
        dU0dt.write();
        write_dUdT = false;
        }

and that's it.

After the first time step, it will create a constant (placed in your constant folder) which will then be referred to (and unedited) for following calculations.
--
Note, I am not sure if you need this value for your first timestep, If you do, you should probably initialize it with some approximation.
You could also move the dU0dt.write(); further up in the solver and use READ_IF_PRESENT.
--
Let me know if this helps,

Tony

yongxiang September 14, 2016 16:58

Hi Tony,
thanks very much for your reply. You provide me an option to complete my desired task. However, the thing is if dU0dt is saved in the /constant folder, there would be problem with parallel computation. As far as I understood, the parallel computation only decompose the /polyMesh folder, where the mesh is stored, into different /processor* folder. Therefore, if dU0dt is stored in the /constant folder, it would not be decomposed, which would make the parallel computing fail.
That's why I think saving dU0dt in time folder would be better, since it would be automatically decomposed for parallel computation. I know function like:

runTime.constant(), and
runTime.timeName(),

but do you know something like:

runTime.startTime() or
runTime.timeName(startTime) or
runTime.timeName().startTime()

which would allow us to read the dU0dt always from the startTime folder. (All the above are not working as I already tried.)

Thanks again for your suggestion.
Yongxiang

AnthonyP September 14, 2016 19:26

Whoops,

Sorry about that...

Note: I accidently posted an incorrect solution.

I'll keep looking for a solution.


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