@@ -39,8 +39,8 @@ def solver_FECS(I, U0, v, L, dt, C, T, user_action=None):
3939
4040
4141def solver (I , U0 , v , L , dt , C , T , user_action = None ,
42- scheme = 'FE' , periodic_bc = True ):
43- Nt = int (round (T / float (dt )))
42+ scheme = 'FE' , periodic_bc = False ):
43+ Nt = int (round (T / np . float64 (dt )))
4444 t = np .linspace (0 , Nt * dt , Nt + 1 ) # Mesh points in time
4545 dx = v * dt / C
4646 Nx = int (round (L / dx ))
@@ -56,8 +56,6 @@ def solver(I, U0, v, L, dt, C, T, user_action=None,
5656
5757 grid = Grid (shape = (Nx + 1 ,), extent = (L ,), dtype = np .float64 )
5858 t_s = grid .time_dim
59- u = None
60- pde = None
6159
6260 def u (to = 1 , so = 1 ):
6361 u = TimeFunction (name = 'u' , grid = grid , time_order = to , space_order = so , save = Nt + 1 )
@@ -72,39 +70,18 @@ def u(to=1, so=1):
7270
7371 elif scheme == 'LF' :
7472 # Use UP scheme for first timestep
75- u1 = TimeFunction (name = 'u1' , grid = grid , save = 2 )
76- pde1 = u1 .dtr + v * u1 .dxl
77-
78- stencil1 = solve (pde1 , u1 .forward )
79- eq1 = Eq (u1 .forward , stencil1 )
80-
81- # Set initial condition u(x,0) = I(x)
82- u1 .data [0 , :] = [I (xi ) for xi in x ]
83-
84- bc1 = [Eq (u1 [t_s + 1 , 0 ], U0 )]
85- pbc1 = [Eq (u1 [t_s , 0 ], u1 [t_s , Nx ])]
86-
87- integral [0 ] = dx * (0.5 * u1 .data [0 ][0 ] + 0.5 * u1 .data [0 ][Nx ] + np .sum (u1 .data [0 ][1 :Nx ]))
88-
89- if user_action is not None :
90- user_action (u1 .data [0 ], x , t , 0 )
91-
92- op1 = Operator (bc1 + (pbc1 if periodic_bc else []) + [eq1 ])
93- op1 .apply (dt = dt )
73+ u = u (to = 2 , so = 2 )
74+ pde0 = u .dtr (fd_order = 1 ) + v * u .dxl (fd_order = 1 )
9475
95- integral [1 ] = dx * (0.5 * u1 .data [1 ][0 ] + 0.5 * u1 .data [1 ][Nx ] + np .sum (u1 .data [1 ][1 :Nx ]))
76+ stencil0 = solve (pde0 , u .forward )
77+ eq0 = Eq (u .forward , stencil0 ).subs (t_s , 0 )
9678
97- if user_action is not None :
98- user_action (u1 .data [1 ], x , t , 1 )
79+ pbc0 = [Eq (u [t_s , 0 ], u [t_s , Nx ]).subs (t_s , 0 )]
9980
100- print ('I:' , integral [1 ])
101-
10281 # Now continue with LF scheme
103- u = u (to = 2 , so = 2 )
104- u .data [0 :2 , :] = u1 .data
10582 pde = u .dtc + v * u .dxc
10683
107- pbc = [Eq (u [t_s + 1 , 0 ], u [t_s - 1 , 0 ] - C * (u [t_s , 1 ] - u [t_s , Nx - 1 ]))]
84+ pbc = [Eq (u [t_s + 1 , 0 ], u [t_s - 1 , 0 ] - C * (u [t_s , 1 ] - u [t_s , Nx - 1 ]))]
10885 pbc += [Eq (u [t_s , Nx ], u [t_s , 0 ])]
10986
11087 elif scheme == 'UP' :
@@ -117,7 +94,7 @@ def u(to=1, so=1):
11794 u = u (so = 2 )
11895 pde = u .dtr + v * u .dxc - 0.5 * dt * v ** 2 * u .dx2
11996
120- pbc = [Eq (u [t_s + 1 , 0 ], u [t_s , 0 ] - 0.5 * C * (u [t_s , 1 ] - u [t_s , Nx - 1 ]) + \
97+ pbc = [Eq (u [t_s + 1 , 0 ], u [t_s , 0 ] - 0.5 * C * (u [t_s , 1 ] - u [t_s , Nx - 1 ]) + \
12198 0.5 * C * (u [t_s , 1 ] - 2 * u [t_s , 0 ] + u [t_s , Nx - 1 ]))]
12299 pbc += [Eq (u [t_s , Nx ], u [t_s , 0 ])]
123100
@@ -127,22 +104,30 @@ def u(to=1, so=1):
127104 stencil = solve (pde , u .forward )
128105 eq = Eq (u .forward , stencil )
129106
130- if scheme != 'LF' :
131- # Set initial condition u(x,0) = I(x)
132- u .data [0 , :] = [I (xi ) for xi in x ]
107+ bc_init = [Eq (u [t_s + 1 , 0 ], U0 ).subs (t_s , 0 )]
108+
109+ # Set initial condition u(x,0) = I(x)
110+ u .data [0 , :] = [I (xi ) for xi in x ]
133111
134- # Compute the integral under the curve
135- integral [0 ] = dx * (0.5 * u .data [0 ][0 ] + 0.5 * u .data [0 ][Nx ] + np .sum (u .data [0 ][1 :Nx ]))
112+ # Compute the integral under the curve
113+ integral [0 ] = dx * (0.5 * u .data [0 ][0 ] + 0.5 * u .data [0 ][Nx ] + np .sum (u .data [0 ][1 :Nx ]))
136114
137- if user_action is not None :
138- user_action (u .data [0 ], x , t , 0 )
115+ if user_action is not None :
116+ user_action (u .data [0 ], x , t , 0 )
139117
140118 bc = [Eq (u [t_s + 1 , 0 ], U0 )]
119+
120+ if scheme == 'LF' :
121+ op = Operator ((pbc0 if periodic_bc else []) + [eq0 ] + bc_init + (pbc if periodic_bc else []) + [eq ] + (bc if not periodic_bc else []))
122+ else :
123+ op = Operator (bc_init + (pbc if periodic_bc else []) + [eq ] + (bc if not periodic_bc else []))
124+
125+ print (op .arguments (dt = dt ))
126+ print (op .ccode )
127+
128+ op .apply (dt = dt )
141129
142- op = Operator (bc + (pbc if periodic_bc else []) + [eq ])
143- op .apply (time_m = 1 if scheme == 'LF' else 0 , time_M = Nt - 1 , dt = float (dt ))
144-
145- for n in range (2 if scheme == 'LF' else 1 , Nt + 1 ):
130+ for n in range (1 , Nt + 1 ):
146131 # Compute the integral under the curve
147132 integral [n ] = dx * (0.5 * u .data [n ][0 ] + 0.5 * u .data [n ][Nx ] + np .sum (u .data [n ][1 :Nx ]))
148133
@@ -296,7 +281,7 @@ def solver_theta(I, v, L, dt, C, T, theta=0.5, user_action=None, FE=False):
296281 Vectorized implementation and sparse (tridiagonal)
297282 coefficient matrix.
298283 """
299- import time ; t0 = time .clock () # for measuring the CPU time
284+ import time ; t0 = time .process_time () # for measuring the CPU time
300285 Nt = int (round (T / float (dt )))
301286 t = np .linspace (0 , Nt * dt , Nt + 1 ) # Mesh points in time
302287 dx = v * dt / C
@@ -368,7 +353,7 @@ def solver_theta(I, v, L, dt, C, T, theta=0.5, user_action=None, FE=False):
368353 # Update u_n before next step
369354 u_n , u = u , u_n
370355
371- t1 = time .clock ()
356+ t1 = time .process_time ()
372357 return integral
373358
374359
0 commit comments