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

Feature/line integrator #1

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

kripnerl
Copy link

  • add line_elements integrator
  • add calculation of vector potential
  • tests
    WIP: use integrate method vs sum:

# summation. However, this method may brake if the coordinates are assign
# to the integration_dim
# integral = integrand.sum(integration_dim)
integral = integrand.integrate(integration_dim)
Copy link
Author

Choose a reason for hiding this comment

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

This should be resolved before the merge. See the todo note.

Copy link
Owner

Choose a reason for hiding this comment

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

Ok, a simple solution would be to add an optional parameter integral_method='trapz' in a similar fashion as you do in pleque and add a warning about this into the docstring.

Copy link
Owner

Choose a reason for hiding this comment

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

I suppose that method='sum' would be something like intergand.sum(integration_dim) * dl?

Copy link
Author

Choose a reason for hiding this comment

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

Nope. integrate here only do "summation" of elements of the integrand, but with Trapez rule (see the example below). dl element is already included in j.

If you assign some coordinates (for example l = cumsum(dl)) the integration will brake.

In [3]: a = xr.DataArray(np.linspace(1, 20, 20))                                                                                     

In [4]: a                                                                                                                            
Out[4]: 
<xarray.DataArray (dim_0: 20)>
array([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12., 13.,
       14., 15., 16., 17., 18., 19., 20.])
Dimensions without coordinates: dim_0

In [5]: a.sum()                                                                                                                      
Out[5]: 
<xarray.DataArray ()>
array(210.)

In [8]: a.integrate("dim_0")                                                                                                         
Out[8]: 
<xarray.DataArray ()>
array(199.5)

Copy link
Author

Choose a reason for hiding this comment

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

A possible solution would be something like this (untested; adjusted your function):

def biot_savart_integral(r, r_c, integration_dim, spatial_dim, I=1):
    # TODO make sure won't divide by coord - integration should cancel, but how accurately at edges?
    dl = r_c.differentiate(integration_dim)
    j = I*dl
    dl = (dl ** 2).sum(spatial_dim) ** 0.5 # normalize to be length element, not vector
    j = j / dl # remove length element

    integrand = bsintegrand(r, r_c, j, spatial_dim)
    integrand[integration_dim] = np.cumsum(dl)

    integral = integrand.integrate(integration_dim)
    return integral

or

def biot_savart_integral(r, r_c, integration_dim, spatial_dim, I=1):
    # TODO make sure won't divide by coord - integration should cancel, but how accurately at edges?
    dl = r_c.differentiate(integration_dim)
    j = I*dl
    integrand = bsintegrand(r, r_c, j, spatial_dim)
    
    integrand[integration_dim] = np.cumsum(dl)
    integral = integrand.integrate(integration_dim) / dl.sum()
    return integral

Copy link
Owner

Choose a reason for hiding this comment

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

Did you mean integrand.coords[integration_dim] = np.cumsum(dl) ?

I suppose the simplest solution would be just to use .sum() instead of .integrate(). Perhaps the possible inaccuracy of sum is not worth all these workarounds?

@kripnerl kripnerl marked this pull request as draft May 14, 2020 16:15
@kripnerl kripnerl changed the title WIP: Feature/line integrator Feature/line integrator May 14, 2020
Copy link
Owner

@smartass101 smartass101 left a comment

Choose a reason for hiding this comment

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

Thank you for the proposed contribution. In general I think that .line_elements seems to be just a slight modification of .curve, so I think it would make more sense to merge it into one module with parametrized functions which could deliver both behaviors.

I'm sorry I didn't explicitly state that I prefer Numpydoc style docstrings, sorry. They are already present in some of the other modules in the ..torodial_sym package, so I would prefer to keep that convention.

biot_savart_integrators/generic/line_elements.py Outdated Show resolved Hide resolved
biot_savart_integrators/generic/line_elements.py Outdated Show resolved Hide resolved
# summation. However, this method may brake if the coordinates are assign
# to the integration_dim
# integral = integrand.sum(integration_dim)
integral = integrand.integrate(integration_dim)
Copy link
Owner

Choose a reason for hiding this comment

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

Ok, a simple solution would be to add an optional parameter integral_method='trapz' in a similar fashion as you do in pleque and add a warning about this into the docstring.

biot_savart_integrators/generic/integrand.py Outdated Show resolved Hide resolved
biot_savart_integrators/generic/line_elements.py Outdated Show resolved Hide resolved
biot_savart_integrators/generic/integrand.py Outdated Show resolved Hide resolved
biot_savart_integrators/generic/integrand.py Outdated Show resolved Hide resolved
@kripnerl
Copy link
Author

Ok I have added space in the notation. I am not an expert on the style. The type is in the declaration of the functions. But I can add typing to the docstring if you want.

@kripnerl
Copy link
Author

There was quite major bug in _prepare_input_ function. It should ve fixed by 2fd95d1.

…cal grid. Both B and A is calculated and then B_calc = rot A_calc is asserts.
@kripnerl
Copy link
Author

kripnerl commented Sep 6, 2020

I've added there two more tests since I experienced some difficulties with signs. Everything is (and was) passing and can serve as and an example for calculation on some grid. I am not sure if this is the best way how to define grid in xarray though.

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.

2 participants