-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvccapp_json_one_var.py
110 lines (85 loc) · 3.19 KB
/
vccapp_json_one_var.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
"""
General Object-oriented Abstraction of VC Cycle
The ideal vaporcompression refrigeration cycle:
Assume
* the evaporator pressure is maintained constant at 0.12MPa
* the mass flow rate of refrigerant is 1 kg/s.
refrigerants: R12,R134a,R22
Condenser pressures: 0.4, 0.5, 0.6, 0.7, 0.8, 0.9,1.0, 1.4 MPa
Calculate the COP of the refrigeration cycle
* Save the results to the csv file
* Plot the COPs against the condenser pressure
Author: Cheng Maohua [email protected]
"""
import os
import matplotlib.pyplot as plt
import csv
import json
from simvcce.vcc.utils import OutFiles
from simvcce.vcc.vccobj import VCCycle
def csv_vars(refrigerants, x, y, FileName):
with open(FileName, 'w', newline='') as csvfile:
fieldnames = ['Condenser_Pressure']
for ant in refrigerants:
fieldnames.append("COP("+ant+")")
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
# rows
for i in range(len(x)):
rowdict = {}
rowdict['Condenser_Pressure'] = f"{x[i]:.2f}"
for j in range(len(refrigerants)):
rowdict["COP("+refrigerants[j] + ")"] = f"{y[j][i]:.2f}"
writer.writerow(rowdict)
def plot_vars(refrigerants, x, y):
plt.title('Variance of Condenser Pressure on Coefficient of Performance')
plt.xlabel('Pressure(MPa)')
plt.ylabel('COP')
for i in range(len(refrigerants)):
plt.plot(x, y[i], "-s", label=refrigerants[i])
plt.legend(loc="best")
plt.show()
if __name__ == "__main__":
import sys
#jsonname = sys.argv[1]
jsonname = "ivcr_var.json"
curpath = os.path.abspath(os.path.dirname(__file__))
json_filename = curpath+'/jsonmodel/'+jsonname
ResultFilePath = curpath+'/result/'
# the base data of cycle
with open(json_filename, 'r') as f:
thedictcycle = json.loads(f.read())
# vars
refrigerants = ["R12", "R134a", "R22"]
cdpressures = {"name": "Condenser",
"oPort": {"p": [0.4, 0.5, 0.6, 0.7, 0.8, 1.0, 1.4]}
}
results = []
for cant in refrigerants:
# var refrigerants
newdictcycle = thedictcycle.copy()
newdictcycle["refrigerant"] = cant
cur_result = []
for i, var in enumerate(cdpressures["oPort"]["p"]):
# var condenser pressures
for device in newdictcycle["components"]:
if device["name"] == cdpressures["name"]:
device["oPort"]["p"] = var
break
# the simulator
cycle = VCCycle(newdictcycle)
cycle.simulator()
cur_result.append(cycle.cop)
# output to console
# OutFiles(cycle)
# output to the file
# ResultFileName = ResultFilePath + \
# thedictcycle['name']+"_"+cant+"_"+str(i+1)+"_"
#OutFiles(cycle, ResultFileName + '.txt')
# after the analysis
results.append(cur_result)
# save to csv
CSVFileName = ResultFilePath + thedictcycle['name']+"_var.csv"
csv_vars(refrigerants, cdpressures["oPort"]["p"], results, CSVFileName)
# plot
plot_vars(refrigerants, cdpressures["oPort"]["p"], results)