Hi Guys,
I am new here, and would like to ask a simple question.
I want to use MatPlotLib for visualization of displacement and stress in an elastic deformation simulation.
Although the GridFunction seems to have 6 times more vectors than the Mesh has vertices, I figured that the first 1/6th of GridFunction vec data corresponds to the displacement of Mesh vertices.
Next, I want to compute the Stress which is defined as a coefficient function. However, I cannot figure out how to input the MeshPoints in the correct format, can someone help me finish writing the last few lines of this code?
# Load CAD file as geometry
from netgen.occ import *
geo = OCCGeometry("Rail.stp")
# Generate mesh
from ngsolve import *
gmsh = geo.GenerateMesh()
msh = Mesh(gmsh)
print(msh.GetBoundaries())
print(msh.GetMaterials())
# Material parameters
E, nu = 2.1e11, 0.2
# Lame parameters
mu = 1/2*(E / (1+nu))
lam = E * nu / ((1+nu)*(1-2*nu))
# Linear strain tensor
def epsilon(u):
return 0.5 * (u.Deriv().trans + u.Deriv())
# linear stress tensor
def stress(u):
return 2*mu*epsilon(u) + lam*Trace(epsilon(u))*Id(msh.dim)
# Set fixed boundary
fes = H1(msh, order=2, dirichlet="bc_1", dim=msh.dim)
u,v = fes.TrialFunction(), fes.TestFunction()
# Define problem
a = BilinearForm(fes)
a += SymbolicBFI( 2*mu*InnerProduct(epsilon(u),epsilon(v)) + lam*Trace(u.Deriv())*Trace(v.Deriv()))
a.Assemble()
# Add load
force = CoefficientFunction( (0,0,-1e6) )
# Solve
f = LinearForm(fes)
f += SymbolicLFI( force*v, definedon=msh.Boundaries("bc_0"))
f.Assemble()
# Get displacement
disp = GridFunction(fes,name="displacement")
disp.vec.data = a.mat.Inverse(fes.FreeDofs())*f.vec
# Export deformed mesh as lists of vertices/triangles
i = 0; scale = 1000;
x = []; y = []; z = [];
for v in msh.vertices:
x.append(v.point[0] + scale*disp.vec[i][0])
y.append(v.point[1] + scale*disp.vec[i][1])
z.append(v.point[2] + scale*disp.vec[i][2])
i += 1
vertices = [x,y,z]
triangles = []
for e in msh.Elements(BND):
triangles.append([e.vertices[0].nr, e.vertices[1].nr, e.vertices[2].nr])
# How to compute stress at mesh nodes??? (strs is a coefficient function, not a grid function)
strs = stress(disp)
Attachment: Rail.stp