Skip to content

Commit c593653

Browse files
committed
better examples
1 parent 99dd0bc commit c593653

File tree

5 files changed

+106
-56
lines changed

5 files changed

+106
-56
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ __pycache__
1010
*.pyc
1111
*.vtu
1212
*.egg-info
13-
*.so
13+
*.so
14+
temp-plot.html

tests/bending.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
11
import unittest
22

33
import polyfempy as pf
4-
4+
import utils
55
import os
66

77

88
class BendingTest(unittest.TestCase):
9-
def test_run(self):
10-
dir_path = os.path.dirname(os.path.realpath(__file__))
11-
mesh_path = os.path.join(dir_path, "../3rdparty/data/square_beam.mesh")
12-
tag_path = os.path.join(dir_path, "../3rdparty/data/square_beam.txt")
9+
def test_run(self):
10+
dir_path = os.path.dirname(os.path.realpath(__file__))
11+
mesh_path = os.path.join(dir_path, "../3rdparty/data/square_beam.mesh")
12+
tag_path = os.path.join(dir_path, "../3rdparty/data/square_beam.txt")
1313

14-
settings = pf.Settings()
15-
settings.discr_order = 1
16-
settings.normalize_mesh = False
14+
settings = pf.Settings()
15+
settings.discr_order = 1
16+
settings.normalize_mesh = False
17+
settings.vismesh_rel_area = 0.1
1718

1819

1920

20-
settings.set_material_params("E", 200000)
21-
settings.set_material_params("nu", 0.35)
21+
settings.set_material_params("E", 200000)
22+
settings.set_material_params("nu", 0.35)
2223

2324

24-
settings.tensor_formulation = pf.TensorFormulations.LinearElasticity
25+
settings.tensor_formulation = pf.TensorFormulations.LinearElasticity
2526

26-
problem = pf.GenericTensor()
27-
problem.add_dirichlet_value(2, [0, 0, 0])
28-
problem.add_neumann_value(1,[0, -100, 0])
27+
problem = pf.GenericTensor()
28+
problem.add_dirichlet_value(2, [0, 0, 0])
29+
problem.add_neumann_value(1,[0, -100, 0])
2930

31+
settings.set_problem(problem)
3032

31-
settings.vismesh_rel_area = 0.00001
3233

33-
settings.set_problem(problem)
34+
solver = pf.Solver()
3435

36+
solver.settings(str(settings))
37+
solver.load_mesh(mesh_path, tag_path)
3538

36-
solver = pf.Solver()
39+
solver.solve()
3740

38-
solver.settings(str(settings))
39-
solver.load_mesh(mesh_path, tag_path)
41+
[pts, tets, disp] = solver.get_sampled_solution()
42+
vertices = pts + disp
43+
mises, _ = solver.get_sampled_mises_avg()
4044

41-
solver.solve()
45+
utils.plot(vertices, tets, mises)
4246

43-
solver.export_vtu("bending.vtu")
4447

4548

4649
if __name__ == '__main__':
47-
unittest.main()
50+
unittest.main()

tests/inflation.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import polyfempy as pf
44
import numpy as np
55

6+
import utils
7+
68
import os
79

810

@@ -61,18 +63,28 @@ def test_run(self):
6163
_, res = np.unique(vertices, axis=0, return_inverse=True)
6264
vertices, resi = np.unique(vertices, axis=0, return_index=True)
6365

66+
faces = np.ndarray([len(tris), 3], dtype=np.int64)
67+
vv = np.ndarray([len(vertices), 3], dtype=np.float64)
68+
69+
for i in range(len(tris)):
70+
faces[i] = np.array([res[tris[i][0]], res[tris[i][1]], res[tris[i][2]]])
71+
72+
for i in range(len(vv)):
73+
vv[i] = np.array([vertices[i][0], vertices[i][1], sol[resi[i]][0]])
74+
75+
utils.plot(vv, faces, None)
6476

65-
#save obj
66-
with open(output, "w") as file:
67-
# use sol as z
68-
for i in range(len(vertices)):
69-
file.write("v {} {} {}\n".format(vertices[i][0], vertices[i][1], sol[resi[i]][0]))
77+
# #save obj
78+
# with open(output, "w") as file:
79+
# # use sol as z
80+
# for i in range(len(vertices)):
81+
# file.write("v {} {} {}\n".format())
7082

71-
for i in range(len(tris)):
72-
i0 = res[tris[i][0]]
73-
i1 = res[tris[i][1]]
74-
i2 = res[tris[i][2]]
75-
file.write("f {} {} {}\n".format(i0+1, i1+1, i2+1))
83+
# for i in range(len(tris)):
84+
# i0 = res[tris[i][0]]
85+
# i1 = res[tris[i][1]]
86+
# i2 = res[tris[i][2]]
87+
# file.write("f {} {} {}\n".format(i0+1, i1+1, i2+1))
7688

7789

7890
if __name__ == '__main__':

tests/torsion.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,54 @@
11
import unittest
22

33
import polyfempy as pf
4+
import utils
45

56
import os
67

78

89
class TorsionTest(unittest.TestCase):
9-
def test_run(self):
10-
dir_path = os.path.dirname(os.path.realpath(__file__))
11-
mesh_path = os.path.join(dir_path, "../3rdparty/data/square_beam_h.HYBRID")
10+
def test_run(self):
11+
dir_path = os.path.dirname(os.path.realpath(__file__))
12+
mesh_path = os.path.join(dir_path, "../3rdparty/data/square_beam_h.HYBRID")
1213

13-
settings = pf.Settings()
14-
settings.discr_order = 1
15-
settings.normalize_mesh = False
14+
settings = pf.Settings()
15+
settings.discr_order = 1
16+
settings.normalize_mesh = False
1617

1718

1819

19-
settings.set_material_params("E", 200)
20-
settings.set_material_params("nu", 0.35)
20+
settings.set_material_params("E", 200)
21+
settings.set_material_params("nu", 0.35)
2122

22-
settings.nl_solver_rhs_steps = 5
23-
settings.tensor_formulation = pf.TensorFormulations.NeoHookean
23+
settings.nl_solver_rhs_steps = 5
24+
settings.tensor_formulation = pf.TensorFormulations.NeoHookean
2425

25-
problem = pf.Torsion()
26-
problem.fixed_boundary = 5
27-
problem.turning_boundary = 6
28-
problem.axis_coordiante = 2
29-
problem.n_turns = 0.5
26+
problem = pf.Torsion()
27+
problem.fixed_boundary = 5
28+
problem.turning_boundary = 6
29+
problem.axis_coordiante = 2
30+
problem.n_turns = 0.5
3031

3132

32-
settings.vismesh_rel_area = 0.00001
33+
settings.vismesh_rel_area = 0.001
3334

34-
settings.set_problem(problem)
35+
settings.set_problem(problem)
3536

3637

37-
solver = pf.Solver()
38+
solver = pf.Solver()
3839

3940

40-
solver.settings(str(settings))
41-
solver.load_mesh(mesh_path)
41+
solver.settings(str(settings))
42+
solver.load_mesh(mesh_path)
4243

43-
solver.solve()
44+
solver.solve()
4445

45-
solver.export_vtu("torsion.vtu")
46+
[pts, tets, disp] = solver.get_sampled_solution()
47+
vertices = pts + disp
48+
mises, _ = solver.get_sampled_mises_avg()
49+
50+
utils.plot(vertices, tets, mises)
4651

4752

4853
if __name__ == '__main__':
49-
unittest.main()
54+
unittest.main()

tests/utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import plotly.offline as plotly
2+
import plotly.graph_objs as go
3+
4+
import numpy as np
5+
6+
7+
def plot(vertices, connectivity, function):
8+
x = vertices[:,0]
9+
y = vertices[:,1]
10+
z = vertices[:,2]
11+
12+
if connectivity.shape[1] == 3:
13+
f = connectivity
14+
else:
15+
f = np.ndarray([len(connectivity)*4, 3], dtype=np.int64)
16+
for i in range(len(connectivity)):
17+
f[i*4+0] = np.array([connectivity[i][1], connectivity[i][0], connectivity[i][2]])
18+
f[i*4+1] = np.array([connectivity[i][0], connectivity[i][1], connectivity[i][3]])
19+
f[i*4+2] = np.array([connectivity[i][1], connectivity[i][2], connectivity[i][3]])
20+
f[i*4+3] = np.array([connectivity[i][2], connectivity[i][0], connectivity[i][3]])
21+
22+
mesh = go.Mesh3d(x=x, y=y, z=z,
23+
i=f[:,0], j=f[:,1], k=f[:,2],
24+
intensity=function, flatshading=function is not None,
25+
hoverinfo="none")
26+
layout = go.Layout(scene=go.layout.Scene(aspectmode='data'))
27+
fig = go.Figure(data=[mesh], layout=layout)
28+
29+
plotly.plot(fig)

0 commit comments

Comments
 (0)