Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/elmerice/examples/Inverse_Methods/SCRIPTS/MakeReport.py
3206 views
1
#
2
# Make a report from output files of the inverse method
3
# make basic plots of cost function and norm of the gradient
4
# and report last lines of M1QN3_<RUN_NAME>.out
5
# Require files: M1QN3_<RUN_NAME>.out
6
# Cost_<RUN_NAME>.dat
7
# CostReg_<RUN_NAME>.dat
8
# GradientNormAdjoint_<RUN_NAME>.dat
9
from collections import deque
10
from matplotlib import pyplot as plt
11
from matplotlib.backends.backend_pdf import PdfPages
12
from pylab import genfromtxt;
13
import numpy as np
14
import sys
15
16
try:
17
sys.argv[1]
18
except:
19
print('error: try ')
20
print(' python MakeReport.py RUN_NAME')
21
exit()
22
23
# Output from M1QN3
24
fname='M1QN3_%s.out'%(sys.argv[1])
25
try:
26
with open(fname) as file:
27
pass
28
except IOError as e:
29
print('Unable to open file %s'%(fname))
30
exit()
31
32
with open(fname) as fin:
33
last = deque(fin, 7)
34
output=''.join(list(last))
35
36
# Cost file
37
fname='Cost_%s.dat'%(sys.argv[1])
38
try:
39
with open(fname) as file:
40
pass
41
except IOError as e:
42
print('Unable to open file %s'%(fname))
43
exit()
44
cost = genfromtxt(fname,skip_header=3);
45
46
# Gradient norm
47
fname='GradientNormAdjoint_%s.dat'%(sys.argv[1])
48
try:
49
with open(fname) as file:
50
pass
51
except IOError as e:
52
print('Unable to open file %s'%(fname))
53
exit()
54
grad = genfromtxt(fname,skip_header=1);
55
56
# Regularisation
57
fname='CostReg_%s.dat'%(sys.argv[1])
58
try:
59
with open(fname) as file:
60
pass
61
except IOError as e:
62
print('Unable to open file %s'%(fname))
63
exit()
64
reg = genfromtxt(fname,skip_header=3);
65
with open(fname, 'r') as f:
66
first_line = f.readline().strip()
67
print(first_line.split((',')))
68
69
# get regularisation patrameter
70
line = open(fname, "r").readlines()[1]
71
lreg=float(line.split((','))[1])
72
print(lreg)
73
74
# Make the report
75
with PdfPages('Report_%s.pdf'%(sys.argv[1])) as pdf:
76
fig = plt.figure()
77
fig.suptitle("Convergence plots", fontsize=16)
78
plt.subplot(3,1,1)
79
plt.semilogx(cost[:,0],cost[:,2])
80
plt.ylabel('rms (m/a)')
81
82
plt.subplot(3,1,2)
83
plt.loglog(cost[:,0],cost[:,1],label='$J_0$')
84
plt.loglog(reg[:,0],lreg*reg[:,1],label=('%1.2e$ * J_{reg}$'%(lreg)))
85
plt.legend(fontsize='10')
86
plt.ylim((0.01*np.amin(cost[:,1]),5*np.amax(cost[:,1])))
87
plt.ylabel('$J$')
88
89
plt.subplot(3,1,3)
90
plt.loglog(grad[:,0],grad[:,1]/grad[0,1])
91
plt.xlabel('iter. number')
92
plt.ylabel(r'$|\!|g|\!|/|\!|g_0|\!|$')
93
plt.tight_layout()
94
fig.subplots_adjust(top=0.88)
95
pdf.savefig()
96
plt.close()
97
98
fig = plt.figure()
99
fig.suptitle("M1QN3 last 7 lines", fontsize=16)
100
fig.text(.1,.5,output)
101
pdf.savefig() # saves the current figure into a pdf page
102
plt.close()
103
104