/
Mesh.pyx
58 lines (40 loc) · 1.36 KB
/
Mesh.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# cython: profile=False
#filename Mesh.pyx
from libc.math cimport ceil, floor
import numpy as np
cimport numpy as np
FTYPE = np.float32
ctypedef np.float32_t FTYPE_t
cdef class UniformMesh(object):
"""Uniform mesh. Right is not inclusive. Left is in construction.
"""
cdef FTYPE_t l
cdef FTYPE_t r
cdef FTYPE_t __dx
cdef int length
def __init__(self, l, r, dx):
self.l = l
self.r = r
self.__dx = dx
self.length = int(ceil((self.r - self.l) / self.__dx))
cpdef FTYPE_t max(self):
return self.r
cpdef FTYPE_t min(self):
return self.l
cpdef int mesh_index(self, FTYPE_t x):
#I'm having trouble deciding to permit things past the boundary from working or not
# assert x >= self.l and x <= self.r, "Mesh point is not within mesh: %g, [%g, %g]" % (x, self.l, self.r)
return max(0, min(self.length - 1, int(floor( (x - self.l) / self.__dx) )))
def __len__(self):
return self.length
def __getitem__(self, i):
return i * self.__dx + self.l
cpdef FTYPE_t cgetitem(self, int i):
return i * self.__dx + self.l
property dx:
def __get__(self):
return self.__dx
def __set__(self, value):
self.__dx = value
def __reduce__(self):
return UniformMesh, (self.l, self.r, self.dx)