~fluidity-core/fluidity/darcy_weak_bcs

« back to all changes in this revision

Viewing changes to tests/darcy_impes_p1_2phase_coreyrelperm_velBCinlet_strongpressoutlet_p1satdiag_parallel/Linear_Interp_1D.py

  • Committer: Brendan Tollit
  • Date: 2012-07-23 10:48:00 UTC
  • Revision ID: brendan.tollit05@imperial.ac.uk-20120723104800-6yytrhxneuwv13zr
Add functionality to adapt mesh at first timestep for darcy model
using same routines as within time step adapt - required to 
recalc all the special darcy impes diagnostic fields

Add an adaptive mesh test and a seperate parallel test

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
""" 
 
4
 
 
5
 Linear_Interp_1D.py
 
6
 
 
7
 The Linear_Interp_1D class will read a 1d solution field,
 
8
 then find the interpolated solution value at specified 
 
9
 x coordinate.
 
10
 
 
11
 The driver main will read a interpolation mesh and 
 
12
 then output the interpolated solution field.
 
13
 
 
14
 The x coordinate assoicated with the solution field must 
 
15
 be ascending.
 
16
 
 
17
 The driver main is used via:
 
18
 
 
19
 Linear_Interp.py solution_file_name interpolation_mesh_file_name interpolation_solution_file_name
 
20
 
 
21
"""
 
22
 
 
23
import sys
 
24
import os
 
25
  
 
26
class Linear_Interp_1D:
 
27
   
 
28
   def __init__(self, solution_file_name):
 
29
      """ Initialise Linear_Interp_1D class """
 
30
      
 
31
      # open two input files
 
32
      self.solution_file = open(solution_file_name,"r")
 
33
      
 
34
      # read the solution file  and x coord into lists
 
35
      self.read_solution_file()
 
36
      
 
37
      # check that the coord mesh has ascending x values
 
38
      self.check_mesh_has_ascending_x_coord()
 
39
      
 
40
      # if number of x_coord not bigger than 1 then not going to work
 
41
      if self.num_x_coord < 2:
 
42
         
 
43
         print "Error as x coord of solution has less than 2 values"
 
44
         
 
45
         sys.exit()
 
46
      
 
47
   def read_solution_file(self):
 
48
      """ Read the solution input file """
 
49
      
 
50
      # initialise the input lists
 
51
      self.x_coord = []
 
52
      self.solution = []
 
53
      
 
54
      # go to start of file
 
55
      self.solution_file.seek(0)      
 
56
      
 
57
      # This assume a particular file format
 
58
                   
 
59
      # initialise counter
 
60
      self.num_x_coord = 0
 
61
 
 
62
      for line in self.solution_file:
 
63
         
 
64
         # skip blank lines (ie a line that cannot be stripped)
 
65
         if not line.strip():
 
66
            
 
67
            continue
 
68
         
 
69
         else:
 
70
            
 
71
            pass
 
72
         
 
73
         # count num_x_coord found
 
74
         self.num_x_coord = self.num_x_coord + 1
 
75
         
 
76
         self.x_coord.append(float(line.split()[0]))
 
77
         self.solution.append(float(line.split()[1]))
 
78
   
 
79
   def check_mesh_has_ascending_x_coord(self):
 
80
      """ Check that the mesh has an ascending x coordinate """
 
81
      
 
82
      for node in range(2,len(self.x_coord)):
 
83
         
 
84
         if self.x_coord[node] <= self.x_coord[node-1]:
 
85
            
 
86
            print "Issue with solution mesh: x coordinate not ascending for nodes:",node,node-1
 
87
            
 
88
            sys.exit()
 
89
            
 
90
   def interp(self, x):
 
91
      """ Find the interpolated value at coordinate x """        
 
92
      
 
93
      # bound the interpolation to the known x_coord min and max values
 
94
      
 
95
      if x <= self.x_coord[0]:
 
96
         
 
97
         s = self.solution[0]
 
98
      
 
99
      elif x >= self.x_coord[-1]:
 
100
         
 
101
         s = self.solution[-1]
 
102
      
 
103
      else:
 
104
         
 
105
         # find which ele (or interval) this x resides in
 
106
         for ele in range(1,len(self.x_coord)):
 
107
            
 
108
            x_right = self.x_coord[ele]
 
109
            
 
110
            if x <= x_right:
 
111
               
 
112
               break
 
113
         
 
114
         # Find the x left, s left and right
 
115
         x_left = self.x_coord[ele - 1]
 
116
         
 
117
         s_right = self.solution[ele]
 
118
         s_left  = self.solution[ele-1]
 
119
         
 
120
         # Find delta x
 
121
         delta_x = (x - x_left) / (x_right - x_left)
 
122
         
 
123
         # linear interpolate
 
124
         s = (1.0 - delta_x) * s_left + delta_x * s_right
 
125
                                    
 
126
      return s
 
127
         
 
128
if __name__ == "__main__":
 
129
   
 
130
   try:
 
131
 
 
132
      solution_file_name = sys.argv[1]
 
133
            
 
134
      interpolation_mesh_file_name = sys.argv[2]
 
135
 
 
136
      interpolation_solution_file_name = sys.argv[3]
 
137
            
 
138
   except:
 
139
    
 
140
     print "Usage: Linear_Interp_1D.py solution_file_name interpolation_mesh_file_name interpolation_solution_file_name"
 
141
     
 
142
     sys.exit()
 
143
   
 
144
   print "Running Linear_Interp_1D.py ",solution_file_name, interpolation_mesh_file_name, interpolation_solution_file_name
 
145
   
 
146
   # initialise
 
147
   LI = Linear_Interp_1D(solution_file_name)
 
148
   
 
149
   # initialise the interpolated x coord and solution lists
 
150
   x_coord_interp = []
 
151
   solution_interp = []
 
152
   
 
153
   # open the file which has the solution interpolation mesh
 
154
   interpolation_mesh_file = open(interpolation_mesh_file_name,"r")
 
155
   
 
156
   # read the interpolated solution x coord and initialise interpolated solution
 
157
   for line in interpolation_mesh_file:
 
158
      
 
159
      # skip blank lines (ie a line that cannot be stripped)
 
160
      if not line.strip():
 
161
            
 
162
         continue
 
163
         
 
164
      else:
 
165
            
 
166
         pass
 
167
      
 
168
      x_coord_interp.append(float(line.split()[0]))         
 
169
       
 
170
      solution_interp.append(0.0)
 
171
   
 
172
   # find the interpolated solution for each of the required points
 
173
   for x_index in range(len(x_coord_interp)): 
 
174
    
 
175
     solution_interp[x_index] = LI.interp(x_coord_interp[x_index])
 
176
   
 
177
   # open the interpolated solution output file name
 
178
   interpolation_solution_file = open(interpolation_solution_file_name,"w")
 
179
   
 
180
   # write out the interpolated x_coord_interp solution_interp
 
181
   for x_index in range(len(x_coord_interp)): 
 
182
      
 
183
     interpolation_solution_file.write(str(x_coord_interp[x_index])+" "+str(solution_interp[x_index])+"\n") 
 
184
   
 
185
   interpolation_solution_file.close()
 
186