Skip to content

Instantly share code, notes, and snippets.

@Foadsf

Foadsf/blockMeshDict

Last active Aug 30, 2018
Embed
What would you like to do?
imports a blockMeshDict files and display it in FreeCAD draft workbench
// test blockMeshDict file
/*--------------------------------*- C++ -*----------------------------------*\
| ========= | |
| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox |
| \\ / O peration | Version: 5 |
| \\ / A nd | Web: www.OpenFOAM.org |
| \\/ M anipulation | |
\*---------------------------------------------------------------------------*/
FoamFile
{
version 2.0;
format ascii;
class dictionary;
object blockMeshDict;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
convertToMeters 0.001;
vertices
(
(-20.6 0 -0.5)
(-20.6 25.4 -0.5) /* Some comment */
(0 -25.4 -0.5)
(0 0 -0.5)
(0 25.4 -0.5)
(206 -25.4 -0.5)
(206 0 -0.5)
(206 25.4 -0.5)
(290 -16.6 -0.5)
(290 0 -0.5)
(290 16.6 -0.5)
(-20.6 0 0.5)
(-20.6 25.4 0.5)
(0 -25.4 0.5)
(0 0 0.5)
(0 25.4 0.5)
(206 -25.4 0.5)
(206 0 0.5)
(206 25.4 0.5)
(290 -16.6 0.5)
(290 0 0.5)
(290 16.6 0.5)
/*(1 2 3 4)*/ // Commented tuple
//(1 2 3 4)
);
/* vertices commented
vertices
(
(-20.6 0 -0.5)
(-20.6 25.4 -0.5)
(0 -25.4 -0.5)
(0 0 -0.5)
(0 25.4 -0.5)
(206 -25.4 -0.5)
(206 0 -0.5)
(206 25.4 -0.5)
(290 -16.6 -0.5)
(290 0 -0.5)
(290 16.6 -0.5)
)
*/
negY
(
(2 4 1)
(1 3 0.3)
);
posY
(
(1 4 2)
(2 3 4)
(2 4 0.25)
);
posYR
(
(2 1 1)
(1 1 0.25)
);
blocks
(
hex (0 3 4 1 11 14 15 12)
(18 30 1)
simpleGrading (0.5 $posY 1)
hex (2 5 6 3 13 16 17 14)
(180 27 1)
edgeGrading (4 4 4 4 $negY 1 1 $negY 1 1 1 1)
hex (3 6 7 4 14 17 18 15)
(180 30 1)
edgeGrading (4 4 4 4 $posY $posYR $posYR $posY 1 1 1 1)
hex (5 8 9 6 16 19 20 17)
(25 27 1)
simpleGrading (2.5 1 1)
hex (6 9 10 7 17 20 21 18)
(25 30 1)
simpleGrading (2.5 $posYR 1)
);
edges
(
);
boundary
(
inlet
{
type patch;
faces
(
(0 1 12 11)
);
}
outlet
{
type patch;
faces
(
(8 9 20 19)
(9 10 21 20)
);
}
upperWall
{
type wall;
faces
(
(1 4 15 12)
(4 7 18 15)
(7 10 21 18)
);
}
lowerWall
{
type wall;
faces
(
(0 3 14 11)
(3 2 13 14)
(2 5 16 13)
(5 8 19 16)
);
}
frontAndBack
{
type empty;
faces
(
(0 3 4 1)
(2 5 6 3)
(3 6 7 4)
(5 8 9 6)
(6 9 10 7)
(11 14 15 12)
(13 16 17 14)
(14 17 18 15)
(16 19 20 17)
(17 20 21 18)
);
}
);
// ************************************************************************* //
#the script must be in the same folder as blockMeshDict file
#changing the active directory to the location of script
import os
os.chdir(os.path.dirname(__file__))
#read the file and import it as a string
with open("blockMeshDict", "r") as f:
s=f.read()
import re
#clean up the C/C++ comments
def stripcomments(text):
return re.sub('//.*?(\r\n?|\n)|/\*.*?\*/', '', text, flags=re.S)
s=stripcomments(s)
#extract the vertices into a list of tuples
r1 = re.search(r'vertices\s*\(\s*(.*)\s*\)', s, re.DOTALL)
vertices = [(float(v[0]),float(v[1]),float(v[2]))
for v in re.findall(r'\(\s*([-0-9.]+)\s+([-0-9.]+)\s+([-0-9.]+)\s*\)', r1.group(1))]
#display the point and labels
App.newDocument("step")
Gui.activateWorkbench("DraftWorkbench")
import Draft
for vertexNum, vertex in enumerate(vertices):
p=Draft.makePoint(vertex[0],vertex[1],vertex[2])
p.Label=str(vertexNum)
Draft.makeText([str(vertexNum)],point=FreeCAD.Vector(vertex[0],vertex[1],vertex[2]))
@Foadsf

This comment has been minimized.

Copy link
Owner Author

@Foadsf Foadsf commented Aug 22, 2018

following the issues I had with my own blockMeshFile I was advised to use some tools to visualise it. unfortunately none of them fits my needs, as I'm using Windows or macOS. Plus blockMesh command doesn't work so I have no mesh to visualize with parafoam ...

I first shared the idea here on reddit/CFD and then asked it on FreeCAD forum. finally with the help of StackOverflow I succeed to implement this. I hope it helps.

@Foadsf

This comment has been minimized.

Copy link
Owner Author

@Foadsf Foadsf commented Aug 22, 2018

there are attempts to integrate it into the FreeCAD FEM module here

@Foadsf

This comment has been minimized.

Copy link
Owner Author

@Foadsf Foadsf commented Aug 23, 2018

I also added the description to the relevant section on openfoamwiki.net

@Foadsf

This comment has been minimized.

Copy link
Owner Author

@Foadsf Foadsf commented Aug 30, 2018

some follow up discussions can be found here in OpenFOAM GitLab repository and here in this StackOverflow chat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.