From bbef6ce542e6d9ca6b5d9108ba957bfee0a3b142 Mon Sep 17 00:00:00 2001 From: domfournier Date: Tue, 3 Feb 2026 13:42:10 -0800 Subject: [PATCH 1/2] Switch deriv comp to einsums --- .../natural_source/receivers.py | 43 ++++++++++++------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/simpeg/electromagnetics/natural_source/receivers.py b/simpeg/electromagnetics/natural_source/receivers.py index 8748bbf5bf..a6c297fcb0 100644 --- a/simpeg/electromagnetics/natural_source/receivers.py +++ b/simpeg/electromagnetics/natural_source/receivers.py @@ -8,7 +8,6 @@ from scipy.constants import mu_0 from scipy.sparse import csr_matrix from ...survey import BaseRx -from simpeg.utils import mkvc def _alpha(src): @@ -1145,20 +1144,34 @@ def _eval_apparent_conductivity_deriv( # ADJOINT if adjoint: # Compute: J_T * v = d_top_T * a_v + d_bot_T * b - a_v = fact * v / bot # term 1 - b_v = -fact * top * v / bot**2 # term 2 - - a_v = np.repeat(mkvc(a_v, n_dims=2), 2, axis=-1) - b_v = np.repeat(mkvc(b_v, n_dims=2), 2, axis=-1) - - hx *= a_v - hy *= a_v - hz *= a_v - ex *= b_v - ey *= b_v - - e_v = 2 * (Pex.T @ ex + Pey.T @ ey).conjugate() - h_v = 2 * (Phx.T @ hx + Phy.T @ hy + Phz.T @ hz).conjugate() + a_v = fact * np.c_[v] / bot[:, None] # term 1 + b_v = np.c_[v] * (-fact * top / (bot**2))[:, None] # term 2 + + # a_v = np.repeat(mkvc(a_v, n_dims=2), 2, axis=-1) + # b_v = np.repeat(mkvc(b_v, n_dims=2), 2, axis=-1) + + # hx *= a_v + # hy *= a_v + # hz *= a_v + # ex *= b_v + # ey *= b_v + + ghx_v = np.einsum("ij,ik->ijk", a_v, hx).reshape((hx.shape[0], -1)) + ghy_v = np.einsum("ij,ik->ijk", a_v, hy).reshape((hy.shape[0], -1)) + ghz_v = np.einsum("ij,ik->ijk", a_v, hz).reshape((hz.shape[0], -1)) + gex_v = np.einsum("ij,ik->ijk", b_v, ex).reshape((ex.shape[0], -1)) + gey_v = np.einsum("ij,ik->ijk", b_v, ey).reshape((ey.shape[0], -1)) + e_v = ( + 2 * (Pex.T @ csr_matrix(gex_v) + Pey.T @ csr_matrix(gey_v)).conjugate() + ) + h_v = ( + 2 + * ( + Phx.T @ csr_matrix(ghx_v) + + Phy.T @ csr_matrix(ghy_v) + + Phz.T @ csr_matrix(ghz_v) + ).conjugate() + ) fu_e_v, fm_e_v = f._eDeriv(src, None, e_v, adjoint=True) fu_h_v, fm_h_v = f._hDeriv(src, None, h_v, adjoint=True) From b919ca8ffa77f8cd7f9d5deaf39e14963476b0fa Mon Sep 17 00:00:00 2001 From: domfournier Date: Tue, 3 Feb 2026 13:44:00 -0800 Subject: [PATCH 2/2] Remove commented code --- simpeg/electromagnetics/natural_source/receivers.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/simpeg/electromagnetics/natural_source/receivers.py b/simpeg/electromagnetics/natural_source/receivers.py index a6c297fcb0..f93e98d5d9 100644 --- a/simpeg/electromagnetics/natural_source/receivers.py +++ b/simpeg/electromagnetics/natural_source/receivers.py @@ -1147,15 +1147,6 @@ def _eval_apparent_conductivity_deriv( a_v = fact * np.c_[v] / bot[:, None] # term 1 b_v = np.c_[v] * (-fact * top / (bot**2))[:, None] # term 2 - # a_v = np.repeat(mkvc(a_v, n_dims=2), 2, axis=-1) - # b_v = np.repeat(mkvc(b_v, n_dims=2), 2, axis=-1) - - # hx *= a_v - # hy *= a_v - # hz *= a_v - # ex *= b_v - # ey *= b_v - ghx_v = np.einsum("ij,ik->ijk", a_v, hx).reshape((hx.shape[0], -1)) ghy_v = np.einsum("ij,ik->ijk", a_v, hy).reshape((hy.shape[0], -1)) ghz_v = np.einsum("ij,ik->ijk", a_v, hz).reshape((hz.shape[0], -1))