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 | """
Template file for hw6:
Directory structure:
hw6/
|-- newton_rootFinder/RootFinder.F90
| findRootMethod_module.F90
| makefile
| read_initFile_module.F90
| definition.h
| ftn_module.F90
| output_module.F90
| setup_module.F90
| (excluding rootFinder.init)
|
|-- pyRun/pyRun_rootFinder.py
All rootFinder.init files and result.png files are stored in the newton_rootFinder directory
"""
import os
import subprocess
import numpy as np
import matplotlib.pyplot as plt
def make_make():
# Change directory from "pyRun/" to
# "newton_rootFinder/" to compile the Fortran code.
path = '../newton_rootFinder'
os.chdir(path)
# Compile the Fortran code if has not been compiled
# (i.e., if 'rootFinder.exe' does not exists yet);
# Otherwise do "make clean" first and then "make".
if os.path.exists('./rootFinder.exe'):
cmd = 'make clean'
os.system(cmd)
os.system("make")
def runtimeParameters_init(run_name, method_type, x_beg, x_end, max_iter, threshold, ftn_type, init_guess, multiplicity):
file_str = 'rootFinder.init'
# check if there is an old 'rootFinder.init' already.
# If so, rename the old one to, say, "rootFinder.init.1" and so on before
# creating a new "rootFinder.init".
if os.path.exists(file_str):
cmd = 'ls rootFinder.init* | wc -l'
i = int(subprocess.check_output(cmd, shell=True))
os.rename(file_str,file_str+str(i))
# generates a new "rootFinder.init"
# runtime parameter file. Use a proper set of
# input arguments to produce a new rootFinder.init
# which has all the necessary input parameters to run the Fortran code.
f = open(file_str, "w")
f.write("run_name"+"\t"+str(run_name)+"\n")
f.write("method_type"+"\t"+method_type+"\n")
f.write("x_beg"+"\t"+str(x_beg)+"\n")
f.write("x_end"+"\t"+str(x_end)+"\n")
f.write("max_iter"+"\t"+str(max_iter)+"\n")
f.write("threshold"+"\t"+str(threshold)+"\n")
f.write("ftn_type"+"\t"+str(ftn_type)+"\n")
f.write("init_guess"+"\t"+str(init_guess)+"\n")
f.write("multiplicity"+"\t"+str(multiplicity)+"\n")
f.close()
def run_rootFinder():
# This one executes the Fortran excutable, "rootFinder.exe"
# using "rootFinder.init" you just created.
cmd = './rootFinder.exe'
os.system(cmd)
def plot_data(plotFileName, ftn_type, threshold, init_guess):
# 1. This function produces two figures:
# (1) solution (y-axis) vs. iteration number (x-axis), and
# (2) error (y-axis) vs. iteration number (x-axis).
iterations = []
result_arr = []
error_arr = []
if os.path.exists(plotFileName):
f = open(plotFileName, 'r')
for line in f:
line = line.split()
iterations.append(int(line[0]))
result_arr.append(float(line[1]))
error_arr.append(float(line[3]))
f.close()
#plot two subfigures and print to screen as well as save to png files
plt.grid(True)
plt.xlim(0, 1000)
plt.ylim(0, 2)
plt.subplot(211)
plt.xlabel('iter')
plt.ylabel('x_new')
plt.title('iter Vs solution')
plt.plot(iterations, result_arr, color="blue")
plt.subplot(212)
plt.xlabel('iter')
plt.ylabel('error')
plt.title('iter Vs error')
plt.plot(iterations, error_arr, color="red")
plt.savefig('result_'+str(ftn_type)+'_'+str(threshold)+'_'+str(init_guess)+'.png')
plt.show()
# After plotting, you save the data file "rootFinder_newton.dat"
# to a new name, e.g.., "rootFinder_newton.dat.1", etc..
cmd = 'ls '+plotFileName+'* | wc -l'
i = int(subprocess.check_output(cmd, shell=True))
os.rename(plotFileName,plotFileName+str(i))
if __name__ == '__main__':
# call to compile Fortran code
make_make()
# Set runtime parameters here
run_name = 'newton'
method_type = 'newton'
x_beg = -10.0
x_end = 10.0
max_iter = 1000
threshold = '1.e-4'
ftn_type = 3
init_guess = 3
multiplicity = 2
# run the rootFinder Fortran code for three different threshold values 1.e-4, 1.e-6, 1.e-8 and plot them
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(" Threshold = 1.e-4, Initial guess = 3 ")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
runtimeParameters_init(run_name, method_type, x_beg, x_end, max_iter, threshold, ftn_type, init_guess, multiplicity)
run_rootFinder()
print("\n")
plotFileName = 'rootFinder_'+run_name+'.dat'
plot_data(plotFileName, ftn_type, threshold, init_guess)
init_guess = 1
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(" Threshold = 1.e-4, Initial guess = 1 ")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
runtimeParameters_init(run_name, method_type, x_beg, x_end, max_iter, threshold, ftn_type, init_guess, multiplicity)
run_rootFinder()
print("\n")
plotFileName = 'rootFinder_'+run_name+'.dat'
plot_data(plotFileName, ftn_type, threshold, init_guess)
threshold = '1.e-6'
init_guess = 3
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(" Threshold = 1.e-6, Initial guess = 3 ")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
runtimeParameters_init(run_name, method_type, x_beg, x_end, max_iter, threshold, ftn_type, init_guess, multiplicity)
run_rootFinder()
print("\n")
plotFileName = 'rootFinder_'+run_name+'.dat'
plot_data(plotFileName, ftn_type, threshold, init_guess)
init_guess = 1
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(" Threshold = 1.e-6, Initial guess = 1 ")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
runtimeParameters_init(run_name, method_type, x_beg, x_end, max_iter, threshold, ftn_type, init_guess, multiplicity)
run_rootFinder()
print("\n")
plotFileName = 'rootFinder_'+run_name+'.dat'
plot_data(plotFileName, ftn_type, threshold, init_guess)
threshold = '1.e-8'
init_guess = 3
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(" Threshold = 1.e-8, Initial guess = 3 ")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
runtimeParameters_init(run_name, method_type, x_beg, x_end, max_iter, threshold, ftn_type, init_guess, multiplicity)
run_rootFinder()
print("\n")
plotFileName = 'rootFinder_'+run_name+'.dat'
plot_data(plotFileName, ftn_type, threshold, init_guess)
init_guess = 1
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print(" Threshold = 1.e-8, Initial guess = 1 ")
print("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
runtimeParameters_init(run_name, method_type, x_beg, x_end, max_iter, threshold, ftn_type, init_guess, multiplicity)
run_rootFinder()
print("\n")
plotFileName = 'rootFinder_'+run_name+'.dat'
plot_data(plotFileName, ftn_type, threshold, init_guess)
print("figures stored in results_<ftn_type>_<threshold>_<init_guess>.png files")
|