CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Software User Forums > OpenFOAM > OpenFOAM Meshing & Mesh Conversion

[blockMesh] Writing if else statement in the blockMeshDict file

Register Blogs Members List Search Today's Posts Mark Forums Read

Like Tree1Likes
  • 1 Post By olesen

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   December 3, 2020, 03:18
Default Writing if else statement in the blockMeshDict file
  #1
New Member
 
Duc Anh
Join Date: Dec 2018
Posts: 22
Rep Power: 7
anhkenyt is on a distinguished road
Hi foamers,
I want to create an if-else statement in my definition of sin alpha and cos alpha in file blockMeshDict, show that lt changes according to a user-defined. I was tried code#{#}; below but it seems not to work( Do Not display line 0.0 and cosAlpha sill constant)
Code:
/*
		      *P
		    / || \ 
		   /      \   		|P-2| = innerRadius
                  /        \	        <P' P 2> = bendingAngle				
		 3    P'    2		|2-1| = |1-0| =R1R2= width/2
	        / _         _\
	       /   _     _    \
	      /      _23_      \
	     4	                1
	    / _              _   \
	   /    _          _      \
          / 	   _  14  _        \
  	   5			   0
	     _	                 _
		 _          _
		    _ 05 _
		      
*/
convertToMeters 1;
mesh
{
	bendingAngle 111;// 8.18;   // >100 convert to rectangular
	innerRadius	4;
	R1R2		1;   //inter radius to outer radius
	nCells_x	10;
	nCells_y	55;
	nCells_z	20;
	length 		5;
}
parameters
{
	width		$:mesh.R1R2;
	innerR		$:mesh.innerRadius;
	outerR		#calc "$innerR + $width";
	CAD		$:mesh.bendingAngle;	// channel angle in degree
	CAR		#calc "degToRad($CAD)/2.0";	// channel angle in radian
	sinAlpha #calc "sin($CAR)";
	cosAlpha #calc "cos($CAR)";
	code
	#{
		if($:mesh.bendingAngle >= 100)
		cosAlpha 1;		
		#calc "Info<<0.0<<endl";
		#calc "Info<<$cosAlpha << endl";	
	#};
	#calc "Info<<0.1<<endl";
	#calc "Info<<$cosAlpha << endl";
And in terminal:
Code:
0.1
Using #calcEntry at line 63 in file "/home/anh/ducanhvan/fe40/dev/PNPNSFoam/StudyCases/r_4/r_4_mesh/constant/polyMesh/blockMeshDict.parameters"
Using #codeStream with "/home/anh/ducanhvan/fe40/dev/PNPNSFoam/StudyCases/r_4/r_4_mesh/dynamicCode/platforms/linux64GccDPInt32Opt/lib/libcodeStream_c1492ef6f3c1453e058e87865d31757234430ca9.so"
0.566406
Anyone can help me, thanks!
anhkenyt is offline   Reply With Quote

Old   December 3, 2020, 13:43
Default
  #2
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
First, remember that the #{ .. #} blocks indicate verbatim text. This is taken directly, without any string expansions or anything. Additionally, you have attempted to embed a #calc with an extra output within something that you already want to have treated as a block of C++ code.


You will need to rework things. You are just trying too many things at once.
BTW: if you have OpenFOAM-v1912 or v2006, you will find that the #eval statement is much, much faster that compiling a #calc. Additionally, you can do really weird things with "${{ .. }}" syntax to embed calculations in other strings.


If you test the tutorials/combustion/XiDyMFoam/annularCombustorTurbine blockMesh with the #eval vs #calc, you will see the difference.


Can also take a look at applications/test/exprEntry/testDict1 for some weird things.

https://develop.openfoam.com/Develop...de#expressions
https://www.openfoam.com/documentati...on-syntax.html
anhkenyt likes this.
olesen is offline   Reply With Quote

Old   December 5, 2020, 07:59
Default
  #3
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Have you tried it with changing to #eval?
I think the following should work (only showing the second dictionary)
Code:
parameters
{
	width		$:mesh.R1R2;
	innerR		$:mesh.innerRadius;
	outerR		#eval{ $innerR + $width };
	CAD		$:mesh.bendingAngle;  // channel angle in degree
	sinAlpha #eval{ sin(degToRad($CAD)) };
	cosAlpha #eval "cos(degToRad($CAD))";
#if #eval{ $:mesh.bendingAngle >= 100 /* upper limit */ }
	cosAlpha 1;
#endif
}
Notice I have used both double quotes and brace brackets to delimit the input for #eval. There is no real difference between them, but IMO the syntactical sugar of the brace brackets improves the readability. For more complex expressions, can embed comments, which are stripped out before evaluation.
olesen is offline   Reply With Quote

Old   December 7, 2020, 04:49
Default
  #4
New Member
 
Duc Anh
Join Date: Dec 2018
Posts: 22
Rep Power: 7
anhkenyt is on a distinguished road
Quote:
Originally Posted by olesen View Post
Have you tried it with changing to #eval?
I think the following should work (only showing the second dictionary)
Code:
parameters
{
	width		$:mesh.R1R2;
	innerR		$:mesh.innerRadius;
	outerR		#eval{ $innerR + $width };
	CAD		$:mesh.bendingAngle;  // channel angle in degree
	sinAlpha #eval{ sin(degToRad($CAD)) };
	cosAlpha #eval "cos(degToRad($CAD))";
#if #eval{ $:mesh.bendingAngle >= 100 /* upper limit */ }
	cosAlpha 1;
#endif
}

Notice I have used both double quotes and brace brackets to delimit the input for #eval. There is no real difference between them, but IMO the syntactical sugar of the brace brackets improves the readability. For more complex expressions, can embed comments, which are stripped out before evaluation.
Thanks for your answer Mark Olesen, but #eval has been seeming not available in openfoam6 it's my version I used. Do you have some other suggestions for me?
Code:
Unknown functionEntry 'eval' in "/home/anh/ducanhvan/fe40/devfe40/StudyCases/r_4_small10_rec/constant/polyMesh/blockMeshDict" near line 80

Valid functionEntries are :

5
(
codeStream
include
calc
includeIfPresent
includeEtc
)
anhkenyt is offline   Reply With Quote

Old   December 13, 2020, 06:45
Default
  #5
Senior Member
 
Mark Olesen
Join Date: Mar 2009
Location: https://olesenm.github.io/
Posts: 1,714
Rep Power: 40
olesen has a spectacular aura aboutolesen has a spectacular aura about
Quote:
Originally Posted by anhkenyt View Post
Thanks for your answer Mark Olesen, but #eval has been seeming not available in openfoam6 it's my version I used. Do you have some other suggestions for me?
I am clearly biased towards using #eval (which I put into OpenFOAM-v1912). You can possibly do some of this with #calc too (although less efficient) but you may need to be careful with what the #if receives.
olesen is offline   Reply With Quote

Reply

Tags
blockmeshdict, codestream, if-else statement

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
[Other] Adding solvers from DensityBasedTurbo to foam-extend 3.0 Seroga OpenFOAM Community Contributions 9 June 12, 2015 18:18
[snappyHexMesh] crash sHM H25E OpenFOAM Meshing & Mesh Conversion 11 November 10, 2014 12:27
centOS 5.6 : paraFoam not working yossi OpenFOAM Installation 2 October 9, 2013 02:41
ParaView Compilation jakaranda OpenFOAM Installation 3 October 27, 2008 12:46
DxFoam reader update hjasak OpenFOAM Post-Processing 69 April 24, 2008 02:24


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