FEM项目之二solidspy+pygmsh整合

1.生成网格:

C:\Users\Yuan\Downloads\SolidsPy\tests\testpygmsh.py

注意pygmsh.generate_mesh的方法默认是mesh4的,我们要生成mesh2的给Solidspy计算用,修改

C:\Users\Yuan\Anaconda2\envs\env-freecad\Lib\site-packages\pygmsh\helpers.py

if mesh_file_type == "msh":
    extra_gmsh_arguments += ["-string", "Mesh.SaveElementTagType=2;"]
    extra_gmsh_arguments += ["-format", "msh2"]

显示网格时候,如果3d要安装vtk mayavi

 

2.生成单元,荷载,约束文件,调用计算:

C:\Users\Yuan\Downloads\SolidsPy\meshes\simplemesh\rect_input.py

 

--------------------------------------------------

rect_input.py

"""
Generate input files from a Gmsh mesh file for a ring with inner pressure
 with symmetry with respect to the horizontal and vertical axws.

It imposes roller constraints for the left side (x==0), and bottom (y==0).
The loading is 1 in the radial direction.
"""
from __future__ import division
import meshio
import numpy as np 
import os
THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
my_file = os.path.join(THIS_FOLDER, 'C:\\Users\\Yuan\\AppData\\Local\\Temp\\tmpb_vp8tgz.msh')#tmpnygiwi5t.mesh
mesh =meshio.read(my_file)


# Elements data
elements = mesh.cells["triangle"]
els_array = np.zeros([elements.shape[0], 6], dtype=int)
els_array[:, 0] = range(elements.shape[0])
els_array[:, 1] = 3
els_array[:, 3::] = elements
# Nodes data
nodes_array = np.zeros([mesh.points.shape[0], 5])
nodes_array[:, 0] = range(mesh.points.shape[0])
nodes_array[:, 1:3] = mesh.points[:, :2]
nodes_array[nodes_array[:, 1]==0, 3] = -1
nodes_array[nodes_array[:, 2]==0, 4] = -1
# Loads data
radius = np.sqrt(mesh.points[:, 0]**2 + mesh.points[:, 1]**2)
nloads = mesh.points[np.abs(radius - 1.5) <= 1e-6, 0].shape[0]
loads_array = np.zeros((nloads, 3))
loads_array[:, 0] = nodes_array[np.abs(radius - 1.5) <= 1e-6, 0]
loads_array[:, 1] = 2.0*(mesh.points[np.abs(radius - 1.5) <= 1e-6, 0]/radius[np.abs(radius - 1.5) <= 1e-6])
loads_array[:, 2] = 2.0*(mesh.points[np.abs(radius - 1.5) <= 1e-6, 1]/radius[np.abs(radius - 1.5) <= 1e-6])
## Material data
mater_array = np.array([[1e3, 1/3]])

ele_file = os.path.join(THIS_FOLDER, 'eles.txt')
nodes_file = os.path.join(THIS_FOLDER, 'nodes.txt')
loads_file = os.path.join(THIS_FOLDER, 'loads.txt')
mater_file = os.path.join(THIS_FOLDER, 'mater.txt')
np.savetxt(ele_file, els_array, fmt="%d")
np.savetxt(nodes_file, nodes_array, fmt=("%d", "%.4f", "%.4f", "%d", "%d"))
np.savetxt(loads_file, loads_array, fmt=("%d", "%.6f", "%.6f"))
np.savetxt(mater_file, mater_array, fmt="%.6f")

 

-----------------------------------------------------

.testpygmshpy

 

import pygmsh
import numpy as np
import pygimli as pg
from pygimli.meshtools import readGmsh
from urllib.request import urlretrieve
import matplotlib.pyplot as plt
print(pg.__version__)
geom = pygmsh.opencascade.Geometry(
  characteristic_length_min=0.1,
  characteristic_length_max=0.1,
  )
#调用pygmsh 生成模型
rectangle = geom.add_rectangle([-1.0, -1.0, 0.0], 2.0, 2.0)
disk1 = geom.add_disk([-1.2, 0.0, 0.0], 0.5)
disk2 = geom.add_disk([+1.2, 0.0, 0.0], 0.5)
union = geom.boolean_union([rectangle, disk1, disk2])
disk3 = geom.add_disk([0.0, -0.9, 0.0], 0.5)
disk4 = geom.add_disk([0.0, +0.9, 0.0], 0.5)
flat = geom.boolean_difference([union], [disk3, disk4])
geom.extrude(flat, [0, 0, 0.3])

#gmsh_path = "C:\\Users\\Yuan\\gmsh-4.1.2-Windows64\\gmsh-4.1.2-Windows64\\gmash.exe"
#points, cells, point_data, cell_data, field_data = pygmsh.generate_mesh(geom)
#print(points)
#显示网格,
fig, ax = plt.subplots()
mesh = readGmsh("C:\\Users\\Yuan\\AppData\\Local\\Temp\\tmpfswmb5q9.msh", verbose=True)
#mesh = readGmsh("mesh.msh", verbose=True)

pg.show(mesh)   
pg.wait()

你可能感兴趣的:(数值分析&FEM)