CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Running, Solving & CFD (
-   -   Programmatically create fvSolution dictionary (

cliffoi February 3, 2009 10:22

Hi all, I'm trying to program
Hi all,
I'm trying to programmatically create the fvSolution dictionary on OF-1.4.1-dev but I'm getting stuck with the individual solver entries since they consist of a keyword, token and dictionary, e.g.
T PCG { ...... }

I am able to create an entry with either the token ( T PCG; )or dictionary ( T { .... } ) but not one with both.
I'm trying to avoid using the low level stream classes (ITstream, etc). I had a look in the FoamX source code for ideas on how they do it and they seem to have rewritten a lot of the working code using the low level stream classes for creating the dictionaries so I didn't have much luck there.

My guess is I need to use the dictionary::add(const tokenList&) method but I don't know how to retrieve the token list for the subdictionary.

Any suggestions would be appreciated.


olesen February 3, 2009 10:31

Hi Ivor, If you look closel
Hi Ivor,

If you look closely, you'll see that they are not actually a 'dictionaryEntry', but rather a 'primitiveEntry' at the moment - the trailing ';' after the closing '}' is a hint here.

Take a look under src/OpenFOAM/db/dictionary for the associated classes.

kev4573 February 3, 2009 10:35

Hi, Have you looked at the PyF
Hi, Have you looked at the PyFOAM utilities? I'm not sure if this accomplishes your goal but it is relatively easy to manipulate dictionaries using those libraries.

cliffoi February 6, 2009 11:06

Thanks for the responses. Mar
Thanks for the responses.
Mark, I'd ideally like to create the dictionary portion of the primitiveEntry using the dictionary class, which will make the programming easier but I'm unable to convert that dictionary object to a tokenList so that I can append it to the solver type name. Any ideas?

Kevin, this is exactly the approach I'm taking.
Thanks for the PyFOAM idea though since oddly enough I was completely unaware of the existence of this contribution and am currently working on my own Swig wrappings for OF. Seems the work is being duplicated. I'll take a closer look at PyFoam.

eugene February 6, 2009 11:44

Hi Ivor, This file contains
Hi Ivor,

This file contains some simple functions I use to write dictionaries so that they look "nice". I'm not 100% sure it will have everything you need, but it should give you a few clues. Let me know if you get stuck.

olesen February 6, 2009 11:51

Hi Ivor, Why not the really
Hi Ivor,

Why not the really simple solution?

solverDict.write(os, true);
os ... // write semi-colon

Of course I would actually cheat a little bit and add the solver name directly into the dictionary of settings. It shows up twice on the output, but the solver itself will just ignore it and it make it much easier to write into a loop.

The above code then looks like this
solverDict.lookupOrDefault lt; word gt;("solver", "something")
solverDict.write(os, true);
os ... // write semi-colon

Wrap all of this into a larger dictionary and/or class and it should work.

gschaider February 8, 2009 07:44

Hi Ivor! If you're wrapping
Hi Ivor!

If you're wrapping OpenFOAM using Swig then PyFoam is NOT duplicating your effort. PyFoam only tries to control an OpenFOAM-run "from the outside": by reading it's output and manipulating directories. OK. It does a number of different things too, but I draw the line at anything that requires compilation to keep it as portable as possible and OF-version-agnostic.

However: Hrv Jasak demonstrated a Python Wrapping to OpenFOAM at the Workshop in Milan this summer. So that might be a duplcation


olesen February 9, 2009 03:24

Hi Ivor, Maybe you want to
Hi Ivor,

Maybe you want to look into cint as well. If you get it working with OpenFOAM, you could prototype (script) in interpreted C++, which could be compiled later as required.

cliffoi February 9, 2009 05:33

Thanks Mark, but I must be mis
Thanks Mark, but I must be missing some intermediate step here. I now have an OStringStream which I can't add directly as an entry to the dictionary. Am I expected to go via file?

Bernhard, the Swig wrappings are based on Hrv's wrappings so no worries there, thanks.

hjasak February 9, 2009 05:37

Heya, How about we add this

How about we add this functionality into the solution.C so if you call it without the solver, the basic layout will be given.


cliffoi February 9, 2009 05:51

Hi Hrv, The thought of someth
Hi Hrv,
The thought of something along these lines had crossed my mind. We could potentially construct from a dictionary with an empty "solvers" subdictionary and include an addSolver method. Is this what you had in mind?


hjasak February 9, 2009 10:43

Yes, exactly: just give it an
Yes, exactly: just give it an empty constructor and create dictionaries on the fly.

I guess you can do that easily (please pass it over for a check-in).


eugene February 10, 2009 10:17

Hi Ivor, To convert a OStri
Hi Ivor,

To convert a OStringStream to an entry:

OStringStream something(<something>);

primitiveEntry MyEntry

Then it is simply:



mariam February 11, 2009 08:40

Dear Hrvoje and all, I read t
Dear Hrvoje and all,
I read the remarks about Swig wrappings with great interest. What kept me from getting proficient with Openfoam is to a good extent the solver codes and their use of included snippets that makes them difficult to understand - for me. Scripted Openfoam has definitely the potential to change that.

Is the scripted version included in the openfoam-extend repositories?


cliffoi February 12, 2009 03:50

Thanks all for your suggestion
Thanks all for your suggestions. I have updated the solution class with a NO_READ constructor and included addSolver and addRelaxationFactor methods. solution-2009-02-12.tar.gz
Also included in the archive is a test application.


All times are GMT -4. The time now is 15:34.