Skip to content

Commit

Permalink
MAINT: removing dependency on NumPy C API
Browse files Browse the repository at this point in the history
  • Loading branch information
saullocastro committed Nov 17, 2023
1 parent 2cd40d8 commit 4b3fb2b
Show file tree
Hide file tree
Showing 57 changed files with 718 additions and 831 deletions.
28 changes: 11 additions & 17 deletions compmech/conecyl/clpt/clpt_commons.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#cython: nonecheck=False
#cython: profile=False
#cython: infer_types=False
cimport numpy as np
import numpy as np
from libc.stdlib cimport malloc, free
from cython.parallel import prange
Expand All @@ -13,8 +12,6 @@ from compmech.conecyl.imperfections.mgi cimport cfw0x, cfw0t

DOUBLE = np.float64
INT = np.int64
ctypedef np.double_t cDOUBLE
ctypedef np.int64_t cINT

cdef extern from "math.h":
double cos(double t) nogil
Expand All @@ -30,12 +27,12 @@ cdef int castro = 0
cdef int num_cores = 4
cdef double pi=3.141592653589793

def fstrain(str model, np.ndarray[cDOUBLE, ndim=1] c,
def fstrain(str model, double [:] c,
double sina, double cosa, double tLA,
np.ndarray[cDOUBLE, ndim=1] xs,
np.ndarray[cDOUBLE, ndim=1] ts,
double [:] xs,
double [:] ts,
double r2, double L, int m1, int m2, int n2,
np.ndarray[cDOUBLE, ndim=1] c0, int m0, int n0, int funcnum,
double [:] c0, int m0, int n0, int funcnum,
int NL_kinematics):
model = model.lower()
if not 'clpt' in model:
Expand All @@ -47,7 +44,7 @@ def fstrain(str model, np.ndarray[cDOUBLE, ndim=1] c,
raise ImportError('Could not cimport {0}'.format(model))
# NL_kinematics = 0 donnell
# NL_kinematics = 1 sanders
cdef np.ndarray[cDOUBLE, ndim=1] es
cdef double [:] es
cdef cfstraintype *cfstrain
if NL_kinematics==0:
cfstrain = &cfstrain_donnell
Expand All @@ -59,9 +56,9 @@ def fstrain(str model, np.ndarray[cDOUBLE, ndim=1] c,
&c0[0], m0, n0, funcnum, &es[0])
return es

def fuvw(str model, np.ndarray[cDOUBLE, ndim=1] c, int m1, int m2, int n2,
def fuvw(str model, double [:] c, int m1, int m2, int n2,
double alpharad, double r2, double L, double tLA,
np.ndarray[cDOUBLE, ndim=1] xs, np.ndarray[cDOUBLE, ndim=1] ts):
double [:] xs, double [:] ts):
model = model.lower()
if not 'clpt' in model:
raise ValueError('{0} is not a valid CLPT model!'.format(model))
Expand All @@ -73,8 +70,8 @@ def fuvw(str model, np.ndarray[cDOUBLE, ndim=1] c, int m1, int m2, int n2,
raise ImportError('Could not cimport {0}'.format(model))
cdef int i, core_size
cdef double sina, cosa
cdef np.ndarray[cDOUBLE, ndim=2] us, vs, ws, phixs, phits
cdef np.ndarray[cDOUBLE, ndim=2] xs_r, ts_r
cdef double[:, ::1] us, vs, ws, phixs, phits
cdef double[:, ::1] xs_r, ts_r

sina = sin(alpharad)
cosa = cos(alpharad)
Expand Down Expand Up @@ -111,11 +108,8 @@ def fuvw(str model, np.ndarray[cDOUBLE, ndim=1] c, int m1, int m2, int n2,
phixs *= -1
r = r2 + xs_r*sina
phits *= -1/r
return (us.ravel()[:size],
vs.ravel()[:size],
ws.ravel()[:size],
phixs.ravel()[:size],
phits.ravel()[:size])
return (np.ravel(us)[:size], np.ravel(vs)[:size], np.ravel(ws)[:size],
np.ravel(phixs)[:size], np.ravel(phits)[:size])

cdef void cfN(double *c, double sina, double cosa, double tLA,
double *xs, double *ts, int size,
Expand Down
20 changes: 10 additions & 10 deletions compmech/conecyl/clpt/clpt_commons_include_cfN.pxi
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
def fstress(np.ndarray[cDOUBLE, ndim=1] c,
np.ndarray[cDOUBLE, ndim=2] F,
def fstress(double [:] c,
double [:, ::1] F,
double sina, double cosa, double tLA,
np.ndarray[cDOUBLE, ndim=1] xs,
np.ndarray[cDOUBLE, ndim=1] ts,
double [:] xs,
double [:] ts,
double r2, double L, int m1, int m2, int n2,
np.ndarray[cDOUBLE, ndim=1] c0, int m0, int n0, int funcnum,
double [:] c0, int m0, int n0, int funcnum,
int NL_kinematics, int num_cores=4):
# NL_kinematics = 0 donnell
# NL_kinematics = 1 sanders
cdef int size_core, i
cdef np.ndarray[cDOUBLE, ndim=2] Ns
cdef np.ndarray[cDOUBLE, ndim=2] xs_core, ts_core
cdef double [:, ::1] Ns
cdef double [:, ::1] xs_core, ts_core

size = xs.shape[0]
add_size = num_cores - (size % num_cores)
Expand All @@ -22,8 +22,8 @@ def fstress(np.ndarray[cDOUBLE, ndim=1] c,
xs_core = np.ascontiguousarray(np.hstack((xs, np.zeros(add_size))).reshape(num_cores, -1), dtype=DOUBLE)
ts_core = np.ascontiguousarray(np.hstack((ts, np.zeros(add_size))).reshape(num_cores, -1), dtype=DOUBLE)
else:
xs_core = np.ascontiguousarray(xs.reshape(num_cores, -1), dtype=DOUBLE)
ts_core = np.ascontiguousarray(ts.reshape(num_cores, -1), dtype=DOUBLE)
xs_core = np.ascontiguousarray(np.reshape(xs, (num_cores, -1)), dtype=DOUBLE)
ts_core = np.ascontiguousarray(np.reshape(ts, (num_cores, -1)), dtype=DOUBLE)

size_core = xs_core.shape[1]

Expand All @@ -33,7 +33,7 @@ def fstress(np.ndarray[cDOUBLE, ndim=1] c,
cfN(&c[0], sina, cosa, tLA, &xs_core[i,0], &ts_core[i,0],
size_core, r2, L, &F[0,0], m1, m2, n2,
&c0[0], m0, n0, funcnum, &Ns[i,0], NL_kinematics)
return Ns.ravel()[:size*e_num]
return np.ravel(Ns)[:size*e_num]

cdef void cfN(double *c, double sina, double cosa, double tLA,
double *xs, double *ts, int size,
Expand Down
18 changes: 9 additions & 9 deletions compmech/conecyl/clpt/clpt_commons_include_fstrain.pxi
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
def fstrain(np.ndarray[cDOUBLE, ndim=1] c,
def fstrain(double [:] c,
double sina, double cosa, double tLA,
np.ndarray[cDOUBLE, ndim=1] xs,
np.ndarray[cDOUBLE, ndim=1] ts,
double [:] xs,
double [:] ts,
double r2, double L, int m1, int m2, int n2,
np.ndarray[cDOUBLE, ndim=1] c0, int m0, int n0, int funcnum,
double [:] c0, int m0, int n0, int funcnum,
int NL_kinematics, int num_cores=4):
# NL_kinematics = 0 donnell
# NL_kinematics = 1 sanders
cdef int size_core, i
cdef np.ndarray[cDOUBLE, ndim=2] es
cdef np.ndarray[cDOUBLE, ndim=2] xs_core, ts_core
cdef double [:, ::1] es
cdef double [:, ::1] xs_core, ts_core
cdef cfstraintype *cfstrain

if NL_kinematics==0:
Expand All @@ -27,8 +27,8 @@ def fstrain(np.ndarray[cDOUBLE, ndim=1] c,
xs_core = np.ascontiguousarray(np.hstack((xs, np.zeros(add_size))).reshape(num_cores, -1), dtype=DOUBLE)
ts_core = np.ascontiguousarray(np.hstack((ts, np.zeros(add_size))).reshape(num_cores, -1), dtype=DOUBLE)
else:
xs_core = np.ascontiguousarray(xs.reshape(num_cores, -1), dtype=DOUBLE)
ts_core = np.ascontiguousarray(ts.reshape(num_cores, -1), dtype=DOUBLE)
xs_core = np.ascontiguousarray(np.reshape(xs, (num_cores, -1)), dtype=DOUBLE)
ts_core = np.ascontiguousarray(np.reshape(ts, (num_cores, -1)), dtype=DOUBLE)

size_core = xs_core.shape[1]

Expand All @@ -38,5 +38,5 @@ def fstrain(np.ndarray[cDOUBLE, ndim=1] c,
cfstrain(&c[0], sina, cosa, tLA, &xs_core[i,0], &ts_core[i,0],
size_core, r2, L, m1, m2, n2,
&c0[0], m0, n0, funcnum, &es[i,0])
return es.ravel()[:size*e_num]
return np.ravel(es)[:size*e_num]

28 changes: 15 additions & 13 deletions compmech/conecyl/clpt/clpt_commons_include_fuvw.pxi
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
def fuvw(np.ndarray[cDOUBLE, ndim=1] c, int m1, int m2, int n2,
def fuvw(double [:] c, int m1, int m2, int n2,
double alpharad, double r2, double L, double tLA,
np.ndarray[cDOUBLE, ndim=1] xs,
np.ndarray[cDOUBLE, ndim=1] ts,
double [:] xs,
double [:] ts,
int num_cores):
cdef int size_core, i
cdef double sina, cosa
cdef np.ndarray[cDOUBLE, ndim=2] us, vs, ws, phixs, phits
cdef np.ndarray[cDOUBLE, ndim=2] xs_core, ts_core
cdef double sina, cosa, r
cdef double [:, ::1] us, vs, ws, phixs, phits
cdef double [:, ::1] xs_core, ts_core

sina = sin(alpharad)
cosa = cos(alpharad)
Expand All @@ -21,8 +21,8 @@ def fuvw(np.ndarray[cDOUBLE, ndim=1] c, int m1, int m2, int n2,
xs_core = np.ascontiguousarray(np.hstack((xs, np.zeros(add_size))).reshape(num_cores, -1), dtype=DOUBLE)
ts_core = np.ascontiguousarray(np.hstack((ts, np.zeros(add_size))).reshape(num_cores, -1), dtype=DOUBLE)
else:
xs_core = np.ascontiguousarray(xs.reshape(num_cores, -1), dtype=DOUBLE)
ts_core = np.ascontiguousarray(ts.reshape(num_cores, -1), dtype=DOUBLE)
xs_core = np.ascontiguousarray(np.reshape(xs, (num_cores, -1)), dtype=DOUBLE)
ts_core = np.ascontiguousarray(np.reshape(ts, (num_cores, -1)), dtype=DOUBLE)

size_core = xs_core.shape[1]

Expand All @@ -40,8 +40,10 @@ def fuvw(np.ndarray[cDOUBLE, ndim=1] c, int m1, int m2, int n2,
&phixs[i,0])
cfwt(&c[0], m1, m2, n2, &xs_core[i,0], &ts_core[i,0], size_core, L,
&phits[i,0])
phixs *= -1.
r = r2 + xs_core*sina
phits *= -1./r
return (us.ravel()[:size], vs.ravel()[:size], ws.ravel()[:size],
phixs.ravel()[:size], phits.ravel()[:size])
for i in range(num_cores):
for j in range(size_core):
phixs[i, j] *= -1.
r = r2 + xs_core[i, j]*sina
phits[i, j] *= -1./r
return (np.ravel(us)[:size], np.ravel(vs)[:size], np.ravel(ws)[:size],
np.ravel(phixs)[:size], np.ravel(phits)[:size])
3 changes: 0 additions & 3 deletions compmech/conecyl/clpt/clpt_commons_include_header.pxi
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
cimport numpy as np
import numpy as np
from libc.stdlib cimport malloc, free
from cython.parallel import prange
Expand All @@ -7,8 +6,6 @@ from ..imperfections.mgi cimport cfw0x, cfw0t

DOUBLE = np.float64
INT = np.int64
ctypedef np.double_t cDOUBLE
ctypedef np.int64_t cINT

cdef extern from "math.h":
double cos(double t) nogil
Expand Down
27 changes: 12 additions & 15 deletions compmech/conecyl/clpt/clpt_donnell_bc1_linear.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@
#cython: infer_types=False
from scipy.sparse import coo_matrix
import numpy as np
cimport numpy as np
cimport cython
from cpython cimport bool


ctypedef np.double_t cDOUBLE
DOUBLE = np.float64
ctypedef np.int64_t cINT
INT = np.int64


Expand All @@ -30,7 +27,7 @@ cdef int num2 = 6
cdef double pi = 3.141592653589793


def fk0(double alpharad, double r2, double L, np.ndarray[cDOUBLE, ndim=2] F,
def fk0(double alpharad, double r2, double L, double [:, ::1] F,
int m1, int m2, int n2, int s):
cdef int i1, k1, i2, j2, k2, l2, c, row, col, section
cdef double A11, A12, A16, A22, A26, A66
Expand All @@ -45,8 +42,8 @@ def fk0(double alpharad, double r2, double L, np.ndarray[cDOUBLE, ndim=2] F,
cdef double cosi2xa, cosi2xb, cos2i2xa, cos2i2xb
cdef double cosk2xa, cosk2xb, sink2xa, sink2xb

cdef np.ndarray[cINT, ndim=1] k0r, k0c
cdef np.ndarray[cDOUBLE, ndim=1] k0v
cdef long [:] k0r, k0c
cdef double [:] k0v

sina = sin(alpharad)
cosa = cos(alpharad)
Expand Down Expand Up @@ -551,15 +548,15 @@ def fk0(double alpharad, double r2, double L, np.ndarray[cDOUBLE, ndim=2] F,
return k0


def fk0_cyl(double r2, double L, np.ndarray[cDOUBLE, ndim=2] F,
def fk0_cyl(double r2, double L, double [:, ::1] F,
int m1, int m2, int n2):
cdef int i1, k1, i2, j2, k2, l2, c, row, col
cdef double A11, A12, A16, A22, A26, A66
cdef double B11, B12, B16, B22, B26, B66
cdef double D11, D12, D16, D22, D26, D66
cdef double r
cdef np.ndarray[cINT, ndim=1] k0r, k0c
cdef np.ndarray[cDOUBLE, ndim=1] k0v
cdef long [:] k0r, k0c
cdef double [:] k0v

# sparse parameters
k11_cond_1 = 5
Expand Down Expand Up @@ -858,8 +855,8 @@ def fk0_cyl(double r2, double L, np.ndarray[cDOUBLE, ndim=2] F,
def fk0edges(int m1, int m2, int n2, double r1, double r2, double L,
double kphixBot, double kphixTop):
cdef int i1, k1, i2, j2, k2, l2, row, col, c
cdef np.ndarray[cINT, ndim=1] k0edgesr, k0edgesc
cdef np.ndarray[cDOUBLE, ndim=1] k0edgesv
cdef long [:] k0edgesr, k0edgesc
cdef double [:] k0edgesv

k11_cond_1 = 1
k11_cond_2 = 1
Expand Down Expand Up @@ -953,8 +950,8 @@ def fkG0(double Fc, double P, double T, double r2, double alpharad, double L,
cdef double cosk2xa, cosk2xb, sink2xa, sink2xb
cdef double sin2i1xa, sin2i1xb

cdef np.ndarray[cINT, ndim=1] kG0r, kG0c
cdef np.ndarray[cDOUBLE, ndim=1] kG0v
cdef long [:] kG0r, kG0c
cdef double [:] kG0v

# sparse parameters
k11_cond_1 = 1
Expand Down Expand Up @@ -1084,8 +1081,8 @@ def fkG0_cyl(double Fc, double P, double T, double r2, double L,
int m1, int m2, int n2):
cdef int i1, k1, i2, j2, k2, l2, c, row, col
cdef double r=r2
cdef np.ndarray[cINT, ndim=1] kG0r, kG0c
cdef np.ndarray[cDOUBLE, ndim=1] kG0v
cdef long [:] kG0r, kG0c
cdef double [:] kG0v

# sparse parameters
k11_cond_1 = 1
Expand Down
Loading

0 comments on commit 4b3fb2b

Please sign in to comment.