forked from PierSaik/RTE-MSDS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndusMachineDomain2.py
279 lines (231 loc) · 8.57 KB
/
IndusMachineDomain2.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
# -*- coding: utf-8 -*-
# Copyright (c) 2016, Pierre Saikaly ([email protected])
# Copyright (c) 2017, RTE (Author: Frederic Troalen)
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#===========================#
# created on 06 june 2016
#===========================#
# Import Python dependencies :
# ----------------------------
import imp
import os
import sys
import numpy as np
from numpy import linalg
import math as m
from math import pi
from math import sqrt
from math import fabs
import random as rand
import time
import shutil
from operator import attrgetter
import subprocess
import multiprocessing as mp
def FwriteFiles(MachinesdtaPath,generators):
"""
Create a file for each machine in reference file :
Inputs :
- MachinesdtaPath : location of reference file
Outputs :
- n single machine dta files
Used in :
- main
"""
regulators = [] # list of regulators for the generator
generators_in = np.zeros(shape=(0,4)) # generators that are in the MachinesdtaPath
generators_out = np.zeros(shape=(0,2)) # generators that are out of MachinesdtaPath
#Opening Reference file :
#------------------------
with open(MachinesdtaPath) as Machinefile:
lines = Machinefile.readlines()
#Looking for machines :
#----------------------
for i, line in enumerate(lines):
if line.startswith("M2 U") or line.startswith("M2 S"):
#Correcting node data :
#----------------------
node = lines[i+1].split(" ", 2)
node[1] = "N1".ljust(len(node[1]))
lines[i+1] = " ".join(node)
#Writing specific machine file :
#--------------------------------
MachineName = lines[i+1].split(None, 1)[0]
if MachineName in generators[:,1]:
gen_in = np.append(generators[MachineName==generators[:,1],:],lines[i+1].split()[2])
gen_in = np.append(gen_in,lines[i+1].split()[3])
generators_in = np.vstack((generators_in,gen_in))
MachineRefName = generators[MachineName==generators[:,1],0][0]
if not os.path.exists(MachineRefName):
os.makedirs(MachineRefName)
time.sleep(0.1)
filename = 'simTest' + '.dta'
filename = os.path.join(MachineRefName, filename)
with open(filename, 'w') as f:
#Writing Header :
#----------------
f.write("HEADER "+ time.strftime("%d/%m/%y") +" 5.1\n \n")
#Writing Machine information :
#-----------------------------
f.write(lines[i])
f.write(lines[i+1])
f.write(lines[i+2])
f.write(lines[i+3])
f.write(lines[i+4])
f.write(lines[i+5])
i += 6
#Writing Regulators information :
#--------------------------------
f.write("\n")
while (("M2 S") not in lines[i] and ("M2 U") not in lines[i]) and i<len(lines)-1:
if lines[i].startswith("R " + MachineName):
f.write(lines[i])
f.write(lines[i+1])
regulators.append(lines[i+1].split()[0])
f.write(" \n \n \n")
i += 1
time.sleep(0.01)
#Writing Network information :
#-----------------------------
f.write("I1 \nN2 0. 0.02 90. 100. \n \n \nLOADP 1\n 1 1. 1. \n \n \n \n \n"
"CH \n1 W\n \n \n")
FcopyRegulators(regulators,MachineRefName)
FwriteSeq(MachineRefName)
time.sleep(0.1)
regulators = []
print MachineName, " is done !"
else:
print MachineName, " already OK"
else:
generators_out = np.vstack((generators_out,generators[MachineName==generators[:,1],:]))
return generators_in, generators_out
def FreadGenerators(GeneratorsPath):
"""
Create a file for each machine in reference file :
Inputs :
- GeneratorsPath : location of generators to process
Outputs :
- Generators : array of generators with dictionary and internal name
Used in :
- main
"""
generators = np.zeros(shape=(0,4))
with open(GeneratorsPath,'r') as GeneratorsFile:
lines = GeneratorsFile.readlines()
for line in lines:
generators = np.vstack((generators,line.split()))
return generators
def FcopyRegulators(regulators, MachineName):
"""
Copy the regulators specified :
Inputs :
- regulators : list of regulators to copy
Outputs :
- None, the regulators are copied in the corresponding folder of the machine
Used in :
- FwriteFiles
"""
for regulator in regulators:
for basename in os.listdir('.'):
if basename.startswith(regulator.lower()):
pathname = os.path.join('.', basename)
if os.path.isfile(pathname):
shutil.copy(pathname, MachineName)
time.sleep(0.01)
def FwriteSeq(MachineName):
"""
Create the seq file for Eurostag simulation
Inputs :
- MachineName
Outputs :
- None, the seq file is created in the folder of the machine
Used in :
-
"""
filename = os.path.join(MachineName,'simTest.seq')
with open(filename,'w') as f:
f.write("HEADER "+ time.strftime("%d/%m/%y") +" 5.1\n \n")
f.write('PARAM\n')
f.write(' 0 0 1\n')
f.write(' 0.0001 0.0001 0.001 0. 0. 1\n')
f.write('\n')
f.write('TIME\n')
f.write(' 0.000001 60. 0.\n')
f.write('\n')
f.write('EVENTS\n')
f.write(' 1000. STOP')
f.write(' \n \n')
def FcallProgram2(generator):
print "Currently working on : ", generator[0]
os.chdir(generator[0])
python_process = os.path.join("..","MachineStableDomainSearch2.py -v 1 -i 0")
time_start = time.clock()
with open("output_indus2.txt",'w') as f:
try:
subprocess.call("python " + python_process, stdout=f, stderr=subprocess.STDOUT)
except:
f.write("WARNING : La recherche n'a pas pu être lancé")
time_elapsed = (time.clock() - time_start)
print generator[0], "is Done : ", time_elpsed
time.sleep(0.1)
os.chdir("..")
def FappendResults2(generators):
"""
Append the results of computed domain to a single text file
Inputs :
- generators : list of inpur generators
Ouputs :
- ampl_generators_domains.txt
Used in :
- main
"""
generator_problematiques = np.zeros(shape=(0,4)) # List of generators with problems
with open("ampl_generators_domains_simplifie.txt", "w") as f:
f.write("# " + time.strftime("%d/%m/%y") + "\n")
f.write("#num id P(MW) Q(MVar) V(kV) RHS(lt) " + "Vnominal(kV)" + " " + "id_internal" + "\n")
with open("ampl_generators_domains_simplifie.txt", "a") as f:
for generator in generators:
resultfile = os.path.join(generator[0],"ampl_generators_domains_MCsvmPQU.txt")
try:
with open(resultfile,'r') as f_res:
lines = f_res.readlines()
for line in lines:
if '#' not in line:
f.write(line)
except IOError:
generator_problematiques = np.vstack((generator_problematiques,generator))
time.sleep(0.1)
return generator_problematiques
'''def Fconstraints(generators_unstabled):
with open("generators_constraints.txt", "w") as f:
f.write("# " + time.strftime("%d/%m/%y") + "\n")
f.write("#num id P(MW) Q(MVar) V(kV) RHS(lt) " + "Vnominal(kV)" + " " + "id_internal" + "\n")
with open("generators_constraints.txt", "r+") as f:
for generator in generators_unstabled:'''
if __name__ == '__main__':
# Get list of worker from cpu count
mp.freeze_support()
PROCESSES = mp.cpu_count()-1
RefdtaFile = sys.argv[1]
GeneratorsFile = sys.argv[2]
DictFile = sys.argv[3]
RefdtaPath = os.path.join(RefdtaFile) # Path of dtaFile containing machine to test
DictPath = os.path.join(DictFile) # Path of file containing the generators
GeneratorsPath = os.path.join(GeneratorsFile) # Path of file containing generators to test
generators = FreadGenerators(GeneratorsPath)
print "Files have all been written \n"
pool = mp.Pool(processes=PROCESSES)
for generator in generators:
pool.apply_async(FcallProgram2, args=(generator,))
time.sleep(0.1)
pool.close()
pool.join()
# waiting for processes to close
time.sleep(1)
print "Fin du calcul - Concatenation des resultats"
generators_prob = FappendResults2(generators)
print "Liste des generateurs ayant eu un prb : \n" , generators_prob
np.savetxt('generators_pb2.txt',generators_prob, fmt='%s')
print "Un export a ete fait dans le fichier :\n > generators_pb2.txt \n"