Skip to content

Commit

Permalink
updated FE related examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tomvanmele committed Nov 13, 2023
1 parent 3925e53 commit 534b17c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 77 deletions.
47 changes: 13 additions & 34 deletions docs/examples/plate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from random import choice
from compas.geometry import Point, Vector
from compas.datastructures import Mesh, mesh_thicken
from compas.utilities import geometric_key_xy
from compas_gmsh.models import MeshModel
from compas_view2.app import App
from compas_view2.shapes import Arrow
Expand All @@ -13,65 +11,46 @@
mesh = Mesh.from_meshgrid(dx=10, nx=5)
plate = mesh_thicken(mesh, 0.3)

# ==============================================================================
# Select random internal vertex for load application
# ==============================================================================

poa = choice(list(set(mesh.vertices()) - set(mesh.vertices_on_boundary())))

# ==============================================================================
# GMSH model
# ==============================================================================

model = MeshModel.from_mesh(plate, targetlength=2.0)

# =============================================================================
# Target lengths
# =============================================================================

poa = list(mesh.vertices_where({"x": 4.0, "y": 4.0}))[0]
model.mesh_targetlength_at_vertex(poa, 0.01)

for vertex in mesh.vertices_on_boundary():
a = geometric_key_xy(mesh.vertex_coordinates(vertex))
for vertex in plate.vertices():
b = geometric_key_xy(plate.vertex_coordinates(vertex))
if a == b:
model.mesh_targetlength_at_vertex(vertex, 0.1)
for point in model.find_points_at_xy(mesh.vertex_coordinates(vertex)):
model.mesh_targetlength_at_point(point, 0.1)

for vertex in mesh.vertices_where({'vertex_degree': 2}):
model.mesh_targetlength_at_vertex(vertex, 0.01)
for vertex in mesh.vertices_where({"vertex_degree": 2}):
tag = model.find_point_at_vertex(vertex)
model.mesh_targetlength_at_point(tag, 0.1)

# model.heal()
model.generate_mesh()
# model.optimize_mesh(niter=10)
# model.recombine_mesh()

# ==============================================================================
# COMPAS mesh
# ==============================================================================

mesh = model.mesh_to_compas()

lengths = [mesh.edge_length(edge) for edge in mesh.edges()]

print(mesh.is_valid())
print(min(lengths))
print(max(lengths))

# omesh = model.mesh_to_openmesh()
# print(omesh)

# ==============================================================================
# Viz
# ==============================================================================

viewer = App(width=1600, height=900)

viewer.view.camera.rz = 0
viewer.view.camera.rx = -55
viewer.view.camera.tx = -5
viewer.view.camera.ty = -2
viewer.view.camera.distance = 10
viewer.view.camera.position = [5, -6, 7]
viewer.view.camera.look_at([5, 5, 0])

viewer.add(mesh)

poa = Point(* plate.vertex_coordinates(poa))
poa = Point(*plate.vertex_coordinates(poa))
start = poa + Vector(0, 0, 1)
vector = Vector(0, 0, -1)
load = Arrow(start, vector, body_width=0.03)
Expand Down
52 changes: 20 additions & 32 deletions docs/examples/remeshing.py
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
from math import radians
from random import choice
import compas
from compas.geometry import Point, Line, Translation, Rotation, Scale
from compas.datastructures import Mesh
from compas.topology import astar_shortest_path
from compas_gmsh.models import MeshModel
from compas_gmsh.options import MeshAlgorithm
from compas_view2.app import App

# ==============================================================================
# Input
# ==============================================================================

mesh = Mesh.from_obj(compas.get('tubemesh.obj'))
mesh = Mesh.from_obj(compas.get("tubemesh.obj"))

centroid = Point(* mesh.centroid())
vector = Point(0, 0, 0) - centroid
vector.z = 0
# =============================================================================
# Target lengths
# =============================================================================

T = Translation.from_vector(vector)
R = Rotation.from_axis_and_angle([0, 0, 1], radians(105))
S = Scale.from_factors([0.3, 0.3, 0.3])
targetlength = {vertex: 1.0 for vertex in mesh.vertices()}

mesh.transform(S * R * T)
corners = list(mesh.vertices_where({"vertex_degree": 2}))

start = choice(list(set(mesh.vertices()) - set(mesh.vertices_on_boundary())))
end = choice(corners)

for vertex in astar_shortest_path(mesh, start, end):
targetlength[vertex] = 0.05

# ==============================================================================
# GMSH model
# ==============================================================================

model = MeshModel.from_mesh(mesh, name='tubemesh', targetlength=0.3)

model.heal()

for vertex in mesh.vertex_sample(size=30):
model.mesh_targetlength_at_vertex(vertex, 0.01)
model = MeshModel.from_mesh(mesh, name="tubemesh", targetlength=targetlength)
model.options.mesh.algorithm = MeshAlgorithm.FrontalDelaunay

model.generate_mesh()
model.optimize_mesh(niter=10)

# ==============================================================================
# COMPAS mesh
Expand All @@ -42,24 +42,12 @@
mesh = model.mesh_to_compas()

# ==============================================================================
# Visualization with viewer
# Visualize
# ==============================================================================

viewer = App(width=1600, height=900)

viewer.view.camera.rx = -75
viewer.view.camera.tx = -1
viewer.view.camera.ty = 0
viewer.view.camera.position = [1, -5, 1]
viewer.view.camera.look_at([1, 5, 0])

viewer.add(mesh)

for u, v in mesh.edges():
a = mesh.vertex_coordinates(u)
b = mesh.vertex_coordinates(v)

if mesh.halfedge[u][v] is None:
viewer.add(Line(a, b), linewidth=10, linecolor=(1, 0, 0))
elif mesh.halfedge[v][u] is None:
viewer.add(Line(a, b), linewidth=10, linecolor=(1, 0, 0))

viewer.run()
26 changes: 15 additions & 11 deletions docs/examples/tets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
# ==============================================================================

model = ShapeModel(name="tets")

model.add_sphere(sphere)

model.options.mesh.lmax = 0.2

model.add_sphere(sphere)
model.generate_mesh(3)

# ==============================================================================
Expand All @@ -33,6 +31,8 @@
# Boundary lookup
# ==============================================================================

# replace by model method

centroid_face = {}
for face in shell.faces():
centroid_face[geometric_key(shell.face_centroid(face))] = face
Expand All @@ -52,13 +52,21 @@
top.append(tet)

for tet in bottom:
if any(geometric_key(centroid_points([tet.vertices[index] for index in face])) in centroid_face for face in tet.faces):
if any(
geometric_key(centroid_points([tet.vertices[index] for index in face]))
in centroid_face
for face in tet.faces
):
bottom_exterior.append(tet)
else:
bottom_interior.append(tet)

for tet in top:
if any(geometric_key(centroid_points([tet.vertices[index] for index in face])) in centroid_face for face in tet.faces):
if any(
geometric_key(centroid_points([tet.vertices[index] for index in face]))
in centroid_face
for face in tet.faces
):
top_exterior.append(tet)
else:
top_interior.append(tet)
Expand All @@ -68,12 +76,8 @@
# ==============================================================================

viewer = App(width=1600, height=900)

viewer.view.camera.rz = 0
viewer.view.camera.rx = -85
viewer.view.camera.tx = 0
viewer.view.camera.ty = 0
viewer.view.camera.distance = 7
viewer.view.camera.position = [0, -8, 0]
viewer.view.camera.look_at([0, 0, 0])

T = Translation.from_vector([0, 0, 0.5])
for tet in top:
Expand Down

0 comments on commit 534b17c

Please sign in to comment.