-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptim.py
251 lines (198 loc) · 7.56 KB
/
optim.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
"""This modelu prepares and performs the optimization of the model Hydrus1D
This modelu read the observer data from existing Fit.out file. It read the
modeled data after the Hydrus computation finishes and compare the modeled and
measured data. The scipy.potimize package is then used to performs the
optimization of parameters.
"""
__author__ = "Jakub Jerabek"
__license__ = "GPL"
__email__ = "jakub.jerabek@fsv.cvut.cz"
import os
import numpy as np
import math
import shutil
from sys import platform
from scipy.interpolate import interp1d
from scipy.optimize import differential_evolution
from scipy.optimize import minimize
class Data(object):
def __init__(self, time, val, position):
self._n = len(time)
self.time = np.zeros(self._n, float)
self.val = np.zeros(self._n, float)
self.positoin = np.zeros(self._n, float)
self.time = np.array([float(i) for i in time])
self.val = np.array([float(i) for i in val])
self.position = np.array([int(i) for i in position])
class ObsData(object):
def __init__(self, time, val, position, nmat):
time = np.array([float(i) for i in time])
val = np.array([float(i) for i in val])
position = np.array([int(i) for i in position])
self.nmat = nmat
self.data = []
for ipos in np.unique(position):
which = ipos == position
self.data.append(Data(time[which], val[which], position[which]))
class ModData(object):
def __init__(self, time, val, position, nmat):
time = np.array([float(i) for i in time])
val = np.array([float(i) for i in val])
position = np.array([int(i) for i in position])
self.nmat = nmat
self.data = []
for ipos in np.unique(position):
which = ipos == position
self.data.append(Data(time[which], val[which], position[which]))
class Optim(object):
def __init__(self, hydro_proj, outdir):
self.hp = hydro_proj
self.outdir = outdir
if platform == "linux" or platform == "linux2":
self.exe = 'wine H1D_CALC.EXE'
self.cmd = '{} {}'.format(self.exe, self.hp)
elif platform == "win32":
self.exe = 'H1D_CALC.EXE'
self.cmd = './{} {}'.format(self.exe, self.hp)
else:
import sys
sys.exit('unknown platform')
tmp = self.read_measured()
self.obs = ObsData(tmp[0], tmp[1], tmp[2], tmp[3])
self.nmat = self.obs.nmat
self.obsnodes = len(self.obs.data)
self.err = '{}{}'.format(self.hp, 'Error.msg')
if os.path.exists(self.err):
os.remove(self.err)
if os.path.exists(self.outdir):
shutil.rmtree(self.outdir)
os.mkdir(self.outdir)
self.outfile = open('{}/{}'.format(self.outdir, 'rrsqrt-pamameters.txt'),'w')
self.Counter = 0
def get_params(self):
file_ = '{}/{}'.format(self.hp, 'SELECTOR.IN')
with open(file_,'r') as f:
lines = f.readlines()
NMat = int(lines[13].split()[0])
params = []
for i in range(NMat):
line = lines[26+i].split()
params.append([float(i) for i in line])
return params
def set_params(self, params):
""" Sets params in to SELECTOR.IN """
file_ = '{}/{}'.format(self.hp, 'SELECTOR.IN')
with open(file_,'r') as f:
lines = f.readlines()
NMat = int(lines[13].split()[0])
# TODO
#if not(len(params) == NMat): ERROR
nparams = len(params)
nmat = self.nmat
i = 0
for i in range(nmat): # replace lines in selector
ii = range(i*(nparams/nmat),((i+1)*nparams/nmat))
p = params[ii]
str_ = ' '.join([str(elem) for elem in p]) + '\n'
lines[26+i] = str_
with open(file_,'w') as f:
f.writelines(lines)
def read_modeled(self):
file_ = '{}/{}'.format(self.hp, 'Obs_Node.out')
with open(file_,'r') as f:
lines = f.readlines()
counter = 0
for line in lines:
if 'time' in line:
start = counter
if 'end' in line:
end = counter
counter += 1
ctime = 0
time = []
val = []
position = []
for i in range(start+1, end):
line = lines[i].replace('*', ' ').split()
for imat in range(self.obsnodes):
cval = imat*3+1
try:
float(line[ctime])
time.append(line[ctime])
val.append(line[cval])
position.append(imat)
except:
print ('error lines')
return time, val, position, self.nmat
def read_measured(self):
file_ = '{}/{}'.format(self.hp, 'Fit.out')
with open(file_,'r') as f:
lines = f.readlines()
counter = 0
for line in lines:
if 'Observed Quantity' in line:
start = counter
if 'Parameter estimation with' in line:
end = counter
counter += 1
time = []
val = []
position = []
for i in range(start+3, end-3):
line = lines[i].split()
time.append(line[1])
val.append(line[2])
position.append(line[4])
file_ = '{}/{}'.format(self.hp, 'SELECTOR.IN')
with open(file_,'r') as f:
lines = f.readlines()
NMat = int(lines[13].split()[0])
return time, val, position, NMat
def _interpolate(self, val, time, time2):
f = interp1d(time, val)
return f(time2)
def sumofsquares(self):
obsval = np.array([])
modval_interp = np.array([])
for imat in range(self.obsnodes):
obsval = np.append(obsval, self.obs.data[imat].val)
modval_interp = np.append(modval_interp,
self._interpolate(self.mod.data[imat].val,
self.mod.data[imat].time, self.obs.data[imat].time))
return np.sum((np.array(obsval) - np.array(modval_interp))**2.)
def model(self, params):
""" params :: thr ths Alfa n Ks l
in case of 2 soils paras list of two lists
"""
newresults = '{}.{}'.format(os.path.split(self.outdir)[1],
str(self.Counter).zfill(4))
self.Counter += 1
params[2] = 10**params[2]
params[8] = 10**params[8]
self.set_params(params)
# RUN HYDRUS
os.system(self.cmd)
if os.path.exists(self.err):
os.remove(self.err)
ss = 100000
str_ = ' '.join([str(elem) for elem in params])
outline = '{} {}\n'.format(ss, str_)
self.outfile.write(outline)
return (ss)
shutil.copytree(self.hp, '{}/{}'.format(self.outdir, newresults))
tmp = self.read_modeled()
self.mod = ModData(tmp[0], tmp[1], tmp[2], tmp[3])
ss = self.sumofsquares()
str_ = ' '.join([str(elem) for elem in params])
outline = '{} {}\n'.format(ss, str_)
self.outfile.write(outline)
print (ss)
return (ss)
def run(self):
pass
bounds = [(0,0.2),(0.25,0.3),(-4, -1), (1.25, 1.37), (2, 10), (0.5,0.5),
(0,0.05),(0.25,0.3),(-4, -1), (1.21, 1.37), (2, 10), (0.5,0.5)]
#bounds = [0.2,0.5,0.014, 1.25, 10, 0.5,
# 0.2,0.5,0.014, 1.25, 10, 0.5]
differential_evolution(self.model, bounds)
#minimize(self.model, bounds, method='CG')