1- from partdiff_common .parse_args import (
2- Options ,
3- CalculationMethod ,
4- PerturbationFunction ,
5- TerminationCondition ,
6- )
7- from dataclasses import dataclass
8- import numpy as np
1+ """
2+ This module contains the shared parts of partdiff, e.g. all the stuff for
3+ initializing matrices or displaying statistics.
4+ """
5+
96import sys
7+ from dataclasses import dataclass
108from math import sin
119
10+ import numpy as np
1211from pympler import asizeof
1312
13+ from partdiff_common .parse_args import (
14+ CalculationMethod ,
15+ Options ,
16+ PerturbationFunction ,
17+ )
18+
1419
1520@dataclass (frozen = True )
1621class CalculationArguments :
22+ """This class contains the internal representation of the problem, i.e. the
23+ initialized matrices. All matrices have the size (n+1)*(n+1).
24+ The tensor may be 1*(n+1)*(n+1) or 2*(n+1)*(n+1), depending on the method.
25+ The perturbation matrix contains the precomputed values of the perturbation function.
26+ """
27+
1728 n : int
18- h : float
1929 tensor : np .ndarray
2030 perturbation_matrix : np .ndarray
2131
2232
2333@dataclass (frozen = True )
2434class CalculationResults :
35+ """This class contains the final results of the calculation."""
36+
2537 final_matrix : np .ndarray
2638 stat_iteration : int
2739 stat_accuracy : float
2840 duration : float
2941
3042
3143def init_arguments (options : Options ) -> CalculationArguments :
44+ """Init the CalculationArguments.
45+
46+ Args:
47+ options (Options): The program options.
48+
49+ Returns:
50+ CalculationArguments: The initialized problem representation.
51+ """
3252 n = (options .interlines * 8 ) + 9 - 1
3353 num_matrices = 2 if options .method == CalculationMethod .JACOBI else 1
3454 h = 1.0 / n
@@ -55,12 +75,22 @@ def init_arguments(options: Options) -> CalculationArguments:
5575 fpisin_i = fpisin * sin (pih * i )
5676 for j in range (1 , n ):
5777 perturbation_matrix [i , j ] = fpisin_i * sin (pih * j )
58- return CalculationArguments (n , h , tensor , perturbation_matrix )
78+ return CalculationArguments (n , tensor , perturbation_matrix )
5979
6080
6181def calculate_memory_usage (
6282 arguments : CalculationArguments , options : Options , results : CalculationResults
6383) -> float :
84+ """Calculate the total memory usage.
85+
86+ Args:
87+ arguments (CalculationArguments): The calculation arguments.
88+ options (Options): The program options.
89+ results (CalculationResults): The calculation results.
90+
91+ Returns:
92+ float: The memory usage in MiB.
93+ """
6494 memory_usage = 0
6595 for o in (arguments , options , results ):
6696 memory_usage += asizeof .asizeof (o )
@@ -71,6 +101,13 @@ def calculate_memory_usage(
71101def display_statistics (
72102 arguments : CalculationArguments , options : Options , results : CalculationResults
73103) -> None :
104+ """Display statistics about the calculation.
105+
106+ Args:
107+ arguments (CalculationArguments): The calculation arguments.
108+ options (Options): The program options.
109+ results (CalculationResults): The calculation results.
110+ """
74111 memory_usage = calculate_memory_usage (arguments , options , results )
75112 print (f"Calculation time: { results .duration :0.6f} s" )
76113 print (f"Memory usage: { memory_usage :0.6f} MiB" )
@@ -83,9 +120,13 @@ def display_statistics(
83120 print ("" )
84121
85122
86- def display_matrix (
87- arguments : CalculationArguments , options : Options , results : CalculationResults
88- ) -> None :
123+ def display_matrix (options : Options , results : CalculationResults ) -> None :
124+ """Display the final matrix in a 9x9 format.
125+
126+ Args:
127+ options (Options): The program options.
128+ results (CalculationResults): The calculation results.
129+ """
89130 interlines = options .interlines
90131 final_matrix = results .final_matrix
91132 print ("Matrix:" )
@@ -97,6 +138,7 @@ def display_matrix(
97138
98139
99140def check_float_info () -> None :
141+ """Check that we're running a platform where Python's builtin float is double."""
100142 float_info = sys .float_info
101143 assert (float_info .max_exp , float_info .mant_dig ) == (1024 , 53 ), (
102144 "This application does only work on platforms where built-in float is IEEE 754 binary64, e.g. CPython." ,
0 commit comments