Adapt `nofib` code to Foldable-generalised Prelude
[nofib.git] / real / fluid / Input_proc.hs
1 {-
2 Functions for data input. Some data have been put
3 into arrays.
4
5 XZ, 24/10/91
6 -}
7
8 {-
9 Modified to adopt S_Arrays.
10
11 XA, 19/2/92
12 -}
13
14 module Input_proc ( read_fs_cs, read_data ) where
15
16 import Defs
17 import S_Array -- not needed w/ proper module handling
18 import Norm -- ditto
19 (=:) a b = (a,b)
20
21 -----------------------------------------------------------
22 -- reading a Int --
23 -----------------------------------------------------------
24
25 rd_int = \ i -> (head (reads i)) :: (Int,String)
26
27 -----------------------------------------------------------
28 -- reading a Floating --
29 -----------------------------------------------------------
30
31 rd_flt = \ f -> (head (reads f)) :: (Frac_type,String)
32
33 -----------------------------------------------------------
34 -- Reading a pair of values ( could be structed and --
35 -- different values ); results are packed into a tuple. --
36 -----------------------------------------------------------
37
38 rd_pair rf1 rf2 i = ((v1,v2),rest)
39 where
40 (v1,rest1) = (rf1 i)
41 (v2,rest) = (rf2 rest1)
42
43 -----------------------------------------------------------
44 -- Reading a number of values ( could be structed ). --
45 -- Results are packed into a list. --
46 -----------------------------------------------------------
47
48 read_n_val rd_f n in_str =
49 (takeval n in_str, dropval n in_str )
50 where
51 takeval n i
52 | n == 0 = []
53 | otherwise = x : (takeval (n-1) rest)
54 where (x,rest) = (rd_f i)
55 dropval n i
56 | null i = []
57 | n == 0 = i
58 | otherwise = dropval (n-1) rest
59 where (x,rest) = (rd_f i)
60
61 -----------------------------------------------------------
62 -- reading a number of Int --
63 -----------------------------------------------------------
64
65 read_n_int = read_n_val rd_int
66
67 -----------------------------------------------------------
68 -- reading a number of Floating --
69 -----------------------------------------------------------
70
71 read_n_flt = read_n_val rd_flt
72
73 -----------------------------------------------------------
74 -- reading a pair of Int and Floating --
75 -----------------------------------------------------------
76
77 read_i_f_pair = rd_pair rd_int rd_flt
78
79 -----------------------------------------------------------
80 -- Skip over n lines --
81 -----------------------------------------------------------
82
83 skip_lines 0 cs = cs
84 skip_lines n cs =
85 skip_lines (n-1) (drop 1 (dropWhile ((/=) '\n') cs))
86
87 -----------------------------------------------------------
88 -- Reading the data file of our local form. --
89 -- Called at the data setup stage. --
90 -----------------------------------------------------------
91
92 read_fs_cs f =
93 (data_file,(mon,m_iter,m_toler,max_jcb_iter,jcb_toler,relax,dlt_t))
94 where
95 hed1 = skip_lines 1 f
96 (i,hed2) = rd_int hed1
97 start' = skip_lines (i+2) hed2
98 mon = (head (head (drop 9 (words start'))))=='t'
99 start = skip_lines (i+14) hed2
100 ([m_iter,max_jcb_iter],rest1) = read_n_int 2 start
101 ([m_toler,jcb_toler,relax],rest2) = read_n_flt 3 rest1
102 rest3 = skip_lines 2 rest2
103 (dlt_t,rest4) = rd_flt rest3
104 rest5 = skip_lines 11 rest4
105 data_file = head (drop 5 (words rest5))
106
107 read_data f =
108 (e_total,n_total,p_total,v_steer,p_steer,coord,
109 (init_p,init_u),(all_bry,(x_fixed,y_fixed)),p_fixed)
110 where
111 hed1 = skip_lines 1 f
112 (i,hed2) = rd_int hed1
113 start = skip_lines (i+4) hed2
114 ((e_total:n_total:bnd_total:_:_:p_total:_),rest1) =
115 read_n_int 6 start
116 rest2 = skip_lines 2 rest1
117 (v_vals,rest3) =
118 read_n_val (read_n_int (v_nodel+2) ) e_total rest2
119 rest4 = skip_lines 2 rest3
120 (c_vals,rest5) =
121 read_n_val (rd_pair rd_int (read_n_flt 2)) n_total rest4
122 rest6 = skip_lines 2 rest5
123 line_size :: Int
124 line_size =
125 length (words (takeWhile ((/=) '\n') rest6)) - 1
126 (init_vals,rest7) =
127 read_n_val (rd_pair rd_int (read_n_flt line_size)) n_total rest6
128 rest8 = skip_lines 2 rest7
129 (bry_vals,rest9) =
130 read_n_val (rd_pair rd_int (read_n_val
131 (rd_pair rd_int rd_flt) line_size)) bnd_total rest8
132 -- velocity steering vector
133 v_steer =
134 s_array (1,e_total) (map (\(i:_:rest)->i=:rest) v_vals)
135 -- pressure steering vector
136 p_steer =
137 s_array (1,e_total) (map (\(i:_:rest)->i =: rest)
138 (map (take (p_nodel+2)) v_vals))
139 -- node coordinates
140 coord =
141 s_array (1,n_total) (map (\(i,(x:y:_))->i =: (x,y)) c_vals)
142 v_init =
143 map (\(i,(x:y:_))->(i,(x,y))) init_vals
144 -- velocity initial conditions
145 init_u =
146 (
147 s_def_array (1,n_total) (0::Frac_type)
148 [ i =:
149 if x_fixed!^i
150 then
151 (fst.snd.head) (dropWhile (\t->(fst t)/=i) bry_xys)
152 else fst v
153 | (i,v) <- v_init
154 ],
155 s_def_array (1,n_total) (0::Frac_type)
156 [ i =:
157 if y_fixed!^i
158 then
159 (snd.snd.head) (dropWhile (\t->(fst t)/=i) bry_xys)
160 else snd v
161 | (i,v) <- v_init
162 ]
163 )
164 -- pressure initial conditions
165 init_p =
166 s_def_array (1,p_total) (0::Frac_type)
167 [ i =:
168 (
169 if i `elem` p_fixed
170 then (snd.head) (dropWhile (\t->(fst t)/=i) p_cond)
171 else
172 if ( line_size == 5 )
173 then head y
174 else x
175 )
176 | (i,(_:_:x:y)) <- init_vals, i <= p_total
177 ]
178 bry_xys =
179 [(i,(x,y)) | (i,((_,x):(_,y):_)) <- bry_vals]
180 -- velocity nodes which are fixed in the x direction
181 x_fixed =
182 s_def_array (1,n_total) False
183 [ i=:True | (i,((1,_):_)) <- bry_vals]
184 -- velocity nodes which are fixed in the y direction
185 y_fixed =
186 s_def_array (1,n_total) False
187 [ i=:True | (i,(_:(1,_):_)) <- bry_vals]
188 -- all boundary nodes
189 all_bry =
190 s_def_array (1,n_total) False
191 [ i=:True | (i,_) <- bry_vals ]
192 -- fixed pressure nodes
193 p_fixed = map fst p_cond
194 p_cond =
195 if ( line_size == 5 )
196 then [(i,p) | (i,(_:_:_:(1,p):_)) <- bry_vals]
197 else [(i,p) | (i,(_:_:(1,p):_)) <- bry_vals]