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

Take only scalar inputs instead of array inputs #50

Merged
merged 4 commits into from
May 5, 2023
Merged

Conversation

leouieda
Copy link
Member

@leouieda leouieda commented May 3, 2023

Choclo functions shouldn't take array inputs because they may require a user to convert their prism, dipole moment, or magnetization format into what Choclo expects with the possible result of reallocating memory or having to iterate over slices in non-optimal ways. Since these functions are meant to be wrapped by other JIT-compiled functions that the user creates, they should be as flexible as possible with their inputs. This means converting the dipole_moment, prism, and magnetization arguments into individual components instead of arrays. The code is now a bit longer because of this but there is no other down side. Had to update some linting rules to account for the longer code and larger number of arguments that this entails.

Relevant issues/PRs:

Fixes #49

For dipole magnetic fields, request the 3 components of the vector
instead of an array.
For the prism code, take both the geometry and the magnetization vector
as individual components. Had to update some flake8 config since some
functions now take more arguments and are a bit larger.
@leouieda leouieda added this to the v0.1.0 milestone May 3, 2023
@leouieda
Copy link
Member Author

leouieda commented May 3, 2023

Did all the code conversion and now just need to finish the docstrings.

@leouieda leouieda marked this pull request as ready for review May 3, 2023 09:30
@leouieda leouieda requested a review from santisoler May 3, 2023 09:30
@santisoler
Copy link
Member

Awesome work @leouieda! I'll check it out later in depth. Now I'm thinking: since we are operating always with floats, does it makes sense to depend on Numpy? Or in another words, using math has any performance gain vs using Numpy in Numba functions? Probably not, but I was just wondering...

@leouieda
Copy link
Member Author

leouieda commented May 3, 2023

Maybe not a performance gain but I wonder if the behaviour of arctan is the same. Also, numba will depend on numpy so maybe it's best if we set explicit versions for it too.

@santisoler
Copy link
Member

Maybe not a performance gain but I wonder if the behaviour of arctan is the same. Also, numba will depend on numpy so maybe it's best if we set explicit versions for it too.

I made a small experiment running some benchmarks, and the performance gain of math is negligible. So, considering that and the arctan factor, maybe it's ok to keep with numpy functions.

Copy link
Member

@santisoler santisoler left a comment

Choose a reason for hiding this comment

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

I think this is looking good. Thanks for the effort @leouieda!

Probably my main concern is the style. Black is creating this long function calls by putting one argument per line. Usually I agree with Black style, but for these cases, I think it creates too long functions and waste too much horizontal space.

Maybe we could ignore on some of these functions and format them as:

@jit(nopython=True)
def gravity_uu(
    easting,
    northing,
    upward,
    prism_west,
    prism_east,
    prism_south,
    prism_north,
    prism_bottom,
    prism_top,
    density,
):
    # Return nan if the observation point falls on a singular point.
    # For the g_uu this are edges perpendicular to the upward direction
    # (parallel to easting and northing)
    if (
        is_point_on_easting_edge(
            easting, northing, upward,
            prism_west, prism_east, prism_south, prism_north, prism_bottom, prism_top
        )
        or is_point_on_northing_edge(
            easting, northing, upward,
            prism_west, prism_east, prism_south, prism_north, prism_bottom, prism_top
        )
    ):  # fmt: skip
        return np.nan
    # Evaluate kernel function on each vertex of the prism
    result = _evaluate_kernel(
        easting, northing, upward,
        prism_west, prism_east, prism_south, prism_north, prism_bottom, prism_top,
        kernel_uu,
    )  # fmt: skip
    # Add 4 pi if computing on the top face to return the limit approaching
    # from outside (approaching from the top)
    if is_point_on_top_face(
        easting, northing, upward,
        prism_west, prism_east, prism_south, prism_north, prism_bottom, prism_top,
    ):  # fmt: skip
        result += 4 * np.pi
    return GRAVITATIONAL_CONST * density * result

Let me know what do you think. Regardless of our opinion on the style, we could totally merge this as it is and then change the style.

choclo/prism/_gravity.py Outdated Show resolved Hide resolved
choclo/prism/_gravity.py Outdated Show resolved Hide resolved
@leouieda
Copy link
Member Author

leouieda commented May 5, 2023

Hey @santisoler thanks for the review! I did a search for those skip comments but missed those.

Probably my main concern is the style. Black is creating this long function calls by putting one argument per line. Usually I agree with Black style, but for these cases, I think it creates too long functions and waste too much horizontal space.

Personally I'd rather never manually format even if it looks a bit worse. It's still readable the way black does it and it avoids creating the precedence of introducing these format skip lines. But I'll go with whatever you think is best since I don't feel strongly about this either way.

@santisoler
Copy link
Member

Let's merge this as it is. If we have strong feelings about the style that black applies, we can always change it in another PR.

Great job changing all these functions!

@santisoler santisoler merged commit a4630fb into main May 5, 2023
@santisoler santisoler deleted the magnetic_moment branch May 5, 2023 15:38
santisoler added a commit to fatiando/harmonica that referenced this pull request May 5, 2023
Update the magnetic forward modelling functions so they pass the prisms
boundaries and magnetization vectors to the Choclo functions as scalars.
This change was triggered by the modifications introduced in
fatiando/choclo#50
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.

Should any inputs be arrays/lists?
2 participants