-
Notifications
You must be signed in to change notification settings - Fork 0
/
we_functions.py
1216 lines (1118 loc) · 64 KB
/
we_functions.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 numpy as np
import os
import shutil
import copy
from scipy import special
from scipy.cluster.vq import kmeans2, ClusterError
import walker
import we_global_variables as gv
import we_check_state_function
import we_parameters as p
from sklearn.metrics import silhouette_score, silhouette_samples
from sklearn.metrics import silhouette_score, silhouette_samples
def calculate_distance_from_center(center, values):
distance = 0.0
for i in range(len(center)):
if gv.angle_cvs[i] == 0:
distance += (values[i] - center[i]) ** 2
else:
if values[i] - center[i] > 180.0:
distance += (values[i] - center[i] - 360.0) ** 2
elif values[i] - center[i] < -180.0:
distance += (values[i] - center[i] + 360.0) ** 2
else:
distance += (values[i] - center[i]) ** 2
if abs(distance) < 1.0e-10:
distance = 0.0
return np.sqrt(distance)
def set_parameters():
gv.main_directory = p.main_directory
gv.initial_configuration_directory = p.initial_configuration_directory
gv.simulation_flag = p.simulation_flag
gv.balls_flag = p.balls_flag
gv.sorting_flag = p.sorting_flag
gv.rate_flag = p.rate_flag
gv.num_states = p.num_states
gv.enhanced_sampling_flag = p.enhanced_sampling_flag
gv.num_balls_limit = p.num_balls_limit
gv.radius = p.radius
gv.num_walkers = p.num_walkers
gv.num_cvs = p.num_cvs
gv.lower_bound = p.lower_bound
gv.upper_bound = p.upper_bound
gv.angle_cvs = p.angle_cvs
gv.initial_step_num = p.initial_step_num
gv.max_num_steps = p.max_num_steps
gv.num_occupied_balls = p.num_occupied_balls
gv.first_walker = p.first_walker
gv.last_walker = p.last_walker
if gv.enhanced_sampling_flag == 2:
gv.less_or_greater_flag = p.less_or_greater_flag
gv.static_threshold_flag = p.static_threshold_flag
gv.threshold_values = p.threshold_values
gv.properties_to_keep_track = p.properties_to_keep_track
elif gv.enhanced_sampling_flag == 3:
gv.num_balls_for_sc = p.num_balls_for_sc
gv.num_clusters = p.num_clusters
gv.num_walkers_for_sc = p.num_walkers_for_sc
ball_volume = (np.pi ** (gv.num_cvs / 2) * gv.radius ** gv.num_cvs) / special.gamma((gv.num_cvs / 2) + 1)
gv.max_num_balls = 0
if ball_volume != 0.0:
gv.max_num_balls = int(np.floor((gv.upper_bound - gv.lower_bound) ** gv.num_cvs / ball_volume))
if gv.max_num_balls > gv.num_balls_limit or gv.max_num_balls < gv.num_balls_limit * 1e-2:
gv.max_num_balls = gv.num_balls_limit
print 'max # of balls (n_b) = ' + str(gv.max_num_balls)
gv.current_num_balls = 0
gv.total_num_walkers = gv.num_occupied_balls*gv.num_walkers
gv.total_num_walkers = gv.num_occupied_balls*gv.num_walkers
gv.num_occupied_clusters = 1
gv.sc_performed = 0
def initialize(input_initial_values_file, walker_list, temp_walker_list, balls, ball_to_walkers, vacant_walker_indices):
for i in range(len(walker_list)):
walker_list[i] = walker.Walker([-1000.0] * gv.num_cvs, [-1000.0] * gv.num_cvs, i, 0.0, [-1000.0] * gv.num_cvs,
[-1000.0] * gv.num_cvs, 0, 0.0, 0.0, 0, 0.0, -1)
if gv.simulation_flag == 0: # new simulation
initial_weight = 1.0/gv.total_num_walkers
f = open(input_initial_values_file, 'r')
for n in range(gv.num_occupied_balls):
initial_values = [None] * gv.num_cvs
for i in range(gv.num_cvs):
initial_values[i] = float(f.readline())
if gv.rate_flag == 1:
initial_state = we_check_state_function.check_state_function(initial_values)
print initial_state
for i in range(n * gv.num_walkers, (n + 1) * gv.num_walkers):
walker_list[i].set(initial_values, initial_weight)
if gv.rate_flag == 1:
walker_list[i].state = initial_state
f.close()
os.system('mkdir WE')
os.chdir(gv.main_directory + '/WE')
for i in range(gv.total_num_walkers):
walker_directory = gv.main_directory + '/WE/walker' + str(i)
shutil.copytree(gv.initial_configuration_directory, walker_directory)
elif gv.simulation_flag == 1: # restarting simulation in the middle of simulation
for i in range(gv.total_num_walkers):
walker_directory = gv.main_directory + '/WE/walker' + str(i)
os.chdir(walker_directory)
f = open('weight_trajectory.txt', 'r')
weight = float(f.readlines()[-1].strip())
walker_list[i].weight = weight
f.close()
trajectory = np.loadtxt('trajectory.txt')
if gv.num_cvs == 1:
previous_coordinates = [trajectory[-2]]
current_coordinates = [trajectory[-1]]
else:
previous_coordinates = trajectory[-2].tolist()
current_coordinates = trajectory[-1].tolist()
walker_list[i].previous_coordinates = previous_coordinates
walker_list[i].current_coordinates = current_coordinates
ball_trajectory = np.loadtxt('ball_trajectory.txt')
previous_ball_center = ball_trajectory[-2][0:gv.num_cvs].tolist()
current_ball_center = ball_trajectory[-1][0:gv.num_cvs].tolist()
current_ball_radius = ball_trajectory[-1][gv.num_cvs]
walker_list[i].previous_ball_center = previous_ball_center
walker_list[i].current_ball_center = current_ball_center
walker_list[i].radius = current_ball_radius
walker_list[i].previous_distance_from_center = calculate_distance_from_center(previous_coordinates,
previous_ball_center)
walker_list[i].current_distance_from_center = calculate_distance_from_center(current_coordinates,
current_ball_center)
if gv.rate_flag == 1:
walker_list[i].state = int(ball_trajectory[-1][-1])
if gv.balls_flag == 1:
os.chdir(gv.main_directory + '/WE')
balls = np.loadtxt('balls_' + str(gv.initial_step_num) + '.txt')
elif gv.simulation_flag == 2: # restarting simulation in the middle of binning
for i in range(gv.total_num_walkers):
walker_directory = gv.main_directory + '/WE/walker' + str(i)
os.chdir(walker_directory)
f = open('weight_trajectory.txt', 'r')
weight = float(f.readlines()[-1].strip())
walker_list[i].weight = weight
f.close()
trajectory = np.loadtxt('trajectory.txt')
previous_coordinates = trajectory[-2].tolist()
current_coordinates = trajectory[-1].tolist()
walker_list[i].previous_coordinates = previous_coordinates
walker_list[i].current_coordinates = current_coordinates
num_lines = sum(1 for line in open('ball_trajectory.txt'))
# if walker is already binned to a ball, delete the binning and have binning start from scratch
if num_lines > gv.initial_step_num:
os.system('sed -i \'$d\' ball_trajectory.txt')
ball_trajectory = np.loadtxt('ball_trajectory.txt')
previous_ball_center = ball_trajectory[-2][0:gv.num_cvs].tolist()
current_ball_center = ball_trajectory[-1][0:gv.num_cvs].tolist()
current_ball_radius = ball_trajectory[-1][gv.num_cvs]
walker_list[i].previous_ball_center = previous_ball_center
walker_list[i].current_ball_center = current_ball_center
walker_list[i].radius = current_ball_radius
walker_list[i].previous_distance_from_center = calculate_distance_from_center(previous_coordinates,
previous_ball_center)
walker_list[i].current_distance_from_center = calculate_distance_from_center(current_coordinates,
current_ball_center)
if gv.rate_flag == 1:
walker_list[i].state = int(ball_trajectory[-1][-1])
if gv.balls_flag == 1:
os.chdir(gv.main_directory + '/WE')
balls = np.loadtxt('balls_' + str(gv.initial_step_num) + '.txt')
elif gv.simulation_flag == 3: # restarting simulation in the middle of resampling
total_weight = 0.0
previous_ball_to_walkers = {}
previous_balls_weights = np.loadtxt('total_weight_of_each_ball_' + str(gv.initial_step_num) + '.txt')
previous_balls_walker_count = np.zeros((previous_balls_weights.shape[0], previous_balls_weights.shape[1]))
for i in range(previous_balls_weights.shape[0]):
previous_balls_walker_count[i] = previous_balls_weights[i]
previous_balls_walker_count[i][-1] = gv.num_walkers
# TODO: make sure that gv.num_occupied_balls is equal to the highest walker number inside the WE folder
for i in range(gv.num_occupied_balls + 1):
walker_directory = gv.main_directory + '/WE/walker' + str(i)
# if all of the files exist in the walker folder, it is a complete walker
if os.path.isfile(walker_directory + '/weight_trajectory.txt') and \
os.path.isfile(walker_directory + '/ball_trajectory.txt') and \
os.path.isfile(walker_directory + '/trajectory.txt') and \
os.path.isfile(walker_directory + '/traj.xtc') and os.path.isfile(walker_directory + '/minim.gro'):
os.chdir(walker_directory)
f = open('weight_trajectory.txt', 'r')
weight = float(f.readlines()[-1].strip())
walker_list[i].weight = weight
total_weight += weight
f.close()
ball_trajectory = np.loadtxt('ball_trajectory.txt')
previous_ball = ball_trajectory[-2].tolist()
previous_ball_key = previous_ball[gv.num_cvs+1]
previous_ball_center = previous_ball[0:gv.num_cvs]
previous_balls_weights[previous_ball_key][-1] -= weight
if previous_balls_weights[previous_ball_key][-1] < 0.0:
print 'ERROR: weight is ' + str(previous_balls_weights[previous_ball_key][-1]) + ' for walker ' + \
str(i) + ' with ball_key ' + str(previous_ball_key)
previous_balls_walker_count[previous_ball_key][-1] -= 1
if previous_balls_walker_count[previous_ball_key][-1] < 0:
print 'ERROR: walker count is ' + str(previous_balls_walker_count[previous_ball_key][-1]) + \
' for walker ' + str(i) + ' with ball key ' + str(previous_ball_key)
if tuple(previous_ball_center) in previous_ball_to_walkers:
previous_ball_to_walkers[tuple(previous_ball_center)].append(i)
else:
previous_ball_to_walkers[tuple(previous_ball_center)] = [i]
current_ball = ball_trajectory[-1].tolist()
current_ball_radius = current_ball[gv.num_cvs]
current_ball_key = current_ball[gv.num_cvs+1]
current_ball_center = current_ball[0:gv.num_cvs]
if tuple(current_ball_center) in ball_to_walkers:
ball_to_walkers[tuple(current_ball_center)].append(i)
else:
ball_to_walkers[tuple(current_ball_center)] = [i]
gv.current_num_balls += 1
walker_list[i].radius = current_ball_radius
walker_list[i].previous_ball_center = previous_ball_center
walker_list[i].current_ball_center = current_ball_center
trajectory = np.loadtxt('trajectory.txt')
previous_coordinates = trajectory[-2].tolist()
current_coordinates = trajectory[-1].tolist()
walker_list[i].previous_coordinates = previous_coordinates
walker_list[i].current_coordinates = current_coordinates
if gv.rate_flag == 1:
current_state = int(current_ball[-1])
else:
current_state = -1
walker_list[i].state = current_state
walker_list[i].ball_key = current_ball_key
previous_distance_from_center = calculate_distance_from_center(previous_coordinates, previous_ball_center)
current_distance_from_center = calculate_distance_from_center(current_coordinates, current_ball_center)
walker_list[i].previous_distance_from_center = previous_distance_from_center
walker_list[i].current_distance_from_center = current_distance_from_center
temp_walker_list[i] = walker.Walker(previous_coordinates, current_coordinates, i, current_ball_radius,
previous_ball_center, current_ball_center, current_ball_key,
previous_distance_from_center, current_distance_from_center, 0,
weight, current_state)
# otherwise, it is an incomplete walker that needs missing files
else:
if os.path.isdir(walker_directory):
os.chdir(gv.main_directory + '/WE')
os.system('rm -rf walker' + str(i))
vacant_walker_indices.append(i)
# create new walkers for the remaining weights
excess_index = gv.num_occupied_balls + 1
for i in range(previous_balls_weights.shape[0]):
if previous_balls_weights[i][-1] > 0.0:
if previous_balls_walker_count[i][-1] <= 0:
print 'ERROR: at least one walker should exist if there is a weight of ' + \
str(previous_balls_weights[i][-1]) + ' for walker ' + str(i)
else:
current_ball_center = previous_balls_weights[i][0:gv.num_cvs].tolist()
reference_walker = ball_to_walkers[tuple(current_ball_center)][0]
reference_walker_directory = gv.main_directory + '/WE/walker/' + str(reference_walker)
if len(vacant_walker_indices) > 0:
walker_index = vacant_walker_indices.pop(0)
else:
walker_index = excess_index
excess_index += 1
walker_directory = gv.main_directory + '/WE/walker' + str(walker_index)
shutil.copytree(reference_walker_directory, walker_directory)
weight = previous_balls_weights[i][-1]
previous_balls_weights[i][-1] -= weight
os.chdir(walker_directory)
f = open('weight_trajectory.txt', 'w')
f.write(str(weight) + '\n')
walker_list[walker_index].weight = weight
total_weight += weight
f.close()
ball_to_walkers[tuple(current_ball_center)].append(walker_index)
walker_list[walker_index].current_ball_center = current_ball_center
trajectory = np.loadtxt('trajectory.txt')
previous_coordinates = trajectory[-2].tolist()
current_coordinates = trajectory[-1].tolist()
walker_list[walker_index].previous_coordinates = previous_coordinates
walker_list[walker_index].current_coordinates = current_coordinates
ball_trajectory = np.loadtxt('ball_trajectory.txt')
previous_ball_center = ball_trajectory[-2][0:gv.num_cvs].tolist()
walker_list[i].previous_ball_center = previous_ball_center
current_state = ball_trajectory[-1][-1]
current_ball_key = ball_trajectory[-1][gv.num_cvs+1]
current_ball_radius = ball_trajectory[-1][gv.num_cvs]
walker_list[walker_index].state = current_state
walker_list[walker_index].ball_key = current_ball_key
walker_list[walker_index].radius = current_ball_radius
previous_distance_from_center = calculate_distance_from_center(previous_coordinates,
previous_ball_center)
current_distance_from_center = calculate_distance_from_center(current_coordinates,
current_ball_center)
walker_list[i].previous_distance_from_center = previous_distance_from_center
walker_list[i].current_distance_from_center = current_distance_from_center
temp_walker_list[walker_index] = walker.Walker(previous_coordinates, current_coordinates,
walker_index, current_ball_radius,
previous_ball_center, current_ball_center,
current_ball_key, previous_distance_from_center,
current_distance_from_center, 0, weight,
current_state)
# check if total weight is 1.0
if total_weight != 1.0:
print 'ERROR: total weight is ' + str(total_weight)
if gv.balls_flag == 1:
os.chdir(gv.main_directory + '/WE')
balls = np.loadtxt('balls_' + str(gv.initial_step_num) + '.txt')
return balls
def binning(step_num, walker_list, temp_walker_list, balls, ball_to_walkers, key_to_ball):
initial_weights = [walker_list[i].weight for i in range(gv.total_num_walkers)]
initial_weights_array = np.array(initial_weights)
flux = np.zeros((gv.num_states, gv.num_states))
if gv.sorting_flag == 1:
walker_indices = np.argsort(initial_weights_array) # sort walkers in ascending order based on their weights
else:
walker_indices = np.argsort(-initial_weights_array) # sort walkers in descending order based on their weights
start = 0 # indicates whether we are dealing with the very first walker or not
if gv.enhanced_sampling_flag == 2:
ref_walker = walker.Walker([-1000.0] * gv.num_cvs, [-1000.0] * gv.num_cvs, 0, 0.0, [-1000.0] * gv.num_cvs,
[-1000.0] * gv.num_cvs, 0, 0.0, 0.0, 0, 0.0, -1)
ref_walker_binning_value = len(gv.properties_to_keep_track)
ref_walker_properties_value = 0.0
if gv.static_threshold_flag == 0:
new_threshold_values = gv.threshold_values
for i in walker_indices:
# first, go to walker directory i
walker_directory = gv.main_directory + '/WE/walker' + str(i)
os.chdir(walker_directory)
# then, obtain new coordinates' values
if os.path.exists(walker_directory + '/coordinates.out'):
coordinates = np.loadtxt('coordinates.out')
if gv.num_cvs > 1:
new_coordinates = coordinates.tolist()
else:
new_coordinates = [float(coordinates)]
rm_command = 'rm -rf *.out'
os.system(rm_command)
# also, write the new coordinates' values on the trajectory file
if gv.enhanced_sampling_flag != 2:
f = open('trajectory.txt', 'a')
f.write(' '.join(str(coordinate) for coordinate in new_coordinates))
f.write('\n')
f.close()
else:
f = open('trajectory.txt', 'r')
new_coordinates = f.readlines()[-1].strip().split()
new_coordinates = [float(coordinate) for coordinate in new_coordinates]
f.close()
previous_coordinates = walker_list[i].current_coordinates
previous_ball_center = walker_list[i].current_ball_center
previous_distance_from_center = walker_list[i].current_distance_from_center
initial_step_num = walker_list[i].initial_step_num
weight = walker_list[i].weight
if gv.rate_flag == 1:
state = we_check_state_function.check_state_function(new_coordinates)
if walker_list[i].state != -1 and state == -1:
state = walker_list[i].state
if walker_list[i].state != -1 and state != -1:
flux[walker_list[i].state, state] += walker_list[i].weight
else:
state = -1
if gv.enhanced_sampling_flag == 2:
properties_to_keep_track = []
for k in range(len(gv.properties_to_keep_track)):
if gv.properties_to_keep_track[k] < 0:
properties_to_keep_track.append(weight)
else:
properties_to_keep_track.append(new_coordinates[gv.properties_to_keep_track[k]])
walker_binning_value = 0
walker_properties_value = 0.0
if gv.less_or_greater_flag == 0:
for m in range(len(gv.properties_to_keep_track)):
if properties_to_keep_track[m] < gv.threshold_values[m]:
walker_binning_value += 1
walker_properties_value += (gv.threshold_values[m]-properties_to_keep_track[m])
else:
for m in range(len(gv.properties_to_keep_track)):
if properties_to_keep_track[m] > gv.threshold_values[m]:
walker_binning_value += 1
walker_properties_value += (properties_to_keep_track[m]-gv.threshold_values[m])
inside = 0 # indicates whether we are dealing with the very first walker or not
# if we're dealing with the very first walker, create the very first ball for the walker
if (gv.balls_flag == 0 and start == 0) or (gv.balls_flag == 1 and start == 0 and step_num == 0):
start += 1
inside += 1
current_ball_center = [coordinate for coordinate in new_coordinates]
center_r_key_num = copy.deepcopy(current_ball_center)
center_r_key_num.append(gv.radius)
center_r_key_num.append(gv.current_num_balls)
center_r_key_num.append(1)
balls[gv.current_num_balls] = np.asarray(center_r_key_num)
ball_to_walkers[tuple(current_ball_center)] = [i]
key_to_ball[tuple(current_ball_center)] = gv.current_num_balls
temp_walker_list[i] = walker.Walker(previous_coordinates, new_coordinates, i, gv.radius,
previous_ball_center, current_ball_center, gv.current_num_balls,
previous_distance_from_center, 0.0, initial_step_num, weight, state)
if gv.enhanced_sampling_flag == 2:
ref_walker = walker.Walker(previous_coordinates, new_coordinates, i, gv.radius, previous_ball_center,
current_ball_center, gv.current_num_balls, previous_distance_from_center,
0.0, initial_step_num, weight, state)
ref_walker_binning_value = walker_binning_value
ref_walker_properties_value = walker_properties_value
gv.current_num_balls += 1
distance = 0.0
ball_key = 0
# otherwise, loop through all of the balls and find the ball that has a center nearest the walker
if inside == 0:
for j in range(balls.shape[0]):
current_ball_center = balls[j][0:gv.num_cvs].tolist()
distance_from_center = calculate_distance_from_center(current_ball_center, new_coordinates)
if distance_from_center <= gv.radius or abs(distance_from_center - gv.radius) < 1.0e-10:
inside += 1
if j == 0:
distance = distance_from_center
ball_key = j
else:
if distance_from_center < distance:
distance = distance_from_center
ball_key = j
# walker is inside some ball
if inside != 0:
balls[ball_key][gv.num_cvs+2] += 1
current_ball_center = balls[ball_key][0:gv.num_cvs].tolist()
ball_to_walkers[tuple(current_ball_center)].append(i)
distance_from_center = calculate_distance_from_center(current_ball_center, new_coordinates)
temp_walker_list[i] = walker.Walker(previous_coordinates, new_coordinates, i, gv.radius,
previous_ball_center, current_ball_center, ball_key,
previous_distance_from_center, distance_from_center,
initial_step_num, weight, state)
if gv.enhanced_sampling_flag == 2 and ((gv.balls_flag == 1 and start == 0) or (walker_binning_value == ref_walker_binning_value and walker_properties_value < ref_walker_properties_value) or walker_binning_value < ref_walker_binning_value):
ref_walker = walker.Walker(previous_coordinates, new_coordinates, i, gv.radius,
previous_ball_center, current_ball_center, ball_key,
previous_distance_from_center, distance_from_center, initial_step_num,
weight, state)
ref_walker_binning_value = walker_binning_value
ref_walker_properties_value = walker_properties_value
new_threshold_values = properties_to_keep_track
# or walker does not belong in any ball -> create a new ball
elif gv.enhanced_sampling_flag != 2:
current_ball_center = [coordinate for coordinate in new_coordinates]
center_r_key_num = copy.deepcopy(current_ball_center)
center_r_key_num.append(gv.radius)
center_r_key_num.append(gv.current_num_balls)
center_r_key_num.append(1)
balls = np.append(balls, [np.asarray(center_r_key_num)], axis=0)
ball_to_walkers[tuple(current_ball_center)] = [i]
key_to_ball[tuple(current_ball_center)] = gv.current_num_balls
temp_walker_list[i] = walker.Walker(previous_coordinates, new_coordinates, i, gv.radius,
previous_ball_center, current_ball_center, gv.current_num_balls,
previous_distance_from_center, 0.0, initial_step_num, weight, state)
gv.current_num_balls += 1
# or if enhanced_sampling_flag = 2 and ref_walker is a "better" walker in terms of its values -> replace
# walker with ref_walker and put the walker in ref_walker's ball
elif gv.enhanced_sampling_flag == 2 and ((walker_binning_value == ref_walker_binning_value and walker_properties_value > ref_walker_properties_value) or walker_binning_value > ref_walker_binning_value):
balls[ref_walker.ball_key][gv.num_cvs+2] += 1
temp_walker_list[i] = walker.Walker([-1000.0] * gv.num_cvs, [-1000.0] * gv.num_cvs, i, 0.0,
[-1000.0] * gv.num_cvs, [-1000.0] * gv.num_cvs, 0, 0.0, 0.0, 0, 0.0,
-1)
temp_walker_list[i].copy_walker(ref_walker)
temp_walker_list[i].weight = weight
current_ball_center = temp_walker_list[i].current_ball_center
ball_to_walkers[tuple(current_ball_center)].append(i)
# or if enhanced_sampling_flag = 2 and walker is a "better" or "equivalent" walker in terms of its values
# -> create a new ball
elif gv.enhanced_sampling_flag == 2 and ((gv.balls_flag == 1 and start == 0) or (walker_binning_value == ref_walker_binning_value and walker_properties_value <= ref_walker_properties_value) or walker_binning_value < ref_walker_binning_value):
current_ball_center = [coordinate for coordinate in new_coordinates]
center_r_key_num = copy.deepcopy(current_ball_center)
center_r_key_num.append(gv.radius)
center_r_key_num.append(gv.current_num_balls)
center_r_key_num.append(1)
balls = np.append(balls, [np.asarray(center_r_key_num)], axis=0)
ball_to_walkers[tuple(current_ball_center)] = [i]
key_to_ball[tuple(current_ball_center)] = gv.current_num_balls
temp_walker_list[i] = walker.Walker(previous_coordinates, new_coordinates, i, gv.radius,
previous_ball_center, current_ball_center, gv.current_num_balls,
previous_distance_from_center, 0.0, initial_step_num, weight, state)
ref_walker = walker.Walker(previous_coordinates, new_coordinates, i, gv.radius, previous_ball_center,
current_ball_center, gv.current_num_balls, previous_distance_from_center,
0.0, initial_step_num, weight, state)
ref_walker_binning_value = walker_binning_value
ref_walker_properties_value = walker_properties_value
new_threshold_values = properties_to_keep_track
gv.current_num_balls += 1
# finally, write the new ball on the trajectory file
if gv.enhanced_sampling_flag != 2:
current_ball_center = temp_walker_list[i].current_ball_center
ball_key = temp_walker_list[i].ball_key
center_r_key_state = copy.deepcopy(current_ball_center)
center_r_key_state.append(gv.radius)
center_r_key_state.append(ball_key)
center_r_key_state.append(state)
f = open('ball_trajectory.txt', 'a')
f.write(' '.join(map(lambda coordinate: str(coordinate), center_r_key_state)))
f.write('\n')
f.close()
# if enhanced_sampling_flag = 2, replace "inadequate" walkers with ref_walker
if gv.enhanced_sampling_flag == 2:
for i in walker_indices:
new_coordinates = temp_walker_list[i].current_coordinates
weight = temp_walker_list[i].weight
properties_to_keep_track = []
for k in range(len(gv.properties_to_keep_track)):
if gv.properties_to_keep_track[k] < 0:
properties_to_keep_track.append(weight)
else:
properties_to_keep_track.append(new_coordinates[gv.properties_to_keep_track[k]])
walker_binning_value = 0
walker_properties_value = 0.0
if gv.less_or_greater_flag == 0:
for m in range(len(gv.properties_to_keep_track)):
if properties_to_keep_track[m] < gv.threshold_values[m]:
walker_binning_value += 1
walker_properties_value += (gv.threshold_values[m]-properties_to_keep_track[m])
else:
for m in range(len(gv.properties_to_keep_track)):
if properties_to_keep_track[m] > gv.threshold_values[m]:
walker_binning_value += 1
walker_properties_value += (properties_to_keep_track[m]-gv.threshold_values[m])
if (walker_binning_value > ref_walker_binning_value or (walker_binning_value == ref_walker_binning_value and walker_properties_value > ref_walker_properties_value)):
previous_ball_center = temp_walker_list[i].current_ball_center
previous_ball_key = temp_walker_list[i].ball_key
balls[previous_ball_key][gv.num_cvs+2] -= 1
balls[ref_walker.ball_key][gv.num_cvs+2] += 1
temp_walker_list[i] = walker.Walker([-1000.0] * gv.num_cvs, [-1000.0] * gv.num_cvs, i, 0.0,
[-1000.0] * gv.num_cvs, [-1000.0] * gv.num_cvs, 0, 0.0, 0.0, 0, 0.0,
-1)
temp_walker_list[i].copy_walker(ref_walker)
temp_walker_list[i].weight = weight
ball_to_walkers[tuple(previous_ball_center)].remove(i)
current_ball_center = temp_walker_list[i].current_ball_center
ball_to_walkers[tuple(current_ball_center)].append(i)
current_ball_center = temp_walker_list[i].current_ball_center
ball_key = temp_walker_list[i].ball_key
center_r_key_state = copy.deepcopy(current_ball_center)
center_r_key_state.append(gv.radius)
center_r_key_state.append(ball_key)
center_r_key_state.append(state)
new_coordinates = temp_walker_list[i].current_coordinates
walker_directory = gv.main_directory + '/WE/walker' + str(i)
os.chdir(walker_directory)
f = open('ball_trajectory.txt', 'a')
f.write(' '.join(map(lambda coordinate: str(coordinate), center_r_key_state)))
f.write('\n')
f.close()
f = open('trajectory.txt', 'a')
f.write(' '.join(str(coordinate) for coordinate in new_coordinates))
f.write('\n')
f.close()
os.chdir(gv.main_directory + '/WE')
np.savetxt('balls_' + str(step_num + 1) + '.txt', balls, fmt=' %+1.5f')
if gv.rate_flag == 1:
np.savetxt('flux_' + str(step_num + 1) + '.txt', flux, fmt=' %1.5e')
# update threshold values if they are better
if gv.enhanced_sampling_flag == 2 and gv.static_threshold_flag == 0:
threshold_replace_value = 0
if gv.less_or_greater_flag == 0:
for m in range(len(gv.properties_to_keep_track)):
if new_threshold_values[m] > gv.threshold_values[m]:
threshold_replace_value += 1
else:
threshold_replace_value -= 1
else:
for m in range(len(gv.properties_to_keep_track)):
if new_threshold_values[m] < gv.threshold_values[m]:
threshold_replace_value += 1
else:
threshold_replace_value -= 1
if threshold_replace_value > 0:
gv.threshold_values = new_threshold_values
return balls
def delta2(c1, c2):
min_dist = np.inf
for i in xrange(0, len(c1)):
for j in xrange(0, len(c2)):
p1 = c1[i, :]
p2 = c2[j, :]
dist = np.sqrt(np.sum(np.square(p2 - p1)))
if dist < min_dist:
min_dist = dist
return min_dist
def delta1(c):
max_dist = 0
for i in xrange(0, len(c)):
for j in xrange(0, len(c)):
if i == j:
continue
p1 = c[i, :]
p2 = c[j, :]
dist = np.sqrt(np.sum(np.square(p2 - p1)))
if dist > max_dist:
max_dist = dist
return max_dist
def minDelta2(ball_coords):
column = ball_coords.shape[1]-1
num_clusters = int(np.max(ball_coords[:, column])+1)
min_delta2 = np.inf
for i in xrange(0, num_clusters):
for j in xrange(0, num_clusters):
if i == j:
continue
i = float(i)
j = float(j)
c1 = ball_coords[ball_coords[:, column] == i, :-1]
c2 = ball_coords[ball_coords[:, column] == j, :-1]
d2 = delta2(c1, c2)
if d2 < min_delta2:
min_delta2 = d2
return min_delta2
def maxDelta1(ball_coords):
column = ball_coords.shape[1]-1
num_clusters = int(np.max(ball_coords[:, column])+1)
max_delta1 = 0
for i in xrange(0,num_clusters):
i = float(i)
c1 = ball_coords[ball_coords[:, column] == i, :-1]
d1 = delta1(c1)
if d1 > max_delta1:
max_delta1 = d1
return max_delta1
def dunn(ball_coords):
return minDelta2(ball_coords)/maxDelta1(ball_coords)
def spectral_clustering(step_num, temp_walker_list, balls, ball_clusters_list):
transition_matrix = np.zeros((balls.shape[0], balls.shape[0]))
for i in range(gv.total_num_walkers):
previous_coordinates = temp_walker_list[i].previous_coordinates
previous_distance = 0.0
previous_ball_key = 0
for j in range(balls.shape[0]):
ball_center = balls[j][0:gv.num_cvs].tolist()
previous_distance_from_center = calculate_distance_from_center(ball_center, previous_coordinates)
if j == 0:
previous_distance = previous_distance_from_center
previous_ball_key = j
else:
if previous_distance_from_center < previous_distance:
previous_distance = previous_distance_from_center
previous_ball_key = j
transition_matrix[previous_ball_key][temp_walker_list[i].ball_key] += temp_walker_list[i].weight
# transition matrix should fulfill detailed balance if simulation is run under Hamiltonian dynamics in the
# canonical ensemble. equation is from Prinz, et al JCP (2011).
new_transition_matrix = np.zeros((balls.shape[0], balls.shape[0]))
for i in range(new_transition_matrix.shape[0]):
for j in range(new_transition_matrix.shape[1]):
new_transition_matrix[i][j] = transition_matrix[i][j] + transition_matrix[j][i]
for i in range(new_transition_matrix.shape[0]):
row_sum = np.sum(new_transition_matrix, axis=1)
if row_sum[i] != 0.0:
new_transition_matrix[i, :] /= row_sum[i]
os.chdir(gv.main_directory + '/WE')
np.savetxt('transition_matrix_' + str(step_num + 1) + '.txt', new_transition_matrix, fmt=' %1.10e')
'''
evalues, evectors = np.linalg.eig(new_transition_matrix.T)
idx = abs(evalues).argsort()[::-1]
evectors = evectors[:, idx]
eq_vector = abs(np.real(evectors[:, 0]))
eq_vec_diag_matrix = np.diag(eq_vector)
inv_eq_vec_diag_matrix = np.zeros((eq_vec_diag_matrix.shape[0], eq_vec_diag_matrix.shape[0]))
for i in range(inv_eq_vec_diag_matrix.shape[0]):
if eq_vec_diag_matrix[i][i] != 0.0:
inv_eq_vec_diag_matrix[i][i] = 1.0 / eq_vec_diag_matrix[i][i]
symmetric_transition_matrix = np.dot(np.sqrt(eq_vec_diag_matrix),
np.dot(new_transition_matrix, np.sqrt(inv_eq_vec_diag_matrix)))
'''
evalues, evectors = np.linalg.eig(new_transition_matrix.T)
idx = abs(evalues).argsort()[::-1]
evalues = evalues[idx]
final_evalues = np.real(evalues)
evectors = evectors[:, idx]
final_evectors = np.real(evectors)
np.savetxt('evalues_' + str(step_num + 1) + '.txt', final_evalues, fmt=' %1.10e')
np.savetxt('evectors_' + str(step_num + 1) + '.txt', final_evectors, fmt=' %1.10e')
num_clusters = gv.num_clusters
normalized_second_evector = np.zeros((final_evectors.shape[0], 1))
for i in range(final_evectors.shape[0]):
if final_evectors[i, 0] != 0.0:
normalized_second_evector[i] = final_evectors[i, 1] / abs(final_evectors[i, 0])
else:
normalized_second_evector[i] = 0.0
'''
sorted_second_evector = np.sort(second_evector, axis=0)
second_evector_order = np.ndarray.argsort(second_evector)
num_balls = int(np.ceil(len(sorted_second_evector) / num_clusters))
array_of_clusters = [sorted_second_evector[i:i + num_balls] for i in
range(0, len(sorted_second_evector), num_balls)]
array_of_orderings = [second_evector_order[i:i + num_balls] for i in range(0, len(second_evector_order), num_balls)]
num_clusters = len(array_of_clusters)
'''
matrix = np.hstack((balls, normalized_second_evector))
while True:
try:
centroids, labels = kmeans2(matrix, num_clusters, minit='points', iter=100, missing='raise')
break
except ClusterError:
num_clusters -= 1
with open('dunn_index_' + str(step_num + 1) + '.txt', 'w') as dunn_index_f:
labeled_matrix = np.zeros((matrix.shape[0], matrix.shape[1] + 1))
labeled_matrix[:, 0:matrix.shape[1]] = matrix
labeled_matrix[:, matrix.shape[1]] = labels
if len(labels) > 1:
silhouette_avg = silhouette_score(matrix, labels)
sample_silhouette_values = silhouette_samples(matrix, labels)
else:
sample_silhouette_values = [-1] * len(labels)
print >>dunn_index_f, "The average silhouette_score is: %f" % silhouette_avg
for i in xrange(int(max(labels))+1):
print >>dunn_index_f, "The average silhouette score for cluster %d is: %f" % (i, np.mean(sample_silhouette_values[labels == i]))
f = open('ball_clustering_' + str(step_num + 1) + '.txt', 'w')
'''
for i in range(num_clusters):
first = 0
cluster = array_of_clusters[i]
ordering = array_of_orderings[i]
for j in range(cluster.shape[0]):
if first == 0:
first += 1
ref_ball_center = balls[ordering[j], 0:gv.num_cvs].tolist()
ball_cluster = copy.deepcopy(ref_ball_center)
ball_cluster.append(i)
ball_cluster.append(abs(final_evectors[ordering[j], 0]))
ball_cluster.append(second_evector[ordering[j]])
ball_cluster.append(final_evectors[ordering[j], 2])
f.write(' '.join(map(lambda coordinate: str(coordinate), ball_cluster)))
f.write('\n')
ball_clusters_list[tuple(ref_ball_center)] = [tuple(ref_ball_center)]
else:
ball_center = balls[ordering[j], 0:gv.num_cvs].tolist()
ball_cluster = copy.deepcopy(ball_center)
ball_cluster.append(i)
ball_cluster.append(abs(final_evectors[ordering[j], 0]))
ball_cluster.append(second_evector[ordering[j]])
ball_cluster.append(final_evectors[ordering[j], 2])
f.write(' '.join(map(lambda coordinate: str(coordinate), ball_cluster)))
f.write('\n')
ball_clusters_list[tuple(ref_ball_center)].append(tuple(ball_center))
'''
for i in range(num_clusters):
first = 0
for j in range(balls.shape[0]):
if labels[j] == i and first == 0:
first += 1
ref_ball_center = balls[j, 0:gv.num_cvs].tolist()
ball_cluster = copy.deepcopy(ref_ball_center)
ball_cluster.append(i)
ball_cluster.append(abs(final_evectors[j, 0]))
ball_cluster.append(final_evectors[j, 1])
ball_cluster.append(final_evectors[j, 2])
f.write(' '.join(map(lambda coordinate: str(coordinate), ball_cluster)))
f.write('\n')
ball_clusters_list[tuple(ref_ball_center)] = [tuple(ref_ball_center)]
balls[j][gv.num_cvs+2] -= 1
elif labels[j] == i and first != 0:
ball_center = balls[j, 0:gv.num_cvs].tolist()
ball_cluster = copy.deepcopy(ball_center)
ball_cluster.append(i)
ball_cluster.append(abs(final_evectors[j, 0]))
ball_cluster.append(final_evectors[j, 1])
ball_cluster.append(final_evectors[j, 2])
f.write(' '.join(map(lambda coordinate: str(coordinate), ball_cluster)))
f.write('\n')
ball_clusters_list[tuple(ref_ball_center)].append(tuple(ball_center))
balls[j][gv.num_cvs+2] -= 1
f.close()
def resampling_for_sc(walker_list, temp_walker_list, balls, ball_to_walkers, ball_clusters_list, vacant_walker_indices):
gv.sc_performed = 1
gv.num_occupied_clusters = 1
num_occupied_clusters = 0
num_occupied_balls = 0
weights = [walker_list[i].weight for i in range(gv.total_num_walkers)]
occupied_indices = np.zeros(gv.max_num_balls*gv.num_walkers_for_sc, int)
excess_index = gv.total_num_walkers
for current_cluster in ball_clusters_list:
if len(ball_clusters_list[current_cluster]) > 0:
num_occupied_clusters += 1
num_bins = len(ball_clusters_list[current_cluster])
bins = []
leftover_bins = []
if num_bins > gv.num_walkers_for_sc:
num_bins = gv.num_walkers_for_sc
bin_indices = np.zeros((len(ball_clusters_list[current_cluster]), 1))
while len(bins) < num_bins:
bin_index = np.random.randint(0, len(ball_clusters_list[current_cluster]))
if bin_indices[bin_index] == 0:
bin_indices[bin_index] = 1
bins.append(ball_clusters_list[current_cluster][bin_index])
for i in range(len(ball_clusters_list[current_cluster])):
if bin_indices[i] == 0:
leftover_bins.append(ball_clusters_list[current_cluster][i])
else:
bins = ball_clusters_list[current_cluster]
num_occupied_balls += num_bins
target_num_walkers = int(np.floor(float(gv.num_walkers_for_sc) / num_bins))
remainder = gv.num_walkers_for_sc - target_num_walkers * num_bins
for b, ball_center in enumerate(bins):
new_weights = []
new_indices = []
new_num_walkers = 0
# add the remaining walkers to the very last bin if there are any
if remainder != 0 and b == num_bins - 1:
target_num_walkers += remainder
weights_bin = []
indices_bin = []
for walker_index in ball_to_walkers[ball_center]:
weights_bin.append(temp_walker_list[walker_index].weight)
indices_bin.append(temp_walker_list[walker_index].global_index)
# reset ball_to_walkers
ball_to_walkers[ball_center] = []
if b == num_bins - 1:
if len(leftover_bins) > 0:
for i in range(len(leftover_bins)):
ball = leftover_bins[i]
for walker_index in ball_to_walkers[ball]:
weights_bin.append(temp_walker_list[walker_index].weight)
indices_bin.append(temp_walker_list[walker_index].global_index)
# reset ball_to_walkers
ball_to_walkers[ball] = []
weights_array = np.array(weights_bin)
walker_indices = np.argsort(-weights_array)
temp_indices = indices_bin
# sorted indices based on descending order of weights
indices_bin = [temp_indices[i] for i in walker_indices]
total_weight = np.sum(weights_bin)
target_weight = total_weight/target_num_walkers
x = indices_bin.pop()
while True:
x_weight = weights[x]
if x_weight >= target_weight or len(indices_bin) == 0:
r = max(1, int(np.floor(x_weight/target_weight)))
r = min(r, target_num_walkers-new_num_walkers)
new_num_walkers += r
for item in np.repeat(x, r):
new_indices.append(item)
new_weights.append(target_weight)
if new_num_walkers < target_num_walkers and x_weight-r*target_weight > 0.0:
indices_bin.append(x)
weights[x] = x_weight-r*target_weight
if len(indices_bin) > 0:
x = indices_bin.pop()
else:
break
else:
y = indices_bin.pop()
y_weight = weights[y]
xy_weight = x_weight+y_weight
p = np.random.random()
# swap x and y
if p < y_weight/xy_weight:
temp = x
x = y
y = temp
weights[x] = xy_weight
if y not in new_indices:
vacant_walker_indices.append(y)
# remove walker y directory
os.chdir(gv.main_directory + '/WE')
os.system('rm -rf walker' + str(y))
for ni, global_index in enumerate(new_indices):
if occupied_indices[global_index] == 0:
occupied_indices[global_index] = 1
walker_list[global_index].copy_walker(temp_walker_list[global_index])
walker_list[global_index].weight = new_weights[ni]
ball_key = walker_list[global_index].ball_key
balls[ball_key][gv.num_cvs+2] += 1
ball_to_walkers[ball_center].append(global_index)
directory = gv.main_directory + '/WE/walker' + str(global_index)
os.chdir(directory)
# write new weights on the trajectory file
f = open('weight_trajectory.txt', 'a')
f.write('% 1.20e' % new_weights[ni] + '\n')
f.close()
else:
if len(vacant_walker_indices) > 0:
new_index = vacant_walker_indices.pop()
else:
new_index = excess_index
excess_index += 1
occupied_indices[new_index] = 1
walker_list[new_index].copy_walker(walker_list[global_index])
ball_key = walker_list[global_index].ball_key
balls[ball_key][gv.num_cvs+2] += 1
ball_to_walkers[ball_center].append(new_index)
old_directory = gv.main_directory + '/WE/walker' + str(global_index)
new_directory = gv.main_directory + '/WE/walker' + str(new_index)
shutil.copytree(old_directory, new_directory)
os.chdir(new_directory)
# write new weights on the trajectory file
f = open('weight_trajectory.txt', 'a')
f.write('% 1.20e' % walker_list[new_index].weight + '\n')
f.close()
if excess_index - num_occupied_clusters*gv.num_walkers_for_sc != len(vacant_walker_indices):
print 'Something wrong with resampling'
if num_occupied_clusters*gv.num_walkers_for_sc >= gv.total_num_walkers:
for i in range(num_occupied_clusters*gv.num_walkers_for_sc, excess_index):
new_index = vacant_walker_indices.pop()
occupied_indices[new_index] = 1
walker_list[new_index].copy_walker(walker_list[i])
# rename the directory with name 'i' to 'new_index'
os.chdir(gv.main_directory + '/WE')
os.system('mv walker' + str(i) + ' walker' + str(new_index))
else:
for i in range(gv.total_num_walkers, excess_index):
new_index = vacant_walker_indices.pop()
occupied_indices[new_index] = 1
walker_list[new_index].copy_walker(walker_list[i])
# rename the directory with name 'i' to 'new_index'
os.chdir(gv.main_directory + '/WE')
os.system('mv walker' + str(i) + ' walker' + str(new_index))
for i in range(num_occupied_clusters*gv.num_walkers_for_sc, gv.total_num_walkers):
if occupied_indices[i] == 1:
new_index = vacant_walker_indices.pop()
while new_index >= num_occupied_clusters*gv.num_walkers_for_sc:
new_index = vacant_walker_indices.pop()
occupied_indices[new_index] = 1
walker_list[new_index].copy_walker(walker_list[i])
# rename the directory with name 'i' to 'new_index'
os.chdir(gv.main_directory + '/WE')
os.system('mv walker' + str(i) + ' walker' + str(new_index))
while len(vacant_walker_indices) > 0:
vacant_walker_indices.pop()
gv.num_occupied_balls = num_occupied_balls
gv.total_num_walkers = num_occupied_clusters*gv.num_walkers_for_sc
gv.num_occupied_clusters = num_occupied_clusters
def resampling(walker_list, temp_walker_list, balls, ball_to_walkers, vacant_walker_indices):
gv.sc_performed = 0
gv.num_occupied_clusters = 1
num_occupied_balls = 0
weights = [walker_list[i].weight for i in range(gv.total_num_walkers)]
occupied_indices = np.zeros(gv.max_num_balls*gv.num_walkers, int)
excess_index = gv.total_num_walkers
for current_ball in range(balls.shape[0]):
if int(balls[current_ball][gv.num_cvs+2]) > 0:
num_occupied_balls += 1