freegsnke.optimise module

Routines for optimising equilibria.

These make use of the generic “optimiser” routines.

Copyright 2019 Ben Dudson, University of York. Email: benjamin.dudson@york.ac.uk

This file is part of FreeGSNKE.

FreeGSNKE is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

FreeGSNKE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with FreeGSNKE. If not, see <http://www.gnu.org/licenses/>.

class freegsnke.optimise.CoilHeight(label: str, minimum: float | None = None, maximum: float | None = None)[source]

Bases: object

Control to modify the height of a specified coil.

__init__(label: str, minimum: float | None = None, maximum: float | None = None) → None[source]

Store the coil label and bounds.

Parameters:
  • label (str) – Label of the coil to be modified.

  • minimum (float or None, optional) – Minimum allowed height.

  • maximum (float or None, optional) – Maximum allowed height.

get(eq: object) → float[source]

Get the coil height.

Parameters:

eq (object) – Equilibrium object.

Returns:

Current height of the coil.

Return type:

float

set(eq: object, Z: float) → None[source]

Set the coil height, clipped to the allowed bounds.

Parameters:
  • eq (object) – Equilibrium object, modified in place.

  • Z (float) – Requested coil height.

class freegsnke.optimise.CoilRadius(label: str, minimum: float | None = 0.0, maximum: float | None = None)[source]

Bases: object

Control to modify the radius of a specified coil.

__init__(label: str, minimum: float | None = 0.0, maximum: float | None = None) → None[source]

Store the coil label and bounds.

Parameters:
  • label (str) – Label of the coil to be modified.

  • minimum (float or None, optional) – Minimum allowed radius.

  • maximum (float or None, optional) – Maximum allowed radius.

get(eq: object) → float[source]

Get the coil radius.

Parameters:

eq (object) – Equilibrium object.

Returns:

Current radius of the coil.

Return type:

float

set(eq: object, R: float) → None[source]

Set the coil radius, clipped to the allowed bounds.

Parameters:
  • eq (object) – Equilibrium object, modified in place.

  • R (float) – Requested coil radius.

class freegsnke.optimise.PlotMonitor[source]

Bases: object

Plot the best solution at the end of each generation.

The plot is saved to a PDF file named generation_<n>.pdf.

__init__() → None[source]

Create the figure and axis used for plotting.

freegsnke.optimise.max_abs_coil_current(eq: object) → float[source]

Return the maximum absolute coil current of an equilibrium.

Parameters:

eq (object) – Equilibrium object.

Returns:

Largest absolute coil current.

Return type:

float

freegsnke.optimise.max_coil_force(eq: object) → float[source]

Return the maximum magnitude of the force on any coil.

Parameters:

eq (object) – Equilibrium object.

Returns:

Largest value of sqrt(force[0]**2 + force[1]**2) over the coils.

Return type:

float

freegsnke.optimise.no_wall_intersection(eq: object) → float[source]

Penalise intersection of the LCFS with the walls.

Parameters:

eq (object) – Equilibrium object.

Returns:

inf if the equilibrium intersects the wall, otherwise 0.0.

Return type:

float

freegsnke.optimise.optimise(eq: object, controls: list[object], measure: Callable[[object], float], maxgen: int = 10, N: int = 10, CR: float = 0.3, F: float = 1.0, monitor: Callable[[int, tuple[float, object], list[tuple[float, object]]], None] | None = None) → object[source]

Use Differential Evolution to optimise an Equilibrium.

See https://en.wikipedia.org/wiki/Differential_evolution

Parameters:
  • eq (object) – Equilibrium used as starting solution. It is deep-copied and passed to controls and measure.

  • controls (list[object]) – Control objects, each with methods set(eq, value), which modifies the given Equilibrium, and get(eq), which returns the value from it.

  • measure (Callable[[object], float]) – Returns a score for a given Equilibrium; the optimiser minimises it.

  • maxgen (int, optional) – Maximum number of generations.

  • N (int, optional) – Population size (must be >= 4).

  • CR (float, optional) – Crossover probability, in [0, 1].

  • F (float, optional) – Differential weight, in [0, 2].

  • monitor (Callable or None, optional) – Called each generation as monitor(generation, best, population), where generation is an int, best is a (score, equilibrium) tuple and population is a list of such tuples.

Returns:

The Equilibrium with the lowest score.

Return type:

object

freegsnke.optimise.weighted_sum(*args: Callable[[object], float] | tuple[Callable[[object], float], float]) → Callable[[object], float][source]

Combine measures together as a weighted sum.

The returned function takes a single argument (the equilibrium) and passes it to each given function. The returned values are multiplied by their weights and summed.

Parameters:

*args (Callable or tuple[Callable, float]) – Measure functions, or (function, weight) pairs. If no weight is supplied a weight of 1.0 is assumed.

Returns:

Function mapping an equilibrium to the weighted sum of the measures.

Return type:

Callable[[object], float]