Skip to content
Closed
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
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Clang-format whole repo
d8f14fdddb5ca0fbb32d8e2bf5ac2960d6ac5ce6
ed2117e6d6826a98b6988e2f18c0c34e408563b6
# ruff format whole repo
289205524393f52634346adcbcf74ea9d00257d5
8 changes: 4 additions & 4 deletions .github/workflows/black-fix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ jobs:
run: |
sudo apt update -y
sudo apt -y install python3-pip python3-setuptools python3-wheel
pip3 install black
pip3 install ruff

- name: Version
run: |
python3 --version
$HOME/.local/bin/black --version
$HOME/.local/bin/ruff --version

- name: Run black
run: |
pwd
ls
$HOME/.local/bin/black tests/ tools/ $(grep -EIlr '^#!.*python.*$' bin/ tests/ tools/ src/ | grep -v _boutpp_build)
$HOME/.local/bin/ruff format .

- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Apply black changes"
commit_message: "Apply ruff format changes"
6 changes: 3 additions & 3 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
fetch-depth: 0
submodules: true
- name: Set up Python
uses: actions/setup-python@v5
uses: actions/setup-python@v6
- name: Install dependencies
run: python -m pip install --upgrade pip &&
pip install --upgrade build &&
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:
fetch-depth: 0
submodules: true
- name: Set up Python
uses: actions/setup-python@v5
uses: actions/setup-python@v6
- name: Install dependencies
run: python -m pip install --upgrade pip &&
pip install --upgrade build &&
Expand Down Expand Up @@ -111,7 +111,7 @@ jobs:
fetch-depth: 0
submodules: true
- name: Set up Python
uses: actions/setup-python@v5
uses: actions/setup-python@v6
- name: Install dependencies
run: python -m pip install --upgrade pip &&
pip install --upgrade build &&
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ jobs:
with:
submodules: true

- uses: actions/setup-python@v5
- uses: actions/setup-python@v6
with:
python-version: '3.x'

Expand Down
20 changes: 5 additions & 15 deletions bin/bout-v5-factory-upgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ def find_factory_calls(factory, source):
\s*=\s*
{factory_name}::
.*{create_method}.*
""".format(
**factory
),
""".format(**factory),
source,
re.VERBOSE,
)
Expand All @@ -75,9 +73,7 @@ def find_type_pointers(factory, source):
r"""
\b{type_name}\s*\*\s* # Type name and pointer
([\w_]+)\s*; # Variable name
""".format(
**factory
),
""".format(**factory),
source,
re.VERBOSE,
)
Expand Down Expand Up @@ -107,9 +103,7 @@ def fix_declarations(factory, variables, source):
(.*?)(class\s*)? # optional "class" keyword
\b({type_name})\s*\*\s* # Type-pointer
({variable_name})\s*; # Variable
""".format(
type_name=factory["type_name"], variable_name=variable
),
""".format(type_name=factory["type_name"], variable_name=variable),
r"\1std::unique_ptr<\3> \4{nullptr};",
source,
flags=re.VERBOSE,
Expand All @@ -123,9 +117,7 @@ def fix_declarations(factory, variables, source):
({variable_name})\s* # Variable
=\s* # Assignment from factory
({factory_name}::.*{create_method}.*);
""".format(
variable_name=variable, **factory
),
""".format(variable_name=variable, **factory),
r"\1auto \4 = \5;",
source,
flags=re.VERBOSE,
Expand All @@ -139,9 +131,7 @@ def fix_declarations(factory, variables, source):
({variable_name})\s* # Variable
=\s* # Assignment
(0|nullptr|NULL);
""".format(
variable_name=variable, **factory
),
""".format(variable_name=variable, **factory),
r"\1std::unique_ptr<\2> \3{nullptr};",
source,
flags=re.VERBOSE,
Expand Down
12 changes: 3 additions & 9 deletions bin/bout-v5-xzinterpolation-upgrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ def fix_header_includes(old_header, new_header, source):
(<|")
({header}) # Header name
(>|")
""".format(
header=old_header
),
""".format(header=old_header),
r"\1\2{header}\4".format(header=new_header),
source,
flags=re.VERBOSE,
Expand All @@ -67,9 +65,7 @@ def fix_interpolations(old_interpolation, new_interpolation, source):
return re.sub(
r"""
\b{}\b
""".format(
old_interpolation
),
""".format(old_interpolation),
r"{}".format(new_interpolation),
source,
flags=re.VERBOSE,
Expand Down Expand Up @@ -120,9 +116,7 @@ def fix_factories(old_factory, new_factory, source):
return re.sub(
r"""
\b{}\b
""".format(
old_factory
),
""".format(old_factory),
r"{}".format(new_factory),
source,
flags=re.VERBOSE,
Expand Down
154 changes: 79 additions & 75 deletions examples/blob2d/blob_velocity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,96 +3,100 @@
import pickle

try:
from past.utils import old_div
from past.utils import old_div
except ImportError:
def old_div(a,b):
return a/b


def blob_velocity(n,**kwargs):

from boututils import calculus as Calc
# Calculate blob velocity in normalized time and normalized grid spacing
#
# Input: Blob density as a 3D vector in the form n[t,x,z] where t is time and x,z are the perpendicular spatial coordinates
#
# Keywords:
#
# type='peak' -> Calculate velocity of the peak density
# type='COM' -> Calculate centre of mass velocity
# Index=True -> return indices used to create velocity
#
# Default: Peak velocity with no index returning

size = n.shape

try:
v_type = kwargs['type']
except:
v_type = 'peak' #Default to peak velocity calculation
try:
return_index = kwargs['Index']
except:
return_index = False #Default to no index returning


if v_type == 'peak':
x = np.zeros(size[0])
z = np.zeros(size[0])
for i in np.arange(size[0]):
nmax,nmin = np.amax((n[i,:,:])),np.amin((n[i,:,:]))
xpos,zpos = np.where(n[i,:,:]==nmax)
x[i] = xpos[0]
z[i] = zpos[0]

if v_type == 'COM':
x = np.zeros(size[0])
z = np.zeros(size[0])
for i in np.arange(size[0]):
data = n[i,:,:] - n[0,0,0] #use corner cell rather than nmin
ntot = np.sum(data[:,:])

z[i] = old_div(np.sum(np.sum(data[:,:],axis=0)*(np.arange(size[2]))),ntot)
x[i] = old_div(np.sum(np.sum(data[:,:],axis=1)*(np.arange(size[1]))),ntot)

vx = Calc.deriv(x)
vz = Calc.deriv(z)

if return_index:
return vx,vz,x,z
else:
return vx,vz



data='data'
def old_div(a, b):
return a / b


def blob_velocity(n, **kwargs):
from boututils import calculus as Calc
# Calculate blob velocity in normalized time and normalized grid spacing
#
# Input: Blob density as a 3D vector in the form n[t,x,z] where t is time and x,z are the perpendicular spatial coordinates
#
# Keywords:
#
# type='peak' -> Calculate velocity of the peak density
# type='COM' -> Calculate centre of mass velocity
# Index=True -> return indices used to create velocity
#
# Default: Peak velocity with no index returning

size = n.shape

try:
v_type = kwargs["type"]
except:
v_type = "peak" # Default to peak velocity calculation
try:
return_index = kwargs["Index"]
except:
return_index = False # Default to no index returning

if v_type == "peak":
x = np.zeros(size[0])
z = np.zeros(size[0])
for i in np.arange(size[0]):
nmax, nmin = np.amax((n[i, :, :])), np.amin((n[i, :, :]))
xpos, zpos = np.where(n[i, :, :] == nmax)
x[i] = xpos[0]
z[i] = zpos[0]

if v_type == "COM":
x = np.zeros(size[0])
z = np.zeros(size[0])
for i in np.arange(size[0]):
data = n[i, :, :] - n[0, 0, 0] # use corner cell rather than nmin
ntot = np.sum(data[:, :])

z[i] = old_div(
np.sum(np.sum(data[:, :], axis=0) * (np.arange(size[2]))), ntot
)
x[i] = old_div(
np.sum(np.sum(data[:, :], axis=1) * (np.arange(size[1]))), ntot
)

vx = Calc.deriv(x)
vz = Calc.deriv(z)

if return_index:
return vx, vz, x, z
else:
return vx, vz


data = "data"

if True:
import sys
if len(sys.argv) > 1:
data=sys.argv[1]
import sys

if len(sys.argv) > 1:
data = sys.argv[1]

n = collect('n', path=data, info=False)
n = collect("n", path=data, info=False)


vx,vy,xx,yy = blob_velocity(n[:,:,0,:],type='COM',Index=True)
vx, vy, xx, yy = blob_velocity(n[:, :, 0, :], type="COM", Index=True)

f = open('Velocity.dat','wb')
pickle.dump(vx,f)
f = open("Velocity.dat", "wb")
pickle.dump(vx, f)
f.close()

f = open('Position.dat','wb')
pickle.dump(xx,f)
f = open("Position.dat", "wb")
pickle.dump(xx, f)
f.close()


f = open('Velocity.dat','rb')
f = open("Velocity.dat", "rb")
vx = pickle.load(f)
f.close()

try:
import matplotlib.pyplot as plt
plt.plot(vx)
plt.show()
import matplotlib.pyplot as plt

plt.plot(vx)
plt.show()
except ImportError:
pass
pass
5 changes: 3 additions & 2 deletions examples/boutpp/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

bc.init("mesh:n=48")


class Model(bc.PhysicsModel):
def init(self,restart):
def init(self, restart):
self.dens = bc.create3D("sin(x)")
self.solve_for(n=self.dens)

def rhs(self,time):
def rhs(self, time):
self.dens.ddt(bc.DDX(self.dens))


Expand Down
24 changes: 16 additions & 8 deletions examples/conduction-snb/fit_temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@
import matplotlib.pyplot as plt

Te_ref = np.loadtxt("temperature.csv", delimiter=",")
Te_ref[:,0] *= 1e-4 # Convert X axis to m
Te_ref[:, 0] *= 1e-4 # Convert X axis to m


def te_function(ypos, mid, wwid, w0, w1, w2, Tmax, Tmin, clip=False):
width = w0 + ((ypos - mid)*w1 + (ypos - mid)**2 * w2) * np.exp(-((ypos - mid)/wwid)**2)
width = w0 + ((ypos - mid) * w1 + (ypos - mid) ** 2 * w2) * np.exp(
-(((ypos - mid) / wwid) ** 2)
)

if clip:
width = np.clip(width, 1e-10, None)

return Tmax - 0.5 * (1 + np.tanh((ypos - mid)/width)) * (Tmax - Tmin)
return Tmax - 0.5 * (1 + np.tanh((ypos - mid) / width)) * (Tmax - Tmin)


popt, pcov = optimize.curve_fit(te_function, Te_ref[:,0], Te_ref[:,1],
p0 = [2.2e-4, 1e-4, 1e-4, 0.0, 0.0, 0.960, 0.190])
popt, pcov = optimize.curve_fit(
te_function,
Te_ref[:, 0],
Te_ref[:, 1],
p0=[2.2e-4, 1e-4, 1e-4, 0.0, 0.0, 0.960, 0.190],
)

print(popt)

xfit = np.linspace(Te_ref[0,0], Te_ref[-1,0], 100)
xfit = np.linspace(Te_ref[0, 0], Te_ref[-1, 0], 100)

plt.plot(xfit, te_function(xfit, *popt, clip=True), '-k')
plt.plot(Te_ref[:,0], Te_ref[:,1], 'or')
plt.plot(xfit, te_function(xfit, *popt, clip=True), "-k")
plt.plot(Te_ref[:, 0], Te_ref[:, 1], "or")
plt.show()
Loading
Loading