How to create unstructured 2D meshes from CAD files in python - python

I'm working on a finite differences code to solve 2D problems. I want to be able to solve complex geometries written as STEP or IGES files. However I don't know how to read and mesh this kind of files.
While I know that there are free and independent meshing applications, I want my code to be self-contained. Is there a way to achieve this on python?

You might be interested in GMSH API. GMSH is well-known for a while as a free open-source mesher, and recently (by relative means), they introduced an API for C,C++, Python, and Julia.
At first, a simple usage of Top level functions GMSH::open and Mesh function GMSH::generate(2) can get you started.

Related

Python: replicate Matlab's generateMesh function

I am in the process of converting a codebase from Matlab to Python and noticed that generateMesh gets called on some polygons before carrying out a finite element analysis.
What I need to get as an output is a list of all the elements and nodes, with their respective coordinates. I don't need any GUI, just the output nodes and elements information.
The best solution I came across is something like this done with gmsh. I know gmsh is a pretty big library and I am afraid it might be a little too much for my needs. Is there any other package you'd suggest?
Triangular meshes are fine for the moment, but I would like the package to support tetrahedral meshes as well in case it's needed in the future.
Thank you
edit: I forgot to mention that I am only dealing with 2D geometries, as the triangular and tetrahedral elements imply.

CFD work with Python

I am a meteorologist, and lately I am trying to investigate the possibility of building my one sondes.
In order to do that, I have the following work plan :
I would like to generate 3D models pyformex. An alternative is openSCAD. But I start with pyformex - to generate simple cylindrical sonde shapes with associated extra features, e.g. intake tube or such.
Next, I will like to split it in Meshes, using PyDistMesh; as well as prepare a raytraced point cloud model with Xrt.
In the third step, I would like to perform the CFD works.
Now, my questions :
Are there some other simple Python Libraries to generate 3D models? I would like a very simple system, where i can issue commands like p = Parallelogram (length, height, width), or p.position(x,y,z) etc. It would be nice to have built in mouse interaction - that is, a built in drawing component, which I can use to display the model, and rotate/ zoom/pan with mouse.
Any other mesh generation tools?
For this step, I would need a multiphysics system. I tried to use OpenFOAM, it is too huge (to hack through). I have taken a look at SU2, but it seems to focus more on aerospace engineering, than Fluid Dynamics (I would like to simulate the flight of the sonde - which is closer to aerospace engineering, as well as the state of the atmosphere). Fluidity seems to suit my needs better, but I dont find a python fork thereof. So are there some general purpose, not too bloated up, multiphysics python library for geophysical and general hydrodynamic simulations? I have taken a look a MOOSE, also dont find a python binding for it.
Scientific visualization : Are there some 3 or 4 (or may be higher dimensional) visualization libraries? I would prefer to issue simple commands as Plot instead of first generating a window / form, and then putting the graphs on it, if possible.
FINALLY, and most importantly, if the same can be done by C++ or Fortan, or some other language besides java, I would also consider using those.
Have a look at http://freecadweb.org/. This seems to be under active development. It is a fairly complete open source CAD package written in python. I believe it also has tools for meshing.
For cfd, you might want to consider openfoam - http://www.openfoam.com/. This is an open source cfd package with the obligatory steep learning curve. There seem to be some python libraries to be available that link to it, however I'm not sure how active these are:
http://openfoamwiki.net/index.php/Contrib/PyFoam
http://pythonflu.wikidot.com/

Is there a python library to generate STL file for 3D printing?

I am currently in a project where a lot of 3D printing designs need to be done. They are all parameterized, so I'd like to write a python code to generate those design files (in .STL format) for me. I was wondering that, is there a python package that can do this? Because currently I am all doing those by hand using SolidWorks.
Thanks!
Yes there is... It's called FreeCAD.
The assembly module is already in the devel version (as of 06/15/2014) and will be of production quality really soon for real assemblies!
http://freecadweb.org/
Yes, more than one.
In my humble experience, I tried many Open Source tools for parametric CAD modeling using Python (FreeCAD, Rhino-Grasshopper, Blender, Salome).
All of them are valid options and the best one is represented by your ability to either model or code.
I recently favour SALOME (www.salome-platform.org) because of the straight forward "dump study" option, the continue development and the good API documentation.
Particularly I did some 3d prints using the exportSTL command once I had a solid worthy of printing and it was ok.
Nevertheless, if you intend to work on surfaces rather than solids, I don't think you will find anything worthy Open Source (Rhino has a little price to pay).
There is also a new one ! called pymadcad
It's a library meant to do complete CAD stuff only with python scripts.
At contrary to FreeCAD, Pymadcad is natively dealing with triangular meshes so it makes it very easy to import/export .stl files.
There is a growing amount of surface generation functions (extrusion, revolution, tube, screw, smooth surface, ...). And there is also all the stuff to generate and deal with 3D primtives such as Lines, Arc, ...
Here is a brief look at the features

Draw performance profile figure in MATLAB and Python

what function should i use to draw the above performance profile of different algorithms, the running time data is from python implementation, stored in lists for different algorithm. Is there any build-in function in Python or Matlab to draw this kind of figure automatically? Thanks
You will have to store the performance of your algorithms in a list (if you are using Python) or in a matrix (Matlab) and then plot the results.
Matlab comes with several plot functions such as figure(), plot(), subplot() that you will need to draw those figures.
In Python you can use the third-party package Matplotlib. By the way, Matplotlib gives you a MatLab like environment called PyLab that tries to emulate Matlab commands.
You can make something like that in Python with Matplotlib using step and plot.
In Matlab you can use plot and stairs.
A Python function that draws these profiles which can be found, with instructions, here. After importing this function and generating a Pandas DataFrame object using your data (see instructions), the generation of the performance profile is easy.
A MATLAB script that draws performance profiles can be found here.
I make simple performance charts, like those posed in the question, via Python scripts that rely on a Python module named leather. For example, within the following gist.github.com repository:
https://gist.github.com/lhuemill/f1273291e5f5e85e4b42e5c7614e60ef
is a Python script, named permutations2x2_chart_results, which does this. By default, this script produces a chart using the SVG format. A format that is directly supported by most of the popular web browsers and that are a vector versus bitmap graphic. Vector graphics tend to be a good choice for these types of performance graphs, in that the user can zoom into any portion of the results, to obtain more detail, without pixelation.

Renderer for 3D model (points/lines) in Python

I have a 3d model that consists of points, lines and balls in space. For debugging, it would be nice to render these objects in real time and rotateable.
What are my easiest options to achieve this in python?
Since I know nothing about graphical programming, I'd like to write as litte boilerplate code as possible.
The easiest way to get 3d graphics on screen in python is VPython, though if your model is stored in a CAD file format, you'll need some other library to load the data. With just VPython, you will either need to hard-code the model or implement your own loader.
Two options I'd consider myself (depends what you're trying to do in the end):
Blender has fairly amazing Python integration. Simple example here. As I understand it, Blender's own file save format is executable python which prods the Blender Python API to reconstruct the scene. There's also a mechanism for introducing gamey logic into the Blender world but I know even little about it. Blender does have a crazy steep learning curve though.
Get into OpenGL using the Python OpenGL bindings. "Simple" example.
If you're more interested in creating the models, go with the former; if you're more interested in the rendering of them, go with the latter.
The first thing that springs to mind is processing which is an easy to use visualization toolkit. Although you actually implement your visualizations in java, a quick google found this which lets you write your sketches in python instead.

Categories

Resources