NGsolveGeometry.py
You can view and download this file on Github: NGsolveGeometry.py
1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2# This is an EXUDYN example
3#
4# Details: Example to show how to create CSG-geometry in Netgen and import as STL into exudyn
5#
6# Author: Johannes Gerstmayr
7# Date: 2021-04-20
8#
9# Copyright:This file is part of Exudyn. Exudyn is free software. You can redistribute it and/or modify it under the terms of the Exudyn license. See 'LICENSE.txt' for more details.
10#
11#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12
13
14import exudyn as exu
15from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
16import exudyn.graphics as graphics #only import if it does not conflict
17from exudyn.FEM import *
18
19SC = exu.SystemContainer()
20mbs = SC.AddSystem()
21
22import numpy as np
23import time
24
25#%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
26#netgen/meshing part:
27
28#geometrical parameters:
29L = 0.2 #Length of body
30sy = 0.04 #width of body (Y)
31sz = 0.03 #height of body (Z)
32dy = 0.005 #additional distance Y
33r = 0.015 #radius of bolt
34R = 0.025 #outer radius
35x = 0.002
36meshH = 0.005 #0.01 is default, 0.002 gives 100000 nodes and is fairly converged;
37curvaturesafety = 5
38
39axis = 0.15
40rotBasis = np.eye(3 )
41
42#steel: (not needed for drawing ...)
43rho = 7850
44Emodulus=2.1e11
45nu=0.3
46
47#test high flexibility
48Emodulus=2e8
49# nModes = 32
50
51
52#helper function for cylinder with netgen
53def CSGcylinder(p0,p1,r):
54 v = VSub(p1,p0)
55 v = Normalize(v)
56 cyl = Cylinder(Pnt(p0[0],p0[1],p0[2]), Pnt(p1[0],p1[1],p1[2]),
57 r) * Plane(Pnt(p0[0],p0[1],p0[2]), Vec(-v[0],-v[1],-v[2])) * Plane(Pnt(p1[0],p1[1],p1[2]), Vec(v[0],v[1],v[2]))
58 return cyl
59
60meshCreated = False
61
62#%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
63import ngsolve as ngs
64import netgen
65from netgen.meshing import *
66
67from netgen.geom2d import unit_square
68#import netgen.libngpy as libng
69from netgen.csg import *
70
71
72showCase = 'revolute'
73showCase = 'spheric'
74
75if showCase == 'revolute':
76 rotBasis = RotationMatrixX(-0.5*pi)
77 #++++++++++++++++++++++++++++++++++++++++++++++++++++
78 #first body
79 geo0 = CSGeometry()
80
81 #plate
82 block = OrthoBrick(Pnt(0, x, -0.5*sz),Pnt(L, sy-x, 0.5*sz))
83 blockCyl = CSGcylinder(p0=[0,0,0], p1=[0,sy,0], r=R)
84 bolt0 = CSGcylinder(p0=[0,sy,0], p1=[0,2*sy+dy,0], r=r)
85 geo0.Add(block+blockCyl+bolt0)
86
87 mesh0 = ngs.Mesh( geo0.GenerateMesh(maxh=meshH, curvaturesafety=curvaturesafety))
88 mesh0.Curve(1)
89
90 #++++++++++++++++++++++++++++++++++++++++++++++++++++
91 #second body
92 geo1 = CSGeometry()
93
94 #plate
95 block = OrthoBrick(Pnt(0, x, -0.5*sz),Pnt(L, sy-x, 0.5*sz))
96 blockCyl = CSGcylinder(p0=[0,0,0], p1=[0,sy,0], r=R)
97 hole = CSGcylinder(p0=[0,-sy*0.1,0], p1=[0,1.1*sy,0], r=r)
98 geo1.Add(block+blockCyl-hole)
99
100 mesh1 = ngs.Mesh( geo1.GenerateMesh(maxh=meshH, curvaturesafety=curvaturesafety))
101 mesh1.Curve(1)
102
103 if True:
104 import netgen.gui #this starts netgen gui; Press button "Visual" and activate "Auto-redraw after (sec)"; Then select "Mesh"
105
106 #%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
107 #import body with fem (this could be simplified in future ...)
108 #body0
109 fem0 = FEMinterface()
110 fem0.ImportMeshFromNGsolve(mesh0, density=rho, youngsModulus=Emodulus, poissonsRatio=nu)
111 graphics0 = graphics.FromPointsAndTrigs(fem0.GetNodePositionsAsArray(), fem0.GetSurfaceTriangles(),
112 color=graphics.color.dodgerblue, )
113 graphics0 = AddEdgesAndSmoothenNormals(graphics0)
114
115 mbs.CreateRigidBody(referencePosition=[0,-sy*2-dy,0],
116 referenceRotationMatrix=RotationMatrixX(0),
117 inertia=InertiaCuboid(1000, [1,1,1]),
118 graphicsDataList=[graphics0])
119
120
121 #+++++++++++++++++++++++++++++++++++++++++++++++++++++
122 #body1
123 fem1 = FEMinterface()
124 fem1.ImportMeshFromNGsolve(mesh1, density=rho, youngsModulus=Emodulus, poissonsRatio=nu)
125 graphics1 = graphics.FromPointsAndTrigs(fem1.GetNodePositionsAsArray(), fem1.GetSurfaceTriangles(),
126 color=graphics.color.lightred)
127 graphics1 = AddEdgesAndSmoothenNormals(graphics1)
128
129 mbs.CreateRigidBody(referencePosition=[0,-sy,0],
130 referenceRotationMatrix=RotationMatrixY(1.2*pi),
131 inertia=InertiaCuboid(1000, [1,1,1]),
132 graphicsDataList=[graphics1])
133
134if showCase == 'spheric':
135 #++++++++++++++++++++++++++++++++++++++++++++++++++++
136 #first body
137 geo0 = CSGeometry()
138 meshH *= 0.5
139
140 block = OrthoBrick(Pnt(0, -0.4*sy, -0.4*sz),Pnt(L, 0.4*sy, 0.4*sz))
141 sphere = Sphere( Pnt(0, 0, 0), R*1.2)
142 cutBlock = OrthoBrick(Pnt(-1, -sy, -sz),Pnt(-0.3*R, sy, sz))
143 cutSphere = Sphere( Pnt(0, 0, 0), R)
144 geo0.Add(block+sphere-cutBlock-cutSphere)
145
146 mesh0 = ngs.Mesh( geo0.GenerateMesh(maxh=meshH, curvaturesafety=curvaturesafety))
147 mesh0.Curve(1)
148
149 #++++++++++++++++++++++++++++++++++++++++++++++++++++
150 #second body
151 geo1 = CSGeometry()
152
153 #plate
154 block = OrthoBrick(Pnt(0, -0.4*sz, -0.4*sz),Pnt(L, 0.4*sz, 0.4*sz))
155 sphere = Sphere( Pnt(0, 0, 0), R)
156 geo1.Add(block+sphere)
157
158 mesh1 = ngs.Mesh( geo1.GenerateMesh(maxh=meshH, curvaturesafety=curvaturesafety))
159 mesh1.Curve(1)
160
161 if True:
162 import netgen.gui #this starts netgen gui; Press button "Visual" and activate "Auto-redraw after (sec)"; Then select "Mesh"
163
164 #%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
165 #import body with fem (this could be simplified in future ...)
166 #body0
167 fem0 = FEMinterface()
168 fem0.ImportMeshFromNGsolve(mesh0, density=rho, youngsModulus=Emodulus, poissonsRatio=nu)
169 graphics0 = graphics.FromPointsAndTrigs(fem0.GetNodePositionsAsArray(), fem0.GetSurfaceTriangles(),
170 color=graphics.color.dodgerblue, )
171 graphics0 = AddEdgesAndSmoothenNormals(graphics0)
172
173 mbs.CreateRigidBody(referencePosition=[0,-sy,0],
174 referenceRotationMatrix=RotationMatrixX(0),
175 inertia=InertiaCuboid(1000, [1,1,1]),
176 graphicsDataList=[graphics0])
177
178
179 #+++++++++++++++++++++++++++++++++++++++++++++++++++++
180 #body1
181 fem1 = FEMinterface()
182 fem1.ImportMeshFromNGsolve(mesh1, density=rho, youngsModulus=Emodulus, poissonsRatio=nu)
183 graphics1 = graphics.FromPointsAndTrigs(fem1.GetNodePositionsAsArray(), fem1.GetSurfaceTriangles(),
184 color=graphics.color.lightred)
185 graphics1 = AddEdgesAndSmoothenNormals(graphics1)
186
187 mbs.CreateRigidBody(referencePosition=[0,-sy,0],
188 referenceRotationMatrix=RotationMatrixZ(-0.12*pi)@RotationMatrixY(1.15*pi),
189 inertia=InertiaCuboid(1000, [1,1,1]),
190 graphicsDataList=[graphics1])
191
192
193#+++++++++++++++++++++++++++++++++++++++++++++++++
194#world basis
195gl=[]
196gl += [graphics.Text(point=rotBasis@[axis,0,0],text='X')]
197gl += [graphics.Text(point=rotBasis@[0,axis,0],text='Y')]
198gl += [graphics.Text(point=rotBasis@[0,0,axis],text='Z')]
199
200gl += [graphics.Basis(origin=[0,0,0], rotationMatrix=rotBasis, length=axis)]
201
202mbs.CreateGround(graphicsDataList=gl)
203
204mbs.Assemble()
205#%%++++++++++++++++++++++++++++++
206SC.visualizationSettings.openGL.polygonOffset = 0.1 #to draw edges clearly
207SC.visualizationSettings.openGL.lineWidth = 2
208SC.visualizationSettings.openGL.multiSampling = 4
209# SC.visualizationSettings.general.drawWorldBasis = True
210# SC.visualizationSettings.general.worldBasisSize = axis
211SC.visualizationSettings.general.drawCoordinateSystem = False
212SC.visualizationSettings.general.textSize = 16
213SC.visualizationSettings.window.renderWindowSize = [1600,1200]
214
215mbs.SolveDynamic()
216
217mbs.SolutionViewer()