Coverage for pySDC/tutorial/step_4/A_spatial_transfer_operators.py: 100%

45 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-16 14:51 +0000

1from collections import namedtuple 

2from pathlib import Path 

3 

4import numpy as np 

5 

6from pySDC.implementations.problem_classes.HeatEquation_ND_FD import heatNd_unforced 

7from pySDC.implementations.transfer_classes.TransferMesh import mesh_to_mesh 

8from pySDC.tutorial.step_1.B_spatial_accuracy_check import get_accuracy_order 

9 

10# setup id for gathering the results (will sort by nvars) 

11ID = namedtuple('ID', 'nvars_fine') 

12 

13 

14def main(): 

15 """ 

16 A simple test program to test interpolation order in space 

17 """ 

18 

19 # initialize problem parameters 

20 problem_params = dict() 

21 problem_params['nu'] = 0.1 # diffusion coefficient 

22 problem_params['freq'] = 3 # frequency for the test value 

23 problem_params['bc'] = 'dirichlet-zero' # boundary conditions 

24 

25 # initialize transfer parameters 

26 space_transfer_params = dict() 

27 space_transfer_params['rorder'] = 2 

28 space_transfer_params['iorder'] = 4 

29 

30 nvars_fine_list = [2**p - 1 for p in range(5, 10)] 

31 

32 # set up dictionary to store results (plus lists) 

33 results = dict() 

34 results['nvars_list'] = nvars_fine_list 

35 

36 for nvars_fine in nvars_fine_list: 

37 print('Working on nvars_fine = %4i...' % nvars_fine) 

38 

39 # instantiate fine problem 

40 problem_params['nvars'] = nvars_fine # number of degrees of freedom 

41 Pfine = heatNd_unforced(**problem_params) 

42 

43 # instantiate coarse problem using half of the DOFs 

44 problem_params['nvars'] = int((nvars_fine + 1) / 2.0 - 1) 

45 Pcoarse = heatNd_unforced(**problem_params) 

46 

47 # instantiate spatial interpolation 

48 T = mesh_to_mesh(fine_prob=Pfine, coarse_prob=Pcoarse, params=space_transfer_params) 

49 

50 # set exact fine solution to compare with 

51 xvalues_fine = np.array([(i + 1) * Pfine.dx for i in range(Pfine.nvars[0])]) 

52 uexact_fine = Pfine.dtype_u(Pfine.init) 

53 uexact_fine[:] = np.sin(np.pi * Pfine.freq[0] * xvalues_fine) 

54 

55 # set exact coarse solution as source 

56 xvalues_coarse = np.array([(i + 1) * Pcoarse.dx for i in range(Pcoarse.nvars[0])]) 

57 uexact_coarse = Pfine.dtype_u(Pcoarse.init) 

58 uexact_coarse[:] = np.sin(np.pi * Pcoarse.freq[0] * xvalues_coarse) 

59 

60 # do the interpolation/prolongation 

61 uinter = T.prolong(uexact_coarse) 

62 

63 # compute error and store 

64 id = ID(nvars_fine=nvars_fine) 

65 results[id] = abs(uinter - uexact_fine) 

66 

67 # print out and check 

68 print('Running order checks...') 

69 orders = get_accuracy_order(results) 

70 Path("data").mkdir(parents=True, exist_ok=True) 

71 f = open('data/step_4_A_out.txt', 'w') 

72 for p in range(len(orders)): 

73 out = 'Expected order %2i, got order %5.2f, deviation of %5.2f%%' % ( 

74 space_transfer_params['iorder'], 

75 orders[p], 

76 100 * abs(space_transfer_params['iorder'] - orders[p]) / space_transfer_params['iorder'], 

77 ) 

78 f.write(out + '\n') 

79 print(out) 

80 assert ( 

81 abs(space_transfer_params['iorder'] - orders[p]) / space_transfer_params['iorder'] < 0.05 

82 ), 'ERROR: did not get expected orders for interpolation, got %s' % str(orders[p]) 

83 f.close() 

84 print('...got what we expected!') 

85 

86 

87if __name__ == "__main__": 

88 main()