[Gmsh] extending gmsh scripting language with python functions

Jonathan Lambrechts jonathanlambrechts at gmail.com
Mon Jul 22 14:07:30 CEST 2013


Dear Moritz,

Here is a version of your script that use a python field to define the 
characteristic mesh size.
It does not use any other file than the python script itself to define 
both the geometry and the mesh size.

regards
Jonathan
On 22/07/13 12:19, moritz braun wrote:
> Dear Jonathan
>
> Please find attached my python script.
>
> It allows me to cut out smaller spheres out of the one large one.
> However, when meshing I can only work with a constant characteristic 
> length.
> What I need is a way access the characteristic length  parameter from 
> python
> and provide function that is smallest close to the centres of the 
> smaller spheres via
> Python. This would be much convenient then
> having to generate a geo file that defines the smaller spheres and
> also defines the Field function in the very restricted expression 
> syntax of geo files!
>
> regards
>
> Moritz
>
> On Mon, Jul 22, 2013 at 10:06 AM, Jonathan Lambrechts 
> <jonathanlambrechts at gmail.com <mailto:jonathanlambrechts at gmail.com>> 
> wrote:
>
>     Sorry, it's not clear for me how you specify your geometry to gmsh.
>     If it is by loading a file (any file, not specially geo file), I
>     think you can use the example, just replace the line that load the
>     geo file by a line loading your file.
>     If it is through the GUI, you can still do it after the script run
>     (just remove the line loading the geo file).
>     if it is something else, please send me an example and I will try
>     to do it in python.
>
>     If you want to construct the geometry directly in python, this is
>     doable but not documented and I'm not sure it's stable. I have no
>     example of this  but I can try to build one if you give me more
>     details.
>
>     regards,
>
>     jonathan
>
>
>>     Dear Jonathan
>>
>>     Thanks a lot for your work and the example.
>>
>>     Since I am however creating the mesh without using a .geo file
>>     but rather
>>     with constructive solid Geometry the
>>     example does not really help me that much!
>>     An example not using a .geo file would be welcome
>>
>>     regards
>>
>>     Moritz
>>
>>
>>     On Thu, Jul 18, 2013 at 5:29 PM, Jonathan Lambrechts
>>     <jonathanlambrechts at gmail.com
>>     <mailto:jonathanlambrechts at gmail.com>> wrote:
>>
>>         sorry, it should have been FieldPython.h and not
>>         PythonField.h (corrected now)
>>
>>         On 18/07/13 16:55, trophime wrote:
>>
>>             On Thu, 2013-07-18 at 15:51 +0200, Jonathan Lambrechts wrote:
>>
>>                 Hi,
>>                 I just added python fields (they will be available in
>>                 the next nightly
>>                 build).
>>
>>                 There is no embedded python interpreter in Gmsh, we
>>                 rather provide
>>                 python bindings for the Gmsh library. This is still
>>                 work in progress,
>>                 in particular there is no documentation nor stable
>>                 API, we just bind
>>                 most of Gmsh internal functions to python.
>>                 Gmsh Library and python bindings can be downloaded
>>                 with the gmsh
>>                 dynamic library from  Gmsh website :
>>                 http://geuz.org/gmsh/bin/Linux/gmsh-svn-Linux64-dynamic.tgz
>>                 http://geuz.org/gmsh/bin/MacOSX/gmsh-svn-MacOSX-dynamic.tgz
>>                 http://geuz.org/gmsh/bin/Windows/gmsh-svn-Windows64-dynamic.zip
>>
>>                 On linux and OSX, to build the python bindings, go to
>>                 the "gmshpy"
>>                 sub-directory the run the python script "setup.py"
>>                 (e.g. python
>>                 setup.py build; python setup.py install). On windows,
>>                 I'm not sure if
>>                 it works ...
>>
>>                 An example of python field is attached, the python
>>                 script imports the
>>                 gmsh python bindings then loads a geo file and adds
>>                 some python fields
>>                 and at the end, the GUI is started.
>>
>>             When trying to package latest rev for debian, the
>>             installation fails :
>>               --
>>             Installing:
>>             /tmp/buildd/gmsh-tetgen-2.8.3~svn16274/debian/tmp/usr/gmshpy/extra_include/Field.h
>>             CMake Error at wrappers/gmshpy/cmake_install.cmake:232
>>             (FILE):
>>                file INSTALL cannot find
>>
>>             "/tmp/buildd/gmsh-tetgen-2.8.3~svn16274/wrappers/gmshpy/../../Mesh/PythonField.h".
>>             Call Stack (most recent call first):
>>                cmake_install.cmake:347 (INCLUDE)
>>
>>                 regards,
>>
>>                 Jonathan
>>
>>                 On 11/07/13 11:49, Nico Schlömer wrote:
>>
>>                     I'd be very much interested in a hands-on example
>>                     of Gmsh + Python
>>                     as well.
>>
>>
>>                     --Nico
>>
>>
>>                     On Thu, Jul 11, 2013 at 10:06 AM, Christophe Geuzaine
>>                     <cgeuzaine at ulg.ac.be
>>                     <mailto:cgeuzaine at ulg.ac.be>> wrote:
>>                                       On 11 Jul 2013, at 09:07,
>>                     moritz braun
>>                              <moritz.braun at gmail.com
>>                     <mailto:moritz.braun at gmail.com>> wrote:
>>                                       > Dear All
>>                              >
>>                              > I am using gmsh in the moment to
>>                              > create 3D grids with  background field
>>                     to control the
>>                              characteristic length.
>>                              > Writing functions as  .F attribute of
>>                     an Field variable is
>>                              rather restrictive
>>                              > it would be nice if  gmsh could be
>>                     extended by embedding
>>                              the python interpreter for
>>                              >  such functions.
>>                              > How difficult whould that be?
>>                              >
>>                                                I think you can
>>                     already define fields in the Gmsh Python
>>                              interface (CC'ing Jon on this one: Jon,
>>                     could you send a
>>                              small example?). If this is the case,
>>                     then your python
>>                              script could simply import the Gmsh
>>                     module, merge your
>>                              current .geo definitions, then define
>>                     the fields directly in
>>                              Python.
>>                                                > regards
>>                              >
>>                              > Moritz
>>                              >
>>                              > --
>>                              > Prof M Braun Tel.:27-12-4298006/8027
>>                              > Physics Department  Fax.: 27-12-4293643
>>                              > University of South Africa (UNISA)
>>                              > moritz.braun at gmail.com
>>                     <mailto:moritz.braun at gmail.com>
>>                              > P.O. Box 392
>>                              > 0003
>>                              > UNISA
>>                              >  South Africa
>>                              > http://moritz-braun.blogspot.com
>>                                       >
>>                     _______________________________________________
>>                              > gmsh mailing list
>>                              > gmsh at geuz.org <mailto:gmsh at geuz.org>
>>                              > http://www.geuz.org/mailman/listinfo/gmsh
>>                                       --
>>                              Prof. Christophe Geuzaine
>>                              University of Liege, Electrical
>>                     Engineering and Computer
>>                              Science
>>                     http://www.montefiore.ulg.ac.be/~geuzaine
>>                     <http://www.montefiore.ulg.ac.be/%7Egeuzaine>
>>                      _______________________________________________
>>                              gmsh mailing list
>>                     gmsh at geuz.org <mailto:gmsh at geuz.org>
>>                     http://www.geuz.org/mailman/listinfo/gmsh
>>
>>
>>                 _______________________________________________
>>                 gmsh mailing list
>>                 gmsh at geuz.org <mailto:gmsh at geuz.org>
>>                 http://www.geuz.org/mailman/listinfo/gmsh
>>
>>
>>
>>         _______________________________________________
>>         gmsh mailing list
>>         gmsh at geuz.org <mailto:gmsh at geuz.org>
>>         http://www.geuz.org/mailman/listinfo/gmsh
>>
>>
>>
>>
>>     -- 
>>     Prof M Braun         Tel.:27-12-4298006/8027
>>     Physics Department  Fax.: 27-12-4293643
>>     University of South Africa (UNISA)
>>     moritz.braun at gmail.com <mailto:moritz.braun at gmail.com>
>>     P.O. Box 392
>>     0003
>>     UNISA
>>      South Africa
>>     http://moritz-braun.blogspot.com
>
>
>
>
> -- 
> Prof M Braun         Tel.:27-12-4298006/8027
> Physics Department  Fax.: 27-12-4293643
> University of South Africa (UNISA)
> moritz.braun at gmail.com <mailto:moritz.braun at gmail.com>
> P.O. Box 392
> 0003
> UNISA
>  South Africa
> http://moritz-braun.blogspot.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.geuz.org/pipermail/gmsh/attachments/20130722/68ed22c8/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: h2plusGrid.py
Type: text/x-python
Size: 1245 bytes
Desc: not available
URL: <http://www.geuz.org/pipermail/gmsh/attachments/20130722/68ed22c8/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: h2plusGrid.png
Type: image/png
Size: 157807 bytes
Desc: not available
URL: <http://www.geuz.org/pipermail/gmsh/attachments/20130722/68ed22c8/attachment.png>