Debian Codegraph @

brian2/codegen/runtime/GSLcython_rt/GSLcython_rt.py

raw
1 """
2 Module containing the Cython CodeObject for code generation for integration using the ODE solver provided in the
3 GNU Scientific Library (GSL)
4 """
5
6 import os
7 import sys
8
9 from distutils.errors import CompileError
10
11 from brian2.core.preferences import prefs
12
13 from ...generators.cython_generator import CythonCodeGenerator
14 from ...generators.GSL_generator import GSLCythonCodeGenerator
15 from ..cython_rt import CythonCodeObject
16
17 __all__ = ["GSLCythonCodeObject", "IntegrationError"]
18
19
20 class GSLCompileError(Exception):
21 pass
22
23
24 class IntegrationError(Exception):
25 """
26 Error used to signify that GSL was unable to complete integration (only works for cython)
27 """
28
29 pass
30
31
32 class GSLCythonCodeObject(CythonCodeObject):
33 templater = CythonCodeObject.templater.derive("brian2.codegen.runtime.GSLcython_rt")
34
35 # CodeGenerator that is used to do bulk of abstract_code --> language specific translation
36 original_generator_class = CythonCodeGenerator
37 generator_class = GSLCythonCodeGenerator
38
39 def compile(self):
40 self.libraries += ["gsl", "gslcblas"]
41 self.headers += [
42 "<stdio.h>",
43 "<stdlib.h>",
44 "<gsl/gsl_odeiv2.h>",
45 "<gsl/gsl_errno.h>",
46 "<gsl/gsl_matrix.h>",
47 ]
48 if sys.platform == "win32":
49 self.define_macros += [("WIN32", "1"), ("GSL_DLL", "1")]
50 if prefs.GSL.directory is not None:
51 self.include_dirs += [prefs.GSL.directory]
52 self.library_dirs += [
53 os.path.abspath(os.path.join(prefs.GSL.directory, "..", "lib"))
54 ]
55 if sys.platform == "win32":
56 GSL_bin = os.path.abspath(
57 os.path.join(os.path.join(prefs.GSL.directory, "..", "bin"))
58 )
59 os.add_dll_directory(GSL_bin)
60 try:
61 super().compile()
62 except CompileError as err:
63 raise GSLCompileError(
64 "\nCompilation of files generated for integration with GSL has failed."
65 "\nOne cause for this could be incorrect installation of GSL itself."
66 "\nIf GSL is installed but Python cannot find the correct files, it is "
67 "also possible to give the gsl directory manually by specifying "
68 "prefs.GSL.directory = ..."
69 ) from err