Coverage for pySDC/projects/DAE/run/synchronous_machine_playground.py: 100%
56 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
1from pathlib import Path
2import numpy as np
3import pickle
4import statistics
6from pySDC.implementations.controller_classes.controller_nonMPI import controller_nonMPI
7from pySDC.projects.DAE.problems.synchronousMachine import SynchronousMachineInfiniteBus
8from pySDC.projects.DAE.sweepers.fullyImplicitDAE import FullyImplicitDAE
9from pySDC.projects.DAE.misc.hooksDAE import LogGlobalErrorPostStepDifferentialVariable
10from pySDC.helpers.stats_helper import get_sorted
11from pySDC.helpers.stats_helper import filter_stats
12from pySDC.implementations.hooks.log_solution import LogSolution
15def main():
16 """
17 A testing ground for the synchronous machine model
18 """
19 # initialize level parameters
20 level_params = dict()
21 level_params['restol'] = 1e-7
22 level_params['dt'] = 1e-1
24 # initialize sweeper parameters
25 sweeper_params = dict()
26 sweeper_params['quad_type'] = 'RADAU-RIGHT'
27 sweeper_params['num_nodes'] = 3
28 sweeper_params['QI'] = 'LU'
30 # initialize problem parameters
31 problem_params = dict()
32 problem_params['newton_tol'] = 1e-3
34 # initialize step parameters
35 step_params = dict()
36 step_params['maxiter'] = 100
38 # initialize controller parameters
39 controller_params = dict()
40 controller_params['logger_level'] = 30
41 controller_params['hook_class'] = [LogGlobalErrorPostStepDifferentialVariable, LogSolution]
43 # Fill description dictionary for easy hierarchy creation
44 description = dict()
45 description['problem_class'] = SynchronousMachineInfiniteBus
46 description['problem_params'] = problem_params
47 description['sweeper_class'] = FullyImplicitDAE
48 description['sweeper_params'] = sweeper_params
49 description['level_params'] = level_params
50 description['step_params'] = step_params
52 Path("data").mkdir(parents=True, exist_ok=True)
54 # instantiate the controller
55 controller = controller_nonMPI(num_procs=1, controller_params=controller_params, description=description)
57 # set time parameters
58 t0 = 0.0
59 Tend = 1.0
61 # get initial values on finest level
62 P = controller.MS[0].levels[0].prob
63 uinit = P.u_exact(t0)
65 # call main function to get things done...
66 uend, stats = controller.run(u0=uinit, t0=t0, Tend=Tend)
68 # check error (only available if reference solution was provided)
69 # err = get_sorted(stats, type='e_global_differential_post_step', sortby='time')
70 # err = np.linalg.norm([err[i][1] for i in range(len(err))], np.inf)
71 # print(f"Error is {err}")
73 uend_ref = P.dtype_u(P.init)
74 uend_ref.diff[:8] = (
75 8.30823565e-01,
76 -4.02584174e-01,
77 1.16966755e00,
78 9.47592808e-01,
79 -3.68076863e-01,
80 -3.87492326e-01,
81 3.10281509e-01,
82 9.94039645e-01,
83 )
84 uend_ref.alg[:6] = (
85 -7.77837831e-01,
86 -1.67347611e-01,
87 1.34810867e00,
88 5.46223705e-04,
89 1.29690691e-02,
90 -8.00823474e-02,
91 )
92 err = abs(uend.diff - uend_ref.diff)
93 assert np.isclose(err, 0, atol=1e-4), "Error too large."
95 # store results
96 sol = get_sorted(stats, type='u', sortby='time')
97 sol_dt = np.array([sol[i][0] for i in range(len(sol))])
98 sol_data = np.array(
99 [
100 [(sol[j][1].diff[id], sol[j][1].alg[ia]) for j in range(len(sol))]
101 for id, ia in zip(range(len(uend.diff)), range(len(uend.alg)))
102 ]
103 )
104 niter = filter_stats(stats, type='niter')
105 niter = np.fromiter(niter.values(), int)
107 data = dict()
108 data['dt'] = sol_dt
109 data['solution'] = sol_data
110 data['niter'] = round(statistics.mean(niter))
111 pickle.dump(data, open("data/dae_conv_data.p", 'wb'))
113 print("Done")
116if __name__ == "__main__":
117 main()