-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDyMMMBatchSimulator.py
More file actions
158 lines (133 loc) · 5.17 KB
/
DyMMMBatchSimulator.py
File metadata and controls
158 lines (133 loc) · 5.17 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
import time
import sys
import os
import traceback
import pandas as pd
from pprint import pprint
import multiprocessing as mp
from importlib import import_module
import DyMMMSettings as settings
from DyMMMODESolver import DyMMMODESolver
import time
from datetime import datetime
from DyMMMDataAnalyzer import DyMMMDataAnalyzer
import numpy as np
from os import environ
import subprocess
class DyMMMBatchSimulator:
columnNames=None
CHUNKSIZE=10
CPU_COUNT=20
def __init__(self):
self.communitiesDir=settings.simSettings["communitiesDir"]
self.communityName=settings.simSettings["communityName"]
print(self.communityName)
sys.path.append(self.communitiesDir)
self.communityDir=self.communitiesDir+"/"+self.communityName
self.DyMMMCommunity = import_module('{}.DyMMMCommunity'.format(self.communityName)).DyMMMCommunity
self.stopTime=settings.simSettings['stopTime']
self.solverName=settings.simSettings["solverName"]
def process_frame(self, id, df):
for index, row in df.iterrows():
outFile=self.outputDir+"/"+self.communityName+"_"+'{0:05}'.format(index)
if os.path.exists(outFile+".csv") == False:
print([sys.executable, "main_runDyMMMOnFile.py",self.paramsFile, str(index)])
subprocess.run([sys.executable, "main_runDyMMMOnFile.py",self.paramsFile, str(index)])
if os.path.exists(outFile+"_j.csv") == False:
print([sys.executable, "main_runDyMMM_jacobianOnFile2.py",self.paramsFile, str(index)])
subprocess.run([sys.executable, "main_runDyMMM_jacobianOnFile2.py",self.paramsFile, str(index)])
#os.system("python main_runDyMMMOnFile.py "outFile+".csv "+str(index))
return
def isSteadyState(self, df, colName):
time1=df['time'].iloc[-1]
time0=time1-1
row0=df.loc[(df['time'] <= time0)]
row1=df.loc[(df['time'] == time1)]
value0=row0[colName].iloc[-1]
value1=row1[colName].iloc[-1]
error=abs(value1-value0)
return error < 1e-2
def runSimulation(self, paramsFile):
self.paramsFile=paramsFile
self.outputDir=paramsFile+"dir"
if not os.path.exists(self.outputDir):
os.makedirs(self.outputDir)
analyzer=DyMMMDataAnalyzer(paramsFile)
pendingList=analyzer.getPendingSimulationList()
if len(pendingList) > 0:
pprint(pendingList)
print(len(pendingList))
else:
print("Data generation complete")
return
df = pd.read_csv(paramsFile)
if (self.columnNames is None):
self.columnNames=df.columns.tolist()
self.process_frame(0, df)
return
def runSimulationParallel(self, paramsFile):
self.paramsFile=paramsFile
self.outputDir=paramsFile+"dir"
if not os.path.exists(self.outputDir):
os.makedirs(self.outputDir)
analyzer=DyMMMDataAnalyzer(paramsFile)
pendingList=analyzer.getPendingSimulationList()
if len(pendingList) > 0:
pprint(pendingList)
print(len(pendingList))
if(len(pendingList) > self.CPU_COUNT):
self.CHUNKSIZE=int(len(pendingList)/self.CPU_COUNT)
else:
self.CHUNKSIZE=1
print("CHUNKSIZE="+str(self.CHUNKSIZE))
else:
print("Data generation complete")
return
reader = pd.read_table(paramsFile, chunksize=self.CHUNKSIZE, sep=",")
pool = mp.Pool(self.CPU_COUNT) # use 4 processes
funclist = []
id=0
for df in reader:
if (self.columnNames is None):
self.columnNames=df.columns.tolist()
# process each data frame
f = pool.apply_async(self.process_frame,[id, df])
funclist.append(f)
id+=1
result = 0
for f in funclist:
try:
id, ret = f.get(timeout=3000) # timeout in 10 seconds
except Exception as e:
#print("function took longer than %d seconds" % error.args[1])
print(e)
continue
result+=ret
print("frame completed------------------"+str(id))
print("There are %d rows of data"%(result))
if __name__ == '__main__':
paramDataFile=None
simulationStatus=True
index=0
analysisDir=settings.simSettings["analysisDir"]
while simulationStatus:
inputFile = analysisDir+"/params_"+'{0:05}'.format(index)
if os.path.exists(inputFile+".csv"):
if os.path.exists(inputFile+"_RESULT.csv")==True and os.path.exists(inputFile+"_j.csv")==True:
index+=1
continue
else:
paramDataFile=inputFile
break
else:
break
if paramDataFile == None:
print("nothing to simulate")
exit(0)
simulator=DyMMMBatchSimulator()
try:
simulator.runSimulationParallel(paramDataFile+".csv")
except Exception as e:
print(e)
traceback.print_exc(file=sys.stdout)
print("Exception occurred in main")