CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Community Contributions (https://www.cfd-online.com/Forums/openfoam-community-contributions/)
-   -   [PyFoam] modifying Dictionary (https://www.cfd-online.com/Forums/openfoam-community-contributions/196949-modifying-dictionary.html)

ashkan December 22, 2017 05:50

modifying Dictionary
 
Hi,
I would like to write a Python/PyFoam script to modify a base setup and perform batch runs. I have a file called "initialConditions" in "0/include" directory which I define initial values. I would like to modify the values within this file. To do so I use "ParsedParameterFile" but when I run my script I get the following error:

Code:

PyFoam.RunDictionary.ParsedParameterFile.PyFoamParserError: Error in PyFoamParser: 'Syntax error in file /media/Data/Work_Research/Codes/OFSims/sim/OF4x/Large_domain/test_PyFoam/Test/ClonedCase/0/include/initialConditions at token' @ 'flowVelocity' (Type: NAME ) in line 9 at position 561
Attached please find my script and the Case.

I would really appreciate any comments and suggestions.

Many thanks
Ashkan

ashkan December 23, 2017 23:59

Problem solved :)

gabrielfelix September 8, 2021 14:45

Quote:

Originally Posted by ashkan (Post 675974)
Problem solved :)

Can you tell us the solution? I'm having the same problem

gabrielfelix September 9, 2021 07:57

Quote:

Originally Posted by gabrielfelix (Post 811861)
Can you tell us the solution? I'm having the same problem

I have found the problem. In my initialConditions file there was spaces instead of tabs in the indentation. I just had to substitute them and certify that all the indentation was made with tabs.

The ParsedParameterFile worked fine after this modification.

Code:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  v2012                                |
|  \\  /    A nd          | Website:  www.openfoam.com                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/


flowVelocity                (0 -20 0);                // m/s
flowStaticPressure        101325;                // Pa
flowTemperature        298;                        // K
nuTildaVal                        4.5e-5;                // m^2/s
nutVal                        3.5e-6;                // m^2/s
rhoVal                        1.225;                // kg/m^3
nuVal                        1.5e-5;                // m^2/s
epsilonVal                        0.0495;                // m^2/s^3
kVal                                0.06;                // m^2/s^2

// ************************************************************************* //


gschaider September 9, 2021 18:55

Quote:

Originally Posted by gabrielfelix (Post 811904)
I have found the problem. In my initialConditions file there was spaces instead of tabs in the indentation. I just had to substitute them and certify that all the indentation was made with tabs.

The ParsedParameterFile worked fine after this modification.

Code:

/*--------------------------------*- C++ -*----------------------------------*\
| =========                |                                                |
| \\      /  F ield        | OpenFOAM: The Open Source CFD Toolbox          |
|  \\    /  O peration    | Version:  v2012                                |
|  \\  /    A nd          | Website:  www.openfoam.com                      |
|    \\/    M anipulation  |                                                |
\*---------------------------------------------------------------------------*/


flowVelocity        (0 -20 0);        // m/s
flowStaticPressure    101325;        // Pa
flowTemperature    298;            // K
nuTildaVal            4.5e-5;        // m^2/s
nutVal            3.5e-6;        // m^2/s
rhoVal            1.225;        // kg/m^3
nuVal            1.5e-5;        // m^2/s
epsilonVal            0.0495;        // m^2/s^3
kVal                0.06;        // m^2/s^2

// ************************************************************************* //





That is strange. If the parser had a tab/space problem I would expect it to fail on tabs. Simply because I usually make my text editor write tabs as spaces (and therefore could have missed tabs-related problems)

gabrielfelix September 9, 2021 19:19

Quote:

Originally Posted by gschaider (Post 811931)
That is strange. If the parser had a tab/space problem I would expect it to fail on tabs. Simply because I usually make my text editor write tabs as spaces (and therefore could have missed tabs-related problems)

In my file there were spaces and tabs mixed. I had had edited the file and in some places there were spaces and on others tabs. So the parser must hava had trouble on dealing with tabs and spaces mixed.

gschaider September 12, 2021 17:19

Quote:

Originally Posted by gabrielfelix (Post 811932)
In my file there were spaces and tabs mixed. I had had edited the file and in some places there were spaces and on others tabs. So the parser must hava had trouble on dealing with tabs and spaces mixed.

Could you provide me with an example file?

gabrielfelix September 13, 2021 13:38

Quote:

Originally Posted by gschaider (Post 812046)
Could you provide me with an example file?

I took another look into it and realised that the error was in my editor. I usually edit the OF files in Notepad++, as I use WSL. When I use tab to indent in Notepad++ it adds a different number of spaces than which was already in the file causing problems in indentation. I used nano to check the file after editiong in npp, as npp shown no indentation problems.

In this topic the user had the same problem as I, and the solution that the community came up with worked fine for me. However, I still check in nano if there are indentation issues on the file before running it.

Quoating the discussion:
Question:
Quote:

I'm coding in Python and I really like Notepad++. However, off late when I use tab to indent, it seems fine in Notepad++, but when I run the program I get an indentation error, and when I check my code in Emacs or something, I find that Notepad++ actually adds more tab spaces than it shows on screen. What is happening?
Answer:
Quote:

There is no universal tab size, so I always make sure to replace tabs by spaces (so you know what you see is what you get everywhere else as well)

Go to Settings -> "Preferences..." -> Language Menu/Tab Settings and check 'Replace by space'

gschaider September 14, 2021 13:50

Quote:

Originally Posted by gabrielfelix (Post 812111)
I took another look into it and realised that the error was in my editor. I usually edit the OF files in Notepad++, as I use WSL. When I use tab to indent in Notepad++ it adds a different number of spaces than which was already in the file causing problems in indentation. I used nano to check the file after editiong in npp, as npp shown no indentation problems.

In this topic the user had the same problem as I, and the solution that the community came up with worked fine for me. However, I still check in nano if there are indentation issues on the file before running it.

Quoating the discussion:
Question:

Answer:


Still strange. The different Tabs and Spaces shouldn't matter for an OpenFOAM-File. For a Python file: yes


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