77
88from time import time
99
10- from libc.math cimport fabs
10+ from libc.math cimport fabs, sin
1111
1212import numpy as np
1313cimport numpy as np
@@ -16,6 +16,7 @@ from partdiff_common.parse_args import (
1616 Options,
1717 CalculationMethod,
1818 TerminationCondition,
19+ PerturbationFunction,
1920)
2021
2122from partdiff_common import (
@@ -27,37 +28,51 @@ cdef int METHOD_GAUSS_SEIDEL = CalculationMethod.GAUSS_SEIDEL.value
2728cdef int METHOD_JACOBI = CalculationMethod.JACOBI.value
2829cdef int TERMINATION_ACCURACY = TerminationCondition.ACCURACY.value
2930cdef int TERMINATION_ITERATIONS = TerminationCondition.ITERATIONS.value
31+ cdef int PERT_FUNC_F0 = PerturbationFunction.F0.value
32+ cdef int PERT_FUNC_FPISIN = PerturbationFunction.FPISIN.value
33+
34+ cdef double PI = 3.14159265358979323846
3035
3136cdef tuple calculate_inner(
3237 int method,
38+ int pert_func,
3339 int termination,
3440 int term_iteration,
3541 double term_accuracy,
3642 int n,
43+ double h,
3744 double [:, :, :] tensor,
38- double [:, :] perturbation_matrix,
3945):
4046 cdef int i, j
41- cdef double star, residuum, maxresiduum
47+ cdef double star, residuum, maxresiduum, pih, fpisin, fpisin_i
4248 cdef int stat_iteration = 0
4349 cdef double stat_accuracy = 0.0
4450 cdef double [:, :] matrix_out = tensor[0 , :, :]
4551 cdef double [:, :] matrix_in = matrix_out
4652 cdef double [:, :] final_matrix
53+ pih = 0.0
54+ fpisin = 0.0
55+ fpisin_i = 0.0
4756 if method == METHOD_JACOBI:
4857 matrix_in = tensor[1 , :, :]
58+ if pert_func == PERT_FUNC_FPISIN:
59+ pih = PI * h
60+ fpisin = 0.25 * (2.0 * PI * PI) * h * h
4961 while True :
5062 stat_iteration += 1
5163 maxresiduum = 0.0
5264 for i in range (1 , n):
65+ if pert_func == PERT_FUNC_FPISIN:
66+ fpisin_i = fpisin * sin(pih * i)
5367 for j in range (1 , n):
5468 star = 0.25 * (
5569 matrix_in[i - 1 , j] +
5670 matrix_in[i, j - 1 ] +
5771 matrix_in[i, j + 1 ] +
5872 matrix_in[i + 1 , j]
5973 )
60- star += perturbation_matrix[i, j]
74+ if pert_func == PERT_FUNC_FPISIN:
75+ star += fpisin_i * sin(pih * j)
6176 if (
6277 termination == TERMINATION_ACCURACY
6378 or term_iteration == stat_iteration
@@ -79,21 +94,23 @@ cdef tuple calculate_inner(
7994
8095def calculate (arguments: CalculationArguments , options: Options ) -> CalculationResults:
8196 cdef int method = options.method.value
97+ cdef int pert_func = options.pert_func.value
8298 cdef int termination = options.termination.value
8399 cdef int term_iteration = options.term_iteration
84100 cdef double term_accuracy = options.term_accuracy
85101 cdef int n = arguments.n
102+ cdef double h = arguments.h
86103 cdef double[:, :, :] tensor = arguments.tensor
87- cdef double[:, :] perturbation_matrix = arguments.perturbation_matrix
88104 start_time = time()
89105 final_matrix , stat_iteration , stat_accuracy = calculate_inner(
90106 method,
107+ pert_func,
91108 termination,
92109 term_iteration,
93110 term_accuracy,
94111 n,
112+ h,
95113 tensor,
96- perturbation_matrix,
97114 )
98115 end_time = time()
99116 duration = end_time - start_time
0 commit comments