Skip to content

Commit

Permalink
Pull changes from master
Browse files Browse the repository at this point in the history
Fix invalid escape sequences

Try/catch for potential UnboundLocalError

Prevent an UnboundLocalError

Add __getitem__ for Circuit

Tidy up optimiser

Fix error where find_psisurface will return random point if approaching magnetic axis

Fix on-axis q calculation
  • Loading branch information
tbody-cfs committed Nov 13, 2023
1 parent c17e466 commit 60b4d73
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 34 deletions.
1 change: 1 addition & 0 deletions freegs/coil.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .gradshafranov import Greens, GreensBr, GreensBz, mu0
import numpy as np
import numbers
import warnings
from shapely.geometry import Point
import warnings

Expand Down
1 change: 1 addition & 0 deletions freegs/critical.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ def find_safety(
# Get variables for loop integral around flux surface
r = psisurf[:, :, 0]
z = psisurf[:, :, 1]

fpol = eq.fpol(psirange[:]).reshape(npsi, 1)
Br = eq.Br(r, z)
Bz = eq.Bz(r, z)
Expand Down
14 changes: 11 additions & 3 deletions freegs/equilibrium.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,18 @@ def q(self, psinorm=None, npsi=100):
psinorm = linspace(1.0 / (npsi + 1), 1.0, npsi, endpoint=False)
return psinorm, critical.find_safety(self, psinorm=psinorm)

result = critical.find_safety(self, psinorm=psinorm)
elif np.any((psinorm < 0.01) | (psinorm > 0.99)):
psinorm_inner = np.linspace(0.01, 0.99, num=npsi)
q_inner = critical.find_safety(self, psinorm=psinorm_inner)

interp = interpolate.interp1d(psinorm_inner, q_inner, kind = "quadratic", fill_value = "extrapolate")
result = interp(psinorm)
else:
result = critical.find_safety(self, psinorm=psinorm)

# Convert to a scalar if only one result
if len(result) == 1:
return result[0]
if np.size(result) == 1:
return float(result)
return result

def tor_flux(self, psi=None):
Expand Down
2 changes: 1 addition & 1 deletion freegs/gradshafranov.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class GSElliptic:
Represents the Grad-Shafranov elliptic operator
.. math::
\Delta^* = R^2 \nabla\cdot\frac{1}{R^2}\nabla
\\Delta^* = R^2 \\nabla\\cdot\\frac{1}{R^2}\\nabla
which is
Expand Down
26 changes: 0 additions & 26 deletions freegs/machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,32 +98,6 @@ def __getitem__(self, name):
if label == name:
return coil
raise KeyError("Circuit does not contain coil with label '{0}'".format(name))

@property
def current(self) -> float:
return self._current

@current.setter
def current(self, value: float):
"""
When updating the circuit current, also update the current in the coils in the circuit (with multipliers)
"""
self._current = value
for _, coil, multiplier in self.coils:
coil.current = multiplier * value

@property
def control(self) -> bool:
return self._control

@control.setter
def control(self, value: bool):
"""
When updating the circuit control switch, also update the control switch in the coils in the circuit
"""
self._control = value
for _, coil, _ in self.coils:
coil.control = value

def psi(self, R, Z):
"""
Expand Down
4 changes: 2 additions & 2 deletions freegs/optimise.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import matplotlib.pyplot as plt
from freegs.plotting import plotEquilibrium

from math import sqrt
from numpy import sqrt, inf

# Measures which operate on Equilibrium objects

Expand Down Expand Up @@ -199,7 +199,7 @@ def solve_and_measure(eq):
return measure(eq)
except:
# Solve failed.
return float("inf")
return float(inf)

# Call the generic optimiser,
return optimiser.optimise(
Expand Down
2 changes: 1 addition & 1 deletion freegs/optimiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
along with FreeGS. If not, see <http://www.gnu.org/licenses/>.
"""

import random
from numpy import random
import copy
import bisect
import sys
Expand Down
2 changes: 1 addition & 1 deletion freegs/picard.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def solve(
bndry = 0.0

# Set an initial value for bndry_change (set high to prevent immediate convergence)
bndry_change = 10.0
bndry_change = np.inf

# Plasma assumed to not be limited at first
has_been_limited = False
Expand Down

0 comments on commit 60b4d73

Please sign in to comment.