-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforce.py
204 lines (131 loc) · 3.96 KB
/
force.py
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/python
import numpy as np
from geometry import *
from math import pi
import matplotlib.pyplot as plt
"""
force.py - computes forces in the current configuration
of the vertex model
author: Lexi Signoriello
date: 1/20/16
"""
def get_forces(vertices, polys, edges, parameters):
# get necessary parameters
lx = parameters['lx']
ly = parameters['ly']
L = np.array([lx,ly])
ka = parameters['ka']
Lambda = parameters['Lambda']
gamma = parameters['gamma']
f1 = F_elasticity(vertices, polys, ka, L)
f2 = F_adhesion(vertices, edges, Lambda, L)
f3 = F_contraction(vertices, polys, gamma, L)
return -(f1 + f2 + f3)
def move_vertices(vertices, forces, parameters):
delta_t = parameters['delta_t']
lx = parameters['lx']
ly = parameters['ly']
vertices = vertices + delta_t * forces
# wrap around periodic boundaries
for i,(x,y) in enumerate(vertices):
if x < 0:
# wrap around to right
vertices[i,0] = x + lx
if x > lx:
# wrap around to left
vertices[i,0] = x - lx
if y < 0:
# wrap around to top
vertices[i,1] = y + ly
if y > ly:
# wrap around to bottom
vertices[i,1] = y - ly
return vertices
def get_clockwise(index, indices, vertices, L):
# get position of vertex in list
pos = [i for i,x in enumerate(indices) if x == index]
pos = pos[0]
# clockwise is position to right
# wrap around to 0 if at end of the list
if pos == len(indices) - 1:
pos = 0
else:
pos += 1
# compute vertex wrt periodic boundaries
v0 = vertices[index]
v = vertices[indices[pos]]
vc = v0 + periodic_diff(v, v0, L)
return vc
def get_counter_clockwise(index, indices, vertices, L):
# get position of vertex in list
pos = [i for i,x in enumerate(indices) if x == index]
pos = pos[0]
# clockwise is position to left
# wrap around to end of list if first value
if pos == 0:
pos = len(indices) - 1
else:
pos -= 1
v0 = vertices[index]
v = vertices[indices[pos]]
vcc = v0 + periodic_diff(v, v0, L)
return vcc
# Force on vertex due to elasticity
def F_elasticity(vertices, polys, ka, L):
n_vertices = len(vertices)
# evert vertex has an associated force
forces = np.zeros((n_vertices, 2))
# iterate over vertices and get force
for i,vertex in enumerate(vertices):
# find polys with this vertex
for poly in polys:
# if this vertex is in current poly
# compute force contributed from this poly
if i in poly.indices:
# get clockwise vector
vc = get_clockwise(i, poly.indices, vertices, L)
# get counter-clockwise vector
vcc = get_counter_clockwise(i, poly.indices, vertices, L)
# get the difference vector
diff = vc - vcc
# compute perpendicular vector
# assure correct direction (pointing towards vertex)
perp_matrix = np.zeros((2,2))
perp_matrix[0,1] = 1.
perp_matrix[1,0] = -1.
f = -0.5 * np.dot(perp_matrix, diff)
# force contributed from this poly stored in f
coeff = ka * (poly.A0 - poly.get_area(vertices, L))
forces[i,:] += coeff * f
return forces
def F_contraction(vertices, polys, gamma, L):
# every vertex has an associated force
n_vertices = len(vertices)
forces = np.zeros((n_vertices, 2))
for i,vertex in enumerate(vertices):
# find polys with this vertex
for poly in polys:
if i in poly.indices:
# get clockwise vector
vc = get_clockwise(i, poly.indices, vertices, L)
uvc = unit_vector(vertex, vc)
# get counter-clockwise vector
vcc = get_counter_clockwise(i, poly.indices, vertices, L)
uvcc = unit_vector(vcc, vertex)
# get perimeter for this poly
p = poly.get_perim(vertices, L)
forces[i,:] += (gamma * p) * (uvc - uvcc)
return forces
def F_adhesion(vertices, edges, Lambda, L):
# every vertex has an associated force
n_vertices = len(vertices)
forces = np.zeros((n_vertices, 2))
for edge in edges:
i1 = edge[0]
i2 = edge[1]
v1 = vertices[i1]
vertex2 = vertices[i2]
v2 = v1 + periodic_diff(vertex2, v1, L)
uv = unit_vector(v1, v2)
forces[i1,:] += Lambda * uv
return forces