-
Notifications
You must be signed in to change notification settings - Fork 0
/
2D-CVM_defined-patterns_analytic-solution_2017-12-08_rev_2018-11-22.py
3450 lines (2676 loc) · 149 KB
/
2D-CVM_defined-patterns_analytic-solution_2017-12-08_rev_2018-11-22.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
# -*- coding: utf-8 -*-
####################################################################################################
# Alianna J. Maren
# Computing configuration variables for the Cluster Variation Method
####################################################################################################
# Import the following Python packages
import random
import itertools
import numpy as np
import pylab
import matplotlib
from math import exp
from math import log
from matplotlib import pyplot as plt
from random import randrange, uniform #(not sure this is needed, since I'm importing random)
####################################################################################################
####################################################################################################
#
# Detailed code documentation is JUST ABOVE main(), at the very end of this program.
#
####################################################################################################
####################################################################################################
#
#
# This specific version of the code computes a randomly-generated distribution of x1 / x2 values,
# dependent on the h-parameter. Then, it computes the configuration variables for the grid. Based on
# these, it then computes the entropy, enthalpy, and free energy values.
#
# The crucial equations are as follows (taken from AJM's 2014 paper, "The Cluster Variation Method II:
# 2-D Grid of Zigzag Chains":
# h = exp(beta*epsilon/4) & lambda = 0 (Beginning of Appendix B, replicating Eqn. 2-16.)
# We can set beta = Boltzmann's constant = 1.
# Thus, eps1 = epsilon = 4*log(h)
# For the equilibrium case (which is where we have an analytic solution), eps0 = 0.
# Thus, x1 = x2 = 0; h controls the distribution among the z, w, & y values.
# At equilibrium, when eps1 = 0, z1 = z6 = z3 = z4 = 0.125; z2 = z5 = 0.25 (due to degeneracy).
#
# y1 = z1 + 0.5*(0.5 - z1 - z3)
# y2 = z3 + 0.5*(0.5 - z1 - z3)
# z3 = (h*h - 3.0)*(h*h + 1.0)/(8.0*(h*h*h*h - 6.0*h*h + 1.0)) App. B, Eqn. 29
# z1 = (1.0 - 3.0*h*h)*(h*h + 1.0)/(8.0*(h*h*h*h - 6.0*h*h + 1.0)) App. B, Eqn. 30
#
#
####################################################################################################
####################################################################################################
#
# Procedure to welcome the user and identify the code
#
####################################################################################################
####################################################################################################
def welcome ():
print()
print()
print()
print()
print()
print()
print( '******************************************************************************')
print()
print( 'Welcome to the 2-D Cluster Variation Method')
print( 'Version 1.2, 01/07/2018, A.J. Maren')
print( ' and updated 11/22/2018, by A.J. Maren')
print( 'This version computes the behavior of a perturbed unit_array,')
print( ' based on minimizing the free energy both before and after perturbation.')
print()
print( 'By changing parameters in the main code, the user can select:' )
print( ' (O) Randomly generating (and then improving) an array, or' )
print( ' (1 .. N) Selecting a pre-stored array' )
print()
print( 'For comments, questions, or bug-fixes, contact: alianna.maren@northwestern.edu')
print( 'Alternate email address: alianna@aliannajmaren.com')
print()
print( ' NOTE: In these calculations, x1 = A (units are at value 1),')
print( ' and x2 = B (units are at value 0).')
print()
print( '******************************************************************************')
print()
return()
####################################################################################################
####################################################################################################
#
# Function to obtain the array size specifications (currently DEFINED for the user; not a choice)
#
# Note: The code is ONLY set up to work with a grid consisting of an EVEN number of rows
#
####################################################################################################
####################################################################################################
def obtain_array_size_specs ():
# x = input('Enter array_length: ')
# array_length = int(x)
# print 'array_length is', array_length
# x = input('Enter layers: ')
# layers = int(x)
# print 'layers is', layers
# NOTE: The system is designed to work with an even number of rows, e.g. layers must be an even number
array_length = 16
layers = 16
array_size_list = (array_length, layers)
return (array_size_list)
# ************************************************************************************************ #
#
# Pattern Storage
#
# This program allows the user to access various pre-stored 16x16 patterns, exemplifying:
# - Scale-free
# - Rich club
# - and potentially other topologies.
#
# I'm going to allow the user to select a specified pattern from a pattern-selection module
# (still to be written) that will be called from __main__
#
# Since grid size is pre-determined (16x16), each pattern is called by specifiying individual rows.
#
# ************************************************************************************************ #
####################################################################################################
####################################################################################################
#
# Function to obtain the choice of a randomly-generated pattern or select a prestored pattern
#
####################################################################################################
####################################################################################################
def obtain_pattern_selection():
pattern_select = 0
return(pattern_select)
####################################################################################################
####################################################################################################
#
# Function to obtain a row of 2-D CVM data - PRESTORED pattern (currently part of a 16x16 grid)
#
####################################################################################################
####################################################################################################
def obtainGridRow (rowNum, patternSelect, h):
# Note: This is some vestigial data, from eary development stages
# This 4x8 pattern corresponds to an illustration in an early paper.
#
# Note: 4 rows of 8 units each - this is the small-scale, 2-D equilibrium test case
# rowArray0 = [1,1,1,0,1,1,1,0] # Row 0 - top row
# rowArray1 = [1,0,0,0,1,0,0,0] # Row 1 - second row (counting down from the top)
# rowArray2 = [1,1,1,0,1,1,1,0] # Row 2 - third row (counting down from the top)
# rowArray3 = [1,0,0,0,1,0,0,0] # Row 3 - fourth row (counting down from the top)
# Note: 16 rows of 16 units each - this is the 2-D scale-free equilibrium test case
# Equilibrium scale-free; rows 0 - 7
if patternSelect == 2:
rowArray0 = [1,0,0,0,1,0,0,1, 0,0,1,1,1,1,0,0] # Row 0 - top row
rowArray1 = [1,0,1,0,0,0,1,1, 0,1,1,1,0,0,1,0] # Row 1 - second row (counting down from the top)
rowArray2 = [0,0,1,1,0,1,0,1, 1,0,1,0,0,1,1,1] # Row 2 - third row (counting down from the top)
rowArray3 = [1,0,1,0,1,1,0,0, 1,0,0,0,1,1,1,0] # Row 3 - fourth row (counting down from the top)
rowArray4 = [1,1,0,0,0,1,0,1, 0,0,0,1,1,1,1,0] # Row 4 - fifth row (counting down from the top)
rowArray5 = [1,1,0,1,0,0,1,1, 1,0,0,1,1,1,0,0] # Row 5 - sixth row (counting down from the top)
rowArray6 = [1,1,0,1,0,0,0,1, 1,0,1,0,1,1,1,0] # Row 6 - seventh row (counting down from the top)
rowArray7 = [1,0,1,1,0,1,0,0, 0,1,1,0,0,0,0,0] # Row 7 - eighth row (counting down from the top)
# Rich Club
if patternSelect == 2:
rowArray0 = [1,1,1,1,0,0,0,0, 0,0,0,1,1,1,1,1] # Row 0 - top row
rowArray1 = [1,1,0,0,0,0,0,0, 0,1,1,1,1,1,1,1] # Row 1 - second row (counting down from the top)
rowArray2 = [1,1,1,0,0,0,0,0, 0,1,1,1,1,1,1,1] # Row 2 - third row (counting down from the top)
rowArray3 = [1,1,0,0,0,0,0,0, 0,1,1,1,1,1,1,1] # Row 3 - fourth row (counting down from the top)
rowArray4 = [1,1,0,0,0,0,0,0, 0,0,0,1,1,1,1,1] # Row 4 - fifth row (counting down from the top)
rowArray5 = [1,1,0,0,0,0,0,0, 0,0,0,1,1,1,1,1] # Row 5 - sixth row (counting down from the top)
rowArray6 = [1,1,1,0,0,0,0,0, 0,0,0,0,1,1,1,1] # Row 6 - seventh row (counting down from the top)
rowArray7 = [1,1,1,0,0,0,0,0, 0,0,0,0,0,1,1,1] # Row 7 - eighth row (counting down from the top)
# Non-equilibrium scale-free; rows 1 - 8 (9 - 16 in graph)
# rowArray0 = [1,0,0,0,1,0,0,0, 0,0,1,1,1,1,0,0] # Row 0 - top row
# rowArray1 = [1,0,1,0,0,0,0,0, 0,1,1,1,0,0,0,0] # Row 1 - second row (counting down from the top)
# rowArray2 = [0,0,1,1,0,1,0,0, 0,0,1,0,0,1,0,0] # Row 2 - third row (counting down from the top)
# rowArray3 = [1,0,1,0,1,1,0,0, 0,0,0,0,1,1,0,0] # Row 3 - fourth row (counting down from the top)
# rowArray4 = [1,1,0,0,0,1,0,1, 0,0,0,0,0,1,1,0] # Row 4 - fifth row (counting down from the top)
# rowArray5 = [1,1,0,1,0,0,1,1, 1,0,0,0,0,0,1,0] # Row 5 - sixth row (counting down from the top)
# rowArray6 = [0,0,0,1,0,0,0,1, 1,0,1,0,0,0,0,0] # Row 6 - seventh row (counting down from the top)
# rowArray7 = [0,0,1,1,0,0,0,0, 0,1,1,0,0,0,0,0] # Row 7 - eighth row (counting down from the top)
# Second non-equilibrium scale-free set (two side clusters removed); rows 1 - 8 (9 - 16 in graph)
# rowArray0 = [1,0,0,0,1,0,0,0, 0,0,1,1,1,1,0,0] # Row 0 - top row
# rowArray1 = [1,0,1,0,0,0,0,0, 0,1,1,1,0,0,0,0] # Row 1 - second row (counting down from the top)
# rowArray2 = [0,0,1,1,0,1,0,0, 0,0,1,0,0,1,0,0] # Row 2 - third row (counting down from the top)
# rowArray3 = [0,0,1,0,1,1,0,0, 0,0,0,0,1,1,0,0] # Row 3 - fourth row (counting down from the top)
# rowArray4 = [0,0,0,0,0,1,0,1, 0,0,0,0,0,1,1,0] # Row 4 - fifth row (counting down from the top)
# rowArray5 = [0,0,0,1,0,0,1,1, 1,0,0,0,0,0,1,0] # Row 5 - sixth row (counting down from the top)
# rowArray6 = [1,1,0,1,0,0,0,1, 1,0,1,0,0,0,0,0] # Row 6 - seventh row (counting down from the top)
# rowArray7 = [1,0,1,1,0,0,0,0, 0,1,1,0,0,0,0,0] # Row 7 - eighth row (counting down from the top)
# Equilibrium scale-free; rows 8 - 15
# rowArray8 = [0,0,0,0,0,1,1,0, 0,0,1,0,1,1,0,1] # Row 0 - ninth row
# rowArray9 = [0,1,1,1,0,1,0,1, 1,0,0,0,1,0,1,1] # Row 1 - tenth row (counting down from the top)
# rowArray10 = [0,0,1,1,1,0,0,1, 1,1,0,0,1,0,1,1] # Row 2 - eleventh row (counting down from the top)
# rowArray11 = [0,1,1,1,1,0,0,0, 1,0,1,0,0,0,1,1] # Row 3 - twelfth row (counting down from the top)
# rowArray12 = [0,1,1,1,0,0,0,1, 0,0,1,1,0,1,0,1] # Row 4 - thirteenth row (counting down from the top)
# rowArray13 = [1,1,1,0,0,1,0,1, 1,0,1,0,1,1,0,0] # Row 5 - fourteenth row (counting down from the top)
# rowArray14 = [0,1,0,0,1,1,1,0, 1,1,0,0,0,1,0,1] # Row 6 - fifteenth row (counting down from the top)
# rowArray15 = [0,0,1,1,1,1,0,0, 1,0,0,1,0,0,0,1] # Row 7 - sixteenth row (counting down from the top)
# Rich Club
rowArray8 = [1,1,1,0,0,0,0,0, 0,0,0,0,0,1,1,1] # Row 0 - ninth row
rowArray9 = [1,1,1,1,0,0,0,0, 0,0,0,0,0,1,1,1] # Row 1 - tenth row (counting down from the top)
rowArray10 = [1,1,1,1,1,0,0,0, 0,0,0,0,0,0,1,1] # Row 2 - eleventh row (counting down from the top)
rowArray11 = [1,1,1,1,1,0,0,0, 0,0,0,0,0,0,1,1] # Row 3 - twelfth row (counting down from the top)
rowArray12 = [1,1,1,1,1,1,1,0, 0,0,0,0,0,0,1,1] # Row 4 - thirteenth row (counting down from the top)
rowArray13 = [1,1,1,1,1,1,1,0, 0,0,0,0,0,1,1,1] # Row 5 - fourteenth row (counting down from the top)
rowArray14 = [1,1,1,1,1,1,1,0, 0,0,0,0,0,0,1,1] # Row 6 - fifteenth row (counting down from the top)
rowArray15 = [1,1,1,1,1,0,0,0, 0,0,0,0,1,1,1,1] # Row 7 - sixteenth row (counting down from the top)
# Non-equilibrium scale-free; rows 9 - 16 (1 - 8 in graph) -- total number of A units reduced by 17 (out of 256)
# rowArray8 = [0,0,0,0,0,1,0,0, 0,0,1,0,1,1,0,1] # Row 0 - ninth row
# rowArray9 = [0,0,0,0,1,1,0,0, 0,0,0,0,1,0,1,1] # Row 1 - tenth row (counting down from the top)
# rowArray10 = [0,1,0,0,0,1,0,1, 1,0,0,0,1,0,1,1] # Row 2 - eleventh row (counting down from the top)
# rowArray11 = [0,1,1,0,0,0,1,1, 1,0,1,0,0,0,1,1] # Row 3 - twelfth row (counting down from the top)
# rowArray12 = [0,0,1,1,0,0,0,0, 1,0,1,1,0,1,0,1] # Row 4 - thirteenth row (counting down from the top)
# rowArray13 = [0,0,1,0,0,1,0,0, 0,0,1,0,1,1,0,0] # Row 5 - fourteenth row (counting down from the top)
# rowArray14 = [0,0,0,0,1,1,1,0, 0,0,0,0,0,1,0,1] # Row 6 - fifteenth row (counting down from the top)
# rowArray15 = [0,0,1,1,1,1,0,0, 1,0,0,1,0,0,0,1] # Row 7 - sixteenth row (counting down from the top)
# # Second non-equilibrium scale-free set (two side clusters removed)
# rowArray8 = [0,0,0,0,0,1,0,0, 0,0,1,0,1,1,0,0] # Row 0 - ninth row
# rowArray9 = [0,0,0,0,1,1,0,0, 0,0,0,0,1,0,0,0] # Row 1 - tenth row (counting down from the top)
# rowArray10 = [0,1,0,0,0,1,0,1, 1,0,0,0,1,0,0,0] # Row 2 - eleventh row (counting down from the top)
# rowArray11 = [0,1,1,0,0,0,1,1, 1,0,1,0,0,0,0,0] # Row 3 - twelfth row (counting down from the top)
# rowArray12 = [0,0,1,1,0,0,0,0, 1,0,1,1,0,1,0,0] # Row 4 - thirteenth row (counting down from the top)
# rowArray13 = [0,0,1,0,0,1,0,0, 0,0,1,0,1,1,0,0] # Row 5 - fourteenth row (counting down from the top)
# rowArray14 = [0,0,0,0,1,1,1,0, 0,0,0,0,0,1,0,1] # Row 6 - fifteenth row (counting down from the top)
# rowArray15 = [0,0,1,1,1,1,0,0, 1,0,0,1,0,0,0,1] # Row 7 - sixteenth row (counting down from the top)
if rowNum == 0: rowArray = rowArray0
if rowNum == 1: rowArray = rowArray1
if rowNum == 2: rowArray = rowArray2
if rowNum == 3: rowArray = rowArray3
if rowNum == 4: rowArray = rowArray4
if rowNum == 5: rowArray = rowArray5
if rowNum == 6: rowArray = rowArray6
if rowNum == 7: rowArray = rowArray7
if rowNum == 8: rowArray = rowArray8
if rowNum == 9: rowArray = rowArray9
if rowNum == 10: rowArray = rowArray10
if rowNum == 11: rowArray = rowArray11
if rowNum == 12: rowArray = rowArray12
if rowNum == 13: rowArray = rowArray13
if rowNum == 14: rowArray = rowArray14
if rowNum == 15: rowArray = rowArray15
return (rowArray)
####################################################################################################
#
# Procedure to print out the 2-D CVM grid size specifications
#
####################################################################################################
def print_debug_status (debug_print_off):
if not debug_print_off:
print()
print( 'Debug printing is on') #debug_print_off false
else:
print( 'Debug printing is off') #debug_print_off true
print()
print ('------------------------------------------------------------------------------')
print()
return
####################################################################################################
#
# Procedure to print out the 2-D CVM grid size specifications
#
####################################################################################################
def print_grid_size_specs ():
print()
print( 'Grid size specifications: ')
print( ' This 2-D CVM process works with a matrix of M x L units, where:' )
print( ' M (the array_length is):', array_length )
print( ' L (the layers is): ', array_layers )
print()
print ('------------------------------------------------------------------------------')
print()
return
####################################################################################################
#
# Procedure to print the parameters for the run
#
####################################################################################################
def print_run_parameters (h0, h_incr, h_range):
print()
print( ' The parameters for this run are: ')
print( ' Initial interaction enthalpy parameter h is:', h0)
print( ' The run will begin with an interaction enthalpy of h at:', h0 + h_incr)
print( ' up through a final value of: ', h0 + h_incr*h_range)
print()
print()
return
####################################################################################################
#
# Procedure to print the parameters for the run
#
####################################################################################################
def print_pattern_selection(pattern_select):
print ()
if pattern_select == 0:
print ( 'The selected pattern for this run is randomly-generated.')
if pattern_select == 1:
print ( 'The selected pattern for this run is a scale-free-like pattern.')
if pattern_select == 2:
print ( 'The selected pattern for this run is a rich-club-like pattern.')
print()
print ('------------------------------------------------------------------------------')
print ()
return()
####################################################################################################
#
# Procedure to print out the 2-D CVM grid
#
####################################################################################################
def print_grid (unit_array):
for i in range (0, pairs):
if i<5: # This puts the single-decimal rows a little to the left
actualEvenRowNum = 2*i
print( 'Row ', actualEvenRowNum, ': ', end =" ")
for j in range(0, array_length):
print( unit_array[actualEvenRowNum,j], end =" ")
print ()
actualOddRowNum = 2*i+1
print( 'Row ', actualOddRowNum, ': ', end =" ")
for j in range(0,array_length):
print( unit_array[actualOddRowNum,j], end =" ")
print ()
else:
actualEvenRowNum = 2*i
print( 'Row', actualEvenRowNum, ': ', end =" ")
for j in range(0,array_length):
print( unit_array[actualEvenRowNum,j], end =" ")
print ()
actualOddRowNum = 2*i+1
print( 'Row ', actualOddRowNum, ': ', end =" ")
for j in range(0,array_length):
print( unit_array[actualOddRowNum,j], end =" ")
print ()
print ()
return
####################################################################################################
#
# Procedure to print out the x1 and x2 variables
#
# Inputs: array_size_list: a list of two integers; array_length and layers
# h: the interaction enthalpy parameter
#
####################################################################################################
def print_x_result (x1, x2, x1_total, x2_total, x1_target, max_x_dif):
# Print the locally-computed values for x1 and x2; these are not passed back to _main__.
print()
print( 'The distribution among states A and B (x1 and x2) units is:' )
print( " ( A ) x1_total =", x1_total, "( B ) x2_total =", x2_total, ' for a total of ', x1_total + x2_total, ' units.' )
print()
print( ' The fractional values for x are: x1 = %.4f' % x1, ' and x2 = %.4f' % x2)
print()
print( ' The actual value for x1 is %.4f' % x1, ' and the desired value for x1 is ', x1_target)
delta_x = x1 - x1_target
print( ' ==>> The difference (x1 - x1_target) is %.4f' % delta_x)
print( ' The acceptable difference between the two values is ', max_x_dif)
if abs(delta_x) > max_x_dif:
if delta_x > 0:
print( ' so we see that x1 is too large, and we want to decrease x1.')
if delta_x < 0:
print( ' so we see that x1 is too small, and we want to increase x1.')
else:
print(' The difference between the actual and the target is within accepted bounds.')
print()
print ('------------------------------------------------------------------------------')
print ()
return
####################################################################################################
#
# Procedure to print out the full set of configuration variables
#
# Inputs: config_vars_list
#
####################################################################################################
def print_config_vars (config_vars_list):
print ()
return
####################################################################################################
####################################################################################################
#
# Procedure to randomly-generate an array, and then permute it to achieve the desired
# z1 & z3 values.
#
# Inputs: array_size_list: a list of two integers; array_length and layers
# h: the interaction enthalpy parameter
# Return: the matrix unit_array, a matrix of 0's and 1's.
#
####################################################################################################
####################################################################################################
def initialize_generated_matrix (array_size_list, h):
array_length = array_size_list[0]
array_layers = array_size_list[1]
hSquared = h*h
hFourth = hSquared*hSquared
denom = 8.0*(hFourth - 6.0*hSquared + 1.0)
z3Analytic = (hSquared - 3.0)*(hSquared + 1.0)/denom # App. B, Eqn. 29
z1Analytic = (1.0 - 3.0*hSquared)*(hSquared + 1.0)/denom # App. B, Eqn. 30
y1Analytic = z1Analytic + 0.5*(0.5 - z1Analytic - z3Analytic)
y2Analytic = z3Analytic + 0.5*(0.5 - z1Analytic - z3Analytic)
# Create the matrix 'unit_array' so that it has a random population of 0's and 1's.
unit_array = np.random.choice([0, 1],size=(array_layers, array_length)) # Create an array filled with random values
# Note: this function can be used to create proportional distributions: np.random.choice([0, 1], size=(10,), p=[1./3, 2./3])
print()
print ('------------------------------------------------------------------------------')
print ('------------------------------------------------------------------------------')
print()
print( 'With h = ', h, ', we begin with a randomly-generated array:' )
print()
# Bring the array closer to the desired configuration variable values
return unit_array
####################################################################################################
####################################################################################################
#
# Procedure to initialize the matrix with EITHER a pre-stored pattern of values
# OR randomly-generate an array, and then permute it to achieve the desired
# z1 & z3 values.
#
# Inputs: array_size_list: a list of two integers; array_length and layers
# patternProb: an integer indicating whether to randomly-generate
# and then permute an array (0), or select a pattern (1 .. N)
# h: the interaction enthalpy parameter
# Return: the matrix unit_array, a matrix of 0's and 1's.
#
####################################################################################################
####################################################################################################
def initialize_matrix (array_size_list, pattern_select, h):
array_length = array_size_list[0]
array_layers = array_size_list[1]
# Note: The passed value patternProb is used to determine if we are returning a stored pattern, or
# are probabilistically-generating our data.
# If patternProb = 0: probabilistic generation, dependent on h
# If patternProb > 1: select one of the N stored patterns (1 ... N)
if pattern_select == 0:
unit_array = initialize_generated_matrix (array_size_list,h)
# Create the initial matrix, 'unit_array,' and populate it with zeros
else:
unit_array = np.zeros((array_layers,array_length), dtype=np.int)
# Read the stored grid into unit_array
x1_total = x2_total = 0
for i in range(0,array_layers):
dataArray = obtainGridRow (i, pattern_select)
# rownum = i+1
for j in range(0, array_length):
unit_array[i,j]=dataArray[j]
if unit_array[i,j]==1:
x1_total = x1_total + 1
else: x2_total = x2_total + 1
# Determining "pairs" - the total number of pairs of zigzag chains - is done in __main__; "pairs" is a global variable
print_grid (unit_array)
return unit_array
####################################################################################################
####################################################################################################
#
# Procedure to compute configuration variables x'i and return as elements of list configXVarsList
# (Yes, the x'i were computed during array creation and randomization. They are being recomputed
# as part of computing a list of ALL the configuration variables.)
#
####################################################################################################
####################################################################################################
def computeConfigXVariables (array_size_list, unit_array):
####################################################################################################
# This section unpacks the input variable array_size_list
####################################################################################################
array_length = array_size_list [0]
array_layers = array_size_list [1]
unit_array = unit_array
# Debug print statements
if not debug_print_off:
print()
print( "Just entered computeConfigXVariables")
# Initialize the y'i variables
x1_total = x2_total = 0
for i in range (0,array_layers):
x1_partial = x2_partial = 0
# Compute the x'i values for each sub-row of the zigzag, just to see
# the distribution
# Start counting through the array elements, L->R.
for j in range(0, array_length):
# If the initial unit is A:
if unit_array[i,j]>0.1:
# The unit is "A," add it to x1
x1_partial = x1_partial + 1
else: # The initial unit is B:
x2_partial = x2_partial + 1
# debug prints
# print "In row", i
# print "x1_partial is", x1_partial, "x2_partial is", x2_partial
x1_total = x1_total + x1_partial
x2_total = x2_total + x2_partial
# print "x1_total (so far) is", x1_total, "x2_total (so far) is", x2_total
x1 = x1_total
x2 = x2_total
configVarsXList = (x1, x2)
# print "Leaving computeConfigXVariables for calling procedure"
# print
return (configVarsXList)
####################################################################################################
####################################################################################################
#
# Procedure to compute the set of configuration variables y'i working across a single zigzag chain
# Procedure returns a list configvar containing the three y configuration variables:
# y1 & y2 & y3
#
####################################################################################################
####################################################################################################
def computeConfigYEvenRowZigzagVariables (array_size_list, unit_array, topRow):
####################################################################################################
# This section unpacks the input variable array_size_list
####################################################################################################
array_length = array_size_list [0]
array_layers = array_size_list [1]
unit_array = unit_array
###################################################################################################
#
# Compute the nearest-neighbor values y(i)
#
###################################################################################################
# y_1 is A-A
# y_3 is B-B
# left_y_2 is A-B
# right_y_2 is B-A
#
# The total number of y'i's is the same as the total number of x'i's.
# Initialize the y'i variables
y1_total = left_y2_total = right_y2_total = y3_total = 0
###################################################################################################
#
# Compute the nearest-neighbor values y(i) for the case of
# downward-right-pointing diagonals, from top to next layer
# going L->R across the zigzag array
#
###################################################################################################
# Start counting through the layers; since we will work with a pair of
# overlapping layers (for diagonal nearest-neighbors), we use a count of
# layers - 1.
# commenting out for debug
#for i in range(0,array_layers-1):
# top_row = i
# next_row = i+1
top_row = topRow
next_row = topRow + 1
# Start counting through the array elements, L->R.
for j in range(0, array_length):
# If the initial unit is A:
if unit_array[top_row,j]>0.1:
# Compare with the same (jth) unit in the overlapping row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[next_row,j] > 0.1:
# h_increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[next_row,j] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
# Debug section: Print totals for right-downwards-pointing diagonals
# print "Subtotals so far (downward-right-pointing-diagonals):"
# print "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total
# print "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total
###########################################################
#
# Compute the nearest-neighbor values y(i) for the case of
# upward-right-pointing diagonals, from next-to-top layer up to
# the top layer, going L->R across the zigzag array
#
###########################################################
# Recall that we are carrying forward previously-computed partial totals
# for the y'i values.
# Start counting through the layers again, however, the computations will start
# with the lower layer and look in an upward-right-diagonal to the layer above.
# commenting out for debug
#for i in range(0,array_layers-1):
# top_row = i
# next_row = i+1
# Start counting through the array elements, L->R.
# Since we are comparing the unit in the lower row to the one shifted diagonally
# above and over to the right, we only step through to the array_length - 1 unit.
# A final step (after this) will be to compute the wrap-around.
for j in range(0, array_length-1):
# If the initial unit is A:
if unit_array[next_row,j]>0.1:
# Compare with the NEXT (j+1) unit in the overlapping top row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[top_row,j+1] > 0.1:
# h_increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[top_row,j+1] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
# Debug section: Print totals for right-upwards-pointing diagonals
# print "Subtotals so far (downward + upward-right-pointing-diagonals):"
# print "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total
# print "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total
# Only one step remains.
# We need to compute the wrap-around for the zigzag chain (to get the total number
# of y'i's to be the same as the total number of x'i's.
# We compute the nearest-neighbor pair similarity between the last unit on the
# lower row with the first unit on the upper row.
if unit_array[next_row,array_length-1]>0.1:
# Compare with the FIRST unit in the overlapping top row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[top_row,0] > 0.1:
# h_increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[top_row,0] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
#Debug section: Print message,"Computing last of the y'i values - wraparound"
# print "Computing last of the y'i values - wraparound"
# This concludes computation of the y'i totals
################################################################
if not debug_print_off:
print()
print( "Totals for the y'i variables:")
print( "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total )
print( "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total)
print()
################################################################
###################################################################################################
#
# Assign the computed configuration variables to elements of the config_vars_list,
# which will be passed back to the calling procedure
#
###################################################################################################
y1 = y1_total
y2 = left_y2_total + right_y2_total
y3 = y3_total
configVarsYList = (y1, y2, y3)
return (configVarsYList)
####################################################################################################
####################################################################################################
#
# Procedure to compute the set of configuration variables y'i
# Procedure returns a list configvar containing the three y configuration variables:
# y1 & y2 & y3
#
####################################################################################################
####################################################################################################
def computeConfigYOddRowZigzagVariables (array_size_list, unit_array, topRow):
####################################################################################################
# This section unpacks the input variable array_size_list
####################################################################################################
array_length = array_size_list [0]
array_layers = array_size_list [1]
unit_array = unit_array
# Initialize the y'i variables
y1_total = left_y2_total = right_y2_total = y3_total = 0
###################################################################################################
#
# Compute the nearest-neighbor values y(i) for the case of
# downward-right-pointing diagonals, from top to next layer
# going L->R across the zigzag array
#
###################################################################################################
# Start counting through the layers; since we will work with a pair of
# overlapping layers (for diagonal nearest-neighbors), we use a count of
# layers - 1.
# commenting out for debug
#for i in range(0,array_layers-1):
# top_row = i
# next_row = i+1
top_row = topRow
next_row = topRow + 1
if top_row == array_layers-1: next_row = 0
# Start counting through the array elements, L->R.
for j in range(0, array_length-1): # Same logic as in the Even Row y(i) computation
# but we go for one (TWO???) less down the array length
# If the initial unit is A:
if unit_array[top_row,j]>0.1:
# Compare with the same (jth) unit in the overlapping row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[next_row,j+1] > 0.1:
# h_increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[next_row,j+1] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
# Debug section: Print totals for right-downwards-pointing diagonals
# print "Subtotals so far (downward-right-pointing-diagonals):"
# print "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total
# print "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total
###########################################################
#
# Compute the nearest-neighbor values y(i) for the case of
# upward-right-pointing diagonals, from next-to-top layer up to
# the top layer, going L->R across the zigzag array
#
###########################################################
# Recall that we are carrying forward previously-computed partial totals
# for the y'i values.
# Start counting through the layers again, however, the computations will start
# with the lower layer and look in an upward-right-diagonal to the layer above.
# commenting out for debug
#for i in range(0,array_layers-1):
# top_row = i
# next_row = i+1
# Start counting through the array elements, L->R.
# Since we are comparing the unit in the lower row to the one shifted diagonally
# above and over to the right, we only step through to the array_length - 1 unit.
# A final step (after this) will be to compute the wrap-around.
for j in range(0, array_length): # Same logic as in the Even Row y(i) computation
# But we can include the full array length (the other was truncated at array_length - 1)
# If the initial unit is A:
if unit_array[next_row,j]>0.1:
# Compare with the NEXT (j+1) unit in the overlapping top row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[top_row,j] > 0.1:
# h_increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[top_row,j] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
# Debug section: Print totals for right-upwards-pointing diagonals
# print "Subtotals so far (downward + upward-right-pointing-diagonals):"
# print "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total
# print "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total
# Only one step remains.
# We need to compute the wrap-around for the zigzag chain (to get the total number
# of y'i's to be the same as the total number of x'i's.
# We compute the nearest-neighbor pair similarity between the last unit on the
# lower row with the first unit on the upper row.
if unit_array[top_row,array_length-1]>0.1:
# Compare with the FIRST unit in the overlapping top row
# comprising the zigzag chain
# If the nearest-neighbor unit is also A:
if unit_array[next_row,0] > 0.1:
# h_increment the y_1; the count of A-A nearest-neighbor pairs:
y1_total = y1_total + 1
else: # The nearest-neighbor unit is B:
left_y2_total = left_y2_total + 1
else: # The initial unit is B:
if unit_array[next_row,0] > 0.1: # If the nearest-neighbor unit is A:
right_y2_total = right_y2_total + 1
else: # The nearest-neighbor unit is also B:
y3_total = y3_total + 1
#Debug section: Print message,"Computing last of the y'i values - wraparound"
# print "Computing last of the y'i values - wraparound"
# This concludes computation of the y'i totals
################################################################
if not debug_print_off:
print()
print( "Totals for the y'i variables:")
print( "(A-A) y1_total =", y1_total, "(A-B) left_y2_total =", left_y2_total )
print( "(B-B) y3_total =", y3_total, "(B-A) right_y2_total =", right_y2_total)
print()
################################################################
###################################################################################################
#
# Assign the computed configuration variables to elements of the config_vars_list,
# which will be passed back to the calling procedure
#
###################################################################################################
y1 = y1_total
y2 = left_y2_total + right_y2_total
y3 = y3_total
configVarsYList = (y1, y2, y3)
return (configVarsYList)
####################################################################################################
####################################################################################################
# This function runs both the even-to-odd and odd-to-even y(i) nearest neighbors; it combines the two in building
# another row on top of the basic 1-D zigzag chain
####################################################################################################
def computeConfigYVariables (array_size_list, unit_array):
# Initialize the y'i variables
y1 = y2 = y3 = 0
if not debug_print_off:
print()
print( ' Starting to compute Y variables')
print( ' Total number of pairs of zigzag chains is: ', pairs)
print()
for i in range (0, pairs):
topRow = 2*i
if not debug_print_off:
print( ' Row: ', topRow)
# Obtain the y(i) values from the first even-to-odd zigzag chain (0 to 1, running top-to-bottom)
configVarsYList = computeConfigYEvenRowZigzagVariables (array_size_list, unit_array, topRow)
# Assign the returned results to the local sum for each of the z(i) triplets
y1 = y1+configVarsYList[0]
y2 = y2+configVarsYList[1]
y3 = y3+configVarsYList[2]
topRow = 2*i+1
if not debug_print_off:
print()
print( ' Row: ', topRow)
configVarsYList = computeConfigYOddRowZigzagVariables (array_size_list, unit_array, topRow)
# Assign the returned results to the local sum for each of the z(i) triplets
y1 = y1+configVarsYList[0]
y2 = y2+configVarsYList[1]
y3 = y3+configVarsYList[2]
# Debug section: Print totals for right-downwards-then-upwards triplets