def read_jsonfile(filename): """ rankine cycle in json file""" # 1 read json file to dict with open(filename, 'r') as f: rkcyc = json.load(f) #rkcyc = json.loads(f.read()) # print(rkcyc) name = rkcyc["name"] dictnodes = rkcyc["nodes"] dictcomps = rkcyc["comps"] # 2 convert dict to the object of nodes countNodes = len(dictnodes) nodes = [None for i in range(countNodes)] for curnode in dictnodes: i = int(curnode['id']) nodes[i] = Node(curnode['name'], i) try: nodes[i].p = float(curnode['p']) except: nodes[i].p = None try: nodes[i].t = float(curnode['t']) except: nodes[i].t = None try: nodes[i].x = float(curnode['x']) except: nodes[i].x = None try: nodes[i].fdot = float(curnode['fdot']) except: nodes[i].fdot = None if nodes[i].p != None and nodes[i].t != None: nodes[i].pt() elif nodes[i].p != None and nodes[i].x != None: nodes[i].px() elif nodes[i].t != None and nodes[i].x != None: nodes[i].tx() # 3 convert dict to the object of Comps DevNum = len(dictcomps) Comps = {} for curdev in dictcomps: Comps[curdev['name']] = compdict[curdev['type']](curdev) return name, nodes, countNodes, Comps, DevNum
def read_nodesfile(filename): """ csvfile:node's info in the file""" countNodes = len(open(filename, 'r').readlines()) - 1 nodes = [None for i in range(countNodes)] csvfile = open(filename, 'r') reader = csv.DictReader(csvfile) for line in reader: i = int(line['NID']) nodes[i] = Node(line['NAME'], i) try: nodes[i].p = float(line['p']) except: nodes[i].p = None try: nodes[i].t = float(line['t']) except: nodes[i].t = None try: nodes[i].x = float(line['x']) except: nodes[i].x = None try: nodes[i].fdot = float(line['fdot']) except: nodes[i].fdot = None if line['p'] != '' and line['t'] != '': nodes[i].pt() elif line['p'] != '' and line['x'] != '': nodes[i].px() elif line['t'] != '' and line['x'] != '': nodes[i].tx() csvfile.close() return nodes, countNodes
def read_nodesfile(filename): """ csvfile:node's info in the file""" countNodes = len(open(filename, 'r').readlines()) - 1 nodes = [None for i in range(countNodes)] ndsFile = open(filename, 'r') discardHeader = ndsFile.readline() for line in ndsFile: NAME, NID, p, t, x, fdot = line.split(',') i = int(NID) nodes[i] = Node(NAME, i) try: nodes[i].p = float(p) except: nodes[i].p = None try: nodes[i].t = float(t) except: nodes[i].t = None try: nodes[i].x = float(x) except: nodes[i].x = None try: nodes[i].fdot = float(fdot) except: nodes[i].fdot = None if nodes[i].p != None and nodes[i].t != None: nodes[i].pt() elif nodes[i].p != None and nodes[i].x != None: nodes[i].px() elif nodes[i].t != None and nodes[i].x != None: nodes[i].tx() ndsFile.close() return nodes, countNodes
def read_jsonfile(filename): """ rankine cycle in json file""" # 1 read json file to dict with open(filename, 'r') as f: rkcyc = json.loads(f.read()) # print(rkcyc) name = rkcyc["name"] dictnodes = rkcyc["nodes"] dictcomps = rkcyc["comps"] # 2 convert dict nodes to the object nodes countNodes = len(dictnodes) nodes = [None for i in range(countNodes)] for curnode in dictnodes: i = int(curnode['id']) nodes[i] = Node(curnode['name'], i) try: nodes[i].p = float(curnode['p']) except: nodes[i].p = None try: nodes[i].t = float(curnode['t']) except: nodes[i].t = None try: nodes[i].x = float(curnode['x']) except: nodes[i].x = None try: nodes[i].fdot = float(curnode['fdot']) except: nodes[i].fdot = None if nodes[i].p != None and nodes[i].t != None: nodes[i].pt() elif nodes[i].p != None and nodes[i].x != None: nodes[i].px() elif nodes[i].t != None and nodes[i].x != None: nodes[i].tx() # 3 convert dict Comps to the object Comps DevNum = len(dictcomps) Comps = {} for curdev in dictcomps: if curdev['type'] == "TURBINE-EX1": Comps[curdev['name']] = Turbine(curdev['name'], curdev['inNode'], curdev['outNode'], curdev['extNode'], ef=curdev['eff']) elif curdev['type'] == "TURBINE-EX0": Comps[curdev['name']] = Turbine(curdev['name'], curdev['inNode'], curdev['outNode'], ef=curdev['eff']) elif curdev['type'] == "BOILER": Comps[curdev['name']] = Boiler(curdev['name'], curdev['inNode'], curdev['outNode']) elif curdev['type'] == "CONDENSER": Comps[curdev['name']] = Condenser(curdev['name'], curdev['inNode'], curdev['outNode']) elif curdev['type'] == "PUMP": Comps[curdev['name']] = Pump(curdev['name'], curdev['inNode'], curdev['outNode'], curdev['eff']) elif curdev['type'] == "OH-FEEDWATER-DW0": Comps[curdev['name']] = Openedheater(curdev['name'], curdev['inNode'], curdev['inNode_fw'], curdev['outNode_fw']) return name, nodes, countNodes, Comps, DevNum