<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import os <EOL> import numpy as np <EOL> import matplotlib . pyplot as plt <EOL> import compare <EOL> import mesh . patch as patch <EOL> import multigrid . variable_coeff_MG as MG <EOL> from util import msg <EOL> def true ( x , y ) : <EOL> return np . sin ( <NUM_LIT> * np . pi * x ) * np . sin ( <NUM_LIT> * np . pi * y ) <EOL> def alpha ( x , y ) : <EOL> return <NUM_LIT> + np . cos ( <NUM_LIT> * np . pi * x ) * np . cos ( <NUM_LIT> * np . pi * y ) <EOL> def f ( x , y ) : <EOL> return - <NUM_LIT> * np . pi ** <NUM_LIT:2> * ( np . cos ( <NUM_LIT:2> * np . pi * x ) * np . cos ( <NUM_LIT:2> * np . pi * y ) + <NUM_LIT:1> ) * np . sin ( <NUM_LIT:2> * np . pi * x ) * np . sin ( <NUM_LIT:2> * np . pi * y ) <EOL> def test_vc_poisson_dirichlet ( N , store_bench = False , comp_bench = False , <EOL> make_plot = False , verbose = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> nx = N <EOL> ny = nx <EOL> g = patch . Grid2d ( nx , ny , ng = <NUM_LIT:1> ) <EOL> d = patch . CellCenterData2d ( g ) <EOL> bc_c = patch . BCObject ( xlb = "<STR_LIT>" , xrb = "<STR_LIT>" , <EOL> ylb = "<STR_LIT>" , yrb = "<STR_LIT>" ) <EOL> d . register_var ( "<STR_LIT:c>" , bc_c ) <EOL> d . create ( ) <EOL> c = d . get_var ( "<STR_LIT:c>" ) <EOL> c . d [ : , : ] = alpha ( g . x2d , g . y2d ) <EOL> a = MG . VarCoeffCCMG2d ( nx , ny , <EOL> xl_BC_type = "<STR_LIT>" , yl_BC_type = "<STR_LIT>" , <EOL> xr_BC_type = "<STR_LIT>" , yr_BC_type = "<STR_LIT>" , <EOL> coeffs = c , coeffs_bc = bc_c , <EOL> verbose = verbose , vis = <NUM_LIT:0> , true_function = true ) <EOL> a . init_zeros ( ) <EOL> rhs = f ( a . x2d , a . y2d ) <EOL> a . init_RHS ( rhs ) <EOL> a . solve ( rtol = <NUM_LIT> ) <EOL> v = a . get_solution ( ) <EOL> b = true ( a . x2d , a . y2d ) <EOL> e = v - b <EOL> enorm = e . norm ( ) <EOL> print ( "<STR_LIT>" % ( enorm , a . relative_error , a . num_cycles ) ) <EOL> if make_plot : <EOL> plt . clf ( ) <EOL> plt . figure ( figsize = ( <NUM_LIT> , <NUM_LIT> ) , dpi = <NUM_LIT:100> , facecolor = '<STR_LIT:w>' ) <EOL> plt . subplot ( <NUM_LIT> ) <EOL> plt . imshow ( np . transpose ( v . v ( ) ) , <EOL> interpolation = "<STR_LIT>" , origin = "<STR_LIT>" , <EOL> extent = [ a . xmin , a . xmax , a . ymin , a . ymax ] ) <EOL> plt . xlabel ( "<STR_LIT:x>" ) <EOL> plt . ylabel ( "<STR_LIT:y>" ) <EOL> plt . title ( "<STR_LIT>" . format ( nx ) ) <EOL> plt . colorbar ( ) <EOL> plt . subplot ( <NUM_LIT> ) <EOL> plt . imshow ( np . transpose ( e . v ( ) ) , <EOL> interpolation = "<STR_LIT>" , origin = "<STR_LIT>" , <EOL> extent = [ a . xmin , a . xmax , a . ymin , a . ymax ] ) <EOL> plt . xlabel ( "<STR_LIT:x>" ) <EOL> plt . ylabel ( "<STR_LIT:y>" ) <EOL> plt . title ( "<STR_LIT:error>" ) <EOL> plt . colorbar ( ) <EOL> plt . tight_layout ( ) <EOL> plt . savefig ( "<STR_LIT>" ) <EOL> bench = "<STR_LIT>" <EOL> bench_dir = os . environ [ "<STR_LIT>" ] + "<STR_LIT>" <EOL> my_data = a . get_solution_object ( ) <EOL> if store_bench : <EOL> my_data . write ( "<STR_LIT>" . format ( bench_dir , bench ) ) <EOL> if comp_bench : <EOL> compare_file = "<STR_LIT>" . format ( bench_dir , bench ) <EOL> msg . warning ( "<STR_LIT>" % ( compare_file ) ) <EOL> bench_grid , bench_data = patch . read ( compare_file ) <EOL> result = compare . compare ( my_data . grid , my_data , <EOL> bench_grid , bench_data ) <EOL> if result == <NUM_LIT:0> : <EOL> msg . success ( "<STR_LIT>" ) <EOL> else : <EOL> msg . warning ( "<STR_LIT>" + compare . errors [ result ] + "<STR_LIT:\n>" ) <EOL> return result <EOL> return enorm <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> N = [ <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:64> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> err = [ ] <EOL> plot = False <EOL> store = False <EOL> do_compare = False <EOL> for nx in N : <EOL> if nx == max ( N ) : <EOL> plot = True <EOL> do_compare = True <EOL> enorm = test_vc_poisson_dirichlet ( nx , make_plot = plot , <EOL> store_bench = store , comp_bench = do_compare ) <EOL> err . append ( enorm ) <EOL> N = np . array ( N , dtype = np . float64 ) <EOL> err = np . array ( err ) <EOL> plt . clf ( ) <EOL> plt . loglog ( N , err , "<STR_LIT:x>" , color = "<STR_LIT:r>" ) <EOL> plt . loglog ( N , err [ <NUM_LIT:0> ] * ( N [ <NUM_LIT:0> ] / N ) ** <NUM_LIT:2> , "<STR_LIT>" , color = "<STR_LIT:k>" ) <EOL> plt . xlabel ( "<STR_LIT:N>" ) <EOL> plt . ylabel ( "<STR_LIT:error>" ) <EOL> f = plt . gcf ( ) <EOL> f . set_size_inches ( <NUM_LIT> , <NUM_LIT> ) <EOL> plt . tight_layout ( ) <EOL> plt . savefig ( "<STR_LIT>" ) </s>
