-
Notifications
You must be signed in to change notification settings - Fork 27
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
Dev guide #353
Draft
dpanici
wants to merge
33
commits into
master
Choose a base branch
from
dev_guide
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.
+4,194
−9
Draft
Dev guide #353
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
94d7e43
start writing development guide for DESC explaining various aspects a…
dpanici 09bb145
add to the compute section of dev guide
dpanici 1b0e56c
make note about objectives
dpanici 602b5f7
updated guide
dpanici 98d81de
add info on optimization prob
dpanici a2a38da
start adding info on constrained opt method for equilibrium solve (ho…
dpanici 588cd64
update Dev guide
dpanici e7d1790
Merge branch 'master' into dev_guide
unalmis 360cda2
Merge branch 'master' into dev_guide
unalmis 3d944e3
Merge branch 'master' into dev_guide
dpanici 73bcd98
split dev guide into smaller notebooks
dpanici 8966db3
update compute notebook
dpanici 023eb1e
Merge branch 'master' into dev_guide
dpanici b7bb59c
update compute notes
dpanici 9ef9983
Merge branch 'master' into dev_guide
unalmis 0080140
clarify dim_f on objective creation docs
unalmis 03dc381
Merge branch 'master' into dev_guide
unalmis 9cd0cee
Merge branch 'master' into dev_guide
unalmis 861de40
Merge branch 'master' into dev_guide
unalmis b2316ca
Update part of the guides
unalmis 91cf72a
Merge branch 'master' into dev_guide
unalmis c284f6a
add missing grid info
unalmis c0232f4
fix typo
unalmis 45f7379
Merge branch 'master' into dev_guide
unalmis 60935d9
Fix tex labels of some compute funs
unalmis 0651329
Merge branch 'master' into dev_guide
YigitElma 326d331
fix after merge conflict
YigitElma 739b4fc
add new notebooks to index.rst, delete some extra copies
YigitElma b4b6aa1
fix some build problems
YigitElma 3b991f8
fix errors
YigitElma c986268
fix more errors
YigitElma 4ffc092
change title versions
YigitElma 6efe224
Merge branch 'master' into dev_guide
YigitElma 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
3 changes: 2 additions & 1 deletion
3
docs/adding_compute_funs.rst → docs/dev_guide/adding_compute_funs.rst
This file contains 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 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
3 changes: 2 additions & 1 deletion
3
docs/adding_optimizers.rst → docs/dev_guide/adding_optimizers.rst
This file contains 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 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,24 @@ | ||
======= | ||
Backend | ||
======= | ||
|
||
|
||
DESC uses JAX for faster compile times, automatic differentiation, and other scientific computing tools. | ||
The purpose of ``backend.py`` is to determine whether DESC may take advantage of JAX and GPUs or default to standard ``numpy`` and CPUs. | ||
|
||
JAX provides a ``numpy`` style API for array operations. | ||
In many cases, to take advantage of JAX, one only needs to replace calls to ``numpy`` with calls to ``jax.numpy``. | ||
A convenient way to do this is with the import statement ``import jax.numpy as jnp``. | ||
|
||
Of course if such an import statement is used in DESC, and DESC is run on a machine where JAX is not installed, then a runtime error is thrown. | ||
We would prefer if DESC still works on machines where JAX is not installed. | ||
With that goal, in functions which can benefit from JAX, we use the following import statement: ``from desc.backend import jnp``. | ||
``desc.backend.jnp`` is an alias to ``jax.numpy`` if JAX is installed and ``numpy`` otherwise. | ||
|
||
While ``jax.numpy`` attempts to serve as a drop in replacement for ``numpy``, it imposes some constraints on how the code is written. | ||
For example, ``jax.numpy`` arrays are immutable. | ||
This means in-place updates to elements in arrays is not possible. | ||
To update elements in ``jax.numpy`` arrays, memory needs to be allocated to create a new array with the updated element. | ||
Similarly, JAX's JIT compilation requires code flow structures such as loops and conditionals to be written in a specific way. | ||
|
||
The utility functions in ``desc.backend`` provide a simple interface to perform these operations. | ||
This file contains 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,96 @@ | ||
============================= | ||
Adding new physics quantities | ||
============================= | ||
|
||
|
||
All calculation of physics quantities takes place in ``desc.compute`` | ||
|
||
As an example, we'll walk through the calculation of the radial component of the MHD | ||
force :math:`F_\rho` | ||
|
||
The full code is below: | ||
:: | ||
|
||
from desc.data_index import register_compute_fun | ||
|
||
@register_compute_fun( | ||
name="F_rho", | ||
label="F_{\\rho}", | ||
units="N \\cdot m^{-2}", | ||
units_long="Newtons / square meter", | ||
description="Covariant radial component of force balance error", | ||
dim=1, | ||
params=[], | ||
transforms={}, | ||
profiles=[], | ||
coordinates="rtz", | ||
data=["p_r", "sqrt(g)", "B^theta", "B^zeta", "J^theta", "J^zeta"], | ||
) | ||
def _F_rho(params, transforms, profiles, data, **kwargs): | ||
data["F_rho"] = -data["p_r"] + data["sqrt(g)"] * ( | ||
data["B^zeta"] * data["J^theta"] - data["B^theta"] * data["J^zeta"] | ||
) | ||
return data | ||
|
||
The decorator ``register_compute_fun`` tells DESC that the quantity exists and contains | ||
metadata about the quantity. The necessary fields are detailed below: | ||
|
||
|
||
* ``name``: A short, meaningful name that is used elsewhere in the code to refer to the | ||
quantity. This name will appear in the ``data`` dictionary returned by ``Equilibrium.compute``, | ||
and is also the argument passed to ``compute`` to calculate the quantity. IE, | ||
``Equilibrium.compute("F_rho")`` will return a dictionary containing ``F_rho`` as well | ||
as all the intermediate quantities needed to compute it. General conventions are that | ||
covariant components of a vector are called ``X_rho`` etc, contravariant components | ||
``X^rho`` etc, and derivatives by a single character subscript, ``X_r`` etc for :math:`\partial_{\rho} X` | ||
* ``label``: a LaTeX style label used for plotting. | ||
* ``units``: SI units of the quantity in LaTeX format | ||
* ``units_long``: SI units of the quantity, spelled out | ||
* ``description``: A short description of the quantity | ||
* ``dim``: Dimensionality of the quantity. Vectors (such as magnetic field) have ``dim=3``, | ||
local scalar quantities (such as vector components, pressure, volume element, etc) | ||
have ``dim=1``, global scalars (such as total volume, aspect ratio, etc) have ``dim=0`` | ||
* ``params``: list of strings of ``Equilibrium`` parameters needed to compute the quantity | ||
such as ``R_lmn``, ``Z_lmn`` etc. These will be passed into the compute function as a | ||
dictionary in the ``params`` argument. Note that this only includes direct dependencies | ||
(things that are used in this function). For most quantities, this will be an empty list. | ||
For example, if the function relies on ``R_t``, this dependency should be specified as a | ||
data dependency (see below), while the function to compute ``R_t`` itself will depend on | ||
``R_lmn`` | ||
* ``transforms``: a dictionary of what ``transform`` objects are needed, with keys being the | ||
quantity being transformed (``R``, ``p``, etc) and the values are a list of derivative | ||
orders needed in ``rho``, ``theta``, ``zeta``. IE, if the quantity requires | ||
:math:`R_{\rho}{\zeta}{\zeta}`, then ``transforms`` should be ``{"R": [[1, 0, 2]]}`` | ||
indicating a first derivative in ``rho`` and a second derivative in ``zeta``. Note that | ||
this only includes direct dependencies (things that are used in this function). For most | ||
quantities this will be an empty dictionary. | ||
* ``profiles``: List of string of ``Profile`` quantities needed, such as ``pressure`` or | ||
``iota``. Note that this only includes direct dependencies (things that are used in | ||
this function). For most quantities this will be an empty list. | ||
* ``coordinates``: String denoting which coordinate the quantity depends on. Most will be | ||
``"rtz"`` indicating it is a function of :math:`\rho, \theta, \zeta`. Profiles and flux surface | ||
quantities would have ``coordinates="r"`` indicating it only depends on `:math:\rho` | ||
* ``data``: What other physics quantities are needed to compute this quantity. In our | ||
example, we need the radial derivative of pressure ``p_r``, the Jacobian determinant | ||
``sqrt(g)``, and contravariant components of current and magnetic field. These dependencies | ||
will be passed to the compute function as a dictionary in the ``data`` argument. Note | ||
that this only includes direct dependencies (things that are used in this function). | ||
For example, we need ``sqrt(g)``, which itself depends on ``e_rho``, etc. But we don't | ||
need to specify ``e_rho`` here, that dependency is determined automatically at runtime. | ||
* ``kwargs``: If the compute function requires any additional arguments they should | ||
be specified like ``kwarg="thing"`` where the value is the name of the keyword argument | ||
that will be passed to the compute function. Most quantities do not take kwargs. | ||
|
||
|
||
The function itself should have a signature of the form | ||
:: | ||
|
||
_foo(params, transforms, profiles, data, **kwargs) | ||
|
||
Our convention is to start the function name with an underscore and have it be | ||
something like the ``name`` attribute, but the name of the function doesn't actually matter | ||
as long as it is registered. | ||
``params``, ``transforms``, ``profiles``, and ``data`` are dictionaries containing the needed | ||
dependencies specified by the decorator. The function itself should do any calculation | ||
needed using these dependencies and then add the output to the ``data`` dictionary and | ||
return it. The key in the ``data`` dictionary should match the ``name`` of the quantity. |
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.
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.
We should add answers to (good) questions like this here:
#854 (comment)
#854 (comment)