update gmsh mesh from console - after shape change

About the development of the FEM module/workbench.

Moderator: bernd

HoWil
Posts: 831
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

update gmsh mesh from console - after shape change

Postby HoWil » Fri Jan 20, 2017 9:25 pm

Hello,
can anyone please tell me how to update a gmsh mesh object from python console. I have a shape and a corresponding gmsh object and change the dimensions of the shape via Spreadsheet. Since I want to run some come in python console I need to know how to update the mesh from console after updating the shape.

Thanks in advance,
HoWil

I found
viewtopic.php?f=18&t=16944
viewtopic.php?f=18&t=19549&p=151633&hil ... er#p151169
but could not figure out how to update an existing gmsh-mesh
User avatar
bernd
Posts: 8021
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: update gmsh mesh from console - after shape change

Postby bernd » Sat Jan 21, 2017 1:26 am

Code: Select all

import Part, Fem, FemMeshGmsh, FemGmshTools

# some geometry
box = Part.makeBox(10,10,10)
box_obj = App.ActiveDocument.addObject('Part::Box', 'MyBox')

# make an initial FEM mesh
femmesh_obj_gmsh = FemMeshGmsh.makeFemMeshGmsh('MyGMSHMeshObj')
femmesh_obj_gmsh.Part = box_obj
App.ActiveDocument.recompute()
gmsh_mesh = FemGmshTools.FemGmshTools(femmesh_obj_gmsh)
error = gmsh_mesh.create_mesh()
print error
App.ActiveDocument.recompute()

Code: Select all

# change box and remesh, we do not need a new instance of gmsh_mesh class
box_obj.Length = 4
box_obj.Height = 20
error = gmsh_mesh.create_mesh()
print error
App.ActiveDocument.recompute()

Code: Select all

# change mesh params and remesh, we need a new instance of gmsh_mesh class
femmesh_obj_gmsh.CharacteristicLengthMax = 1
gmsh_mesh = FemGmshTools.FemGmshTools(femmesh_obj_gmsh)
error = gmsh_mesh.create_mesh()
print error
App.ActiveDocument.recompute()
HoWil
Posts: 831
Joined: Sun Jun 14, 2015 7:31 pm
Location: Austria

Re: update gmsh mesh from console - after shape change

Postby HoWil » Mon Jan 23, 2017 7:08 am

Thanks ; I already used it very successful to update my mesh during parametric studies. Will post these efforts soon.
Br
Howil
Jee-Bee
Posts: 1913
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: update gmsh mesh from console - after shape change

Postby Jee-Bee » Sun Feb 05, 2017 8:09 am

bernd wrote:

Code: Select all

import Part, Fem, FemMeshGmsh, FemGmshTools

# some geometry
box = Part.makeBox(10,10,10)
box_obj = App.ActiveDocument.addObject('Part::Box', 'MyBox')

# make an initial FEM mesh
femmesh_obj_gmsh = FemMeshGmsh.makeFemMeshGmsh('MyGMSHMeshObj')
femmesh_obj_gmsh.Part = box_obj
App.ActiveDocument.recompute()
gmsh_mesh = FemGmshTools.FemGmshTools(femmesh_obj_gmsh)
error = gmsh_mesh.create_mesh()
print error
App.ActiveDocument.recompute()
This fails for me. I get this Error

Code: Select all

Traceback (most recent call last):
  File "/Users/USER/Library/Preferences/FreeCAD/Macro/FEM_GMSH.FCMacro", line 5, in <module>
    box_obj = App.ActiveDocument.addObject('Part::Box', 'MyBox')
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'addObject'
And when adding the mesh part to your Fem Tutorial I get

Code: Select all

We gone start GMSH FEM mesh run!
  Part to mesh: Name --> Box,  Label --> Box, ShapeType --> Solid
  CharacteristicLengthMax: 1e+22
  CharacteristicLengthMin: 0.0
  ElementOrder: 2
  ElementDimension: 3
  /var/folders/s0/_y95t9710_xb4dt8yjykpqzc0000gn/T/Box_Geometry.brep  # Some temp folder
  /var/folders/s0/_y95t9710_xb4dt8yjykpqzc0000gn/T/Box_Mesh_TmpGmsh.unv  # Some temp folder
  /var/folders/s0/_y95t9710_xb4dt8yjykpqzc0000gn/T/shape2mesh.geo  # Some temp folder
Traceback (most recent call last):
  File "/Users/USER/Library/Preferences/FreeCAD/Macro/FEA_tutorial.FCMacro", line 77, in <module>
    error = gmsh_mesh.create_mesh()
  File " /Applications/FreeCAD.app/Contents/Mod/Fem/FemGmshTools.py", line 121, in create_mesh
    self.get_gmsh_command()
  File " /Applications/FreeCAD.app/Contents/Mod/Fem/FemGmshTools.py", line 215, in get_gmsh_command
    print('  ' + self.gmsh_bin)
<type 'exceptions.TypeError'>: cannot concatenate 'str' and 'NoneType' objects
So i don't understand what's going wrong...
User avatar
bernd
Posts: 8021
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: update gmsh mesh from console - after shape change

Postby bernd » Sun Feb 05, 2017 9:17 am

for the first problem, read the error csrefully and you will find, ahh sure I have to make a new document to have something to be able to add objects to.

for the second, could you give more informations? post some code or file, to reproduce the error.

cheers bernd
Jee-Bee
Posts: 1913
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: update gmsh mesh from console - after shape change

Postby Jee-Bee » Sun Feb 05, 2017 2:33 pm

Modified example from https://www.freecadweb.org/wiki/index.p ... ial_Python

Code: Select all

# new document
doc = App.newDocument("CalculiX_Buckle_Example3D")
#
# part
import Part
beam_obj = doc.addObject('Part::Box', 'Beam')
beam_obj.Height = beam_obj.Width = 1000
beam_obj.Length = 8000
#
# see how our part looks like
import FreeCADGui
FreeCADGui.ActiveDocument.activeView().viewAxonometric()
FreeCADGui.SendMsgToActiveView("ViewFit")
#
# analysis
import FemAnalysis
analysis_object = FemAnalysis.makeFemAnalysis('Analysis')
#
# solver
import FemSolverCalculix
solver_object = FemSolverCalculix.makeFemSolverCalculix('CalculiX')
solver_object.AnalysisType = 'static' 
solver_object.GeometricalNonlinearity = 'linear'
#solver_object.SteadyState = True
#solver_object.Accurency = 0.01
#solver_object.EigenmodeNumber = 1
solver_object.ThermoMechSteadyState = True
solver_object.MatrixSolverType = 'default'
solver_object.IterationsControlParameterTimeUse = False
doc.Analysis.Member = doc.Analysis.Member + [solver_object]
#
# material
import FemMaterial
#import MechanicalMaterial
material_object = FemMaterial.makeFemMaterial('MechanicalMaterial')
#material_object = MechanicalMaterial.makeMechanicalMaterial('MechanicalMaterial')
mat = material_object.Material
mat['Name'] = "Calculix-Steel"
mat['YoungsModulus'] = "210000 MPa"
mat['PoissonRatio'] = "0.30"
mat['Density'] = "7900 kg/m^3"
material_object.Material = mat
doc.Analysis.Member = doc.Analysis.Member + [material_object]
#
# fixed_constraint
fixed_constraint = doc.addObject("Fem::ConstraintFixed", "FemConstraintFixed")
fixed_constraint.References = [(doc.Beam, "Face1")]
doc.Analysis.Member = doc.Analysis.Member + [fixed_constraint]
#
# force_constraint
force_constraint = doc.addObject("Fem::ConstraintForce", "FemConstraintForce")
force_constraint.References = [(doc.Beam, "Face2")]
force_constraint.Force = 9000000.0
#force_constraint.Direction = (doc.Beam, ["Edge5"])
force_constraint.Reversed = True
doc.Analysis.Member = doc.Analysis.Member + [force_constraint]

## From here forum example
import Fem, FemMeshGmsh, FemGmshTools

# make an initial FEM mesh
femmesh_obj_gmsh = FemMeshGmsh.makeFemMeshGmsh('Beam_Mesh')
femmesh_obj_gmsh.Part = beam_obj
App.ActiveDocument.recompute()
gmsh_mesh = FemGmshTools.FemGmshTools(femmesh_obj_gmsh)
error = gmsh_mesh.create_mesh()
print error

## to here
doc.Analysis.Member = doc.Analysis.Member + [femmesh_obj_gmsh] 
User avatar
bernd
Posts: 8021
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: update gmsh mesh from console - after shape change

Postby bernd » Sun Feb 05, 2017 3:38 pm

Your code works great for me. Would you post your system info? Are you on Linux? Have you package gmsh installed? What is the output of:

Code: Select all

which gmsh
for me it is:

Code: Select all

$ which gmsh
/usr/local/bin/gmsh
cheers bernd
Jee-Bee
Posts: 1913
Joined: Tue Jun 16, 2015 10:32 am
Location: Netherlands

Re: update gmsh mesh from console - after shape change

Postby Jee-Bee » Sun Feb 05, 2017 7:22 pm

Code: Select all

MacBook:~ USER$ which gmsh
/usr/local/bin/gmsh
MacBook:~ USER$ gmsh -version
2.14.0

Code: Select all

We gone start GMSH FEM mesh run!
  Part to mesh: Name --> Beam,  Label --> Beam, ShapeType --> Solid
  CharacteristicLengthMax: 1e+22
  CharacteristicLengthMin: 0.0
  ElementOrder: 2
  ElementDimension: 3
  /var/folders/s0/_y95t9710_xb4dt8yjykpqzc0000gn/T/Beam_Geometry.brep
  /var/folders/s0/_y95t9710_xb4dt8yjykpqzc0000gn/T/Beam_Mesh_TmpGmsh.unv
  /var/folders/s0/_y95t9710_xb4dt8yjykpqzc0000gn/T/shape2mesh.geo
  /usr/local/bin/
  No mesh group objects.
  No anlysis members for group meshing.
  {}
  No mesh regions.
  {}
  {}
Traceback (most recent call last):
  File "/Users/jan-bertwinkel/Library/Preferences/FreeCAD/Macro/FEA_tutorial.FCMacro", line 77, in <module>
    error = gmsh_mesh.create_mesh()
  File "/Applications/FreeCAD.app/Contents/Mod/Fem/FemGmshTools.py", line 125, in create_mesh
    error = self.run_gmsh_with_geo()
  File "/Applications/FreeCAD.app/Contents/Mod/Fem/FemGmshTools.py", line 401, in run_gmsh_with_geo
    error = 'Error executing: {}\n'.format(self.gmsh_command)
<type 'exceptions.AttributeError'>: FemGmshTools instance has no attribute 'gmsh_command'
weird now set manually to gmsh location and get a different error...
User avatar
bernd
Posts: 8021
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: update gmsh mesh from console - after shape change

Postby bernd » Sun Feb 05, 2017 8:11 pm

Jee-Bee wrote:

Code: Select all

MacBook:~ USER$ which gmsh
/usr/local/bin/gmsh
MacBook:~ USER$ gmsh -version
2.14.0
That explains a lot. You might be the first one on OSX trying GMSH meshing of FEM workbench. That's great for FEM workbench :mrgreen: , but may be not for you because it does not seam to work at the moment ... :ugeek: :geek:

would you post the FreeCAD python output of

Code: Select all

FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Fem/Gmsh").GetString("gmshBinaryPath", "")
it should be for you:

Code: Select all

/usr/local/bin/gmsh
User avatar
bernd
Posts: 8021
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: update gmsh mesh from console - after shape change

Postby bernd » Sun Feb 05, 2017 8:16 pm

Jee-Bee wrote:[weird now set manually to gmsh location and get a different error...
ahh did you set it to /usr/local/bin or /usr/local/bin/gmsh ? You should set it to /usr/local/bin/gmsh !