Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/boututils/View3D.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

import numpy as np
from mayavi import mlab
from past.utils import old_div
from scipy import interpolate

from boutdata.collect import collect
from boututils.calculus import old_div
from boututils.View2D import View2D

from .boutgrid import create_grid
Expand Down Expand Up @@ -64,9 +64,7 @@ def View3D(g, path=None, gb=None):

# define the x (psi) array
xf = (
np.arange(np.float(g.qpsi.size))
* (g.sibdry - g.simagx)
/ np.float(g.qpsi.size - 1)
np.arange(float(g.qpsi.size)) * (g.sibdry - g.simagx) / float(g.qpsi.size - 1)
+ g.simagx
)

Expand Down
18 changes: 11 additions & 7 deletions src/boututils/calculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
B.Dudson, University of York, Nov 2009
"""

try:
from past.utils import old_div
except ImportError:

def old_div(a, b):
return a / b

import numbers

from numpy import arange, array, multiply, pi, sum, transpose, where, zeros
from numpy.fft import irfft, rfft


def old_div(a, b):
"""Python 2 style division: floor division if both are integers

Equivalent to past.utils.old_div from the python-future package
"""
if isinstance(a, numbers.Integral) and isinstance(b, numbers.Integral):
return a // b
return a / b


def deriv(*args, **kwargs):
"""Take derivative of 1D array

Expand Down
8 changes: 4 additions & 4 deletions src/boututils/fft_integrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def fft_integrate(y, loop=None):
n = np.size(y)

f = np.fft.fft(y) / n
imag = np.complex(0.0, 1.0)
imag = complex(0.0, 1.0)

result = np.arange(n) * f[0]

Expand All @@ -16,7 +16,7 @@ def fft_integrate(y, loop=None):
# even number of points

for i in range(1, n // 2):
a = imag * 2.0 * np.pi * np.float(i) / np.float(n)
a = imag * 2.0 * np.pi * float(i) / float(n)
f[i] = f[i] / a # positive frequencies
f[n - i] = -f[n - i] / a # negative frequencies

Expand All @@ -25,7 +25,7 @@ def fft_integrate(y, loop=None):
# odd number of points

for i in range(1, (n + 1) // 2):
a = imag * 2.0 * np.pi * np.float(i) / np.float(n)
a = imag * 2.0 * np.pi * float(i) / float(n)
f[i] = f[i] / a
f[n - i] = -f[n - i] / a

Expand All @@ -38,7 +38,7 @@ def fft_integrate(y, loop=None):

def test_integrate():
n = 10
dx = 2.0 * np.pi / np.float(n)
dx = 2.0 * np.pi / float(n)
x = dx * np.arange(n)

y = 1 + np.cos(x) - 0.5 * np.sin(2 * x)
Expand Down
11 changes: 6 additions & 5 deletions src/boututils/int_func.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import copy

import numpy
from past.utils import old_div
from scipy.integrate import simps
from scipy.integrate import simpson

from boututils.calculus import old_div

# integrate a function, always using the maximum
# number of grid-points possible for highest accuracy
Expand Down Expand Up @@ -36,13 +37,13 @@ def int_func(xin, fin=None, simple=None):
g[i] = g[i - 1] + 0.5 * (x[i] - x[i - 1]) * (f[i] + f[i - 1])

else:
n2 = numpy.int(old_div(n, 2))
n2 = int(old_div(n, 2))

g[0] = 0.0
for i in range(n2, n):
g[i] = simps(f[0 : i + 1], x[0 : i + 1])
g[i] = simpson(f[0 : i + 1], x=x[0 : i + 1])

for i in range(1, n2):
g[i] = g[n - 1] - simps(f[i::], x[i::])
g[i] = g[n - 1] - simpson(f[i::], x=x[i::])

return g
18 changes: 8 additions & 10 deletions src/boututils/mode_structure.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import sys

import numpy as numpy
from past.utils import old_div
from pylab import clf, draw, gca, plot, savefig, show, title, xlabel, xlim, ylim

from boutupgrader.common import yes_or_no as query_yes_no
from boututils.calculus import old_div
from boututils.fft_integrate import fft_integrate

# ; Calculates mode structure from BOUT++ output
Expand Down Expand Up @@ -107,7 +107,7 @@ def mode_structure(

nz = s[2]

dz = 2.0 * numpy.pi / numpy.float(period * (nz - 1))
dz = 2.0 * numpy.pi / float(period * (nz - 1))

# GET THE TOROIDAL SHIFT
tn = list(grid.keys())
Expand Down Expand Up @@ -187,8 +187,8 @@ def mode_structure(
dzi = old_div((zip1 - zi0), (nskip[y] + 1))

for i in range(nskip[y]):
zi = zi0 + numpy.float(i + 1) * dzi # zindex
w = old_div(numpy.float(i + 1), numpy.float(nskip[y] + 1)) # weighting
zi = zi0 + float(i + 1) * dzi # zindex
w = old_div(float(i + 1), float(nskip[y] + 1)) # weighting

f[ypos + i] = w * zinterp(vr[x, y + 1, :], zi) + (1.0 - w) * zinterp(
vr[x, y, :], zi
Expand Down Expand Up @@ -235,9 +235,7 @@ def mode_structure(
# ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
# ; take Fourier transform in theta angle

tarr = (
2.0 * numpy.pi * numpy.arange(np) / numpy.float(np)
) # regular array in theta
tarr = 2.0 * numpy.pi * numpy.arange(np) / float(np) # regular array in theta

farr = numpy.interp(tarr, theta, f)

Expand Down Expand Up @@ -265,7 +263,7 @@ def mode_structure(
xtitle = "Radial index"
if xq is not None:
# show as a function of q*n
xarr = qprof * numpy.float(n)
xarr = qprof * float(n)

xtitle = "q * n"
elif xpsi is not None:
Expand Down Expand Up @@ -306,7 +304,7 @@ def mode_structure(
xlabel(xtitle)
show(block=False)

q = old_div(numpy.float(i + 1), numpy.float(n))
q = old_div(float(i + 1), float(n))

pos = numpy.interp(q, qprof, xarr)

Expand Down Expand Up @@ -377,7 +375,7 @@ def mode_structure(
for i in range(minind, maxind + 1, subset):
plot(xarr, famp[:, i])

q = old_div(numpy.float(i + 1), numpy.float(n))
q = old_div(float(i + 1), float(n))
pos = numpy.interp(q, qprof, xarr)

plot([pos, pos], [0, 2.0 * numpy.max(fmax)], "--")
Expand Down
2 changes: 1 addition & 1 deletion src/boututils/moment_xyzt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from past.utils import old_div

from boututils.bunch import Bunch
from boututils.calculus import old_div


def RMSvalue(vec1d):
Expand Down
4 changes: 2 additions & 2 deletions src/boututils/spectrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

"""

from numpy import arange, cos, exp, linspace, max, min, power, sin, transpose, zeros
from scipy import fftpack, pi
from numpy import arange, cos, exp, linspace, max, min, pi, power, sin, transpose, zeros
from scipy import fftpack


def spectrogram(data, dx, sigma, clip=1.0, optimise_clipping=True, nskip=1.0):
Expand Down
5 changes: 2 additions & 3 deletions src/boututils/surface_average.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import numpy as np
import scipy
from past.utils import old_div

from boututils.calculus import deriv
from boututils.calculus import deriv, old_div
from boututils.int_func import int_func


Expand Down Expand Up @@ -73,7 +72,7 @@ def surface_average(var, grid, area=None):
if xi == nx - 1:
last = 1

dtheta = 2.0 * np.pi / np.float(ny)
dtheta = 2.0 * np.pi / float(ny)
r = grid["Rxy"][xi, yi]
z = grid["Zxy"][xi, yi]
n = np.size(r)
Expand Down
36 changes: 36 additions & 0 deletions src/boututils/tests/test_integrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import numpy as np

from boututils.calculus import old_div
from boututils.fft_integrate import fft_integrate
from boututils.spectrogram import spectrogram


def test_fft_integrate():
n = 64
x = 2.0 * np.pi * np.arange(n) / n
result = fft_integrate(np.cos(x))
dx = x[1] - x[0]
np.testing.assert_allclose(np.real(result) * dx, np.sin(x), atol=1e-10)


def test_int_func():
from boututils.int_func import int_func

x = np.linspace(0.0, 1.0, 101)
result = int_func(x, 3.0 * x**2)
np.testing.assert_allclose(result, x**3, atol=1e-6)


def test_old_div():
assert old_div(7, 2) == 3
assert old_div(-7, 2) == -4
assert old_div(np.int64(7), 2) == 3
assert old_div(7.0, 2) == 3.5
np.testing.assert_allclose(old_div(np.array([1, 3]), 2), [0.5, 1.5])


def test_spectrogram():
t = np.linspace(0.0, 1.0, 256)
data = np.sin(2.0 * np.pi * 20.0 * t)
result = spectrogram(data, t[1] - t[0], 0.1, nskip=1)
assert len(result) == 3
5 changes: 2 additions & 3 deletions src/boututils/volume_integral.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""Integrate over a volume"""

import numpy as np
from past.utils import old_div

from boututils.calculus import deriv
from boututils.calculus import deriv, old_div


def volume_integral(var, grid, xr=False):
Expand Down Expand Up @@ -75,7 +74,7 @@ def volume_integral(var, grid, xr=False):
last = 1

if (xi >= np.min(xr)) & (xi <= np.max(xr)):
dtheta = 2.0 * np.pi / np.float(ny)
dtheta = 2.0 * np.pi / float(ny)
r = grid["Rxy"][xi, yi]
z = grid["Zxy"][xi, yi]
# n = np.size(r)
Expand Down
Loading