-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Conversation
# 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) |
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.
This should be resolved before the merge. See the todo note.
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.
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.
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.
I suppose that method='sum'
would be something like intergand.sum(integration_dim) * dl
?
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.
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
.
j = I*dl |
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)
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.
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
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.
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?
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.
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.
# 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) |
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.
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.
add: testing of various inputs of biot_savart_integral_B function
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. |
There was quite major bug in |
…cal grid. Both B and A is calculated and then B_calc = rot A_calc is asserts.
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. |
…vart_integrators into feature/line_integrator
WIP: use integrate method vs sum: