Guest User

Untitled

a guest
Jun 12th, 2025
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. import gmsh
  2. import sys
  3.  
  4.  
  5. gmsh.initialize()
  6.  
  7. gmsh.model.add("mover")
  8.  
  9. def four_square_mag(height, offset):
  10.     volumes = {
  11.         "magnet-N": [
  12.             gmsh.model.occ.addBox(0, 0, 0, offset, offset, height),
  13.             gmsh.model.occ.addBox(0, 0, 0, -offset, -offset, height),
  14.         ],
  15.         "magnet-S": [
  16.             gmsh.model.occ.addBox(0, 0, 0, -offset, offset, height),
  17.             gmsh.model.occ.addBox(0, 0, 0, offset, -offset, height),
  18.         ]
  19.     }
  20.     return volumes
  21.  
  22.  
  23.  
  24. def add_iron(base, offset, height, radius):
  25.     base["iron"] = [
  26.             gmsh.model.occ.add_cylinder(0, 0, offset, 0, 0, height, radius),
  27.     ]
  28.     return base
  29.  
  30. # We can log all messages for further processing with:
  31. gmsh.logger.start()
  32.  
  33. # air volume
  34. air_orig = gmsh.model.occ.addSphere(0, 0, 0, 100)
  35.  
  36. volumes = four_square_mag(3, 12.5)
  37.  
  38. #add a back iron
  39. volumes = add_iron(volumes, 3, 3, 30.1776/2)
  40.  
  41.  
  42.  
  43. all_vols = []
  44. for name, tags in volumes.items():
  45.     all_vols += [(3, x) for x in tags]
  46.  
  47. # use fragment to stich it all together
  48. ov, ovv = gmsh.model.occ.fragment([(3, air_orig)], all_vols)
  49.  
  50.  
  51. gmsh.model.occ.synchronize()
  52.  
  53. # assign physical groups using dictionary
  54. for name, tags in volumes.items():
  55.     gmsh.model.addPhysicalGroup(3, tags, name=name)
  56.  
  57. # air phys group
  58. gmsh.model.addPhysicalGroup(3, [ov[-1][1]], name="air")
  59.  
  60.  
  61. #mesh size for air
  62. gmsh.model.mesh.setSize(gmsh.model.getBoundary([(3, ov[-1][1])], False, False, True),
  63.                         10)
  64.  
  65. #mesh size for inner objects
  66. gmsh.model.mesh.setSize(gmsh.model.getBoundary(all_vols, False, False, True),
  67.                         0.5)
  68.  
  69. # gmsh.option.setNumber("Mesh.MeshSizeFromCurvature", 120)
  70.  
  71. gmsh.model.mesh.generate(3)
  72.  
  73. gmsh.write("mover.msh")
  74.  
  75. # Inspect the log:
  76. log = gmsh.logger.get()
  77. print("Logger has recorded " + str(len(log)) + " lines")
  78. gmsh.logger.stop()
  79.  
  80. # Launch the GUI to see the results:
  81. if '-nopopup' not in sys.argv:
  82.     gmsh.fltk.run()
  83.  
  84. gmsh.finalize()
  85.  
Advertisement
Add Comment
Please, Sign In to add comment