CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Community Contributions

[PyFoam] modifying Dictionary

Register Blogs Community New Posts Updated Threads Search

Like Tree1Likes
  • 1 Post By ashkan

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 22, 2017, 06:50
Default modifying Dictionary
  #1
Member
 
Join Date: Jul 2010
Posts: 55
Rep Power: 16
ashkan is on a distinguished road
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
Raffaele10 likes this.

Last edited by ashkan; December 24, 2017 at 00:57.
ashkan is offline   Reply With Quote

Old   December 24, 2017, 00:59
Smile
  #2
Member
 
Join Date: Jul 2010
Posts: 55
Rep Power: 16
ashkan is on a distinguished road
Problem solved
ashkan is offline   Reply With Quote

Old   September 8, 2021, 15:45
Default
  #3
Member
 
Gabriel Felix
Join Date: May 2021
Location: Brazil
Posts: 35
Rep Power: 6
gabrielfelix is on a distinguished road
Quote:
Originally Posted by ashkan View Post
Problem solved
Can you tell us the solution? I'm having the same problem
gabrielfelix is offline   Reply With Quote

Old   September 9, 2021, 08:57
Default
  #4
Member
 
Gabriel Felix
Join Date: May 2021
Location: Brazil
Posts: 35
Rep Power: 6
gabrielfelix is on a distinguished road
Quote:
Originally Posted by gabrielfelix View Post
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

// ************************************************************************* //
gabrielfelix is offline   Reply With Quote

Old   September 9, 2021, 19:55
Default
  #5
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by gabrielfelix View Post
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)
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   September 9, 2021, 20:19
Default
  #6
Member
 
Gabriel Felix
Join Date: May 2021
Location: Brazil
Posts: 35
Rep Power: 6
gabrielfelix is on a distinguished road
Quote:
Originally Posted by gschaider View Post
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.
gabrielfelix is offline   Reply With Quote

Old   September 12, 2021, 18:19
Default
  #7
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by gabrielfelix View Post
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?
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Old   September 13, 2021, 14:38
Default
  #8
Member
 
Gabriel Felix
Join Date: May 2021
Location: Brazil
Posts: 35
Rep Power: 6
gabrielfelix is on a distinguished road
Quote:
Originally Posted by gschaider View Post
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'
gabrielfelix is offline   Reply With Quote

Old   September 14, 2021, 14:50
Default
  #9
Assistant Moderator
 
Bernhard Gschaider
Join Date: Mar 2009
Posts: 4,225
Rep Power: 51
gschaider will become famous soon enoughgschaider will become famous soon enough
Quote:
Originally Posted by gabrielfelix View Post
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
__________________
Note: I don't use "Friend"-feature on this forum out of principle. Ah. And by the way: I'm not on Facebook either. So don't be offended if I don't accept your invitation/friend request
gschaider is offline   Reply With Quote

Reply

Tags
parsedparameterfile, pyfoam


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
ill defined primitiveEntry starting at keyword 'value' on line 197 ChangeDictionary Struggle_Achieve OpenFOAM Pre-Processing 2 December 20, 2017 04:58
Including lists in a dictionary. Divyaprakash OpenFOAM Programming & Development 0 June 18, 2015 07:14
New Boundary Condition: Reading Dictionary Problem Koga OpenFOAM Programming & Development 0 November 26, 2012 06:01
Problem with rhoSimpleFoam matteo_gautero OpenFOAM Running, Solving & CFD 0 February 28, 2008 07:51
FoamX error aachenBomb case Ervin Adorean (Adorean) OpenFOAM Pre-Processing 13 March 7, 2005 04:50


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