_tools.py 1.88 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
2
3
4
import time
import atexit
import gmsh
import signal
5
6
import numpy as np
import ctypes
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
7
8
9
10
11
12
13
14

gmsh.initialize()
gmsh.option.setNumber("General.Terminal",1)

signal.signal(signal.SIGINT, signal.SIG_DFL)

timers = {}

Jonathan Lambrechts's avatar
clean    
Jonathan Lambrechts committed
15
16
def _np2c(a,dtype=float,order="C") :
    tmp = np.require(a,dtype,order)
17
18
19
20
    r = ctypes.c_void_p(tmp.ctypes.data)
    r.tmp = tmp
    return r

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
21
22
23
24
25
26
27
28
29
30
31
def timeit(func):
    def wrapper(*args, **kwargs):
        self = args[0]
        tic = time.process_time()
        r = func(*args, **kwargs)
        toc = time.process_time()
        name = func.__name__
        timers[name] = timers.get(name, 0) + toc - tic
        return r
    return wrapper

Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
32
33
34
35
36
def timeprint(timers) :
    if len(timers) != 0 :
        print(timers)

atexit.register(timeprint, timers)
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
37
38
39
40
41
42
43
44
45
46
47

try :
    from . import mumpslsys
    have_mumps = True
except:
    have_mumps = False
try :
    from . import petsclsys
    have_petsc = True
except :
    have_petsc = False
48
49
50
51
52
try :
    from . import petsc4pylsys
    have_petsc4py = True
except :
    have_petsc4py = False
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
53
54
55
56
57
58
59
60
try :
    from . import scipylsys
    have_scipy = True
except :
    have_scipy = False

def get_linear_system_package(choices=None):
    if choices is None:
Jonathan Lambrechts's avatar
clean    
Jonathan Lambrechts committed
61
        choices = ["mumps","petsc","petsc4py","scipy"]
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
62
63
64
65
66
67
68
69
70
71
72
73
    if isinstance(choices, str):
        choices = [choices]
    for choice in choices:
        if have_mumps and choice=="mumps":
            print("using mumps linear system")
            return mumpslsys.LinearSystem
        if have_petsc and choice=="petsc":
            print("using petsc linear system")
            return petsclsys.LinearSystem
        if have_scipy and choice=="scipy":
            print("using scipy linear system")
            return scipylsys.LinearSystem
74
75
76
        if have_petsc4py and choice=="petsc4py":
            print("using petsc4py linear system")
            return petsc4pylsys.LinearSystem
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
77
78
    raise ValueError("linear system not available")