-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.py
1137 lines (954 loc) · 49.8 KB
/
simulator.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import time
import logging
import numpy as np
import multiprocessing
from multiprocessing import Pool
import sys
'''
Class ParticleWaterSimulator
this class is used to simulator a particle system
simulation formulas(Explicit Euler):
q means: point position (2*1)
q_vel means: point velocity (2*1)
q_acc means: point acceleration (2*1)
"DOTIMESTEP" IS THE CORE FUNCTION FOR SIMULATION PROCEDURE.
'''
class Emmiter:
emit_pos = None
emit_mode = None
emit_point_mass = 1
emit_base_dimentsion = -1
emit_total_num = 0
def __init__(self, emit_pos_, emit_mode_):
assert emit_pos_.shape == (2, ) or emit_pos_.shape == (3, )
self.emit_base_dimentsion = emit_pos_.shape[0]
self.emit_pos = emit_pos_
self.emit_mode = emit_mode_
def blow(self, point_num):
point_pos = np.reshape(self.emit_pos, (self.emit_base_dimentsion, 1)).repeat(point_num, axis = 1)
point_vel = None
point_acc = None
point_mass = None
if "linear" == self.emit_mode:
single_vel = np.ones(self.emit_base_dimentsion) * 10
point_vel = np.reshape(single_vel, (self.emit_base_dimentsion, 1)).repeat(point_num, axis = 1)
point_acc = np.zeros([self.emit_base_dimentsion, point_num])
point_mass = np.ones(point_num) * self.emit_point_mass
if "circle" == self.emit_mode:
single_vel = None
rotate_factor = 5
if self.emit_base_dimentsion == 2:
single_vel = 10 * np.array([np.cos(rotate_factor * self.emit_total_num / 180 * np.pi), np.sin(rotate_factor * self.emit_total_num/180 * np.pi) ])
elif self.emit_base_dimentsion == 3:
single_vel = 10 * np.array([np.cos(rotate_factor * self.emit_total_num / 180 * np.pi), np.sin(rotate_factor * self.emit_total_num / 180 * np.pi), 0])
point_vel = np.reshape(single_vel, (self.emit_base_dimentsion, 1)).repeat(point_num, axis=1)
point_acc = np.zeros([self.emit_base_dimentsion, point_num])
point_mass = np.ones(point_num) * self.emit_point_mass
self.emit_total_num += point_num
return point_pos, point_vel, point_acc, point_mass
class ParticleWaterSimulatorBase:
# 2D simulator or 3D simulator?
simulator_base_dimension = 2
# simulation params
cur_time = 0.0
timestep = 0.0
frameid = 0
particles_num = -1
space_left_down_corner = None
space_right_up_corner = None
g = 0
collision_detect = False
# simulation space
space_length = 0
space_height = 0
space_width = 0
# collision penalty force coeff
collision_epsilon = -1
collision_penalty_k = 5e3 # control the distance
collision_penalty_b = 1 # control the velocity
# damping coeff
damping_coeff = 1
# status varibles
point_pos = np.zeros([simulator_base_dimension, 0])
point_vel = np.zeros([simulator_base_dimension, 0])
point_acc = np.zeros([simulator_base_dimension, 0])
point_mass = np.zeros(0)
# system cost time record
time_cost_dotimestep = 0.0
time_cost_compute_force = 0.0
time_cost_collision_test = 0.0
# emiiter
# emmiter1 =
emit_amount = -1
emmitter_list = []
# logging module
logger = None
log = None
# record mode
record = False
record_dir = None
record_filename = None
# SP or MP, it will decide by the simulator automatically
MULTIPLEPROCESS = -1
multipleprocess_num = -1
multipleprocess_infolist = []
def __init__(self,
simulator_base_dimension_,
particle_nums_,
timestep_,
space_left_down_corner_,
space_right_up_corner_,
gravity_,
collision_detect_,
record_save_dir_,
multi_processor_):
'''
:param particle_nums_:
:param timestep_:
:param space_left_down_corner_:
:param space_right_up_corner_:
:param gravity_:
'''
# logging module init
log_filename = "./log/" + str(time.strftime("%Y-%m-%d %H%M%S", time.localtime())) + str('.txt')
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(name)s - %(message)s")
fh = logging.FileHandler(filename=log_filename) # ouput log both the the console and the file
# self.logger = logging.getLogger(self.__class__.__name__)
# self.logger.addHandler(fh)
# self.logger.info('[SimulatorBase] Init begin')
# system property
# x_num = int(particle_nums_ ** 0.5)
# y_num = int(particle_nums_ ** 0.5)
# self.particles_num = x_num * y_num
self.simulator_base_dimension = simulator_base_dimension_
self.particles_num = particle_nums_
self.point_pos = np.zeros([self.simulator_base_dimension, self.particles_num])
self.point_vel = np.zeros([self.simulator_base_dimension, self.particles_num])
self.point_acc = np.zeros([self.simulator_base_dimension, self.particles_num])
self.point_mass = np.ones(self.particles_num)
self.space_left_down_corner = space_left_down_corner_
self.space_right_up_corner = space_right_up_corner_
assert len(self.space_left_down_corner) == self.simulator_base_dimension
assert len(self.space_right_up_corner) == self.simulator_base_dimension
# self.point_pos[0, :] = space_length / 2 * np.tile(np.linspace(0, 1, num = x_num), y_num) + space_left_down_corner_[
# 0] + space_length / 4
# self.point_pos[1, :] = space_height / 2 * np.repeat(np.linspace(0, 1, num = y_num), x_num) + space_left_down_corner_[
# 1] + space_height / 3
self.init_points_variables(space_left_down_corner_, space_right_up_corner_)
# simulation property
self.g = gravity_
self.timestep = timestep_
self.collision_detect = collision_detect_
if self.simulator_base_dimension == 2:
self.collision_epsilon = min(self.space_height, self.space_length) / 100
elif self.simulator_base_dimension == 3:
self.collision_epsilon = min(self.space_height, self.space_length, self.space_width) / 100
# record mode init
self.record_dir = record_save_dir_
self.record_filename = self.record_dir + str(time.strftime("%Y-%m-%d %H%M%S", time.localtime())) + str('.txt')
# init emitter
if self.simulator_base_dimension == 2:
emmiter1 = Emmiter(np.array([(self.space_left_down_corner[0]+self.space_right_up_corner[0])/2,
(self.space_left_down_corner[1]+self.space_right_up_corner[1])/2]), "circle")
self.emmitter_list.append(emmiter1)
self.emit_amount = 1
elif self.simulator_base_dimension == 3:
# self.emmiter1 = Emmiter(np.array([0, 0, 0]), "linear")
emmiter1 = Emmiter(np.array([(self.space_left_down_corner[0] + self.space_right_up_corner[0]) / 2,
(self.space_left_down_corner[1] + self.space_right_up_corner[1]) / 2,
(self.space_left_down_corner[2] + self.space_right_up_corner[2]) / 2],
), "circle")
self.emmitter_list.append(emmiter1)
self.emit_amount = 1
# MP computation setting
# if self.particles_num > 1300:
self.MULTIPLEPROCESS = multi_processor_
self.multipleprocess_num = int(min(self.particles_num / 5000, 1) * multiprocessing.cpu_count())
# print('particle_num = %d' % self.particles_num)
# print('timestep = %d' % self.timestep)
# print('active space = (%f, %f) - (%f, %f)' % (
# space_left_down_corner_[0], space_left_down_corner_[1], self.space_right_up_corner[0],
# self.space_right_up_corner[1]))
# print('******************Simulator Init Succ****************')
# self.logger.info('[SimulatorBase] Init succ')
return
def init_points_variables(self, space_left_down_corner_, space_right_up_corner_):
# init these points
if self.simulator_base_dimension == 2:
self.space_length = space_right_up_corner_[0] - space_left_down_corner_[0]
self.space_height = space_right_up_corner_[1] - space_left_down_corner_[1]
self.point_pos[0, :] = self.space_length / 2 * np.random.rand( self.particles_num) + space_left_down_corner_[
0] + self.space_length / 4
self.point_pos[1, :] = self.space_height / 2 * np.random.rand( self.particles_num) + space_left_down_corner_[
1] + self.space_height / 4
elif self.simulator_base_dimension == 3:
self.space_length = space_right_up_corner_[0] - space_left_down_corner_[0]
self.space_height = space_right_up_corner_[1] - space_left_down_corner_[1]
self.space_width = space_right_up_corner_[2] - space_left_down_corner_[2]
self.point_pos[0, :] = self.space_length / 2 * np.random.rand( self.particles_num) + space_left_down_corner_[
0] + self.space_length / 4
self.point_pos[1, :] = self.space_height / 2 * np.random.rand( self.particles_num) + space_left_down_corner_[
1] + self.space_height / 4
self.point_pos[2, :] = self.space_width / 2 * np.random.rand(self.particles_num) + space_left_down_corner_[
2] + self.space_width / 4
else:
raise("the dimension is illegal")
def dotimestep(self):
# dynamic simulation - compute total forces and acceleration
st = time.time()
# time++
self.cur_time += self.timestep
self.frameid += 1
self.time_cost_dotimestep = time.time() - st
return self.point_pos
# get and set methods
def get_cur_time(self):
return self.cur_time
def get_frameid(self):
return self.frameid
def get_particle_num(self):
return self.particles_num
# update simulation state
# currently, the explicit euler method
def update_state(self):
'''
q_vel = q_vel + timestep * q_accel
q_pos = q_pos + timestep * q_accel
'''
self.point_vel += self.timestep * self.point_acc
self.point_pos += self.timestep * self.point_vel
def do_collision_test_between_wall_and_particles_3d(self):
st = time.time()
collision_force = np.zeros([3, self.particles_num])
x_left = self.space_left_down_corner[0]
x_right = self.space_right_up_corner[0]
y_down = self.space_left_down_corner[1]
y_up = self.space_right_up_corner[1]
z_low = self.space_left_down_corner[2]
z_high = self.space_right_up_corner[2]
for i in range(self.particles_num):
# for a point i
pos_i = self.point_pos[:, i]
vel_i = self.point_vel[:, i]
next_pos = pos_i + self.timestep * vel_i
if abs(pos_i[0] - x_left) < self.collision_epsilon:
collision_force[:, i][0] += abs(pos_i[0] - x_left) * self.collision_penalty_k
if vel_i[0] < 0:
collision_force[:, i][0] += np.abs(vel_i[0]) * self.collision_penalty_b
# print('[debug][collision] %d point collision with x' % i)
if abs(pos_i[0] - x_right) < self.collision_epsilon:
collision_force[:, i][0] += -(abs(pos_i[0] - x_right) * self.collision_penalty_k)
if vel_i[0] > 0:
collision_force[:, i][0] += -abs(vel_i[0]) * self.collision_penalty_b
# print('[debug][collision] %d point collision with x' % i)
if abs(pos_i[1] - y_down) < self.collision_epsilon:
collision_force[:, i][1] += abs(pos_i[1] - y_down) * self.collision_penalty_k
if vel_i[1] < 0:
collision_force[:, i][1] += np.abs(vel_i[1]) * self.collision_penalty_b
if abs(pos_i[1] - y_up) < self.collision_epsilon:
collision_force[:, i][1] += -abs(pos_i[1] - y_up) * self.collision_penalty_k
if vel_i[1] > 0:
collision_force[:, i][1] += -np.abs(vel_i[1]) * self.collision_penalty_b
if abs(pos_i[2] - z_low) < self.collision_epsilon:
collision_force[:, i][2] += abs(pos_i[2] - z_low) * self.collision_penalty_k
if vel_i[2] < 0:
collision_force[:, i][2] += np.abs(vel_i[2]) * self.collision_penalty_b
if abs(pos_i[2] - z_high) < self.collision_epsilon:
collision_force[:, i][2] += -abs(pos_i[2] - z_high) * self.collision_penalty_k
if vel_i[2] > 0:
collision_force[:, i][2] += -np.abs(vel_i[2]) * self.collision_penalty_b
return collision_force
def do_collision_test_between_wall_and_particles_2d(self):
st = time.time()
collision_force = np.zeros([2, self.particles_num])
x_left = self.space_left_down_corner[0]
x_right = self.space_right_up_corner[0]
y_down = self.space_left_down_corner[1]
y_up = self.space_right_up_corner[1]
for i in range(self.particles_num):
# for a point i
pos_i = self.point_pos[:, i]
vel_i = self.point_vel[:, i]
next_pos = pos_i + self.timestep * vel_i
if abs(pos_i[0] - x_left) < self.collision_epsilon:
collision_force[:, i][0] += abs(pos_i[0] - x_left) * self.collision_penalty_k
if vel_i[0] < 0:
collision_force[:, i][0] += np.abs(vel_i[0]) * self.collision_penalty_b
# print('[debug][collision] %d point collision with x' % i)
if abs(pos_i[0] - x_right) < self.collision_epsilon:
collision_force[:, i][0] += -(abs(pos_i[0] - x_right) * self.collision_penalty_k)
if vel_i[0] > 0:
collision_force[:, i][0] += -abs(vel_i[0]) * self.collision_penalty_b
# print('[debug][collision] %d point collision with x' % i)
if abs(pos_i[1] - y_down) < self.collision_epsilon:
collision_force[:, i][1] += abs(pos_i[1] - y_down) * self.collision_penalty_k
if vel_i[1] < 0:
collision_force[:, i][1] += np.abs(vel_i[1]) * self.collision_penalty_b
if abs(pos_i[1] - y_up) < self.collision_epsilon:
collision_force[:, i][1] += -abs(pos_i[1] - y_up) * self.collision_penalty_k
if vel_i[1] > 0:
collision_force[:, i][1] += -np.abs(vel_i[1]) * self.collision_penalty_b
return collision_force
def do_collision_test_between_wall_and_particles(self):
'''
@Function: do_collision_test_between_wall_and_particles
this function is used to:
1. detect the collision (judgement according to some creatia collision for or not)
2. compute the collision penalty force accordly
Now this function can only handle a box boundary, limited to the big computation amout
@params: None
@return: collision force
@date: 12/06/2019
'''
# print('[log][simulation] do collision test')
collision_force = None
if self.simulator_base_dimension == 2:
collision_force = self.do_collision_test_between_wall_and_particles_2d()
elif self.simulator_base_dimension == 3:
# print('[warning] the collision force in 3d hasn\'t been implemented')
collision_force = self.do_collision_test_between_wall_and_particles_3d()
return collision_force
def compute_damping_force(self):
return -1 * self.point_vel * self.damping_coeff
def compute_gravity_force(self):
assert self.g > 0
points_gravity = None
if self.simulator_base_dimension == 2:
points_gravity = np.zeros([2, self.particles_num])
points_gravity[1,] = -1 * self.g * self.point_mass
elif self.simulator_base_dimension == 3:
points_gravity = np.zeros([3, self.particles_num])
points_gravity[2,] = -1 * self.g * self.point_mass
else:
raise ("the dimension is illegal")
return points_gravity
def update_multipleprocessor_infolist(self):
'''
compute the info list for multiple processor division
:return:
'''
# judge whether to use MP or not
if self.particles_num * self.simulator_base_dimension> 2000:
self.MULTIPLEPROCESS = True
# update the particle num division
self.multipleprocess_infolist = []
for i in range(self.multipleprocess_num):
if 0 == i:
st_id = 0
ed_id = int((i + 1) / self.multipleprocess_num * self.particles_num)
elif i == self.multipleprocess_num - 1:
st_id = ed_id
ed_id = self.particles_num
else:
st_id = ed_id
ed_id = int((i + 1) / self.multipleprocess_num * self.particles_num)
if st_id > ed_id:
st_id = ed_id
para = np.array([i, st_id, ed_id])
self.multipleprocess_infolist.append(para)
def record_data(self):
with open(self.record_filename, 'a') as f:
f.write("%d %d " % (self.frameid, self.particles_num))
for i in range(self.particles_num):
if self.simulator_base_dimension == 2:
f.write("%.5f %.5f " % (self.point_pos[0, i], self.point_pos[1, i]))
elif self.simulator_base_dimension == 3:
f.write("%.5f %.5f %.5f " % (self.point_pos[0, i], self.point_pos[1, i], self.point_pos[2, i]))
f.write("\n")
def emitter_inject(self):
if self.emit_amount <=0: # there is no emitter
return
for cur_emiter in self.emmitter_list:
self.particles_num += self.emit_amount
pos, vel, acc, mass = cur_emiter.blow(self.emit_amount)
self.point_pos = np.concatenate((self.point_pos, pos), axis=1)
self.point_vel = np.concatenate((self.point_vel, vel), axis=1)
self.point_acc = np.concatenate((self.point_acc, acc), axis=1)
self.point_mass = np.append(self.point_mass, mass)
# you must update the division info after add or diminish some vertex
self.update_multipleprocessor_infolist()
class ParticleWaterSimulatorEasy(ParticleWaterSimulatorBase):
# lennard jones forces coef
lennard_jones_k1 = 0.01
lennard_jones_k2 = 0.01
lennard_jones_m = 4
lennard_jones_n = 2
def __init__(self,simulator_base_dimension_, particle_nums_, timestep_, space_left_down_corner_, space_right_up_corner_, gravity_,
collision_detect_, record_save_dir_, multi_processor_):
ParticleWaterSimulatorBase.__init__(self,simulator_base_dimension_, particle_nums_, timestep_, space_left_down_corner_, space_right_up_corner_, gravity_,
collision_detect_, record_save_dir_, multi_processor_)
return
def dotimestep(self):
# dynamic simulation - compute total forces and acceleration
st = time.time()
points_force = self.compute_forces()
# compute accel
# self.point_acc[0,] = points_force[0,] * (1.0 / self.point_mass)
# self.point_acc[1,] = points_force[1,] * (1.0 / self.point_mass)
self.point_acc = np.multiply(points_force, 1.0 / self.point_mass)
assert self.point_acc.shape == (self.simulator_base_dimension, self.particles_num)
# update the state - forward euler
self.update_state()
# time++
self.cur_time += self.timestep
self.frameid += 1
self.time_cost_dotimestep = time.time() - st
print('[log][simulator] do timestep, cur time = %.3f s, cur frameid = %d' % (self.cur_time, self.frameid))
print('[log][simulator] dotimestep cost %.5f s, jones force cost %.5f s' % (
self.time_cost_dotimestep, self.time_cost_jones_force))
# print(self.point_vel.dtype)
return self.point_pos
# compute the lennard jones forces for particle system
'''
Lennard_Jones force is aimed at pushing 2 particles far away from each other
f(xi, xj) = ( k1 / (|xi - xj|^m) - k2 / (|xi - xj|)^n)
*
(xi - xj) / |xi - xj|
'''
def compute_lennard_jones_force(self):
st = time.time()
jones_force = np.zeros([self.simulator_base_dimension, self.particles_num])
for pi in range(self.particles_num):
for pj in range(pi + 1, self.particles_num):
pos_xi = self.point_pos[:, pi]
pos_xj = self.point_pos[:, pj]
xi_xj_dist = np.linalg.norm(pos_xi - pos_xj, ord = 2)
force_coeff = (
self.lennard_jones_k1 / xi_xj_dist ** self.lennard_jones_m - self.lennard_jones_k2 / xi_xj_dist ** self.lennard_jones_n)
force_xi_xj = force_coeff / xi_xj_dist * (pos_xi - pos_xj)
jones_force[:, pi] += force_xi_xj
jones_force[:, pj] += -force_xi_xj
ed = time.time()
self.time_cost_jones_force = ed - st
jones_force = np.clip(jones_force, a_max = 100, a_min = -100)
# print('[log][jones_force] cost time %.3f s' % (ed-st))
return jones_force
# Function: self.compute_forces
def compute_forces(self):
'''
this function is aimed at computing total forces for the whole particle system
:return:
'''
st = time.time()
points_force = np.zeros([self.simulator_base_dimension, self.particles_num])
## gravity computation
points_gravity = self.compute_gravity_force()
# jone forces computation
points_jone_forces = self.compute_lennard_jones_force()
# damping
points_damping_forces = self.compute_damping_force()
# collision detect
if self.collision_detect == True:
collision_force = self.do_collision_test_between_wall_and_particles()
else:
collision_force = np.zeros([self.simulator_base_dimension, self.particles_num])
# total force summary
points_force += points_gravity
points_force += points_jone_forces
points_force += points_damping_forces
points_force += collision_force
# print(collision_force[1,])
self.time_cost_compute_force = time.time() - st
# print(points_damping_forces)
return points_force
class ParticleWaterSimulatorSPH(ParticleWaterSimulatorBase):
# kernel parameters
kernel_poly6_d = -1
kernel_poly6_coeff = -1
# sph variables
sph_point_density = -1
sph_point_pressure = -1
# constant
gas_constant = 8.314
# simulation variables
viscosity_coeff = 1e-3 # the viscosity of water is 1e-3
def __init__(self, simulator_base_dimension_, particle_nums_, timestep_, space_left_down_corner_, space_right_up_corner_, gravity_,
collision_detect_, kernel_poly6_d_, record_save_dir_,multi_processor_):
ParticleWaterSimulatorBase.__init__(self, simulator_base_dimension_, particle_nums_, timestep_, space_left_down_corner_, space_right_up_corner_, gravity_,
collision_detect_, record_save_dir_,multi_processor_)
# init sph variable
self.kernel_poly6_d = kernel_poly6_d_
self.kernel_poly6_coeff = 315.0 / (64.0 * np.pi * (self.kernel_poly6_d ** 9))
# print('[log][simulator] ParticleWaterSimulatorSPH init succ')
return
def dotimestep(self):
st = time.time()
# update Multiple Processor usage info
self.update_multipleprocessor_infolist()
# emit
self.emitter_inject()
# compute force
points_force = self.compute_force()
# compute accel
self.point_acc = np.multiply(points_force, 1.0 / self.point_mass)
# self.point_acc[0,] = points_force[0,] * (1.0 / self.point_mass)
# self.point_acc[1,] = points_force[1,] * (1.0 / self.point_mass)
# update the state - forward euler
self.update_state()
# time++
self.cur_time += self.timestep
self.frameid += 1
self.time_cost_dotimestep = time.time() - st
print('[log][simulator] do timestep, cur time = %.3f s, cur frameid = %d' % (self.cur_time, self.frameid))
# record
self.record_data()
return self.point_pos
def compute_force(self):
st = time.time()
sum_force = np.zeros([self.simulator_base_dimension, self.particles_num])
# 1.1 compute the point density
st1 = time.time()
self.compute_point_density()
st2 = time.time()
time_compute_density = st2 - st1
# 1.2 compute the pressure (not force) in eache point
st1 = time.time()
self.compute_point_pressure()
st2 = time.time()
time_compute_point_pressure = st2 - st1
# 1.3 compute the pressure force for each point
st1 = time.time()
pressure_force = self.compute_pressure_force()
st2 = time.time()
time_compute_pressure = st2 - st1
# 1.4 compute the viscosity force
st1 = time.time()
viscosity_force = self.compute_viscosity_force()
st2 = time.time()
time_viscosity = st2 - st1
# 1.5 compute the gravity
gravity = self.compute_gravity_force()
# 1.6 compute the collision force
# print('collsion = ' + str(self.collision_detect))
st1 = time.time()
if self.collision_detect == True:
collision_force = self.do_collision_test_between_wall_and_particles()
else:
collision_force = np.zeros([self.simulator_base_dimension, self.particles_num])
st2 = time.time()
time_collision = st2 - st1
# 1.7 compute the damping force
damping_force = self.compute_damping_force()
# 1.6 summary
sum_force += pressure_force
print(np.linalg.norm(pressure_force[0]))
sum_force += viscosity_force
sum_force += gravity
sum_force += collision_force
sum_force += damping_force
ed = time.time()
time_total = ed - st + 1e-6
# self.logger.info("compute force cost time (%.3f) s, collision %.3f s(%.3f%%), pressure %.3f s(%.3f%%), viscosity %.3f s(%.3f%%), point_density %.3f s(%.3f%%) , point pressure %.3fs(%.3f%%)."
# % (time_total, time_collision, time_collision/time_total * 100,
# time_compute_pressure, time_compute_pressure/time_total * 100,
# time_viscosity, time_viscosity/time_total * 100,
# time_compute_density, time_compute_density/time_total * 100, time_compute_point_pressure, time_compute_point_pressure/time_total * 100))
with open("./log/stastistic.txt", "a") as f:
str = "particle num = %d, compute force cost time (%.3f) s, collision %.3f s(%.3f%%), pressure %.3f s(%.3f%%), viscosity %.3f s(%.3f%%), point_density %.3f s(%.3f%%) , point pressure %.3fs(%.3f%%)." % (self.particles_num, time_total, time_collision, time_collision/time_total * 100,
time_compute_pressure, time_compute_pressure/time_total * 100,
time_viscosity, time_viscosity/time_total * 100,
time_compute_density, time_compute_density/time_total * 100, time_compute_point_pressure, time_compute_point_pressure/time_total * 100)
print(str)
f.write(str + "\n")
return sum_force
def compute_sub_viscosity_force(self, para):
assert para.shape == (3,) # procnum, st_point_id, ed_point_id
procnum = para[0]
st_point_id = para[1]
ed_point_id = para[2]
# print('st ed = %d %d' % (st_point_id, ed_point_id))
cur_particles_num = ed_point_id - st_point_id
viscosity_force = np.zeros([self.simulator_base_dimension, cur_particles_num])
# compute
# print("[sub pressure force] density = " + str(self.sph_point_density[-1]))
for i in range(cur_particles_num):
id = i + st_point_id
velocity_i = np.reshape(self.point_vel[:, id], (self.simulator_base_dimension, 1))
velocity_diff = self.point_vel - velocity_i # 2*n
mass_div_density = self.point_mass / self.sph_point_density
assert mass_div_density.shape == (self.particles_num,)
velocity_diff_coef_vec = velocity_diff * mass_div_density # 2 * n
velocity_diff_coef_vec = velocity_diff_coef_vec.flatten(order='F')
# velocity_diff_coef_vec = 2n * 1, flatten按列展开
# (2i, 2i+1)数据对就是第i个点的x y速度差 * 对应系数
assert velocity_diff_coef_vec.shape == (self.particles_num * self.simulator_base_dimension,)
# compute the ∇^2_matrix
pos_i = np.reshape(self.point_pos[:, id], (self.simulator_base_dimension, 1))
pos_diff = pos_i - self.point_pos
# the only reason for divide these code into 2 parts is for good names"ddW_dxy2" and "ddW_dxyz2"
if self.simulator_base_dimension == 2:
ddW_dxy2 = self.W_poly6_2_order_jacob(pos_diff)
assert ddW_dxy2.shape == (2, 2 * self.particles_num)
# compute the result
viscosity_force[:, i] = self.viscosity_coeff * np.dot(ddW_dxy2, velocity_diff_coef_vec)
elif self.simulator_base_dimension == 3:
ddW_dxyz2 = self.W_poly6_2_order_jacob(pos_diff)
assert ddW_dxyz2.shape == (3, 3 * self.particles_num)
# compute the result
viscosity_force[:, i] = self.viscosity_coeff * np.dot(ddW_dxyz2, velocity_diff_coef_vec)
else:
assert 0 == 1
return (procnum, viscosity_force)
def compute_viscosity_force(self):
'''
this function will compute the viscosity force for each point
and the govern formula is:
f_viscosity = [f_viscosity_0, ..., f_viscosity_n]_{2*n}
f_viscosity_i_{2*1} = μ∇^2 v
f_vis_i = μΣ_j mj * (vj - vi)/ρj *∇^2 W(|xi - xj|)
= μΣ_j mj/ρj * (vj - vi) * ∇^2 W(|xi - xj|)
= μΣ_j ∇^2 W(|xi - xj|)_{2*2} * velocity_diff_coef_{2*1}
= μ ∇^2_matrix_{2*2n} * velocity_diff_coef_vec{2n*1}
= (2*1)
:return:
'''
viscosity_force = np.zeros([self.simulator_base_dimension, self.particles_num])
if self.MULTIPLEPROCESS == True:
pool = Pool(self.multipleprocess_num)
data = pool.map(self.compute_sub_viscosity_force, self.multipleprocess_infolist)
for i in range(len(data)):
procnum, force = data[i]
st_id = self.multipleprocess_infolist[procnum][1]
ed_id = self.multipleprocess_infolist[procnum][2]
assert force.shape == (self.simulator_base_dimension, ed_id - st_id)
viscosity_force[:, st_id: ed_id] = force
pool.close()
pool.join()
else:
for i in range(self.particles_num):
# compute the velocity_diff_coef_vec
velocity_i = np.reshape(self.point_vel[:, i], (self.simulator_base_dimension, 1))
velocity_diff = self.point_vel - velocity_i # 2*n or 3*n, it depens
mass_div_density = self.point_mass / self.sph_point_density
assert mass_div_density.shape == (self.particles_num, )
velocity_diff_coef_vec = velocity_diff * mass_div_density # 2*n or 3*n
velocity_diff_coef_vec = velocity_diff_coef_vec.flatten(order='F')
# velocity_diff_coef_vec = 2n * 1 or 3n * 1, flatten按列展开
# 2d case: (2i, 2i+1)数据对就是第i个点的x y速度差 * 对应系数
# 3d case: (3i, 3i+1)数据对就是第i个点的x y z速度差 * 对应系数
assert velocity_diff_coef_vec.shape == (self.particles_num * self.simulator_base_dimension, )
# compute the ∇^2_matrix
pos_i = np.reshape(self.point_pos[:, i], (self.simulator_base_dimension, 1))
pos_diff = pos_i - self.point_pos
# these following codes are divided into 2 parts is just for simplicit and good name.
# "ddW_dxy2" and "ddW_dxyz2", they are different
if self.simulator_base_dimension == 2:
ddW_dxy2 = self.W_poly6_2_order_jacob(pos_diff)
assert ddW_dxy2.shape == (2, 2 * self.particles_num)
# compute the result
viscosity_force[:, i] = self.viscosity_coeff * np.dot(ddW_dxy2 , velocity_diff_coef_vec)
elif self.simulator_base_dimension == 3:
ddW_dxyz2 = self.W_poly6_2_order_jacob(pos_diff)
assert ddW_dxyz2.shape == (3, 3 * self.particles_num)
# compute the result
viscosity_force[:, i] = self.viscosity_coeff * np.dot(ddW_dxyz2, velocity_diff_coef_vec)
# np.set_printoptions(linewidth=200, floatmode='fixed')
# print('ddW_dxy = ' + str(ddW_dxy2))
# print('coef = ' + str(velocity_diff_coef_vec))
# print('viscosity force = ' + str(viscosity_force))
return viscosity_force
def compute_sub_pressure_force(self, para):
assert para.shape == (3,) # procnum, st_point_id, ed_point_id
procnum = para[0]
st_point_id = para[1]
st_point_id = para[1]
ed_point_id = para[2]
# print('st ed = %d %d' % (st_point_id, ed_point_id))
cur_particles_num = ed_point_id - st_point_id
pressure_force = np.zeros([self.simulator_base_dimension, cur_particles_num])
# compute
# print("[sub pressure force] density = " + str(self.sph_point_density[-1]))
for i in range(cur_particles_num):
id = i + st_point_id
coeff_vector = self.point_mass * \
(self.sph_point_pressure + self.sph_point_pressure[id]) / (2 * self.sph_point_density)
assert coeff_vector.shape == (self.particles_num,)
# compute the ∇W(|xi - xj|)
pos_diff = np.reshape(self.point_pos[:, id], (self.simulator_base_dimension, 1)) - self.point_pos
pressure_force_i = np.zeros(self.simulator_base_dimension)
if self.simulator_base_dimension == 2:
dW_dxy = self.W_poly6_1_order_gradient(pos_diff)
assert dW_dxy.shape == (2, self.particles_num)
# compute the pressure_i
pressure_force_i = -np.dot(dW_dxy, coeff_vector)
elif self.simulator_base_dimension == 3:
dW_dxyz = self.W_poly6_1_order_gradient(pos_diff)
assert dW_dxyz.shape == (3, self.particles_num)
# compute the pressure_i
pressure_force_i = -np.dot(dW_dxyz, coeff_vector)
# compute the pressure_i
pressure_force[:, i] = pressure_force_i
return (procnum, pressure_force)
def compute_pressure_force(self):
'''
Function: compute_pressure
this function is aimed at computing pressure for ith point, its formula:
pressure_i = - Σj mj * (pi + pj) / 2 * pj * ∇W(|xi - xj|) = (2, 1) or (3, 1)
= - Σj coeff_j * ∇W(|xi - xj|) = (2, 1)
= - np.dot(∇W(|xi - xj|)_{2*n}, coeff_vec_j_{n*1}) = (2, 1) or (3, 1)
'''
pressure_force = np.zeros([self.simulator_base_dimension, self.particles_num])
if self.MULTIPLEPROCESS == True:
# startTime = time.time()
pool = Pool(self.multipleprocess_num)
data = pool.map(self.compute_sub_pressure_force, self.multipleprocess_infolist)
for i in range(len(data)):
procnum, force = data[i]
st_id = self.multipleprocess_infolist[procnum][1]
ed_id = self.multipleprocess_infolist[procnum][2]
assert force.shape == (self.simulator_base_dimension, ed_id - st_id)
pressure_force[:, st_id : ed_id] = force
pool.close()
pool.join()
# endTime = time.time()
# pressure_force_bak = pressure_force.copy()
# print("MP time : %.3F" % (endTime - startTime))
else:
startTime = time.time()
for i in range(self.particles_num):
# compute the coeff vector
coeff_vector = self.point_mass * (self.sph_point_pressure + self.sph_point_pressure[i]) / (2 * self.sph_point_density)
assert coeff_vector.shape == (self.particles_num, )
# compute the ∇W(|xi - xj|)
pos_diff = np.reshape(self.point_pos[:, i], (self.simulator_base_dimension, 1)) - self.point_pos
pressure_force_i = np.zeros(self.simulator_base_dimension)
if self.simulator_base_dimension == 2:
dW_dxy = self.W_poly6_1_order_gradient(pos_diff)
assert dW_dxy.shape == (2, self.particles_num)
# if np.linalg.norm(dW_dxy) > 1:
# print(' %d th point info' % i)
# print('dW_dxy = ' + str(dW_dxy))
# print('coeff_vector = ' + str(coeff_vector))
# compute the pressure_i
pressure_force_i = -np.dot(dW_dxy, coeff_vector)
elif self.simulator_base_dimension == 3:
dW_dxyz = self.W_poly6_1_order_gradient(pos_diff)
assert dW_dxyz.shape == (3, self.particles_num)
# compute the pressure_i
pressure_force_i = -np.dot(dW_dxyz, coeff_vector)
else:
assert 0 == 1
pressure_force[:, i] = pressure_force_i
endTime = time.time()
# print("SP time : %.3F" % (endTime - startTime))
# print('***************************')
# print('pressure force = ' + str(pressure_force))
# print("pressure force")
# print("%d th frame pressforce = %.3f" % (self.frameid, np.linalg.norm(pressure_force_bak - pressure_force)))
return pressure_force
def compute_sub_point_density(self, para):
assert para.shape == (3,) # procnum, st_point_id, ed_point_id
procnum = para[0]
st_point_id = para[1]
ed_point_id = para[2]
# print(para)
# print('st ed = %d %d' % (st_point_id, ed_point_id))
cur_particles_num = ed_point_id - st_point_id
W_xi_xj = np.zeros([self.particles_num, cur_particles_num])
for i in range(cur_particles_num):
id = i + st_point_id
dist = np.reshape(self.point_pos[:, id], (self.simulator_base_dimension, 1)) - self.point_pos
assert dist.shape == (self.simulator_base_dimension, self.particles_num)
W_xi_xj[:, i] = self.W_poly6_0_order_constant(dist)
# print(id)
# if id == self.particles_num - 1:
# print('false Wxixj %d th col = %s' % (id, str(W_xi_xj[:, i])))
return (procnum, W_xi_xj)
def compute_point_density(self):
'''
this function will compute the point density "self.sph_point_density" # (it's useful for the computation of 2 forces)
from the formula:
ρ(x) = \sum_j mj * W(|x-xj|)
ρ(xi)_{1*1} = np.dot(self.point_mass_{1*n}, W(|xi-xj|)_{n*1})
ρ(x)_{1*n} = np.dot(self.point_mass_{1*n}, W(|xi-xj|)_{n*n})
:return: None
'''
W_xi_xj = np.zeros([self.particles_num, self.particles_num])
# W_xi_xj_bak = None
# sph_point_density_bak = None
if self.MULTIPLEPROCESS == True:
pool = Pool(self.multipleprocess_num)
data = pool.map(self.compute_sub_point_density, self.multipleprocess_infolist)
pool.close()
pool.join()
for i in range(len(data)):
procnum, Wij = data[i]
# print(procnum)
st_id = self.multipleprocess_infolist[procnum][1]
ed_id = self.multipleprocess_infolist[procnum][2]
assert Wij.shape == (self.particles_num, ed_id - st_id)
W_xi_xj[:, st_id : ed_id] = Wij
assert W_xi_xj.shape == (self.particles_num, self.particles_num)
# W_xi_xj_bak = W_xi_xj.copy()
# print('false W_xixj = ' + str(W_xi_xj))
# sph_point_density_bak = np.dot(self.point_mass, W_xi_xj)
# print(W_xi_xj)
else:
# compute W(|xi-xj|_{n*n})
for i in range(W_xi_xj.shape[1]):
dist = np.reshape(self.point_pos[:, i], (self.simulator_base_dimension, 1)) - self.point_pos
assert dist.shape == (self.simulator_base_dimension, self.particles_num)
W_xi_xj[:, i] = self.W_poly6_0_order_constant(dist)
# print(W_xi_xj)
# compute the point density
assert self.point_mass.shape == (self.particles_num, )
# print(self.point_mass.shape)
# print('true W_xixj = ' + str(W_xi_xj))
self.sph_point_density = np.dot(self.point_mass, W_xi_xj)
assert self.sph_point_density.shape == (self.particles_num, )
return
def compute_point_pressure(self):
'''
this function will compute the point pressure (not force), according to the formula:
p = k(ρ-ρ0). k is the gas constant
now, ρ0 = min(ρ) / 500
:return: None
'''
# rho_0 = np.min(self.sph_point_density) / 500
rho_0 = 0
# print(self.sph_point_density)
self.sph_point_pressure = self.gas_constant * (self.sph_point_density - rho_0)
assert self.sph_point_pressure.shape == (self.particles_num, )
return
def W_poly6_0_order_constant(self, pos):
'''
Function: W_poly6
this function is used to compute the value of kernel poly6:
315 / (64 * pi * d^2) * (d^2 - r^2)^3, 0<=r<=d
W_poly6(r) =
0, othersize
'''
assert pos.shape == (self.simulator_base_dimension, self.particles_num)
radius = np.linalg.norm(pos, ord = 2, axis = 0) # radius means r
radius_2 = radius ** 2
d_2 = self.kernel_poly6_d ** 2
# compute the W_poly6(r)
W_poly6 = self.kernel_poly6_coeff * np.array([ (d_2 - radius_2[i]) ** 3 if radius[i]<= self.kernel_poly6_d else 0 for i in range(self.particles_num)])
assert W_poly6.shape == (self.particles_num, )
# return
return W_poly6
def W_poly6_1_order_gradient(self, pos):
'''
this function will compute
2d case:
dW_poly6_dxy =
∇W(|xi - xj|) = (∂x_1, ∂y_1
...
∂x_j, ∂y_j
...