Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Peter230655
Copy link
Contributor

@Peter230655 Peter230655 commented Oct 10, 2025

  • use generate_ode_function instead of lambdify
  • use PyDY Visualization to create the 3D animation
  • make it more conformant to PEP8

I could not find a simple way to check the conformity using flake8. The original ipynb files were checked so I hope most is o.k.

@Peter230655 Peter230655 changed the title 3d-n-body-pendulum made a bit nicer 3d-n-body-pendulum made a bit nicer. Shoulf fix #534 Oct 10, 2025
@Peter230655 Peter230655 changed the title 3d-n-body-pendulum made a bit nicer. Shoulf fix #534 3d-n-body-pendulum made a bit nicer. Should fix #534 Oct 10, 2025
@moorepants moorepants closed this Oct 10, 2025
@moorepants moorepants reopened this Oct 10, 2025

t_span = (0., intervall)

def gradient(t, y, args):
Copy link
Member

Choose a reason for hiding this comment

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

This gradient function should not be needed. generate_ode_function creates the gradient function.

Copy link
Contributor Author

@Peter230655 Peter230655 Oct 10, 2025

Choose a reason for hiding this comment

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

This may not work:

  • the signature of solve_ivp is not the same as generate_ode_function (I believe generate_ode_function has the signature of odeit.)
  • If in solve_ivp a method other than RK45 is used, the y is not C contiguous, so y = np.ascontiguousarray(y) is needed.

Copy link
Member

Choose a reason for hiding this comment

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

Doing solve_ivp(lambda t, x: rhs(x, t) should be sufficient. If there is some issue with RK4 and y not being contiguous, this sounds like a new bug. That has never been required in the past.

Copy link
Contributor Author

@Peter230655 Peter230655 Oct 10, 2025

Choose a reason for hiding this comment

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

I must admit, I am not goot with these lambda functions.
Yes, there is an issue if e.g. method='Radau' in solve_ivp: I raised issue # 23657 with scipy, so
``y = np.ascontiguousarray(y) is needed, best I can tell.

This is also needed, where I calculate the reaction forces.

@Peter230655
Copy link
Contributor Author

I tried to move more comments into text between code blocks.
Just an 'intermediate' push to see if the direction is o.k.

@Peter230655
Copy link
Contributor Author

Moved more explanationd from code to text.
Total running time on my PC is 12.5 sec.

Objectives
----------

- Show how to use *generate_ode_function* as an alternative to *lambdify*
Copy link
Member

Choose a reason for hiding this comment

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

There are several instances of variables that need to be in double backticks throughout the page. Fixed width font in ReST is via double backticks.


resultat1 = solve_ivp(gradient, t_span, y0, t_eval = times, args=(pL_vals,), method='Radau')
resultat1 = solve_ivp(gradient, t_span, y0, t_eval=times, args=(pL_vals,),
method='RK45',
Copy link
Member

Choose a reason for hiding this comment

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

RK45 is likely not appropriate choice for a model with collision (i.e. it is a stiff system).

If you use odeint() then you don't need the gradient function and it uses LSODA as the integrator by default which switches to a stiff integrator when needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

scipy discourages the use of odeint for new code: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html
I could try 'Radau', which is for stiff systems.

Copy link
Member

Choose a reason for hiding this comment

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

PyDy uses odeint by default. So, until PyDy changes that I don't think we need to abide by SciPy's statement.

Copy link
Member

Choose a reason for hiding this comment

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

You can run LSODA with odeint or solve_ivp, btw.

Copy link
Contributor Author

@Peter230655 Peter230655 Oct 13, 2025

Choose a reason for hiding this comment

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

That's what I assumed, given the signature of generate_ode_function.
Of course I can switch to odeint, just thought that given scipy's recommendation it may be good to show how to use it with generate_ode_function. Mostly this np.ascontiguousarray(..) was not trivial for me.

Radau works fine.

Copy link
Member

Choose a reason for hiding this comment

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

However, if rhs_gen was generated with 'cython' 'and is used to, say, calculate the accelerations needed for the reaction forces still np.ascontiguousarray(..) is needed.

You'll need a minimal example to show this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The example is pushed shows this:

.. jupyter-execute::

    RHS = np.empty((resultat.shape))
    #pl_vals = np.ascontiguousarray(pL_vals)
    for i in range(resultat.shape[0]):
        #res = np.ascontiguousarray(resultat[i])
        res = resultat[i]
        RHS[i] = rhs_gen(res, 0.0, np.array(pL_vals))

If the np.ascontiguousarray(..) are blanked out like above, it will raise an error.

Copy link
Contributor Author

@Peter230655 Peter230655 Oct 14, 2025

Choose a reason for hiding this comment

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

However, if rhs_gen was generated with 'cython' 'and is used to, say, calculate the accelerations needed for the reaction forces still np.ascontiguousarray(..) is needed.

You'll need a minimal example to show this.

I made a minimum simulation and could NOT repeat this problem!

  • I had failed to convert my constants from list into an np.array
  • Strangely, when I use resultat[i] as given from solve_ivp, it does not work with generator='cython'. (it works with generator='symjit'). If I put resultat[i] = np.array(resultat[i]) it works with generator='cython'

Le me know if I should send you this minimum example.

I played around some more and found that somehow applying np.array(x) to an np.array x seems to 'heal' to non C contiguous issue.

Copy link
Member

Choose a reason for hiding this comment

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

Interesting!

You can always report a new issue. The best is to include a minimal reproducing example that I can copy/paste from the issue.

Copy link
Contributor Author

@Peter230655 Peter230655 Oct 14, 2025

Choose a reason for hiding this comment

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

Interesting!

You can always report a new issue. The best is to include a minimal reproducing example that I can copy/paste from the issue.

I raised issue #547 with code showing it.

kd.append(me.dot(rot[i] - rot1[i], uv))

# Kanes's equations
Kanes's Equations
Copy link
Member

Choose a reason for hiding this comment

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

Many of these non-sentences are likely meant to be section headers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will change accordingly.

@Peter230655
Copy link
Contributor Author

Peter230655 commented Oct 13, 2025

  • Use symjit as generator to avoid np.ascontiguousarray(...). Actually even a bit faster than cython.
  • Use keyword time_first. In gradient I switched the signature, so it works. Where I calculate the reaction forces I also switched the signature. so again this shows it works.

Thanks!

@Peter230655
Copy link
Contributor Author

Peter230655 commented Oct 14, 2025

  • added time_first keyword with generate_ode_function
  • removed gradient function and integrated rhs_gen directly into solve_ivp
  • removed np.ascontiguousarray(..) as not needed with this simulation

Looks to me, that the unsuccessful check may be due to the documentation with (3.9), may not allow 'symjit'

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