forked from sofa-framework/SofaPython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForceField.py
More file actions
94 lines (74 loc) · 3.34 KB
/
ForceField.py
File metadata and controls
94 lines (74 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# coding: utf8
import unittest
import Sofa
import Sofa.Core
import Sofa.Helper
import Sofa.Simulation
import SofaRuntime
from MyRestShapeForceField import *
from numpy.linalg import norm as np_norm
def createIntegrationScheme(node, use_implicit_scheme):
if use_implicit_scheme is True:
node.addObject('EulerImplicitSolver', name='odeImplicitSolver',
rayleighStiffness='0.1', rayleighMass='0.1')
else:
node.addObject('EulerExplicitSolver', name='odeExplicitSolver')
def createSolver(node, use_iterative_solver):
if use_iterative_solver == True:
node.addObject('CGLinearSolver', name='linearSolver',
iterations=30, tolerance=1.e-9, threshold=1.e-9)
else:
node.addObject('SparseCholeskySolver', name='ldlSolver')
def createParticle(node, node_name, use_implicit_scheme, use_iterative_solver):
p = node.addChild(node_name)
createIntegrationScheme(p, use_implicit_scheme)
createSolver(p, use_iterative_solver)
dofs = p.addObject('MechanicalObject', name="MO", position=[[0, 0, 0]])
p.addObject('UniformMass', totalMass=1.0)
print ("dofs.rest_position " + str(dofs.rest_position.value))
myRSSFF = NaiveRestShapeSpringsForcefield(name="Springs",
stiffness=50,
mstate=dofs, rest_pos=dofs.rest_position)
p.addObject(myRSSFF)
def rssffScene(use_implicit_scheme=True, use_iterative_solver=True):
SofaRuntime.importPlugin("SofaComponentAll")
SofaRuntime.importPlugin("SofaSparseSolver")
node = Sofa.Core.Node("root")
node.gravity = [0, -10, 0]
createParticle(node, "particle", use_implicit_scheme, use_iterative_solver)
return node
class Test(unittest.TestCase):
def test_0_explicit(self):
root = rssffScene(use_implicit_scheme=False,
use_iterative_solver=False)
# do some steps here
Sofa.Simulation.init(root)
for i in range(0, 100):
Sofa.Simulation.animate(root, root.dt.value)
print(root.particle.MO.position.value)
self.assertLess(np_norm(
root.particle.MO.rest_position.value - root.particle.MO.position.value), 0.41,
"Passed threshold on step " + str(i) + ".")
return
def test_1_implicit_iterative(self):
root = rssffScene(use_implicit_scheme=True, use_iterative_solver=True)
# do some steps here
Sofa.Simulation.init(root)
for i in range(0, 100):
Sofa.Simulation.animate(root, root.dt.value)
print(root.particle.MO.position.value)
self.assertLess(np_norm(
root.particle.MO.rest_position.value - root.particle.MO.position.value), 0.26,
"Passed threshold on step " + str(i) + ".")
return
def test_2_implicit_direct(self):
root = rssffScene(use_implicit_scheme=True, use_iterative_solver=False)
# do some steps here
Sofa.Simulation.init(root)
for i in range(0, 100):
Sofa.Simulation.animate(root, root.dt.value)
print(root.particle.MO.position.value)
self.assertLess(np_norm(
root.particle.MO.rest_position.value - root.particle.MO.position.value), 0.26,
"Passed threshold on step " + str(i) + ".")
return