Skip to content

Commit 3178f69

Browse files
committed
Fix some linter warnings
1 parent a1fef2f commit 3178f69

6 files changed

Lines changed: 145 additions & 18 deletions

File tree

cython/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
partdiff-py is a Python port of partdiff.
3+
This is the "cython" variant. It uses Cython to translate the calculate method to C.
4+
"""
5+
16
from calculate import calculate
27
from partdiff_common import (
38
check_float_info,
@@ -14,7 +19,7 @@ def main() -> None:
1419
arguments = init_arguments(options)
1520
results = calculate(arguments, options)
1621
display_statistics(arguments, options, results)
17-
display_matrix(arguments, options, results)
22+
display_matrix(options, results)
1823

1924

2025
if __name__ == "__main__":

np_vectorize/main.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
partdiff-py is a Python port of partdiff.
3+
This is the "np_vectorize" variant. It uses numpy's vectorized math for the Jacobi variant.
4+
"""
5+
16
from itertools import count
27
from time import time
38

@@ -21,6 +26,15 @@
2126
def calculate_jacobi(
2227
arguments: CalculationArguments, options: Options
2328
) -> CalculationResults:
29+
"""Solve the Poisson equation iteratively using the Jacobi method.
30+
31+
Args:
32+
arguments (CalculationArguments): The internal representation of the problem.
33+
options (Options): The program options.
34+
35+
Returns:
36+
CalculationResults: The results of the calculation.
37+
"""
2438
start_time = time()
2539
n = arguments.n
2640
tensor = arguments.tensor
@@ -60,6 +74,15 @@ def calculate_jacobi(
6074
def calculate_gauss_seidel(
6175
arguments: CalculationArguments, options: Options
6276
) -> CalculationResults:
77+
"""Solve the Poisson equation iteratively using the Gauß-Seidel method.
78+
79+
Args:
80+
arguments (CalculationArguments): The internal representation of the problem.
81+
options (Options): The program options.
82+
83+
Returns:
84+
CalculationResults: The results of the calculation.
85+
"""
6386
start_time = time()
6487
n = arguments.n
6588
tensor = arguments.tensor
@@ -99,6 +122,15 @@ def calculate_gauss_seidel(
99122

100123

101124
def calculate(arguments: CalculationArguments, options: Options) -> CalculationResults:
125+
"""Solve the Poisson equation iteratively using the Jacobi or Gauß-Seidel method.
126+
127+
Args:
128+
arguments (CalculationArguments): The internal representation of the problem.
129+
options (Options): The program options.
130+
131+
Returns:
132+
CalculationResults: The results of the calculation.
133+
"""
102134
match options.method:
103135
case CalculationMethod.JACOBI:
104136
return calculate_jacobi(arguments, options)
@@ -112,7 +144,7 @@ def main() -> None:
112144
arguments = init_arguments(options)
113145
results = calculate(arguments, options)
114146
display_statistics(arguments, options, results)
115-
display_matrix(arguments, options, results)
147+
display_matrix(options, results)
116148

117149

118150
if __name__ == "__main__":

numba/main.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
partdiff-py is a Python port of partdiff.
3+
This is the "numba" variant. It uses numba to just-in-time compile the calculation.
4+
"""
5+
16
from time import time
27

38
import numpy as np
@@ -30,6 +35,22 @@ def calculate_iterate(
3035
tensor: np.ndarray,
3136
perturbation_matrix: np.ndarray,
3237
) -> tuple[np.ndarray, int, float]:
38+
"""The inner calculation part of the calculate method which is just-in-time compiled
39+
with numba here.
40+
41+
Args:
42+
method (CalculationMethod): The method (Gauß-Seidel or Jacobi).
43+
termination (TerminationCondition): Termination (Iterations or Accuracy).
44+
term_iteration (TermIterations): Max iterations.
45+
term_accuracy (TermAccuracy): Min accuracy.
46+
n (int): Problem size (matrix is (n+1)*(n+1)).
47+
tensor (np.ndarray): The problem matrices.
48+
perturbation_matrix (np.ndarray): Precomputed perturbation function values.
49+
50+
Returns:
51+
tuple[np.ndarray, int, float]: A tuple containing the final matrix,
52+
actual iterations performed, and residuum reached.
53+
"""
3354
stat_iteration = 0
3455
stat_accuracy = 0.0
3556
matrix_out = tensor[0, :, :]
@@ -68,6 +89,15 @@ def calculate_iterate(
6889

6990

7091
def calculate(arguments: CalculationArguments, options: Options) -> CalculationResults:
92+
"""Solve the Poisson equation iteratively using the Jacobi or Gauß-Seidel method.
93+
94+
Args:
95+
arguments (CalculationArguments): The internal representation of the problem.
96+
options (Options): The program options.
97+
98+
Returns:
99+
CalculationResults: The results of the calculation.
100+
"""
71101
start_time = time()
72102
final_matrix, stat_iteration, stat_accuracy = calculate_iterate(
73103
options.method,
@@ -89,7 +119,7 @@ def main() -> None:
89119
arguments = init_arguments(options)
90120
results = calculate(arguments, options)
91121
display_statistics(arguments, options, results)
92-
display_matrix(arguments, options, results)
122+
display_matrix(options, results)
93123

94124

95125
if __name__ == "__main__":

partdiff_common/src/partdiff_common/__init__.py

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,54 @@
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+
96
import sys
7+
from dataclasses import dataclass
108
from math import sin
119

10+
import numpy as np
1211
from pympler import asizeof
1312

13+
from partdiff_common.parse_args import (
14+
CalculationMethod,
15+
Options,
16+
PerturbationFunction,
17+
)
18+
1419

1520
@dataclass(frozen=True)
1621
class 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)
2434
class 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

3143
def 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

6181
def 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(
71101
def 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

99140
def 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.",

partdiff_common/src/partdiff_common/parse_args.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
"""
2+
Partdiff argument parser.
3+
"""
4+
15
import argparse
26
import inspect
37
import typing
48
from enum import Enum
59
from typing import Annotated
610

711
from annotated_types import Ge, Le
8-
from pydantic import TypeAdapter, ValidationError, Field
12+
from pydantic import Field, TypeAdapter, ValidationError
913
from pydantic.dataclasses import dataclass
1014

1115

simple/main.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
partdiff-py is a Python port of partdiff.
3+
This is the "simple" variant.
4+
"""
5+
16
from itertools import count
27
from time import time
38

@@ -18,6 +23,15 @@
1823

1924

2025
def calculate(arguments: CalculationArguments, options: Options) -> CalculationResults:
26+
"""Solve the Poisson equation iteratively using the Jacobi or Gauß-Seidel method.
27+
28+
Args:
29+
arguments (CalculationArguments): The internal representation of the problem.
30+
options (Options): The program options.
31+
32+
Returns:
33+
CalculationResults: The results of the calculation.
34+
"""
2135
start_time = time()
2236
n = arguments.n
2337
tensor = arguments.tensor
@@ -66,7 +80,7 @@ def main() -> None:
6680
arguments = init_arguments(options)
6781
results = calculate(arguments, options)
6882
display_statistics(arguments, options, results)
69-
display_matrix(arguments, options, results)
83+
display_matrix(options, results)
7084

7185

7286
if __name__ == "__main__":

0 commit comments

Comments
 (0)