CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   pyFoam WriteParameterFile: Orders of entries (http://www.cfd-online.com/Forums/openfoam-programming-development/107255-pyfoam-writeparameterfile-orders-entries.html)

Horus September 21, 2012 12:31

pyFoam WriteParameterFile: Orders of entries
 
Hello,

I use pyFoam's WriteParameterFile to write to the files in 0 directory (U, p, ...)

Code:

field_file = WriteParameterFile(field_name), className=_OF_units[field_name][1])
field_file["internalField"] = internalField
field_file["dimensions"] = dimensions

boundaryField = {}
# Fill boundaryField in a loop

field_file["boundaryField"] = boundaryField
field_file.writeFile()

My problem is that in thesulting field there is the boundaryField first and after that dimensions and internalField. Since I use $internalField as a value to several BCs this a problem since it can't be referenced before used. I also tried and changed the order in which I write I set the dictionaries entries in the field_file but it changed nothing.

Is there a way to change the order? It have to, cause the pyFoamCaseBuilder uses the "correct" order. (though python dictionaries do not have any order...)

Thanks!

Ok, kind of an addendum. I found that piece of code:

Code:

        if type(dic)==DictProxy:
            order=dic._order
        else:
            order=dic.keys()
            order.sort()

in PyFoam.Basics.FoamFileGenerator.FoamFileGenerator. strDict. So it seems to I need to use PyFoam DictProxy objects... Is that correct so far?

gschaider September 24, 2012 18:31

Quote:

Originally Posted by Horus (Post 383001)
Hello,

I use pyFoam's WriteParameterFile to write to the files in 0 directory (U, p, ...)

Code:

field_file = WriteParameterFile(field_name), className=_OF_units[field_name][1])
field_file["internalField"] = internalField
field_file["dimensions"] = dimensions

boundaryField = {}
# Fill boundaryField in a loop

field_file["boundaryField"] = boundaryField
field_file.writeFile()

My problem is that in thesulting field there is the boundaryField first and after that dimensions and internalField. Since I use $internalField as a value to several BCs this a problem since it can't be referenced before used. I also tried and changed the order in which I write I set the dictionaries entries in the field_file but it changed nothing.

Is there a way to change the order? It have to, cause the pyFoamCaseBuilder uses the "correct" order. (though python dictionaries do not have any order...)

Thanks!

Ok, kind of an addendum. I found that piece of code:

Code:

        if type(dic)==DictProxy:
            order=dic._order
        else:
            order=dic.keys()
            order.sort()

in PyFoam.Basics.FoamFileGenerator.FoamFileGenerator. strDict. So it seems to I need to use PyFoam DictProxy objects... Is that correct so far?

You're right in the constructor of WriteParameterFile the line
self.content={}
should be replaced with
self.content=DictProxy()

DictProxy is a wrapper around a regular dictionary whose purpose it is to preserve the order of the entries (and preserve comments) and for purposes of output it will order the entries in the order they were added

Let me know if the fix works for you and I'll add it to the next release

Horus September 26, 2012 15:01

I have worked around it in another way.

After writing the internalField and dimensions I close the file and reopen it (as ParsedParameterFile). This keeps the entries so far in order that the ones that already existed are kept on top.

gschaider September 26, 2012 15:30

Quote:

Originally Posted by Horus (Post 383794)
I have worked around it in another way.

After writing the internalField and dimensions I close the file and reopen it (as ParsedParameterFile). This keeps the entries so far in order that the ones that already existed are kept on top.

OK. But I've already implemented the fix (it was as easy as described above) and it will be in the next release


All times are GMT -4. The time now is 05:27.