|
[Sponsors] |
October 12, 2009, 09:49 |
Perfoming operation if field is present
|
#1 |
Senior Member
Sebastian Gatzka
Join Date: Mar 2009
Location: Frankfurt, Germany
Posts: 729
Rep Power: 20 |
Hello World.
I'm trying to make a tool work with a simple condition: If a field (k) exists perfom an operation, if a different field (nuSgs) exists perform an other operation, if non of these two exist, just do nothing. I'm not sure how to check if a field is present, but I'm sure you can help me. Thank you! S.
__________________
Schrödingers wife: "What did you do to the cat? It's half dead!" |
|
October 12, 2009, 10:03 |
|
#2 |
Super Moderator
Niklas Nordin
Join Date: Mar 2009
Location: Stockholm, Sweden
Posts: 693
Rep Power: 29 |
Is this what you're after...
Code:
IOobject header ( name, mesh.time().timeName(), mesh, IOobject::NO_READ ); if (header.headerOk()) { latida.... } else { } applications/solvers/multiphase/twoPhaseEulerFoam/phaseModel/phaseModel/phaseModel.C |
|
October 12, 2009, 10:25 |
|
#3 |
Senior Member
Sebastian Gatzka
Join Date: Mar 2009
Location: Frankfurt, Germany
Posts: 729
Rep Power: 20 |
Maybe. I have thought about something like this.
But now I'm having a different problem. How can I read the field itself? And how can I prevent the tool from crashing if the field file is not present?! I have tried: Code:
// Reading field k volScalarField k ( IOobject ( "k", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::AUTO_WRITE ), mesh ); // Reading field nuSgs volScalarField nuSgs ( IOobject ( "nuSgs", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::AUTO_WRITE ), mesh );
__________________
Schrödingers wife: "What did you do to the cat? It's half dead!" |
|
October 12, 2009, 10:32 |
|
#4 |
Super Moderator
Niklas Nordin
Join Date: Mar 2009
Location: Stockholm, Sweden
Posts: 693
Rep Power: 29 |
of course that wont work.
just think!!! what will the k-field be if it cant read it? grep READ_IF_PRESENT in the solver/incompressible and you might find this. Code:
volScalarField hTotal ( IOobject ( "hTotal", runTime.timeName(), mesh, IOobject::READ_IF_PRESENT, IOobject::AUTO_WRITE ), h+h0 ); |
|
October 12, 2009, 10:48 |
|
#5 |
Senior Member
Sebastian Gatzka
Join Date: Mar 2009
Location: Frankfurt, Germany
Posts: 729
Rep Power: 20 |
Hmm. But I don't see how this can be useful for me.
What do I have to do?! Where is the link to my problem?
__________________
Schrödingers wife: "What did you do to the cat? It's half dead!" |
|
October 13, 2009, 13:12 |
|
#6 |
Senior Member
Sebastian Gatzka
Join Date: Mar 2009
Location: Frankfurt, Germany
Posts: 729
Rep Power: 20 |
I have absolutely no idea. Maybe it will be nothing at all?!
So what's wrong with my idea: Read the field if present and perform an operation if the header is ok. So the problem is that the field file was attempted to be read - although it's NOT present! So whats READ_IF_PRESENT used for, if not exactly this? I still don't see the link between my problem and your code snipped: What's the point in summing up two different fields (h,h0) when reading hTotal? To make this clear again:
__________________
Schrödingers wife: "What did you do to the cat? It's half dead!" |
|
October 15, 2009, 03:26 |
|
#7 | ||
Super Moderator
Niklas Nordin
Join Date: Mar 2009
Location: Stockholm, Sweden
Posts: 693
Rep Power: 29 |
Quote:
so if you write the code double alpha; it is a perfectly valid code, except what you really have written is double alpha(0.0) because if you dont set an initial value, it will be zero. you can not create a complex class like this, so volScalarField beta; will not work, because a volScalarField needs to know dimension, bc's and size of the field, upon creation. Quote:
But if it doesnt exist, it must take its value from somewhere else. you can not create a volScalarField that is uninitialized, you have to set it to something. if the variable exists, it must have a value. (and since volScalarField doesnt have a default constructor, you have to set its value) so in the constructor you give it two options, the first option is to set the field according to the file, the second option is to set the field according to the specified value if that file didnt exist (in this case hTotal = h + h0). |
|||
October 19, 2009, 14:27 |
|
#8 | |
Senior Member
Sebastian Gatzka
Join Date: Mar 2009
Location: Frankfurt, Germany
Posts: 729
Rep Power: 20 |
Dear niklas.
I had to think about this for a while and tried to use it on my code. I haven't succeeded so far and will post my questions below. Quote:
Considering the case the file does not exist: I need a value "from somewhere else"? Where do I get this value? I have to use an already initialized variable and assign it? So, how do I initialize a variable which I can assign to a volScalarField? I have tried something like this, but it doesn't work out: Code:
volScalarField k0(1); How do I actually assign it to the variable? I'm confused by the example above: Is hTotal the file to be read? Or is it h or h0? Which one is the value with data from somewhere else? You see I actually don't understand very much of what I'm dealing with. I'm really hoping for your input in this matter. Consider a next thought of mine: If I worked out a way of making the above possible I have an empty field named after the file that should be there (but is not)? Further down the road of the tool I'm manipulating the fields and write them to new files but with equal names. So will there be a field named after this empty field in the end? I'm so sorry to make such a stupid impression in this matter, but I don't see the mystery behind these file operations. Hope for your answers & see you. sega
__________________
Schrödingers wife: "What did you do to the cat? It's half dead!" |
||
October 20, 2009, 04:00 |
|
#9 |
Super Moderator
Niklas Nordin
Join Date: Mar 2009
Location: Stockholm, Sweden
Posts: 693
Rep Power: 29 |
Havent you tried just this?
Code:
IOobject kHeader ( "k", runTime.timeName(), mesh, IOobject::NO_READ ); if (kHeader.headerOk()) { Info<< "Reading k.\n" << endl; volScalarField k ( IOobject ( "k", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh ); // Here I do whatever... } |
|
October 20, 2009, 04:43 |
|
#10 |
Senior Member
Sebastian Gatzka
Join Date: Mar 2009
Location: Frankfurt, Germany
Posts: 729
Rep Power: 20 |
No. But this looks very promising.
I will give it a try in the next days and will get back to you.
__________________
Schrödingers wife: "What did you do to the cat? It's half dead!" |
|
October 20, 2009, 10:44 |
|
#11 |
Senior Member
Sebastian Gatzka
Join Date: Mar 2009
Location: Frankfurt, Germany
Posts: 729
Rep Power: 20 |
Works! Thank you!!!
Now, how can I check weather the variable is present or not? Suppose one field (k) is present and was read from file, while the other (nuSgs) is not present and was not read from file, so the variable doesn't exist. So, what I need is a condition to determine if the variables was initialized. Do you know, what I mean?
__________________
Schrödingers wife: "What did you do to the cat? It's half dead!" |
|
October 21, 2009, 06:53 |
|
#12 | |
Senior Member
Sebastian Gatzka
Join Date: Mar 2009
Location: Frankfurt, Germany
Posts: 729
Rep Power: 20 |
Quote:
Unfortunately my idea didn't turn out the way it should be, so I'm bothering you again. To check if a field was successfully read I thought about introducing a variable kPresent just like this Code:
int kPresent = 0; IOobject kHeader ( "k", runTime.timeName(), mesh, IOobject::NO_READ ); if (kHeader.headerOk()) { kPresent = 1; \\ Successfully read from file! Info<< "Reading k.\n" << endl; volScalarField k ( IOobject ( "k", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh ); } So far no magic. In the main program I check the variable kPresent and can determine weather the file was read or not. For example like this: Code:
if (kPresent == 1) { surfaceScalarField ks = fvc::interpolate(k); } The problem is that the lines within every if-condition containing k give me trouble when running wmake. In this case: Code:
error: ‘k’ was not declared in this scope Or is there a problem because k is actually declared inside a if (kHeader.headerOk()){}-bracket and therefore is not know to the "outside world"? Any ideas? Have a nice day!
__________________
Schrödingers wife: "What did you do to the cat? It's half dead!" |
||
October 21, 2009, 07:20 |
|
#13 |
Super Moderator
Niklas Nordin
Join Date: Mar 2009
Location: Stockholm, Sweden
Posts: 693
Rep Power: 29 |
something like this maybe...
Code:
IOobject kHeader ( "k", runTime.timeName(), mesh, IOobject::NO_READ ); volScalarField* kPointer; bool kExist = false; if (kHeader.headerOk()) { kExist = true; Info<< "Reading k.\n" << endl; kPointer = new volScalarField ( IOobject ( "k", runTime.timeName(), mesh, IOobject::MUST_READ, IOobject::AUTO_WRITE ), mesh ); } if (kExist) { surfaceScalarField ks = fvc::interpolate(*kPointer); } |
|
August 16, 2023, 06:08 |
|
#14 |
New Member
Join Date: Jun 2018
Posts: 20
Rep Power: 8 |
In case someone reads this:
The syntax changed for newer OF versions. Code:
if(MyHeader.typeHeaderOk<volScalarField>(true)) { do something } else { do something else } |
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Phase Field modeling in OpenFOAM | adona058 | OpenFOAM Running, Solving & CFD | 35 | November 16, 2021 01:16 |
Moving mesh | Niklas Wikstrom (Wikstrom) | OpenFOAM Running, Solving & CFD | 122 | June 15, 2014 07:20 |
Operation to READ_IF_PRESENT field | sega | OpenFOAM Programming & Development | 6 | February 15, 2011 10:57 |
Porosity field in Fluent | wojciech | FLUENT | 1 | September 20, 2010 12:19 |
Problem with rhoSimpleFoam | matteo_gautero | OpenFOAM Running, Solving & CFD | 0 | February 28, 2008 07:51 |