-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLatticeHandler.py
More file actions
1210 lines (1099 loc) · 46.5 KB
/
Copy pathLatticeHandler.py
File metadata and controls
1210 lines (1099 loc) · 46.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
'''
Created on 20 dic. 2018
@author: Iñigo
@version: 04/08/19
'''
#LatticeCounter.py
#Class responsible for the operations on the lattice simulation, theoretical calculations and result management.
from InputData import *
from Lattice import *
from Constant import *
from Numericalmethods import *
import numpy as np
import random
import math
import statistics
import matplotlib.pyplot as plt
class LatticeHandler:
#Method to get the state of the neighbors of each cell and register it in the neighborHistogram variable
def neighborStatistics(self, lattice):
L=lattice.getInputData().getL()
for x in range(0, L):
for y in range(0, L):
for z in range(0, L):
neighbors=lattice.getNeighbors()[x,y,z] #Storing neighbor coords
n=0 #Number of neighbors in V+ state
for neighborCoord in neighbors: #Getting coordinates in the lattice for every neighbor
n=n+lattice.getLattice()[neighborCoord[0], neighborCoord[1], neighborCoord[2]]
lattice.changeHistogram(n)
#Method that tries a random cell volume change and checks if the system accepts, making
#the changes in volume, energy and configuration when necessary.
def changeVol(self, lattice, inputData):
#Choosing random cell in the lattice
x=random.randint(0,inputData.getL()-1)
y=random.randint(0,inputData.getL()-1)
z=random.randint(0,inputData.getL()-1)
volumeChange=0 #Parameter to store the change in volume
tempLattice=lattice.getLattice() #Storing state of the lattice (matrix of 0s and 1s)
neighbors=lattice.getNeighbors()[x,y,z] #Storing neighbor coords
volume=lattice.getVolume() #Storing volume of the lattice
energy=lattice.getEnergy() #Storing energy of the lattice
#Counting number of neighbors with V+
n=0 #parameter to count the number of neighbors in V+ state
#Counting V+ neighbors
for neighborCoord in neighbors: #Getting coordinates in the lattice for every neighbor
n=n+tempLattice[neighborCoord[0], neighborCoord[1], neighborCoord[2]] #Cumulative counting (V+=1, V-=0)
#Getting intermolecular energy change
#Case initialV=V-
if tempLattice[x,y,z]==0:
if random.uniform(0,1)<lattice.probArrayMinus[n]: #The change of state of the cell is accepted
intEnergyVar=n*(inputData.getEb()-inputData.getEs()) #Calculating variation of the energy
volumeChange=inputData.getVb()-inputData.getVs() #Calculating variation of the volume
#Changing volume
lattice.changeVolume(volume+volumeChange)
#Changing cell volume
lattice.changeCellVolume(x,y,z)
#Changing energy
lattice.changeEnergy(energy+intEnergyVar)
#Changing cell type counter
lattice.changeMinusToPlus()
#Case initialV=V+
else:
#Checking if the change is accepted
if random.uniform(0,1)<lattice.probArrayPlus[n]: #The change of state of the cell is accepted
intEnergyVar=n*(inputData.getEs()-inputData.getEb()) #Calculating variation of the energy
volumeChange=inputData.getVs()-inputData.getVb() #Calculating variation of the volume
#Changing volume
lattice.changeVolume(lattice.getVolume()+volumeChange)
#Changing cell volume
lattice.changeCellVolume(x,y,z)
#Changing energy
lattice.changeEnergy(energy+intEnergyVar)
#Changing cell type counter
lattice.changePlusToMinus()
#Method to get the volume, enthalpy and intermolecular energy in intervals separated
#by the indicated amount of steps with a total of MonteCarlo steps indicated by the inputData.
#Returns three arrays as shown
#[[VolumeValues],
#[EnergyValues],
#[EnthalpyValues]]
#Writes in a file the measurements of the system every meanSteps
def getSystemEvolution(self, lattice, inputData, meanSteps):
#Opening file and writting header
fileName="Measurements_T"+str(inputData.getT())
fw=open(fileName, "w")
#Opening file to store probabilities
fileName="probabilities"
fw2=open(fileName, "a")
#Writting lattice size
line_new="Lattice length="+str(inputData.getL())
fw.write(line_new+"\n")
#writting temperature
line_new ="Temperature="+str(inputData.getT())
fw.write(line_new+"\n")
#Writting number of steps
line_new="Monte Carlo steps="+str(inputData.getN())
fw.write(line_new+"\n")
#Writting equilibrium steps
line_new="Equilibrium steps="+str(inputData.getEq())
fw.write(line_new+"\n")
#Writting V+ volume
line_new="Big cell volume="+str(inputData.getVb())
fw.write(line_new+"\n")
#Writting V- volume
line_new="Small cell volume="+str(inputData.getVs())
fw.write(line_new+"\n")
#Writting big free volume
line_new="Big free volume="+str(inputData.getFbv())
fw.write(line_new+"\n")
#Writting small free volume
line_new="Small free volume="+str(inputData.getFsv())
fw.write(line_new+"\n")
#Writting energy of V+ V+ interaction
line_new="+ Interaction energy="+str(inputData.getEb())
fw.write(line_new+"\n")
#Writting energy of V+ V- or V- V- interaction
line_new="- Interaction energy="+str(inputData.getEs())
fw.write(line_new+"\n")
#Writting pressure
line_new="Pressure="+str(inputData.getP())
fw.write(line_new+"\n")
#Writting mean steps
line_new="Steps used for measurement="+str(meanSteps)
fw.write(line_new+"\n")
#Data columns
line_new="Volume"+"\t"+"Energy"+"\t"+"Enthalpy"
fw.write(line_new+"\n")
#Data arrays
volumeValues=[]
energyValues=[]
enthalpyValues=[]
#The lattice state is stored every meanSteps steps
counter=1
#Number of volumes to be registered (Total amount of steps divided by the steps taken between measurements)
nVols=int(inputData.getN()/meanSteps)
print("Taking measurements")
for i in range(0, nVols):
vol=0
energy=0
enthalpy=0
#Changing cell volumes meanSteps times before taking a measurement
for j in range(0, meanSteps):
LatticeHandler().changeVol(lattice, inputData) #Try to change state of one cell
#Getting state of the lattice
vol=lattice.getVolume() #Getting volume of the lattice
energy=lattice.getEnergy() #Getting energy of the lattice
enthalpy=energy+inputData.getP()*vol #Getting enthalpy of the lattice
#Adding measurements to the data arrays
volumeValues.append(vol) #Adding measured volume to measurements array
energyValues.append(energy) #Adding measured energy to measurements array
enthalpyValues.append(enthalpy) #Adding measured enthalpy to measurements array
#Saving parameters in the measurements file
line_new=str(vol)+"\t"+str(energy)+"\t"+str(enthalpy)
fw.write(line_new+"\n")
#Simulation progress indicator
if math.fmod(counter/nVols*100,1.0)==0: #Checking progress of the measurement phase
print(str(counter/(nVols)*100)+"%")
LatticeHandler().neighborStatistics(lattice) #Updating neighbor histogram array
counter+=1
fw.close() #Closing file
fw2.write("Probabilities for T="+str(inputData.getT())+"\n")
fw2.write("+ to -"+"\n")
fw2.write(str(lattice.probArrayPlus)+"\n")
fw2.write("- to +"+"\n")
fw2.write(str(lattice.probArrayMinus)+"\n"+"\n")
fw2.close()
return [volumeValues, energyValues, enthalpyValues]
#Method to calculate the mean thermal expansivity for a given temperature from the data files
#generated in a previous simulation and the number of values to be used when computing the mean values.
#Returns an array with the mean value and the standard deviation and writes the values
#(alpha values) used for the mean in the measurements file.
def thermalExpansivity(self, T, valuesForMean):
#Data sets
volumeSets=[]
enthalpySets=[]
#Data file corresponding to the needed parameters
fileName="Measurements_T"+str(T)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading inputData parameters
line=lines[2].split("=")
n=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
#Closing file
fr.close()
#Number of data sets
nDataSets=int(n/meanSteps/valuesForMean)
#Reading all the measurements before the response functions
nLine=13
for j in range(0, nDataSets):
#Filling volume data set
volumeSet=[]
enthalpySet=[]
for i in range(0, valuesForMean):
values=lines[nLine].split("\t")
#Introducing read volume (values[0]) in the data set
volumeSet.append(float(values[0]))
enthalpySet.append(float(values[2]))
#Changing line
nLine+=1
#Introducing data set into data array in molar units
volumeSets.append(volumeSet)
enthalpySets.append(enthalpySet)
#Array to store the calculated values
thermExpArray=[]
product=[]
#Number of data sets, necessary to get mean values
nDataSets=len(volumeSets)
#Product of measurements
for i in range(0,nDataSets):
product.append(np.multiply(volumeSets[i],enthalpySets[i]))
#Calculation
for i in range(0,nDataSets):
#Getting the mean of the data set of this iteration made of valuesForMean values
meanVolume=sum(volumeSets[i])/valuesForMean
meanEnthalpy=sum(enthalpySets[i])/valuesForMean
meanProduct=sum(product[i])/valuesForMean
thermExpArray.append(1/meanVolume/Constant().K()/math.pow(T,2)*\
(meanProduct-meanEnthalpy*meanVolume))
thermExp=sum(thermExpArray)/nDataSets
#Calculating deviation
residue=0
for i in range(0,nDataSets):
residue=residue+math.pow((thermExpArray[i]-thermExp),2)
stdDeviation=math.sqrt(residue/(nDataSets-1))
#Writting results in file (multiple values not mean value)
fileName="Measurements_T"+str(T)
fw=open(fileName, "a")
line_new="Values for mean="+str(valuesForMean)
fw.write(line_new+"\n")
line_new ="Alpha_p"+"\n"+str(thermExpArray)
fw.write(line_new+"\n")
return [thermExp, stdDeviation]
#Method to calculate the mean isothermal compressibility of a given temperature from data files generated
#in a previous simulation and the amont of values to be used when calculating the mean values.
#Returns an array with the mean value and the standard deviation and
#writes the values used for the mean in the measurements file.
def isothermalCompressibility(self, T, valuesForMean):
#Data sets
volumeSets=[]
#Data file corresponding to the needed parameters
fileName="Measurements_T"+str(T)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading inputData parameters
line=lines[2].split("=")
n=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
#Closing file
fr.close()
#Number of data sets
nDataSets=int(n/meanSteps/valuesForMean)
#Reading all the measurements before the response functions
nLine=13
for j in range(0, nDataSets):
#Filling volume data set
volumeSet=[]
for i in range(0, valuesForMean):
values=lines[nLine].split("\t")
#Introducing read volume (values[0]) in the data set
volumeSet.append(float(values[0]))
#Changing line
nLine+=1
#Introducing data set into data array
volumeSets.append(volumeSet)
#Squared volume
volumeSq=[]
#Number of data sets necessary to get mean values
nDataSets=len(volumeSets)
#Filling squared volume array
for i in range(0,nDataSets):
volumeSq.append(np.multiply(volumeSets[i],volumeSets[i]))
#Array to store the calculated values
isothCompressArray=[]
#Calculation
for i in range(0,nDataSets):
#Getting the mean of the data set of this iteration made of 10 values
meanVolume=sum(volumeSets[i])/valuesForMean
meanVolumeSq=sum(volumeSq[i])/valuesForMean
isothCompressArray.append(1/meanVolume/Constant().K()/T*(meanVolumeSq-math.pow(meanVolume,2)))
isothCompress=sum(isothCompressArray)/nDataSets
#Calculating deviation
residue=0
for i in range(0,nDataSets):
residue=residue+math.pow((isothCompressArray[i]-isothCompress),2)
stdDeviation=math.sqrt(residue/(nDataSets-1))
#Writting results in file (multiple values not mean value)
fileName="Measurements_T"+str(T)
fw=open(fileName, "a")
line_new ="Beta_t"+"\n"+str(isothCompressArray)
fw.write(line_new+"\n")
return [isothCompress, stdDeviation]
#Method to calculate the heat capacity for a given temperature from the data files generated
#in a previous similation and the amount of values that have to be used for
#the mean. Returns an array with the mean value and the standard deviation.
def heatCapacity(self, T, valuesForMean):
#Data sets
volumeSets=[]
energySets=[]
enthalpySets=[]
#Data sets
volumeSets=[]
enthalpySets=[]
#Data file corresponding to the needed parameters
fileName="Measurements_T"+str(T)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading inputData parameters
line=lines[0].split("=")
l=float(line[1])
line=lines[2].split("=")
n=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
#Closing file
fr.close()
#Number of data sets
nDataSets=int(n/meanSteps/valuesForMean)
#Reading all the measurements before the response functions
nLine=13
for j in range(0, nDataSets):
#Filling volume data set
volumeSet=[]
energySet=[]
enthalpySet=[]
for i in range(0, valuesForMean):
values=lines[nLine].split("\t")
#Introducing read volume (values[0]) in the data set
volumeSet.append(float(values[0]))
energySet.append(float(values[1]))
enthalpySet.append(float(values[2]))
#Changing line
nLine+=1
#Introducing data set into data array
volumeSets.append(volumeSet)
energySets.append(energySet)
enthalpySets.append(enthalpySet)
#Product arrays
productE=[]
productV=[]
#Number of data sets, necessary to get the mean values
nDataSets=len(volumeSets)
#Product of measurements
for i in range(0,nDataSets):
productE.append(np.multiply(volumeSets[i],enthalpySets[i]))
productV.append(np.multiply(energySets[i],enthalpySets[i]))
#Array to store calculated values
heatCapArray=[]
#Calculation of the heat capacity
for i in range(0,nDataSets):
#Getting mean values for the iteration
meanVolume=sum(volumeSets[i])/valuesForMean
meanEnergy=sum(energySets[i])/valuesForMean
meanEnthalpy=sum(enthalpySets[i])/valuesForMean
meanProductE=sum(productE[i])/valuesForMean
meanProductV=sum(productV[i])/valuesForMean
#Residual heat capacity
resHeatCap=1/Constant().K()/math.pow(T,2)*(meanProductE-\
meanEnergy*meanEnthalpy)+1/Constant().K()/math.pow(T,2)*(meanProductV-\
meanVolume*meanEnthalpy)
#Turning to molar units
heatCapArray.append(resHeatCap/math.pow(l,3)*6.022e23+5/2*6.022e23*Constant().K())
heatCap=sum(heatCapArray)/nDataSets
#Calculating deviation
residue=0
for i in range(0,nDataSets):
residue=residue+math.pow((heatCapArray[i]-heatCap),2)
stdDeviation=math.sqrt(residue/(nDataSets-1))
#Writting results in file (multiple values not mean value)
fileName="Measurements_T"+str(T)
fw=open(fileName, "a")
line_new ="C_p"+"\n"+str(heatCapArray)
fw.write(line_new+"\n")
return [heatCap, stdDeviation]
#Method to calculate the isentropic compressibility from the data files stored of the system
#evolution. It needs the response coefficients calculated previously and the amount of values used
#for the mean.
def isentropicCompressibility(self, T, valuesForMean):
#Creating data arrays
volumes=[]
beta_t=[]
alpha_p=[]
c_p=[]
#Data file corresponding to the needed parameters
fileName="Measurements_T"+str(T)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading steps before measurement
line=lines[0].split("=")
l=float(line[1])
line=lines[2].split("=")
n=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
#Line number, skipping input data lines
nLine=13
#Reading all the volume measurements before the response functions
for j in range(0, int(n/meanSteps/valuesForMean)):
#Filling volume data set
volumeSet=[]
for i in range(0, valuesForMean):
values=lines[nLine].split("\t")
#Introducing read volume (values[0]) in the data set
volumeSet.append(float(values[0]))
#Changing line
nLine+=1
#Introducing data set into data array
volumes.append(volumeSet)
#Skipping "values for mean"
nLine+=1
#Skipping "Alpha_P" header line, not used
nLine+=1
#Reading alpha_p values, removing brackets from both ends of the line
lines[nLine]=lines[nLine].replace("[","")
lines[nLine]=lines[nLine].replace("]","")
#Spliting line into separate values
values=lines[nLine].split(",")
#Introducing values into data array of alpha_p
for i in range(0, len(values)):
alpha_p.append(float(values[i]))
nLine+=1
#Skipping "Beta_T" header line, not used
nLine+=1
#Reading beta_t values, removing brackets from both ends of the line
lines[nLine]=lines[nLine].replace("[","")
lines[nLine]=lines[nLine].replace("]","")
#Spliting line into separate values
values=lines[nLine].split(",")
#Introducing values into data array of alpha_p
for i in range(0, len(values)):
beta_t.append(float(values[i]))
nLine+=1
#Skippimg "C_P" header line, not used
nLine+=1
#Reading c_p values, removing brackets from both ends of the line
lines[nLine]=lines[nLine].replace("[","")
lines[nLine]=lines[nLine].replace("]","")
#Spliting line into separate values
values=lines[nLine].split(",")
#Introducing values into data array of alpha_p
for i in range(0, len(values)):
c_p.append(float(values[i]))
#Closing file
fr.close()
#Calculating mean values
meanVolumes=[]
#Introducing mean volume of each set into array
for volumeSet in volumes:
meanVolumes.append(sum(volumeSet)/len(volumeSet))
#Array to store values of Ks
isentCompressArray=[]
#Calculating values of the isentropic compressibility
for i in range(0, len(meanVolumes)):
isentCompress=beta_t[i]-T*meanVolumes[i]*\
math.pow(alpha_p[i],2)/c_p[i]
isentCompressArray.append(isentCompress)
isentropicCompressibility=sum(isentCompressArray)/len(isentCompressArray)
#Calculating deviation
residue=0
for i in range(0,len(isentCompressArray)):
residue=residue+math.pow((isentCompressArray[i]-isentropicCompressibility),2)
stdDeviation=math.sqrt(residue/(len(isentCompressArray)-1))
#Writting results in file (multiple values not mean value)
fileName="Measurements_T"+str(T)
fw=open(fileName, "a")
line_new ="K_s"+"\n"+str(isentCompressArray)
fw.write(line_new+"\n")
return [isentropicCompressibility, stdDeviation]
#Method that shows the evolution of the volume for a given temperature using the corresponding
#data file 'Measurements_Tx'. It needs the number of values for the mean
def volEvo(self, T, valuesForMean):
#Creating data arrays
volumes=[]
#Data file corresponding to the needed parameters
fileName="Measurements_T"+str(T)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading inputData parameters
line=lines[0].split("=")
l=float(line[1])
line=lines[2].split("=")
n=float(line[1])
line=lines[4].split("=")
vb=float(line[1])
line=lines[5].split("=")
vs=float(line[1])
line=lines[6].split("=")
fvb=float(line[1])
line=lines[7].split("=")
fvs=float(line[1])
line=lines[8].split("=")
eb=float(line[1])
line=lines[9].split("=")
es=float(line[1])
line=lines[10].split("=")
p=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
lambdaVol=fvb/fvs
#Calculating deltaE and deltaV
deltaV=vb-vs
deltaE=es-eb
#Number of data sets
nDataSets=int(n/meanSteps/valuesForMean)
#Reading all the measurements before the response functions
nLine=13
for j in range(0, nDataSets):
#Filling volume data set
volumeSet=[]
for i in range(0, valuesForMean):
values=lines[nLine].split("\t")
#Introducing read volume (values[0]) in the data set
volumeSet.append(float(values[0]))
#Changing line
nLine+=1
#Introducing data set into data array
volumes.append(volumeSet)
#Plotting mean values of the volume sets
x=[]
y=[]
error=[]
#Calculating deviation to get error of every volume
#k counter
k=0
for dataSet in volumes:
#Initial residue
residue=0
#Calculating mean
meanVolume=statistics.mean(dataSet)
#Adding mean to data array
y.append(meanVolume)
x.append(k)
#Calculating standard deviation of every data set
for value in dataSet:
residue=residue+math.pow((value-meanVolume),2)
stdDeviation=math.sqrt(residue/(nDataSets-1))
error.append(stdDeviation)
k+=1
#Calculating value given by mean-field approximation
#Function to calculate the volume given by the mean field theory
def fun(v):
return Constant().K()/deltaV*math.log(lambdaVol*(vb-v)/(v-vs))-(p-6*deltaE/deltaV*\
(v-vs)/deltaV)/T
volumeUnitCell=Numericalmethods().newton(fun, (vb+vs)/2, 10e-9, 10e-32)
meanFieldVolume=volumeUnitCell*math.pow(l,3)
x2=x
y2=[]
for o in range(0, nDataSets):
y2.append(meanFieldVolume)
plt.errorbar(x,y,error)
plt.plot(x2,y2,linewidth=2.0, linestyle='dashed')
plt.title("Volume-Time", fontsize=14)
plt.xscale('log')
plt.xlabel("t/Steps")
plt.ylabel("V/Jm^3")
plt.show()
#Method that shows the evolution of the volume as the temperature increases and compares it to the theoretical value
def volEvoTemps(self, iniT, finT, tempIncrement):
Volumes=[]
error=[]
temps=[]
#Data file corresponding to the needed parameters
fileName="Measurements_T"+str(iniT)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading inputData parameters
line=lines[0].split("=")
l=float(line[1])
line=lines[2].split("=")
n=float(line[1])
line=lines[4].split("=")
vb=float(line[1])
line=lines[5].split("=")
vs=float(line[1])
line=lines[6].split("=")
fvb=float(line[1])
line=lines[7].split("=")
fvs=float(line[1])
line=lines[8].split("=")
eb=float(line[1])
line=lines[9].split("=")
es=float(line[1])
line=lines[10].split("=")
p=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
lambdaVol=fvb/fvs
deltaV=vb-vs
deltaE=es-eb
#Number of measurements
nMeasurements=int(n/meanSteps)
#File to write simulation mean energy values
writeFileName="Volume_Temperature"
fw=open(writeFileName, "w")
#Header of the file
fw.write("Pressure="+str(p)+" Pa"+"\n")
fw.write("L="+str(l)+"\n")
fw.write("dV="+str(deltaV*6.022e23)+" m^3/mol"+"\n")
fw.write("dE="+str(deltaE*6.022e23)+" J/mol"+"\n")
fw.write("Lambda="+str(lambdaVol)+"\n")
#Header of the file
fw.write("T/K"+"\t"+"V/m^3"+"\n")
for T in range(iniT, finT+tempIncrement, tempIncrement):
temps.append(T)
#Data file corresponding to the needed parameters
fileName="Measurements_T"+str(T)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading inputData parameters
line=lines[0].split("=")
l=float(line[1])
line=lines[2].split("=")
n=float(line[1])
line=lines[4].split("=")
vb=float(line[1])
line=lines[5].split("=")
vs=float(line[1])
line=lines[6].split("=")
fvb=float(line[1])
line=lines[7].split("=")
fvs=float(line[1])
line=lines[8].split("=")
eb=float(line[1])
line=lines[9].split("=")
es=float(line[1])
line=lines[10].split("=")
p=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
lambdaVol=fvb/fvs
deltaV=vb-vs
deltaE=es-eb
#Number of measurements
nMeasurements=int(n/meanSteps)
#Reading all the measurements before the response functions
nLine=13
volumeSet=[]
for j in range(0, nMeasurements):
values=lines[nLine].split("\t")
#Introducing read volume (values[0]) in the data set
volumeSet.append(float(values[0]))
#Changing line
nLine+=1
#Changing to molar units
volumeSet=np.multiply(volumeSet, 6.022e23/math.pow(l,3))
#Introducing data set into data array
Volumes.append(statistics.mean(volumeSet))
error.append(statistics.stdev(volumeSet))
fw.write(str(T)+"\t"+str(statistics.mean(volumeSet))+" +- "+str(statistics.stdev(volumeSet))+"\n")
#Calculating theoretical values
#Array of volumes
v=np.linspace(2.11e-5, 2.4999999e-5, num=10000)
#Performing unit change
vs=vs*6.022e23
deltaV=deltaV*6.022e23
deltaE=deltaE*6.022e23
#Array of temperatures
T=[]
for volume in v:
T.append((p-6*deltaE/deltaV*(volume-vs)/deltaV)/8.31*deltaV/math.log(lambdaVol*(vs+deltaV-volume)/(volume-vs)))
#Plotting results
plt.errorbar(temps,Volumes,error,marker='o', linewidth=2.0, label='Sim')
plt.plot(T, v, linewidth=2.0, linestyle='dashed', label='Theory')
plt.legend()
plt.title("Volume-Temperature", fontsize=14)
plt.xlabel("T/K")
plt.ylabel(r"$V\ /\ m^3\ mol^-1$")
plt.show()
#Method that shows the evolution of the volume as the pressure increases
def volEvoPress(self, iniP, finP, pressureIncrement):
Volumes=[]
error=[]
pressures=[]
#File to write simulation mean volume values
writeFileName="Volume_Pressure"
fw=open(writeFileName, "w")
#Data file corresponding to the needed parameters
fileName="Measurements_P"+str(iniP)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading inputData parameters
line=lines[0].split("=")
l=float(line[1])
line=lines[2].split("=")
n=float(line[1])
line=lines[4].split("=")
vb=float(line[1])
line=lines[5].split("=")
vs=float(line[1])
line=lines[6].split("=")
fvb=float(line[1])
line=lines[7].split("=")
fvs=float(line[1])
line=lines[8].split("=")
eb=float(line[1])
line=lines[9].split("=")
es=float(line[1])
line=lines[1].split("=")
T=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
lambdaVol=fvb/fvs
deltaV=vb-vs
deltaE=es-eb
#Header of the file
#Number of measurements
nMeasurements=int(n/meanSteps)
#File to write simulation mean energy values
writeFileName="Pressure_Volume"
fw=open(writeFileName, "w")
#Header of the file
fw.write("Temperature="+str(T)+" K"+"\n")
fw.write("L="+str(l)+"\n")
fw.write("dV="+str(deltaV*6.022e23)+" m^3/mol"+"\n")
fw.write("dE="+str(deltaE*6.022e23)+" J/mol"+"\n")
fw.write("Lambda="+str(lambdaVol)+"\n")
fw.write("P/Pa"+"\t"+"V/m^3 mol^-1"+"\n")
for P in range(iniP, finP, pressureIncrement):
pressures.append(P)
#Data file corresponding to the needed parameters
fileName="Measurements_P"+str(P)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading inputData parameters
line=lines[0].split("=")
l=float(line[1])
line=lines[1].split("=")
T=float(line[1])
line=lines[2].split("=")
n=float(line[1])
line=lines[4].split("=")
vb=float(line[1])
line=lines[5].split("=")
vs=float(line[1])
line=lines[6].split("=")
fvb=float(line[1])
line=lines[7].split("=")
fvs=float(line[1])
line=lines[8].split("=")
eb=float(line[1])
line=lines[9].split("=")
es=float(line[1])
line=lines[10].split("=")
p=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
lambdaVol=fvb/fvs
deltaV=vb-vs
deltaE=es-eb
#Number of measurements
nMeasurements=int(n/meanSteps)
#Reading all the measurements before the response functions
nLine=13
volumeSet=[]
for j in range(0, nMeasurements):
values=lines[nLine].split("\t")
#Introducing read volume (values[0]) in the data set
volumeSet.append(float(values[0]))
#Changing line
nLine+=1
#Changing to molar units
volumeSet=np.multiply(volumeSet, 6.022e23/math.pow(l,3))
#Introducing data set into data array
Volumes.append(statistics.mean(volumeSet))
error.append(statistics.stdev(volumeSet))
fw.write(str(P)+"\t"+str(statistics.mean(volumeSet))+" +- "+str(statistics.stdev(volumeSet))+"\n")
#System parameters
c=6
v0=2e-5
deltaV=0.5e-5
deltaE=1000
lambdaVol=0.2
T=250
#Array of volumes
v=np.linspace(2.02e-5, 2.45e-5, num=1000)
#Array of presures
p1=[]
p2=[]
p3=[]
k=0
for volume in v:
p1.append(T*8.314/deltaV*math.log(lambdaVol*(v0+deltaV-volume)/(volume-v0)))
p2.append(c*deltaE/deltaV*(volume-v0)/deltaV)
p3.append(p1[k]+p2[k])
k+=1
#Plotting results
plt.errorbar(pressures,Volumes,error, marker='o', linewidth=2.0, label='sim')
plt.plot(p3,v, linestyle='dashed', label='theory')
plt.title("Volume-Pressure", fontsize=14)
plt.xlabel("P/Pa")
plt.ylabel(r"$V\ /\ m^3\ mol^-1$")
plt.legend()
plt.show()
#Method that shows the evolution of the energy for a given temperature using the corresponding
#data file 'Measurements_Tx'. It needs the number of values for the mean
def energyEvo(self, T, valuesForMean):
#Creating data arrays
energies=[]
#Data file corresponding to the needed parameters
fileName="Measurements_T"+str(T)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading inputData parameters
line=lines[0].split("=")
l=float(line[1])
line=lines[2].split("=")
n=float(line[1])
line=lines[4].split("=")
vb=float(line[1])
line=lines[5].split("=")
vs=float(line[1])
line=lines[6].split("=")
fvb=float(line[1])
line=lines[7].split("=")
fvs=float(line[1])
line=lines[8].split("=")
eb=float(line[1])
line=lines[9].split("=")
es=float(line[1])
line=lines[10].split("=")
p=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
lambdaVol=fvb/fvs
deltaV=vb-vs
deltaE=es-eb
#Number of data sets
nDataSets=int(n/meanSteps/valuesForMean)
#Reading all the measurements before the response functions
nLine=13
for j in range(0, nDataSets):
#Filling energy data set
energySet=[]
for i in range(0, valuesForMean):
values=lines[nLine].split("\t")
#Introducing read energy (values[1]) in the data set
energySet.append(float(values[1]))
#Changing line
nLine+=1
#Introducing data set into data array
energies.append(energySet)
#Plotting mean values of the energy sets
x=[]
y=[]
error=[]
#Calculating deviation to get error of every energy
#k counter
k=0
for dataSet in energies:
#Initial residue
residue=0
#Calculating mean
meanEnergy=statistics.mean(dataSet)
#Adding mean to data array
y.append(meanEnergy)
x.append(k)
#Calculating standard deviation of every data set
for value in dataSet:
residue=residue+math.pow((value-meanEnergy),2)
stdDeviation=math.sqrt(residue/(nDataSets-1))
error.append(stdDeviation)
k+=1
#Calculating value given by mean-field approximation
#Function to calculate the volume given by the mean field theory
def fun(v):
return Constant().K()/deltaV*math.log(lambdaVol*(vb-v)/(v-vs))-(p-6*deltaE/deltaV*\
(v-vs)/deltaV)/T
volumeUnitCell=Numericalmethods().newton(fun, (vb+vs)/2, 10e-9, 10e-32)
energyUnitCell=-3*deltaE*math.pow((volumeUnitCell-vs)/deltaV,2)
meanFieldEnergy=energyUnitCell*math.pow(l,3)
x2=x
y2=[]
for o in range(0, nDataSets):
y2.append(meanFieldEnergy)
plt.errorbar(x,y,error, label='Sim')
plt.plot(x2,y2,linewidth=2.0, linestyle='dashed', label='Theory')
plt.title("Energy-Time", fontsize=14)
plt.xscale('log')
plt.xlabel("t/Steps")
plt.ylabel("E/J")
plt.legend()
plt.show()
#Method that shows the evolution of the energy as the temperature increases. Creates a file with the simulation results.
def energyEvoTemps(self, iniT, finT, tempIncrement):
Energies=[]
error=[]
temps=[]
#Data file corresponding to the needed parameters
fileName="Measurements_T"+str(iniT)
#Retrieving data from the file
fr=open(fileName, "r")
#Getting file lines
lines=fr.readlines()
#Reading inputData parameters
line=lines[0].split("=")
l=float(line[1])
line=lines[2].split("=")
n=float(line[1])
line=lines[4].split("=")
vb=float(line[1])
line=lines[5].split("=")
vs=float(line[1])
line=lines[6].split("=")
fvb=float(line[1])
line=lines[7].split("=")
fvs=float(line[1])
line=lines[8].split("=")
eb=float(line[1])
line=lines[9].split("=")
es=float(line[1])
line=lines[10].split("=")
p=float(line[1])
line=lines[11].split("=")
meanSteps=float(line[1])
lambdaVol=fvb/fvs