Skip to content
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

Some changes for FCI #251

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Some changes for FCI #251

wants to merge 5 commits into from

Conversation

dschwoerer
Copy link
Contributor

f08d14d is not FCI related, but I hope to add soon attributes to zoidberg grids, in which case it would be great if that would be preserved ...

@dschwoerer dschwoerer marked this pull request as ready for review September 30, 2022 10:57
@codecov-commenter
Copy link

codecov-commenter commented Sep 30, 2022

Codecov Report

Merging #251 (07ebb9f) into master (280a570) will decrease coverage by 1.93%.
The diff coverage is 3.15%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master     #251      +/-   ##
==========================================
- Coverage   69.01%   67.09%   -1.93%     
==========================================
  Files          15       16       +1     
  Lines        3208     3300      +92     
  Branches      790      802      +12     
==========================================
  Hits         2214     2214              
- Misses        732      822      +90     
- Partials      262      264       +2     
Impacted Files Coverage Δ
xbout/tracing.py 0.00% <0.00%> (ø)
xbout/geometries.py 69.58% <75.00%> (-0.37%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Collaborator

@johnomotani johnomotani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dschwoerer, looks nice!

Is it worth adding a method to BoutDataset so that if a dataset has FCI geometry it's possible to get a plot from a one-line call, something like ds.bout.poincare_plot()?

It would be lovely to have a test for the field-line tracing, but I guess that could be pretty complicated to set up.

@@ -0,0 +1,115 @@
import numpy as np
import eudist
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add eudist as a dependency? Also would be nice if it could be added to conda-forge, for those of us who use conda 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably. I thought of maybe an extra [fci] section?
I have no experience with conda. Do you want to do it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably. I thought of maybe an extra [fci] section?

Sounds good!

I have no experience with conda. Do you want to do it?

No prob. I'll set it up and make you a maintainer on the 'feedstock' repo. It's easy to maintain once it's set up, just click 'merge' on auto-generated PRs that're triggered by releases to PyPi and update the dependency list if it changes.

xbout/tracing.py Outdated
from warnings import warn


def rz_to_ab(rz, mesh):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comment about what this function does would be useful. Looks like a Newton iteration? Finds position within a cell in grid-index space? Using some sort of linear algebra on basis vectors(?) describing the cell?

xbout/tracing.py Outdated
return albe, ij


def ab_to_rz(ab, ij, mesh):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on what this does. Grid-index space -> spatial position? What are a, b, c?

xbout/tracing.py Outdated


def setup_mesh(x, y):
def per(d):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could maybe call this function apply_periodicity() to be slightly easier to read?

xbout/tracing.py Outdated
return mymesh(x, y)


class mymesh(eudist.PolyMesh):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe PascalCase form - MyMesh - as it's a class?

Also a brief docstring would be nice.

xbout/tracing.py Outdated
return A + al * a + be * b + al * be * c


def setup_mesh(x, y):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add docstring? E.g. something like "Creates a MyMesh object from R and Z coordinates..." and also explain why the periodicity thing is needed (I guess because the z-dimension of the grid is assumed to be periodic?).

self.shape = tuple([x - 1 for x in x.shape])


class Tracer:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring would be nice. How does this work? Somehow relies on the field line tracing that was already done by Zoidberg to make the grid file?

)
self.meshes = meshes

def poincare(self, rz, yind=0, num=100, early_exit="warn"):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a docstring that explains the arguments.

xbout/tracing.py Outdated

def rz_to_ab(rz, mesh):
ij = mesh.find_cell(rz)
assert ij >= 0, "We left the domain"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be an actual exception - maybe define an OutOfDomainError or something? If for some reason someone runs Python in 'optimised' mode with -O, all assert statements just get ignored (https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement), so they should only be used for debugging, not program logic.

for d, meshes in enumerate(thismeshes):
try:
abij = rz_to_ab(rz, meshes[0])
except AssertionError as e:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: will need updating if the exception is changed as suggested above.

@dschwoerer
Copy link
Contributor Author

Is it worth adding a method to BoutDataset so that if a dataset has FCI geometry it's possible to get a plot from a one-line call, something like ds.bout.poincare_plot()?

Do you know how to do that? That certainly would be nice.

It would be lovely to have a test for the field-line tracing, but I guess that could be pretty complicated to set up.

Test would be easy if we had an example grid. Can we download one for the tests? At least for CI it should be no issue. We could host it on zenodo or at the mpcdf gitlab or something else?

@johnomotani
Copy link
Collaborator

johnomotani commented Oct 28, 2022

For a method, in BoutDataset add something like (?)

"""
docstring...
"""
def poincare_plot(
    self,
    rz,
    yind=0,
    num=100,
    early_exit="warn",
    direction="forward",
    new_fig=True,
    show=False,
    save_as=None
):
    ds = self.data
    mytracer = Tracer(ds, direction=direction)
    rz = mytracer.poincare(rz=rz, yind=yind, num=num, early_exit=early_exit)

    if new_fig:
        plt.figure()
    p = plt.scatter(rz)
    if save_as:
        plt.savefig(save_as)
    if show:
        plt.show()

    return p

Probably need to also set some default arguments so that the plot looks nice. Maybe also have a scatter_kwargs=None argument that lets a dict be passed to tweak the plot?

@johnomotani
Copy link
Collaborator

About the grid file for testing: Indeed don't want to add a binary file to the install, so would be a test that only runs from the source repo. We could add another directory like extra_tests so it doesn't get installed. For managing the file, we could either add it to the repo with git-lfs, or put it on Zenodo and wget it. I think git-lfs is simpler, and I guess it won't be a huge file (?) - I don't think developers would mind downloading 100MB or so...

@dschwoerer
Copy link
Contributor Author

Oh sorry, I misread. I thought you wanted to only add the method if it is an fci dataset.

Concerning git-lfs, I do not like the github one, they have not only strict size limits, which should be fine, but also limits on traffic, so the fci grid repo is already putting me at the limit ...
So I will go with zenodo + caching, if you do not like the mpcdf gitlab approach, which I use for xemc3 ...

@johnomotani
Copy link
Collaborator

Oh sorry, I misread. I thought you wanted to only add the method if it is an fci dataset.

That would be complicated! Probably should add some check that the dataset is an fci one in the method though.

If it's not a lot more complicated, I think I'd prefer Zenodo to mpcdf-gitlab, as it's not dependent on having someone with an mpcdf account to keep it going.

The forward/backward_{R,Z} are needed for field line tracing
* Rename internal stuff with _
* Use OutOfDomainError if we leave the domain
* Add doc strings
Currently we only need eudist additionally
@dschwoerer
Copy link
Contributor Author

Still missing:

  • tests
  • dataset accessor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants