-
Notifications
You must be signed in to change notification settings - Fork 73
Cbs/curvealongz #461
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
smiet
wants to merge
6
commits into
master
Choose a base branch
from
cbs/curvealongz
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Cbs/curvealongz #461
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e984634
A curve along Z, always nice to have and can be displaced to perturb …
smiet 23adad1
tests, a util to create the coil, improvements
smiet dfb5942
import fix because file had name of fn
smiet afc5ab0
added helper function and some tests
smiet 35b4ea4
remove misnamed stale file
smiet ed474ff
Merge remote-tracking branch 'origin/master' into cbs/curvealongz
smiet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import jax.numpy as jnp | ||
| from math import pi | ||
| import numpy as np | ||
| from .curve import JaxCurve | ||
|
|
||
| __all__ = ['CurveAlongZ'] | ||
|
|
||
|
|
||
| def jaxcurvealongz_pure(dofs, quadpoints, zscale): | ||
| """ | ||
| Pure function for the CurveAlongZ, returns the points along the curve given the degrees of freedom and the quadpoints. | ||
|
|
||
| Args: | ||
| dofs: [xpos, ypos]; degrees of freedom of the curve, must be len(3) | ||
| quadpoints: points in [0,1]; points on which to evaluate the curve | ||
| """ | ||
| lenquadpoints = len(quadpoints) | ||
| x = dofs[0]*jnp.ones(lenquadpoints) | ||
| y = dofs[1]*jnp.ones(lenquadpoints) | ||
| if lenquadpoints < 2: | ||
| halfstep = 0 # don't offset for one point or two points | ||
| else: | ||
| halfstep = .5/(lenquadpoints-1) # avoid evaluating at 0 or 1. | ||
| z = jnp.tan(((quadpoints + halfstep) - .5)*pi)*zscale | ||
| gamma = jnp.stack((x, y, z), axis=1) | ||
| return gamma | ||
|
|
||
|
|
||
| class CurveAlongZ(JaxCurve): | ||
| r''' | ||
| Straight vertical curve, parallel to the z-axis. | ||
|
|
||
| Useful for quickly generating a toroidal field, comparing to tokamak equilibria | ||
| where an axisymmetric 1/R field is present, and testing. | ||
|
|
||
| Degrees of freedom are the x, y coordinates of the vertical coil. | ||
| Displacing this from [0,0] can give a 1/1 perturbation if you feel like it. | ||
|
|
||
|
|
||
| Args: | ||
| quadpoints: number of grid points/resolution along the curve; | ||
| xpos: the x-coordinate of the vertical coil | ||
| ypos: the y-coordinate of the vertical coil | ||
| zscale: points are closer together at z=0, and spread apart using a zscale*tan(pi*(gamma-.5)) scaling. | ||
| ''' | ||
|
|
||
| def __init__(self, quadpoints, xpos=0., ypos=0., zscale=10, fix_dofs=True, **kwargs): | ||
| if isinstance(quadpoints, int): | ||
| quadpoints = np.linspace(0, 1, quadpoints, endpoint=False) | ||
| self.xpos = xpos | ||
| self.ypos = ypos | ||
| self.set_dofs_impl([self.xpos, self.ypos]) | ||
| self._fix_dofs = fix_dofs | ||
| self.zscale = zscale | ||
|
|
||
|
|
||
| pure = lambda dofs, points: jaxcurvealongz_pure( | ||
| dofs, points, self.zscale) | ||
|
|
||
|
|
||
|
|
||
| super().__init__(quadpoints, pure, x0=np.array([self.xpos, self.ypos]), names=self.make_dof_names(), **kwargs) | ||
| # unless you are doing strange things, you don't want to move the coil so we | ||
| # set the dofs fixed. | ||
| if fix_dofs: | ||
| self.fix_all() | ||
|
|
||
| def num_dofs(self): | ||
| return 2 | ||
|
|
||
| def get_dofs(self): | ||
| return np.array([self.xpos, self.ypos]) | ||
|
|
||
| def set_dofs_impl(self, dofs): | ||
| self.xpos = dofs[0] | ||
| self.ypos = dofs[1] | ||
|
|
||
|
|
||
| def make_dof_names(self): | ||
| return ['xpos', 'ypos'] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """ | ||
| This file contains a helper function for coils. | ||
| Currently only a simple function that returns a simsopt.field.Coil object that represents a current along the z-axis. | ||
|
|
||
| Useful for adding a toroidal field, for example to perturb a stellarator equilibrium. | ||
| """ | ||
|
|
||
| __all__ = ['current_along_z',] | ||
|
|
||
| from simsopt.geo import CurveAlongZ | ||
| from simsopt.field import Coil, Current | ||
|
|
||
| __all__ = ['current_along_z',] | ||
|
|
||
|
|
||
| def current_along_z(current, quadpoints=100, x0=0., y0=0., zscale=10, coil_dofs_fixed=True): | ||
| """ | ||
| Returns a Coil object that represents a current along the z-axis. | ||
| Add this to your a coilset before calling simsopt.BiotSavart to | ||
| add a toroidal field. | ||
|
|
||
| The dofs of the coil are only the current value, unles you set coil_dofs_fixed=False. | ||
|
|
||
| Args: | ||
| current: the current in Amperes | ||
| quadpoints: number of grid points/resolution along the curve; | ||
| x0: (default 0) the x-coordinate . | ||
| y0: (default 0) the y-coordinate. | ||
| zscale: (default 10) points are closer together at z=0, and spread apart using a zscale*tan(pi*(gamma-.5)) scaling. | ||
| coil_dofs_fixed: (default True) unless you are doing strange things, you don't want to move the coil from the axis. | ||
| """ | ||
| return Coil(CurveAlongZ(quadpoints, x0, y0, zscale, coil_dofs_fixed=coil_dofs_fixed), Current(current)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you really want this approximation of an axisymmetric 1/R field? why not just implement the analytical magnetic field instead of this approximation?
see how we do this here:
simsopt/src/simsopt/field/magneticfieldclasses.py
Line 236 in 90e276f