Skip to content

Bugs in getJ for the SIP simulation #1654

Open

Description

@santisoler

This issue is a follow up of #1361. In that PR we noticed that the SIP simulation doesn't behave very well when calling the getJ method without passing actinds as argument. An error is raised due to undefined simulation._P attribute, that only gets defined when actinds gets assigned.

I owe a reproducible example for this, I'll add it later to this issue.

Activity

  1. added this to the 0.25.0 milestone on Apr 24, 2025
  2. santisoler commented on May 19, 2025

    @santisoler
    MemberAuthor

    Here it is a reproducible example of the bug found in #1361. When trying to run the getJ method with storeJ set to False, we obtain the following error:

    import numpy as np
    import discretize
    import simpeg
    from simpeg.electromagnetics import spectral_induced_polarization as sip
    from simpeg import utils
    
    # Mesh
    cs = 25.0
    hx = [(cs, 0, -1.3), (cs, 21), (cs, 0, 1.3)]
    hy = [(cs, 0, -1.3), (cs, 21), (cs, 0, 1.3)]
    hz = [(cs, 0, -1.3), (cs, 20)]
    mesh = discretize.TensorMesh([hx, hy, hz], x0="CCN")
    
    # Survey
    x = mesh.cell_centers_x[(mesh.cell_centers_x > -155.0) & (mesh.cell_centers_x < 155.0)]
    y = mesh.cell_centers_y[(mesh.cell_centers_y > -155.0) & (mesh.cell_centers_y < 155.0)]
    Aloc = np.r_[-200.0, 0.0, 0.0]
    Bloc = np.r_[200.0, 0.0, 0.0]
    M = utils.ndgrid(x - 25.0, y, np.r_[0.0])
    times = np.arange(10) * 1e-3 + 1e-3
    rx = sip.receivers.Pole(M, times)
    src = sip.sources.Dipole([rx], Aloc, Bloc)
    survey = sip.Survey([src])
    
    # Simulation
    wires = simpeg.maps.Wires(("eta", mesh.nC), ("taui", mesh.nC))
    sigma = np.ones(mesh.nC) * 1e-2
    simulation = sip.Simulation3DNodal(
        mesh,
        sigma=sigma,
        survey=survey,
        etaMap=wires.eta,
        tauiMap=wires.taui,
    )
    model = np.r_[sigma, 1.0 / sigma]
    simulation.getJ(model)
    AttributeError                            Traceback (most recent call last)
    File ~/git/simpeg/sip_getj.py:40
         37 model_1 = np.r_[sigma, 1.0 / sigma]
         38 model_2 = np.r_[sigma * 2, 1.0 / sigma]
    ---> 40 simulation.getJ(model_1)
         42 # Simulation (with actinds)
         43 wires = simpeg.maps.Wires(("eta", mesh.nC), ("taui", mesh.nC))
    
    File ~/git/simpeg/simpeg/electromagnetics/static/spectral_induced_polarization/simulation.py:373, in BaseSIPSimulation.getJ(self, m, f)
        369 if f is None:
        370     f = self.fields(m)
        372 Jt = np.zeros(
    --> 373     (self._P.shape[1], int(self.survey.nD / self.survey.unique_times.size)),
        374     order="F",
        375 )
        376 istrt = int(0)
        377 iend = int(0)
    
    AttributeError: 'Simulation3DNodal' object has no attribute '_P'
    

    The _P attribute gets defined in L96, as part of the actinds setter and only if storeJ is True.

  3. santisoler commented on May 19, 2025

    @santisoler
    MemberAuthor

    If we set storeJ to True and we pass actinds, then we don't get the previous error, but we get the following one instead:

    import numpy as np
    import discretize
    import simpeg
    from simpeg.electromagnetics import spectral_induced_polarization as sip
    from simpeg import utils
    
    # Mesh
    cs = 25.0
    hx = [(cs, 0, -1.3), (cs, 21), (cs, 0, 1.3)]
    hy = [(cs, 0, -1.3), (cs, 21), (cs, 0, 1.3)]
    hz = [(cs, 0, -1.3), (cs, 20)]
    mesh = discretize.TensorMesh([hx, hy, hz], x0="CCN")
    
    # Survey
    x = mesh.cell_centers_x[(mesh.cell_centers_x > -155.0) & (mesh.cell_centers_x < 155.0)]
    y = mesh.cell_centers_y[(mesh.cell_centers_y > -155.0) & (mesh.cell_centers_y < 155.0)]
    Aloc = np.r_[-200.0, 0.0, 0.0]
    Bloc = np.r_[200.0, 0.0, 0.0]
    M = utils.ndgrid(x - 25.0, y, np.r_[0.0])
    times = np.arange(10) * 1e-3 + 1e-3
    rx = sip.receivers.Pole(M, times)
    src = sip.sources.Dipole([rx], Aloc, Bloc)
    survey = sip.Survey([src])
    
    # Simulation
    wires = simpeg.maps.Wires(("eta", mesh.nC), ("taui", mesh.nC))
    sigma = np.ones(mesh.nC) * 1e-2
    simulation = sip.Simulation3DNodal(
        mesh,
        sigma=sigma,
        survey=survey,
        etaMap=wires.eta,
        tauiMap=wires.taui,
        storeJ=True,
        actinds=np.ones(mesh.n_cells, dtype=bool),
    )
    model = np.r_[sigma, 1.0 / sigma]
    simulation.getJ(model)
    AttributeError                            Traceback (most recent call last)
    File ~/git/simpeg/bug2.py:38
         28 simulation = sip.Simulation3DNodal(
         29     mesh,
         30     sigma=sigma,
       (...)
         35     actinds=np.ones(mesh.n_cells, dtype=bool),
         36 )
         37 model = np.r_[sigma, 1.0 / sigma]
    ---> 38 simulation.getJ(model)
    
    File ~/git/simpeg/simpeg/electromagnetics/static/spectral_induced_polarization/simulation.py:370, in BaseSIPSimulation.getJ(self, m, f)
        368     print("Calculating J and storing")
        369 if f is None:
    --> 370     f = self.fields(m)
        372 Jt = np.zeros(
        373     (self._P.shape[1], int(self.survey.nD / self.survey.unique_times.size)),
        374     order="F",
        375 )
        376 istrt = int(0)
    
    File ~/git/simpeg/simpeg/electromagnetics/static/spectral_induced_polarization/simulation.py:353, in BaseSIPSimulation.fields(self, m)
        350                     rx._Ps[self.mesh] = sdiag(1.0 / rx.dc_voltage) * P
        351                 rx.data_type = "apparent_chargeability"
    --> 353 self._pred = self.forward(m, f=self._f)
        355 return self._f
    
    File ~/git/simpeg/simpeg/electromagnetics/static/spectral_induced_polarization/simulation.py:442, in BaseSIPSimulation.forward(self, m, f)
        440 # When sensitivity matrix is stored
        441 if self.storeJ:
    --> 442     J = self.getJ(m, f=f)
        444     ntime = len(self.survey.unique_times)
        446     self.model = m
    
    File ~/git/simpeg/simpeg/electromagnetics/static/spectral_induced_polarization/simulation.py:385, in BaseSIPSimulation.getJ(self, m, f)
        383 u_src = f[src, self._solutionType]
        384 for rx in src.receiver_list:
    --> 385     P = rx.getP(self.mesh, rx.projGLoc(f)).toarray()
        386     ATinvdf_duT = self.Ainv * (P.T)
        387     dA_dmT = self.getADeriv(u_src, ATinvdf_duT, adjoint=True)
    
    AttributeError: 'Pole' object has no attribute 'projGLoc'
    

    The projGLoc method was removed in 5a8b1ba.

  4. santisoler commented on May 19, 2025

    @santisoler
    MemberAuthor

    For reference, this is my output of simpeg.Report():

    Details
    --------------------------------------------------------------------------------
      Date: Mon May 19 11:07:16 2025 PDT
    
                    OS : Linux (Debian GNU/Linux 12)
                CPU(s) : 8
               Machine : x86_64
          Architecture : 64bit
                   RAM : 15.5 GiB
           Environment : IPython
           File system : ext4
    
      Python 3.11.10 | packaged by conda-forge | (main, Oct 16 2024, 01:27:36)
      [GCC 13.3.0]
    
                simpeg : 0.24.0
            discretize : 0.11.2
           pymatsolver : 0.3.1
                 numpy : 2.0.2
                 scipy : 1.15.2
            matplotlib : 3.10.1
                geoana : 0.7.2
                libdlf : 0.3.0
                pydiso : 0.1.2
                 numba : 0.60.0
                  dask : 2024.10.0
               sklearn : 1.6.1
                pandas : 2.2.3
                 sympy : 1.13.3
               IPython : 8.28.0
            ipywidgets : 8.1.5
                plotly : 6.0.1
                   vtk : 9.3.1
       memory_profiler : 0.61.0
                choclo : 0.3.2
    --------------------------------------------------------------------------------
    
  5. changed the title [-]Improvements to `getJ` for the SIP simulation[/-] [+]Bugs in `getJ` for the SIP simulation[/+] on May 19, 2025
  6. modified the milestones: 0.25.0, 0.26.0 on Oct 9, 2025
  7. modified the milestones: 0.26.0, 0.27.0 on Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions