PhIMaL / DeePyMoD

https://phimal.github.io/DeePyMoD/
MIT License
44 stars 11 forks source link

Dictionary entries #78

Closed lsptrsn closed 6 months ago

lsptrsn commented 6 months ago

I have a question regarding the order of the entries in the dictionary for the KS example.

https://github.com/PhIMaL/DeePyMoD/blob/master/examples/PDE_keller_segel.ipynb

The library is defined as follows

library = Library1D(poly_order=1, diff_order=2)

Looking into model/library.py I can see

Thank you in advance!

GJBoth commented 6 months ago

Hi,

It's been a while since I wrote this so I don't remember exactly, but we calculate the library as a crossproduct of polynomial terms [1, u, v, uv] and derivative terms [1, u_z, u_zz, v_z, v_zz]. You should be able to reconstruct the terms from this!

lsptrsn commented 6 months ago

Thank you very much for your answer!

I spend some time debugging this case and now want to clarify for anyone interested.

Polynomials

Derivatives

Mixed Terms

$\theta{mixed}$ is made up of a total of 4 terms, namely $\theta{udu}, \theta{vdv}, \theta{udv}, \theta{vdv}$. Bildschirmfoto von 2024-05-03 08-15-41 However, in your implementation, we are only having two of the terms, which are repeated twice, namely $\theta{udv}$ (for the entries 1+2 and 3+4) and $\theta_{vdv}$ (for the entries 4+5 and 6+7).

I have a rather hard coded workaround and replace lines 164-175 in library.py with:

          # Access the first elements in poly_list and deriv_list
          u = poly_list[0]
          du = deriv_list[0]
          # Access the second elements in poly_list and deriv_list
          v = poly_list[1]
          dv = deriv_list[1]

          # Perform the operations for the first elements u
          theta_udu = torch.matmul(u[:, 1:, None], du[:, None, 1:]).view(
              samples,
              (poly_list[0].shape[1] - 1) * (deriv_list[0].shape[1] - 1),
          )

          # Perform the operations for the second elements v
          theta_vdv = torch.matmul(v[:, 1:, None], dv[:, None, 1:]).view(
              samples,
              (poly_list[1].shape[1] - 1) * (deriv_list[1].shape[1] - 1),
          )

          # Calculate the mixed terms
          theta_udv = torch.matmul(u[:, 1:, None], dv[:, None, 1:]).view(
              samples,
              (poly_list[0].shape[1] - 1) * (deriv_list[1].shape[1] - 1),
          )
          theta_vdu = torch.matmul(v[:, 1:, None], du[:, None, 1:]).view(
              samples,
              (poly_list[1].shape[1] - 1) * (deriv_list[0].shape[1] - 1),
          )

          # Concatenate all the results
          theta_mixed = torch.cat([theta_udu, theta_vdv, theta_udv, theta_vdu], 1)
          theta = torch.cat([theta_uv, theta_dudv, theta_mixed], dim=1)

So, for poly=1 and deriv=2, we get the following lists (we always start with the second element): poly_u: [u] poly_v: [v] poly_du: [u_x, u_xx] poly_dv: [v_x, v_xx]

$\theta_{udu}$: [u*u_x, u*uxx] $\theta{vdv}$: [v*v_x, v*vxx] $\theta{udv}$: [u*v_x, u*vxx] $\theta{vdv}$: [v*u_x, v*u_xx]