CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM (https://www.cfd-online.com/Forums/openfoam/)
-   -   Extension to foamInfoExec/changeDictionary? (https://www.cfd-online.com/Forums/openfoam/70209-extension-foaminfoexec-changedictionary.html)

wikstrom November 17, 2009 07:27

Extension to foamInfoExec/changeDictionary?
 
Hi,

I'd like a utility to add or change values of entries in a dictionary. I.e. I would like to write an application similar to foamInfoExec but with a "-set" (and possibly an "-insert") option. This would be a changeDictionary-ish util reading the command line.

Does someone have something similar for me to learn from? I keep getting tangled up in pointers and virtuals...

best regards
Niklas

gschaider November 18, 2009 13:01

Quote:

Originally Posted by wikstrom (Post 236615)
Hi,

I'd like a utility to add or change values of entries in a dictionary. I.e. I would like to write an application similar to foamInfoExec but with a "-set" (and possibly an "-insert") option. This would be a changeDictionary-ish util reading the command line.

Does someone have something similar for me to learn from? I keep getting tangled up in pointers and virtuals...

best regards
Niklas

Why do you want to bring a gun to knife-fight? In other words: write a C++-program for something that can be easily (without compiling) done with a script (those who know me will say "Oh, my god. There he goes again with that PyFoam" and they are right).
A simple example for manipulating dictionaries can be found at
http://openfoamwiki.net/index.php/Co...ions_for_walls

There is a read-made utility pyFoamWriteDictionary.py for setting single values in a dictionary, but it has its drawbacks

Bernhard

wikstrom November 19, 2009 03:34

Hi Bernhard,

simply because the dictionary and IO framework is already there in OpenFOAM. I really appreciate PyFOAM (encores!) and love python, but nevertheless find it neat to do OpenFOAM stuff within OpenFOAM.


Cheers
Niklas

wikstrom November 19, 2009 03:40

BTW, I might have found a way using dictionary::add(...,true)...

fs82 November 19, 2009 04:10

Check your OpenFoam Source directory. There are bash scripts which manipulate the dictionaries. They provide a whole bunch of functions for this task: For example the script: foamEndJob. There you could find for example:

setRawEntry() {
oldNumLine=`getNumberedLine $1 "$2"`
lineNo=`echo "$oldNumLine" | sed -e 's/:.*//'`
oldLine=`echo "$oldNumLine" | sed -e 's/^[^:]*://'`
oldKey=`getKey "$oldLine"`
oldVal=`getRawEntry $1 "$2"`
if [ ! "$oldKey" -o ! "$oldVal" -o ! "$oldLine" ]; then
echo "setRawStringEntry: entry $2 not found in $1"
echo "oldKey=$oldKey"
echo "lineNo=$lineNo"
echo "oldLine=$oldLine"
exit 1
fi
#echo "oldKey=$oldKey"
#echo "lineNo=$lineNo"
#echo "oldLine=$oldLine"
#echo "oldVal=$oldVal"
mv $1 ${1}_tmp
sed -e "${lineNo}s/ ${oldVal}/ $3;/" ${1}_tmp > $1
rm -f ${1}_tmp
}

This function sets an exiting entry to a new value. But there are many more and it should be easy to adopt this to your case :-D

gschaider November 19, 2009 04:23

Hi Niklas

Don't get me wrong. This is not about advertising Python/PyFoam it was about choosing the appropriate language for the job. If you have a look at http://wwwipd.ira.uka.de/~prechelt/Biblio/jccpprtTR.pdf Figure 15 (just the first example I stumbled upon) you will see that if you want to get a programming job done quickly almost any scripting language is better than C++. The advantage of C++ is the pure speed (and you don't need that for dictionary manipulation)

Quote:

Originally Posted by wikstrom (Post 236856)
simply because the dictionary and IO framework is already there in OpenFOAM. I really appreciate PyFOAM (encores!) and love python, but nevertheless find it neat to do OpenFOAM stuff within OpenFOAM.

So if you see it as a way to get to know the OF-classes it is alright

Bernhard


All times are GMT -4. The time now is 10:23.