Merge /u/hjasak/foam-extend-3.1/ branch HrvojeJasak into nextRelease
[foam-extend-3.2.git] / bin / plotResidual.py
blob53d6504153fde218e758c9e520b518d198af9ad3
1 #!/usr/bin/python
3 import sys
4 if len(sys.argv) != 2:
5 print 'script requires name of log file'
6 sys.exit()
8 logfilename = sys.argv[1]
9 print 'Reading file', logfilename
11 import re
12 UpRegex=r"([A-Z,a-z]*):*.*Solving for Up, Initial residual = \(([0-9.Ee\-+]*)\s([0-9.Ee\-+]*)\s([0-9.Ee\-+]*)\s([0-9.Ee\-+]*)\), Final residual = \(([0-9.Ee\-+]*)\s([0-9.Ee\-+]*)\s([0-9.Ee\-+]*)\s([0-9.Ee\-+]*)\), No Iterations ([0-9]*)"
13 kRegex=r"([A-Z,a-z]*):*.*Solving for k, Initial residual = ([0-9.Ee\-+]*), Final residual = ([0-9.Ee\-+]*), No Iterations ([0-9]*)"
14 omegaRegex=r"([A-Z,a-z]*):*.*Solving for omega, Initial residual = ([0-9.Ee\-+]*), Final residual = ([0-9.Ee\-+]*), No Iterations ([0-9]*)"
15 epsilonRegex=r"([A-Z,a-z]*):*.*Solving for epsilon, Initial residual = ([0-9.Ee\-+]*), Final residual = ([0-9.Ee\-+]*), No Iterations ([0-9]*)"
17 tUp = []
18 Ux = []
19 Uy = []
20 Uz = []
21 p = []
22 iUp = 0
24 tk = []
25 k = []
26 ik = 0
28 tomega = []
29 omega = []
30 iomega = 0
32 tepsilon = []
33 epsilon = []
34 iepsilon = 0
36 #HJ take name of log file as script argument
37 pipefile=open(logfilename,'r')
38 lines = pipefile.readlines()
40 for line in lines:
41 matchUp=re.search(UpRegex,line)
42 if matchUp:
43 iUp = iUp + 1
44 tUp.append(iUp)
45 Ux.append(float(matchUp.group(2)))
46 Uy.append(float(matchUp.group(3)))
47 Uz.append(float(matchUp.group(4)))
48 p.append(float(matchUp.group(5)))
49 matchk=re.search(kRegex,line)
50 if matchk:
51 ik = ik + 1
52 tk.append(ik)
53 k.append(float(matchk.group(2)))
54 matchomega=re.search(omegaRegex,line)
55 if matchomega:
56 iomega = iomega + 1
57 tomega.append(iomega)
58 omega.append(float(matchomega.group(2)))
59 matchepsilon=re.search(epsilonRegex,line)
60 if matchepsilon:
61 iepsilon = iepsilon + 1
62 tepsilon.append(iepsilon)
63 epsilon.append(float(matchepsilon.group(2)))
65 outfile=open('residual.dat','w')
67 #HJ need better way of combining lists
68 if iomega > 0:
69 for index in range(0,iomega):
70 outfile.write(str(tUp[index])+' '+str(Ux[index])+' '+str(Uy[index])+' '+str(Uz[index])+' '+str(p[index])+' '+str(k[index])+' '+str(omega[index])+'\n')
71 elif iepsilon > 0:
72 for index in range(0,iepsilon):
73 outfile.write(str(tUp[index])+' '+str(Ux[index])+' '+str(Uy[index])+' '+str(Uz[index])+' '+str(p[index])+' '+str(k[index])+' '+str(epsilon[index])+'\n')
74 elif iUp > 0:
75 for index in range(0,iUp):
76 outfile.write(str(tUp[index])+' '+str(Ux[index])+' '+str(Uy[index])+' '+str(Uz[index])+' '+str(p[index])+'\n')
78 outfile.close()
80 # prepare plot
81 import pylab
82 pylab.xlabel('iteration')
83 pylab.ylabel('residual')
84 pylab.grid(True)
86 # plot in log scale
87 if iUp > 0:
88 pylab.semilogy(tUp,Ux,'-',label="Ux")
89 pylab.semilogy(tUp,Uy,'-',label="Uy")
90 pylab.semilogy(tUp,Uz,'-',label="Uz")
91 pylab.semilogy(tUp,p,'-',label="p")
93 if ik > 0:
94 pylab.semilogy(tk,k,'-',label="k")
96 if iomega > 0:
97 pylab.semilogy(tomega,omega,'-',label="omega")
99 if iepsilon > 0:
100 pylab.semilogy(tepsilon,epsilon,'-',label="epsilon")
102 pylab.legend()
103 pylab.show()