|
3 | 3 | import numpy
|
4 | 4 |
|
5 | 5 | from src.utils import visu1D
|
6 |
| -from ctypes import POINTER, c_int, c_float, cdll |
| 6 | +from ctypes import POINTER, c_int, c_double, cdll |
7 | 7 |
|
8 | 8 | """
|
9 | 9 | MacOS: Before running this script, please build fortran library [1]:
|
10 |
| - gfortran -dynamiclib src/utils/tridiag.f90 -o src/tridiag.dylib |
| 10 | + src$ gfortran -shared -fPIC libs/tridiag.f90 -o tridiag.dylib |
11 | 11 | There will be a file called 'tridiag.dylib' in the project src folder.
|
12 | 12 | Reference:
|
13 | 13 | [1] http://jean-pierre.moreau.pagesperso-orange.fr/Fortran/tridiag_f90.txt
|
14 | 14 | [2] https://stackoverflow.com/questions/19263879/speeding-up-element-wise-array-multiplication-in-python/19458585#19458585
|
15 | 15 | """
|
16 | 16 | ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
|
17 | 17 | FORTRAN_LIB_PATH = os.path.join(ROOT_DIR, 'tridiag.dylib')
|
18 |
| -fortran = cdll.LoadLibrary(FORTRAN_LIB_PATH) |
19 |
| -fortran.tridiag.argtypes = [POINTER(c_float), |
20 |
| - POINTER(c_float), |
21 |
| - POINTER(c_float), |
22 |
| - POINTER(c_float), |
23 |
| - POINTER(c_float), |
24 |
| - POINTER(c_int), |
25 |
| - POINTER(c_int)] |
26 |
| -fortran.tridiag.restype = None |
| 18 | +fort_lib = cdll.LoadLibrary(FORTRAN_LIB_PATH) |
| 19 | +tri_diag = fort_lib.__getattr__("tridiag") |
| 20 | +tri_diag.argtypes = [POINTER(c_double), |
| 21 | + POINTER(c_double), |
| 22 | + POINTER(c_double), |
| 23 | + POINTER(c_double), |
| 24 | + POINTER(c_double), |
| 25 | + POINTER(c_int), |
| 26 | + POINTER(c_int)] |
| 27 | +tri_diag.restype = None |
27 | 28 |
|
28 | 29 | if __name__ == "__main__":
|
29 | 30 | # FDM, implicit scheme for the heat equation:
|
30 | 31 | # INPUT
|
31 | 32 | # Advection velocity:
|
32 | 33 | mu = 1 / 16
|
33 | 34 | # Grid size; Use periodic boundary conditions
|
34 |
| - X, M, Tend = 1, 10, 0.5 |
| 35 | + X, M, Tend = 1, 5000, 1 |
35 | 36 | h = 1 / (M + 1)
|
36 | 37 | Dt = 0.02
|
37 | 38 | x = numpy.linspace(0, X, M + 2) # x(1) =0, x(2) = h, , ..., x(M) = X -h; x(M+1) = X;
|
|
51 | 52 | B = numpy.zeros(M + 2)
|
52 | 53 | B[:] = 1.0 + 2.0 * mu
|
53 | 54 |
|
| 55 | + code = c_int(-1) |
| 56 | + n = c_int(M + 2) |
| 57 | + |
54 | 58 | while time < Tend:
|
55 | 59 | a, b, c = A, B, C
|
56 |
| - code = -1 |
57 |
| - fortran.tridiag(a.ctypes.data_as(POINTER(c_float)), |
58 |
| - b.ctypes.data_as(POINTER(c_float)), |
59 |
| - b.ctypes.data_as(POINTER(c_float)), |
60 |
| - wm.ctypes.data_as(POINTER(c_float)), |
61 |
| - wn.ctypes.data_as(POINTER(c_float)), |
62 |
| - c_int(M + 2), c_int(code)) |
| 60 | + tri_diag(c.ctypes.data_as(POINTER(c_double)), |
| 61 | + b.ctypes.data_as(POINTER(c_double)), |
| 62 | + a.ctypes.data_as(POINTER(c_double)), |
| 63 | + wm.ctypes.data_as(POINTER(c_double)), |
| 64 | + wn.ctypes.data_as(POINTER(c_double)), |
| 65 | + n, code) |
63 | 66 | wn = wm
|
64 | 67 |
|
65 | 68 | time += Dt
|
|
0 commit comments