@@ -24,6 +24,14 @@ def solve(self):
2424 logging .info (f"Starting { self .__class__ .__name__ } with { self .equation .x_nodes + 1 } spatial nodes and { self .equation .t_nodes + 1 } time nodes." )
2525 start = time .perf_counter ()
2626
27+ if (self .equation .left_boundary is None or
28+ self .equation .right_boundary is None or
29+ self .equation .top_boundary is None or
30+ self .equation .bottom_boundary is None ):
31+ raise ValueError ("All boundary conditions must be set before solving" )
32+ if self .equation .initial_temp is None :
33+ raise ValueError ("Initial condition must be set before solving" )
34+
2735 x = np .linspace (0 , self .equation .xlength , self .equation .x_nodes )
2836 y = np .linspace (0 , self .equation .ylength , self .equation .y_nodes )
2937 t = np .linspace (0 , self .equation .time , self .equation .t_nodes )
@@ -45,7 +53,7 @@ def solve(self):
4553 self .equation .initial_temp ,
4654 x , y , t )
4755
48- print ("Calculating temperature evolution..." )
56+ print (f "Calculating temperature evolution with { self . equation . t_nodes - 1 } iterations..." , flush = True )
4957 for tau in range (self .equation .t_nodes - 1 ):
5058 for i in range (1 , self .equation .x_nodes - 1 ):
5159 for j in range (1 , self .equation .y_nodes - 1 ):
@@ -69,13 +77,27 @@ def __init__(self, equation: heat.HeatEquation2D):
6977 def solve (self ):
7078 logging .info (f"Starting { self .__class__ .__name__ } with { self .equation .x_nodes + 1 } spatial nodes and { self .equation .t_nodes + 1 } time nodes." )
7179 start = time .perf_counter ()
80+
81+ if (self .equation .left_boundary is None or
82+ self .equation .right_boundary is None or
83+ self .equation .top_boundary is None or
84+ self .equation .bottom_boundary is None ):
85+ raise ValueError ("All boundary conditions must be set before solving" )
86+ if self .equation .initial_temp is None :
87+ raise ValueError ("Initial condition must be set before solving" )
88+
7289 x = np .linspace (0 , self .equation .xlength , self .equation .x_nodes )
7390 y = np .linspace (0 , self .equation .ylength , self .equation .y_nodes )
7491 t = np .linspace (0 , self .equation .time , self .equation .t_nodes )
7592
7693 dx = x [1 ]- x [0 ]
7794 dy = y [1 ]- y [0 ]
7895 dt = t [1 ]- t [0 ]
96+ c = self .equation .k * dt / 2
97+ cx = c / (dx ** 2 )
98+ cy = c / (dy ** 2 )
99+ alpha = 1 + 2 * cx + 2 * cy
100+ beta = 1 - 2 * cx - 2 * cy
79101
80102 print ("Initializing matrix..." )
81103 U = utility .Heat2DHelper .initMatrix (self .equation .t_nodes ,
@@ -89,9 +111,33 @@ def solve(self):
89111 x , y , t )
90112
91113 # create sparse matrix
92-
93- # time-stepping loop
114+ G , n_interior_x , n_interior_y = utility .Heat2DHelper .innitTriDiagMatrix (self .equation .x_nodes , self .equation .y_nodes , cx , cy , alpha )
94115
116+ # time-stepping loop
117+ print (f"Calculating temperature evolution with { self .equation .t_nodes - 1 } iterations..." , flush = True )
118+ for tau in range (self .equation .t_nodes - 1 ):
119+ rhs = np .zeros (n_interior_x * n_interior_y )
120+ idx = 0
121+ for j in range (1 , self .equation .y_nodes - 1 ):
122+ for i in range (1 , self .equation .x_nodes - 1 ):
123+ # RHS = β*U_τ + cx*(neighbors_x) + cy*(neighbors_y) + boundary_terms
124+ rhs [idx ] = beta * U [tau , i , j ]
125+ rhs [idx ] += cx * (U [tau , i - 1 , j ] + U [tau , i + 1 , j ])
126+ rhs [idx ] += cy * (U [tau , i , j - 1 ] + U [tau , i , j + 1 ])
127+ # Boundary contributions
128+ if i == 1 :
129+ rhs [idx ] += cx * U [tau + 1 , 0 , j ]
130+ if i == self .equation .x_nodes - 2 :
131+ rhs [idx ] += cx * U [tau + 1 , - 1 , j ]
132+ if j == 1 :
133+ rhs [idx ] += cy * U [tau + 1 , i , 0 ]
134+ if j == self .equation .y_nodes - 2 :
135+ rhs [idx ] += cy * U [tau + 1 , i , - 1 ]
136+ idx += 1
137+ # Solve G*u_{τ+1} = rhs
138+ u_next_interior = spsolve (G , rhs )
139+ U [tau + 1 , 1 :- 1 , 1 :- 1 ] = u_next_interior .reshape ((n_interior_x , n_interior_y ))
140+
95141 end = time .perf_counter ()
96142 duration = end - start
97143 logging .info (f"Solver completed in { duration } seconds." )
0 commit comments