<commit_before>import unittest
import SimPEG.VRM as VRM
import numpy as np
from SimPEG import Mesh

# SUMMARY OF TESTS
#
# Convergence tests with sensitivity refinement (breaks if it finds zero refinement cells)
# Tensor mesh bs octree mesh
# Can you accurately approximate halfspace solution?
# Linear and log normal codes need to match


class VRM_fwd_tests(unittest.TestCase):
    """Can the code match the analytic solution for the case that the
excitation is dipolar
    """

    def test_predict_dipolar(self):

        h = [0.05, 0.05]
        meshObj = Mesh.TensorMesh((h, h, h), x0='CCC')

        dchi = 0.01
        tau1 = 1e-8
        tau2 = 1e0
        mod = (dchi/np.log(tau2/tau1))*np.ones(meshObj.nC)

        times = np.logspace(-4, -2, 3)
        waveObj = VRM.WaveformVRM.SquarePulse(0.02)

        phi = np.random.uniform(-np.pi, np.pi)
        psi = np.random.uniform(-np.pi, np.pi)
        R = 2.
        loc_rx = R*np.c_[np.sin(phi)*np.cos(psi), np.sin(phi)*np.sin(psi), np.cos(phi)]

        rxList = [VRM.Rx.Point_dhdt(loc_rx, times, 'x')]
        rxList.append(VRM.Rx.Point_dhdt(loc_rx, times, 'y'))
        rxList.append(VRM.Rx.Point_dhdt(loc_rx, times, 'z'))

        alpha = np.random.uniform(0, np.pi)
        beta = np.random.uniform(-np.pi, np.pi)
        loc_tx = [0., 0., 0.]
        Src = VRM.Src.CircLoop(rxList, loc_tx, 25., np.r_[alpha, beta], 1., waveObj)
        txList = [Src]

        Survey = VRM.Survey(txList)
        Problem = VRM.ProblemVRM.LinearVRM(meshObj, refFact=0)
        Problem.pair(Survey)
        Fields = Problem.fields(mod)

        H0 = Src.getH0(np.c_[0., 0., 0.])
        dmdtx = -H0[0, 0]*0.1**3*(dchi/np.log(tau2/tau1))*(1/times[1] - 1/(times[1]+0.02))
        dmdty = -H0[0, 1]*0.1**3*(dchi/np.log(tau2/tau1))*(1/times[1] - 1/(times[1]+0.02))
        dmdtz = -H0[0, 2]*0.1**3*(dchi/np.log(tau2/tau1))*(1/times[1] - 1/(times[1]+0.02))
        dmdot = np.dot(np.r_[dmdtx, dmdty, dmdtz], loc_rx.T)

        fx = (1/(4*np.pi))*(3*loc_rx[0, 0]*dmdot/R**5 - dmdtx/R**3)
        fy = (1/(4*np.pi))*(3*loc_rx[0, 1]*dmdot/R**5 - dmdty/R**3)
        fz = (1/(4*np.pi))*(3*loc_rx[0, 2]*dmdot/R**5 - dmdtz/R**3)

        self.assertTrue(np.all(np.abs(Fields[1:-1:3] - np.r_[fx, fy, fz]) < 1e-5*np.sqrt((Fields[1:-1:3]**2).sum())))

    def test_sources(self):
        """If all the source types are setup to product dipolar fields, will
they produce the same fields"""

        h = [0.5, 0.5]
        meshObj = Mesh.TensorMesh((h, h, h), x0='CCC')

        dchi = 0.01
        tau1 = 1e-8
        tau2 = 1e0
        mod = (dchi/np.log(tau2/tau1))*np.ones(meshObj.nC)

        times = np.logspace(-4, -2, 3)
        waveObj = VRM.WaveformVRM.SquarePulse(0.02)

        phi = np.random.uniform(-np.pi, np.pi)
        psi = np.random.uniform(-np.pi, np.pi)
        Rrx = 3.
        loc_rx = Rrx*np.c_[np.sin(phi)*np.cos(psi), np.sin(phi)*np.sin(psi), np.cos(phi)]

        rxList = [VRM.Rx.Point_dhdt(loc_rx, times, 'x')]
        rxList.append(VRM.Rx.Point_dhdt(loc_rx, times, 'y'))
        rxList.append(VRM.Rx.Point_dhdt(loc_rx, times, 'z'))

        alpha = np.random.uniform(0, np.pi)
        beta = np.random.uniform(-np.pi, np.pi)
        Rtx = 4.
        loc_tx = Rtx*np.r_[np.sin(alpha)*np.cos(beta), np.sin(alpha)*np.sin(beta), np.cos(alpha)]

        txList = [VRM.Src.MagDipole(rxList, loc_tx, [0., 0., 0.01], waveObj)]
        txList.append(VRM.Src.CircLoop(rxList, loc_tx, np.sqrt(0.01/np.pi), np.r_[0., 0.], 1., waveObj))
        px = loc_tx[0]+np.r_[-0.05, 0.05, 0.05, -0.05, -0.05]
        py = loc_tx[1]+np.r_[-0.05, -0.05, 0.05, 0.05, -0.05]
        pz = loc_tx[2]*np.ones(5)
        txList.append(VRM.Src.LineCurrent(rxList, np.c_[px, py, pz], 1., waveObj))

        Survey = VRM.Survey(txList)
        Problem = VRM.ProblemVRM.LinearVRM(meshObj, refFact=0)
        Problem.pair(Survey)
        Fields = Problem.fields(mod)

        err1 = np.all(np.abs((Fields[9:18]-Fields[0:9])/(Fields[0:9]+1e-12)) < 0.001)
        err2 = np.all(np.abs((Fields[18:]-Fields[0:9])/(Fields[0:9]+1e-12)) < 0.001)
        err3 = np.all(np.abs((Fields[9:18]-Fields[18:])/(Fields[18:]+1e-12)) < 0.001)

        self.assertTrue(err1 and err2 and err3)


<commit_msg>Update vrm tests<commit_after>import unittest
import SimPEG.VRM as VRM
import numpy as np
from SimPEG import Mesh

# SUMMARY OF TESTS
#
# Tensor mesh bs octree mesh
# Linear and log normal codes need to match


class VRM_fwd_tests(unittest.TestCase):
    """Can the code match the analytic solution for the case that the
excitation is dipolar
    """

    def test_predict_dipolar(self):

        h = [0.05, 0.05]
        meshObj = Mesh.TensorMesh((h, h, h), x0='CCC')

        dchi = 0.01
        tau1 = 1e-8
        tau2 = 1e0
        mod = (dchi/np.log(tau2/tau1))*np.ones(meshObj.nC)

        times = np.logspace(-4, -2, 3)
        waveObj = VRM.WaveformVRM.SquarePulse(0.02)

        phi = np.random.uniform(-np.pi, np.pi)
        psi = np.random.uniform(-np.pi, np.pi)
        R = 2.
        loc_rx = R*np.c_[np.sin(phi)*np.cos(psi), np.sin(phi)*np.sin(psi), np.cos(phi)]

        rxList = [VRM.Rx.Point_dhdt(loc_rx, times, 'x')]
        rxList.append(VRM.Rx.Point_dhdt(loc_rx, times, 'y'))
        rxList.append(VRM.Rx.Point_dhdt(loc_rx, times, 'z'))

        alpha = np.random.uniform(0, np.pi)
        beta = np.random.uniform(-np.pi, np.pi)
        loc_tx = [0., 0., 0.]
        Src = VRM.Src.CircLoop(rxList, loc_tx, 25., np.r_[alpha, beta], 1., waveObj)
        txList = [Src]

        Survey = VRM.Survey(txList)
        Problem = VRM.ProblemVRM.LinearVRM(meshObj, refFact=0)
        Problem.pair(Survey)
        Fields = Problem.fields(mod)

        H0 = Src.getH0(np.c_[0., 0., 0.])
        dmdtx = -H0[0, 0]*0.1**3*(dchi/np.log(tau2/tau1))*(1/times[1] - 1/(times[1]+0.02))
        dmdty = -H0[0, 1]*0.1**3*(dchi/np.log(tau2/tau1))*(1/times[1] - 1/(times[1]+0.02))
        dmdtz = -H0[0, 2]*0.1**3*(dchi/np.log(tau2/tau1))*(1/times[1] - 1/(times[1]+0.02))
        dmdot = np.dot(np.r_[dmdtx, dmdty, dmdtz], loc_rx.T)

        fx = (1/(4*np.pi))*(3*loc_rx[0, 0]*dmdot/R**5 - dmdtx/R**3)
        fy = (1/(4*np.pi))*(3*loc_rx[0, 1]*dmdot/R**5 - dmdty/R**3)
        fz = (1/(4*np.pi))*(3*loc_rx[0, 2]*dmdot/R**5 - dmdtz/R**3)

        self.assertTrue(np.all(np.abs(Fields[1:-1:3] - np.r_[fx, fy, fz]) < 1e-5*np.sqrt((Fields[1:-1:3]**2).sum())))

    def test_sources(self):
        """If all the source types are setup to product dipolar fields, will
they produce the same fields"""

        h = [0.5, 0.5]
        meshObj = Mesh.TensorMesh((h, h, h), x0='CCC')

        dchi = 0.01
        tau1 = 1e-8
        tau2 = 1e0
        mod = (dchi/np.log(tau2/tau1))*np.ones(meshObj.nC)

        times = np.logspace(-4, -2, 3)
        waveObj = VRM.WaveformVRM.SquarePulse(0.02)

        phi = np.random.uniform(-np.pi, np.pi)
        psi = np.random.uniform(-np.pi, np.pi)
        Rrx = 3.
        loc_rx = Rrx*np.c_[np.sin(phi)*np.cos(psi), np.sin(phi)*np.sin(psi), np.cos(phi)]

        rxList = [VRM.Rx.Point_dhdt(loc_rx, times, 'x')]
        rxList.append(VRM.Rx.Point_dhdt(loc_rx, times, 'y'))
        rxList.append(VRM.Rx.Point_dhdt(loc_rx, times, 'z'))

        alpha = np.random.uniform(0, np.pi)
        beta = np.random.uniform(-np.pi, np.pi)
        Rtx = 4.
        loc_tx = Rtx*np.r_[np.sin(alpha)*np.cos(beta), np.sin(alpha)*np.sin(beta), np.cos(alpha)]

        txList = [VRM.Src.MagDipole(rxList, loc_tx, [0., 0., 0.01], waveObj)]
        txList.append(VRM.Src.CircLoop(rxList, loc_tx, np.sqrt(0.01/np.pi), np.r_[0., 0.], 1., waveObj))
        px = loc_tx[0]+np.r_[-0.05, 0.05, 0.05, -0.05, -0.05]
        py = loc_tx[1]+np.r_[-0.05, -0.05, 0.05, 0.05, -0.05]
        pz = loc_tx[2]*np.ones(5)
        txList.append(VRM.Src.LineCurrent(rxList, np.c_[px, py, pz], 1., waveObj))

        Survey = VRM.Survey(txList)
        Problem = VRM.ProblemVRM.LinearVRM(meshObj, refFact=0)
        Problem.pair(Survey)
        Fields = Problem.fields(mod)

        err1 = np.all(np.abs((Fields[9:18]-Fields[0:9])/(Fields[0:9]+1e-12)) < 0.001)
        err2 = np.all(np.abs((Fields[18:]-Fields[0:9])/(Fields[0:9]+1e-12)) < 0.001)
        err3 = np.all(np.abs((Fields[9:18]-Fields[18:])/(Fields[18:]+1e-12)) < 0.001)

        self.assertTrue(err1 and err2 and err3)

    def test_convergence(self):
        """Test the convergence of the solution to analytic results from
Cowan (2016) and test accuracy
        """

        h = [(2, 20)]
        meshObj = Mesh.TensorMesh((h, h, h), x0='CCN')

        dchi = 0.01
        tau1 = 1e-8
        tau2 = 1e0
        mod = (dchi/np.log(tau2/tau1))*np.ones(meshObj.nC)

        times = np.array([1e-3])
        waveObj = VRM.WaveformVRM.SquarePulse(0.02)

        z = 0.5
        a = 0.1
        loc_rx = np.c_[0., 0., z]
        rxList = [VRM.Rx.Point_dhdt(loc_rx, times, 'z')]
        txList = [VRM.Src.CircLoop(rxList, np.r_[0., 0., z], a, np.r_[0., 0.], 1., waveObj)]

        Survey2 = VRM.Survey(txList)
        Survey3 = VRM.Survey(txList)
        Survey4 = VRM.Survey(txList)
        Survey5 = VRM.Survey(txList)
        Problem2 = VRM.ProblemVRM.LinearVRM(meshObj, refFact=2)
        Problem3 = VRM.ProblemVRM.LinearVRM(meshObj, refFact=3)
        Problem4 = VRM.ProblemVRM.LinearVRM(meshObj, refFact=4)
        Problem5 = VRM.ProblemVRM.LinearVRM(meshObj, refFact=5)
        Problem2.pair(Survey2)
        Problem3.pair(Survey3)
        Problem4.pair(Survey4)
        Problem5.pair(Survey5)
        Fields2 = Problem2.fields(mod)
        Fields3 = Problem3.fields(mod)
        Fields4 = Problem4.fields(mod)
        Fields5 = Problem5.fields(mod)

        F = -(1/np.log(tau2/tau1))*(1/times - 1/(times+0.02))
        Fields_true = (0.5*np.pi*a**2/np.pi)*(dchi/(2+dchi))*((2*z)**2 + a**2)**-1.5*F

        Errs = np.abs((np.r_[Fields2, Fields3, Fields4, Fields5] - Fields_true)/Fields_true)

        Test1 = Errs[-1] < 0.005
        Test2 = np.all(Errs[1:]-Errs[0:-1] < 0.)

        self.assertTrue(Test1 and Test2)



{'fraction_non_alphanumeric': 0.19035228182546038, 'fraction_numerical': 0.056144915932746196, 'mean_word_length': 2.4152426520847574, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '2980331', 'n_tokens_mistral': 4506, 'n_tokens_neox': 4179, 'n_words': 712}