netgenSTLtest.py

You can view and download this file on Github: netgenSTLtest.py

  1#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2# This is an EXUDYN example
  3#
  4# Details:  Example to import .stl mesh, mesh with Netgen, create FEM model,
  5#           reduced order CMS and simulate under gravity
  6#
  7# Author:   Johannes Gerstmayr
  8# Date:     2023-04-21
  9#
 10# 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.
 11#
 12#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 13
 14
 15import exudyn as exu
 16from exudyn.itemInterface import *
 17from exudyn.utilities import * #includes itemInterface and rigidBodyUtilities
 18import exudyn.graphics as graphics #only import if it does not conflict
 19from exudyn.FEM import *
 20from exudyn.graphicsDataUtilities import *
 21
 22SC = exu.SystemContainer()
 23mbs = SC.AddSystem()
 24
 25import numpy as np
 26import time
 27
 28
 29#%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
 30#netgen/meshing part:
 31fem = FEMinterface()
 32
 33nModes = 16
 34
 35#steel:
 36rho = 7850
 37nu=0.3
 38Emodulus=1e8#use some very soft material to visualize deformations
 39
 40#%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
 41if True: #needs netgen/ngsolve to be installed to compute mesh, see e.g.: https://github.com/NGSolve/ngsolve/releases
 42    import sys
 43    import ngsolve as ngs
 44    import netgen
 45    from netgen.meshing import *
 46
 47    print('load stl file...')
 48    import netgen.stl as nstl
 49    #load STL file; needs to be closed (no holes) and consistent!
 50    #               and may not have defects (may require some processing of STL files!)
 51    geom = nstl.STLGeometry('testData/gyro.stl') #Peter's gyro
 52
 53    maxh=0.01
 54    mesh = ngs.Mesh( geom.GenerateMesh(maxh=maxh))
 55    # mesh.Curve(1) #don't do that!
 56
 57    #set True to see mesh in netgen tool:
 58    if False: #set this to true, if you want to visualize the mesh inside netgen/ngsolve
 59        import netgen.gui #this starts netgen gui; Press button "Visual" and activate "Auto-redraw after (sec)"; Then select "Mesh"
 60
 61
 62    # sys.exit()
 63    #%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
 64    #Use fem to import FEM model and create FFRFreducedOrder object
 65    [bfM, bfK, fes] = fem.ImportMeshFromNGsolve(mesh, density=rho, youngsModulus=Emodulus,
 66                                                poissonsRatio=nu, meshOrder=1)
 67
 68
 69#%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
 70#compute Hurty-Craig-Bampton modes
 71if True: #now import mesh as mechanical model to EXUDYN
 72    print("nNodes=",fem.NumberOfNodes())
 73
 74
 75    cyl=np.array([0,0,0])
 76    rCyl = 0.011/2
 77    nodesOnCyl = fem.GetNodesOnCylinder(cyl-[0,0.01,0], cyl+[0,0.01,0], radius=rCyl, tolerance=0.001)
 78    # #print("boundary nodes bolt=", nodesOnBolt)
 79    nodesOnCylWeights = fem.GetNodeWeightsFromSurfaceAreas(nodesOnCyl)
 80    pMid = fem.GetNodePositionsMean(nodesOnCyl)
 81    print('cyl midpoint=', pMid)
 82
 83
 84    #boundaryList = [nodesOnBolt, nodesOnBolt, nodesOnBushing] #for visualization, use first interface twice
 85    boundaryList = [nodesOnCyl]
 86
 87    print("compute HCB modes... (may take some seconds)")
 88    fem.ComputeHurtyCraigBamptonModes(boundaryNodesList=boundaryList,
 89                                  nEigenModes=nModes,
 90                                  useSparseSolver=True,
 91                                  computationMode = HCBstaticModeSelection.RBE2)
 92
 93    print("eigen freq.=", fem.GetEigenFrequenciesHz())
 94
 95    #draw cylinder to see geometry of hole
 96    # gGround = [graphics.Cylinder([0,0,0],[0,0.02,0], radius=0.011/2, color=graphics.color.dodgerblue, nTiles=128)]
 97    # oGround = mbs.AddObject(ObjectGround(referencePosition= [0,0,0], visualization=VObjectGround(graphicsData=gGround)))
 98
 99
100    #%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
101    #compute stress modes for postprocessing (inaccurate for coarse meshes, just for visualization):
102    if True:
103        mat = KirchhoffMaterial(Emodulus, nu, rho)
104        varType = exu.OutputVariableType.StressLocal
105        print("ComputePostProcessingModes ... (may take a while)")
106        start_time = time.time()
107        fem.ComputePostProcessingModesNGsolve(fes, material=mat,
108                                       outputVariableType=varType)
109        SC.visualizationSettings.contour.reduceRange=False
110        SC.visualizationSettings.contour.outputVariable = varType
111        SC.visualizationSettings.contour.outputVariableComponent = -1 #norm
112    else:
113        varType = exu.OutputVariableType.DisplacementLocal
114        SC.visualizationSettings.contour.outputVariable = exu.OutputVariableType.DisplacementLocal
115        SC.visualizationSettings.contour.outputVariableComponent = 0
116
117    #%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
118    print("create CMS element ...")
119    cms = ObjectFFRFreducedOrderInterface(fem)
120
121    objFFRF = cms.AddObjectFFRFreducedOrder(mbs, positionRef=[0,0,0],
122                                                  initialVelocity=[0,0,0],
123                                                  initialAngularVelocity=[0,0,0],
124                                                  color=[0.9,0.9,0.9,1.],
125                                                  gravity=[0,0,-9.81]
126                                                  )
127
128    #%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
129    #add markers and joints
130    nodeDrawSize = 0.0005 #for joint drawing
131
132    #add constraint for cylinder
133    if True:
134
135        oGround = mbs.AddObject(ObjectGround(referencePosition= [0,0,0]))
136
137        altApproach = True
138        mCyl = mbs.AddMarker(MarkerSuperElementRigid(bodyNumber=objFFRF['oFFRFreducedOrder'],
139                                                      meshNodeNumbers=np.array(nodesOnCyl), #these are the meshNodeNumbers
140                                                      weightingFactors=nodesOnCylWeights))
141
142        #due to meshing effects and weighting, the center point is not exactly at [0,1.5,0] as intended ...
143        pm0 = mbs.GetMarkerOutput(mCyl, exu.OutputVariableType.Position,exu.ConfigurationType.Reference)
144        print('marker0 ref pos=', pm0)
145
146        mGroundCyl = mbs.AddMarker(MarkerBodyRigid(bodyNumber=oGround,
147                                                    localPosition=pm0,
148                                                    visualization=VMarkerBodyRigid(show=True)))
149        mbs.AddObject(GenericJoint(markerNumbers=[mGroundCyl, mCyl],
150                                    constrainedAxes = [1]*6,
151                                    visualization=VGenericJoint(show=False, axesRadius=0.01, axesLength=0.01)))
152
153
154    if False: #activate to animate modes
155        from exudyn.interactive import AnimateModes
156        mbs.Assemble()
157        SC.visualizationSettings.nodes.show = False
158        SC.visualizationSettings.openGL.showFaceEdges = True
159        SC.visualizationSettings.openGL.multiSampling=4
160        SC.visualizationSettings.openGL.lineWidth=2
161        SC.visualizationSettings.window.renderWindowSize = [1600,1080]
162        SC.visualizationSettings.contour.showColorBar = False
163        SC.visualizationSettings.general.textSize = 16
164
165        #%%+++++++++++++++++++++++++++++++++++++++
166        #animate modes of ObjectFFRFreducedOrder (only needs generic node containing modal coordinates)
167        SC.visualizationSettings.general.autoFitScene = False #otherwise, model may be difficult to be moved
168
169        nodeNumber = objFFRF['nGenericODE2'] #this is the node with the generalized coordinates
170        AnimateModes(SC, mbs, nodeNumber, period=0.1, showTime=False, renderWindowText='Hurty-Craig-Bampton: 2 x 6 static modes and 8 eigenmodes\n',
171                     runOnStart=True)
172        # import sys
173        # sys.exit()
174
175    #%%+++++++++++++++++++++++++++++++++++++++++++++++++++++
176    #animate modes
177    SC.visualizationSettings.markers.show = True
178    SC.visualizationSettings.markers.defaultSize=nodeDrawSize
179    SC.visualizationSettings.markers.drawSimplified = False
180
181    SC.visualizationSettings.loads.show = False
182
183    SC.visualizationSettings.openGL.multiSampling=4
184    SC.visualizationSettings.openGL.lineWidth=2
185
186    mbs.Assemble()
187
188    simulationSettings = exu.SimulationSettings()
189
190    SC.visualizationSettings.nodes.defaultSize = nodeDrawSize
191    SC.visualizationSettings.nodes.drawNodesAsPoint = False
192    SC.visualizationSettings.connectors.defaultSize = 2*nodeDrawSize
193
194    SC.visualizationSettings.nodes.show = False
195    SC.visualizationSettings.nodes.showBasis = True #of rigid body node of reference frame
196    SC.visualizationSettings.nodes.basisSize = 0.12
197    SC.visualizationSettings.bodies.deformationScaleFactor = 100 #use this factor to scale the deformation of modes
198
199    SC.visualizationSettings.openGL.showFaceEdges = True
200    SC.visualizationSettings.openGL.showFaces = True
201
202    SC.visualizationSettings.sensors.show = True
203    SC.visualizationSettings.sensors.drawSimplified = False
204    SC.visualizationSettings.sensors.defaultSize = 0.01
205
206    h=2e-5 #make small to see some oscillations
207    tEnd = 0.5
208
209    simulationSettings.timeIntegration.numberOfSteps = int(tEnd/h)
210    simulationSettings.timeIntegration.endTime = tEnd
211    simulationSettings.solutionSettings.writeSolutionToFile = False
212    simulationSettings.timeIntegration.verboseMode = 1
213    simulationSettings.timeIntegration.simulateInRealtime = True
214    simulationSettings.timeIntegration.realtimeFactor = 0.01
215    simulationSettings.timeIntegration.newton.useModifiedNewton = True
216
217    simulationSettings.solutionSettings.sensorsWritePeriod = h
218
219    simulationSettings.timeIntegration.generalizedAlpha.spectralRadius = 0.8
220    #simulationSettings.displayStatistics = True
221    simulationSettings.displayComputationTime = True
222
223    SC.visualizationSettings.window.renderWindowSize=[1920,1080]
224    SC.visualizationSettings.openGL.multiSampling = 4
225
226    SC.visualizationSettings.general.autoFitScene=False
227
228    SC.renderer.Start()
229    if 'renderState' in exu.sys: SC.renderer.SetState(exu.sys['renderState']) #load last model view
230
231    SC.renderer.DoIdleTasks() #press space to continue
232
233    mbs.SolveDynamic(simulationSettings=simulationSettings)
234
235    if varType == exu.OutputVariableType.StressLocal:
236        mises = CMSObjectComputeNorm(mbs, 0, exu.OutputVariableType.StressLocal, 'Mises')
237        print('max von-Mises stress=',mises)
238
239    SC.renderer.DoIdleTasks()
240    SC.renderer.Stop() #safely close rendering window!
241
242    # mbs.PlotSensor(sensorNumbers=[sensBushingVel], components=[1])