1- import math
2- from typing import Sequence
3-
41import numpy as np
52import sympy as sp
63
1613 make_identity_diff_op ,
1714)
1815
16+ import matplotlib .pyplot as plt
17+ from matplotlib import cm , ticker
18+
1919def produce_error_for_recurrences (coords , pde , g_x_y , deriv_order , m = 100 ):
2020
2121 #Possibly reshape coords?
@@ -172,35 +172,49 @@ def generate_true(i):
172172
173173 return interactions_on_axis , interactions_off_axis , interactions_true , interactions_total
174174
175+ def create_logarithmic_mesh (res ):
176+
177+ x_grid = [10 ** (pw ) for pw in np .linspace (- 8 , 0 , res )]
178+ y_grid = [10 ** (pw ) for pw in np .linspace (- 8 , 0 , res )]
179+
180+ mesh = np .meshgrid (x_grid , y_grid )
181+ mesh_points = np .array (mesh ).reshape (2 , - 1 )
182+
183+ return mesh_points , x_grid , y_grid
184+
185+ def create_plot (relerr_on , str_title ):
186+ fig , ax = plt .subplots (1 , 1 , figsize = (15 , 8 ))
187+
188+ n_levels = 18
189+ levels = 10 ** np .linspace (- n_levels + 2 , 1 , n_levels )
190+ cs = ax .contourf (x_grid , y_grid , relerr_on .reshape (res , res ), locator = ticker .LogLocator (), cmap = cm .coolwarm , levels = levels , extend = "both" )
191+ cbar = fig .colorbar (cs )
192+
193+ cbar .set_ticks (levels )
194+ cbar .set_ticklabels (["1e" + str (int (i )) for i in np .linspace (- n_levels + 2 , 1 , n_levels )])
195+
196+ ax .set_xscale ('log' )
197+ ax .set_yscale ('log' )
198+ ax .set_xlabel ("x-coordinate" , fontsize = 15 )
199+ ax .set_ylabel ("y-coordinate" , fontsize = 15 )
200+ plt .title (str_title )
201+
202+ #========================= LAPLACE 2D ====================================
203+ res = 32
204+ mesh_points , x_grid , y_grid = create_logarithmic_mesh (res )
175205
176206w = make_identity_diff_op (2 )
177207laplace2d = laplacian (w )
178208var = _make_sympy_vec ("x" , 2 )
179209var_t = _make_sympy_vec ("t" , 2 )
180- g_x_y = (- 1 / (2 * np .pi )) * sp .log (sp .sqrt ((var [0 ]- var_t [0 ])** 2 +
210+ g_x_y_laplace = (- 1 / (2 * np .pi )) * sp .log (sp .sqrt ((var [0 ]- var_t [0 ])** 2 +
181211 (var [1 ]- var_t [1 ])** 2 ))
182212
183- res = 32
184- x_grid = [10 ** (pw ) for pw in np .linspace (- 8 , 0 , res )]
185- y_grid = [10 ** (pw ) for pw in np .linspace (- 8 , 0 , res )]
186-
187- mesh = np .meshgrid (x_grid , y_grid )
188- mesh_points = np .array (mesh ).reshape (2 , - 1 )
189-
190- interactions_on_axis , interactions_off_axis , interactions_true , interactions_total = produce_error_for_recurrences (mesh_points , laplace2d , g_x_y , 9 )
191-
192-
193- import matplotlib .pyplot as plt
194- from matplotlib import cm , ticker
195- fig , ax = plt .subplots (1 , 1 , figsize = (15 , 8 ))
213+ interactions_on_axis , interactions_off_axis , interactions_true , interactions_total = produce_error_for_recurrences (mesh_points , laplace2d , g_x_y_laplace , 9 )
196214
197215relerr_on = np .abs ((interactions_on_axis - interactions_true )/ interactions_true )
198- cs = ax .contourf (x_grid , y_grid , relerr_on .reshape (res , res ), locator = ticker .LogLocator (), cmap = cm .PuBu_r )
199- fig .colorbar (cs )
200216
201- ax .set_xscale ('log' )
202- ax .set_yscale ('log' )
203- ax .set_xlabel ("source x-coord" , fontsize = 15 )
204- ax .set_ylabel ("source y-coord" , fontsize = 15 )
217+ create_plot (relerr_on , "Laplace (2D): On-Axis Recurrence, 9th Order Derivative Evaluation Error" )
205218
219+ #========================= HELMOLTZ 2D ====================================
206220plt .show ()
0 commit comments