<commit_before>import numpy as np
import scipy as sp
import properties
from discretize import TensorMesh
from ....utils.code_utils import deprecate_class

from .... import props
from ....data import Data
from ....utils import mkvc, sdiag
from ...base import BaseEMSimulation
from ..utils import gettopoCC

from ..resistivity.fields import FieldsDC, Fields3DCellCentered, Fields3DNodal
from ..resistivity import Simulation3DCellCentered as BaseSimulation3DCellCentered
from ..resistivity import Simulation3DNodal as BaseSimulation3DNodal
from ..resistivity import (
    Simulation3DCellCenteredFictitiousSources as BaseSimulation3DCellCenteredFictitiousSources,
)
from ..resistivity import (
    Simulation3DNodalFictitiousSources as BaseSimulation3DNodalFictitiousSources,
)
from ..resistivity.survey import Survey
from ..resistivity.sources import Pole, Dipole


class BaseIPSimulation(BaseEMSimulation):

    sigma = props.PhysicalProperty("Electrical conductivity (S/m)")

    rho = props.PhysicalProperty("Electrical resistivity (Ohm m)")

    props.Reciprocal(sigma, rho)

    eta, etaMap, etaDeriv = props.Invertible("Electrical Chargeability")

    data_type = properties.StringChoice(
        "IP data type", default="volt", choices=["volt", "apparent_chargeability"],
    )

    fieldsPair = FieldsDC
    Ainv = None
    _f = None
    storeJ = False
    _Jmatrix = None
    gtgdiag = None
    sign = None
    _pred = None
    gtgdiag = None
    _dc_data_set = False

    def fields(self, m=None):

        if m is not None:
            self.model = m
            # sensitivity matrix is fixed
            # self._Jmatrix = None

        if self._f is None:

            if self.verbose is True:
                print(">> Solve DC problem")

            self._f = self.fieldsPair(self)
            A = self.getA()
            self.Ainv = self.solver(A, **self.solver_opts)
            RHS = self.getRHS()
            Srcs = self.survey.source_list
            self._f[Srcs, self._solutionType] = self.Ainv * RHS

        if not self._dc_data_set:
            # loop through receievers to check if they need to set the _dc_voltage
            for src in self.survey.source_list:
                for rx in src.receiver_list:
                    if (
                        rx.data_type == "apparent_chargeability"
                        and rx._dc_voltage is None
                    ):
                        rx.data_type == "volt"  # make the rx evaluate a voltage
                        rx._dc_voltage = rx.eval(src, self.mesh, self._f)
                        rx.data_type == "apparent_chargeability"
                        rx._Ps = {}
            self._dc_data_set = True  # avoid loop through after first call

        if self.verbose is True:
            print(">> Compute predicted data")

        self._pred = self.forward(m, f=self._f)

        # if not self.storeJ:
        #     self.Ainv.clean()

        return self._f

    def dpred(self, m=None, f=None):
        """
            Predicted data.

            .. math::

                d_\\text{pred} = Pf(m)<commit_msg>change == to =<commit_after>import numpy as np
import scipy as sp
import properties
from discretize import TensorMesh
from ....utils.code_utils import deprecate_class

from .... import props
from ....data import Data
from ....utils import mkvc, sdiag
from ...base import BaseEMSimulation
from ..utils import gettopoCC

from ..resistivity.fields import FieldsDC, Fields3DCellCentered, Fields3DNodal
from ..resistivity import Simulation3DCellCentered as BaseSimulation3DCellCentered
from ..resistivity import Simulation3DNodal as BaseSimulation3DNodal
from ..resistivity import (
    Simulation3DCellCenteredFictitiousSources as BaseSimulation3DCellCenteredFictitiousSources,
)
from ..resistivity import (
    Simulation3DNodalFictitiousSources as BaseSimulation3DNodalFictitiousSources,
)
from ..resistivity.survey import Survey
from ..resistivity.sources import Pole, Dipole


class BaseIPSimulation(BaseEMSimulation):

    sigma = props.PhysicalProperty("Electrical conductivity (S/m)")

    rho = props.PhysicalProperty("Electrical resistivity (Ohm m)")

    props.Reciprocal(sigma, rho)

    eta, etaMap, etaDeriv = props.Invertible("Electrical Chargeability")

    data_type = properties.StringChoice(
        "IP data type", default="volt", choices=["volt", "apparent_chargeability"],
    )

    fieldsPair = FieldsDC
    Ainv = None
    _f = None
    storeJ = False
    _Jmatrix = None
    gtgdiag = None
    sign = None
    _pred = None
    gtgdiag = None
    _dc_data_set = False

    def fields(self, m=None):

        if m is not None:
            self.model = m
            # sensitivity matrix is fixed
            # self._Jmatrix = None

        if self._f is None:

            if self.verbose is True:
                print(">> Solve DC problem")

            self._f = self.fieldsPair(self)
            A = self.getA()
            self.Ainv = self.solver(A, **self.solver_opts)
            RHS = self.getRHS()
            Srcs = self.survey.source_list
            self._f[Srcs, self._solutionType] = self.Ainv * RHS

        if not self._dc_data_set:
            # loop through receievers to check if they need to set the _dc_voltage
            for src in self.survey.source_list:
                for rx in src.receiver_list:
                    if (
                        rx.data_type == "apparent_chargeability"
                        and rx._dc_voltage is None
                    ):
                        rx.data_type = "volt"  # make the rx evaluate a voltage
                        rx._dc_voltage = rx.eval(src, self.mesh, self._f)
                        rx.data_type = "apparent_chargeability"
                        rx._Ps = {}
            self._dc_data_set = True  # avoid loop through after first call

        if self.verbose is True:
            print(">> Compute predicted data")

        self._pred = self.forward(m, f=self._f)

        # if not self.storeJ:
        #     self.Ainv.clean()

        return self._f

    def dpred(self, m=None, f=None):
        """
            Predicted data.

            .. math::

                d_\\text{pred} = Pf(m)
{'fraction_non_alphanumeric': 0.08415441770311743, 'fraction_numerical': 0.0032304958811177516, 'mean_word_length': 2.108433734939759, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '374557', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1744, 'n_words': 491}