Example #1
0
 def test_linerelaxation(self):
     var = solver.MGParameters(cycle='F',
                               sslsolver=False,
                               semicoarsening=False,
                               linerelaxation=True,
                               shape_cells=self.shape_cells,
                               verb=0)
     assert 'linerelaxation : True [4 5 6]' in var.__repr__()
     var = solver.MGParameters(cycle='F',
                               sslsolver=False,
                               semicoarsening=False,
                               linerelaxation=1247,
                               shape_cells=self.shape_cells,
                               verb=0)
     assert 'linerelaxation : True [1 2 4 7]' in var.__repr__()
     var = solver.MGParameters(cycle='F',
                               sslsolver=False,
                               semicoarsening=False,
                               linerelaxation=1,
                               shape_cells=self.shape_cells,
                               verb=0,
                               clevel=1)
     assert 'linerelaxation : True [1]' in var.__repr__()
     assert_allclose(var.clevel, 1)
     with pytest.raises(ValueError, match='`linerelaxation` must be one o'):
         solver.MGParameters(cycle='F',
                             sslsolver=False,
                             semicoarsening=False,
                             linerelaxation=-9,
                             shape_cells=self.shape_cells,
                             verb=0)
Example #2
0
    def test_bad_grid_size(self):
        inp = {
            'cycle': 'F',
            'sslsolver': False,
            'semicoarsening': False,
            'linerelaxation': False,
            'verb': 0
        }
        txt = ":: Grid not optimal for MG solver ::"

        # One large lowest => warning.
        var = solver.MGParameters(shape_cells=(11 * 2**3, 2**5, 2**4), **inp)
        assert txt in var.__repr__()

        # Large lowest, but clevel smaller => no warning.
        var = solver.MGParameters(shape_cells=(11 * 2**5, 11 * 2**4,
                                               11 * 2**5),
                                  clevel=4,
                                  **inp)
        assert txt not in var.__repr__()

        # Large lowest, clevel bigger => warning.
        var = solver.MGParameters(shape_cells=(11 * 2**5, 11 * 2**4,
                                               11 * 2**5),
                                  clevel=5,
                                  **inp)
        assert txt in var.__repr__()

        # Only 2 times dividable => warning.
        var = solver.MGParameters(shape_cells=(2**3, 2**3, 2**3), **inp)
        assert txt in var.__repr__()
Example #3
0
 def test_semicoarsening(self):
     var = solver.MGParameters(cycle='F',
                               sslsolver=False,
                               semicoarsening=True,
                               linerelaxation=False,
                               shape_cells=self.shape_cells,
                               verb=0)
     assert 'semicoarsening : True [1 2 3]' in var.__repr__()
     var = solver.MGParameters(cycle='V',
                               sslsolver=False,
                               semicoarsening=1213,
                               linerelaxation=False,
                               shape_cells=self.shape_cells,
                               verb=0)
     assert 'semicoarsening : True [1 2 1 3]' in var.__repr__()
     var = solver.MGParameters(cycle='F',
                               sslsolver=False,
                               semicoarsening=2,
                               linerelaxation=False,
                               shape_cells=self.shape_cells,
                               verb=0)
     assert 'semicoarsening : True [2]' in var.__repr__()
     with pytest.raises(ValueError, match='`semicoarsening` must be one o'):
         solver.MGParameters(cycle='F',
                             sslsolver=False,
                             semicoarsening=5,
                             linerelaxation=False,
                             shape_cells=self.shape_cells,
                             verb=0)
Example #4
0
    def test_cycle_gcrotmk(self, capsys):

        # Load any case.
        dat = REGRES['res']
        model = emg3d.Model(**dat['input_model'])
        grid = model.grid
        sfield = emg3d.get_source_field(**dat['input_source'])
        vmodel = emg3d.models.VolumeModel(model, sfield)
        efield = emg3d.Field(grid)  # Initiate e-field.

        # Get var-instance
        var = solver.MGParameters(
            cycle='F',
            sslsolver='gcrotmk',
            semicoarsening=False,
            linerelaxation=False,
            shape_cells=grid.shape_cells,
            verb=4,
            maxit=5,
        )
        var.l2_refe = sl.norm(sfield.field, check_finite=False)

        # Call krylov and ensure it fails properly.
        solver.krylov(vmodel, sfield, efield, var)
        out, _ = capsys.readouterr()
        assert 'DIVERGED' in out
Example #5
0
    def test_basic(self, capsys):
        # This should reach every line of solver.multigrid.
        dat = REGRES['res']
        model = emg3d.Model(**dat['input_model'])
        grid = model.grid
        sfield = emg3d.get_source_field(**dat['input_source'])
        vmodel = emg3d.models.VolumeModel(model, sfield)
        efield = emg3d.Field(grid)  # Initiate e-field.

        # Get var-instance
        var = solver.MGParameters(
            cycle='F',
            sslsolver=False,
            semicoarsening=True,
            linerelaxation=True,
            shape_cells=grid.shape_cells,
            verb=5,
            nu_init=2,
            maxit=-1,
        )
        var.l2_refe = sl.norm(sfield.field, check_finite=False)

        # Call multigrid.
        solver.multigrid(vmodel, sfield, efield, var)
        out, _ = capsys.readouterr()
        assert '> CONVERGED' in out
Example #6
0
    def test_bicgstab_error(self, capsys):
        # Load any case.
        dat = REGRES['res']
        model = emg3d.Model(**dat['input_model'])
        grid = model.grid
        model.property_x *= 100000  # Set stupid input to make bicgstab fail.
        model.property_y /= 100000  # Set stupid input to make bicgstab fail.
        sfield = emg3d.get_source_field(**dat['input_source'])
        vmodel = emg3d.models.VolumeModel(model, sfield)
        efield = emg3d.Field(grid)  # Initiate e-field.

        # Get var-instance
        var = solver.MGParameters(
            cycle=None,
            sslsolver=True,
            semicoarsening=False,
            linerelaxation=False,
            shape_cells=grid.shape_cells,
            verb=3,
            maxit=-1,
        )
        var.l2_refe = sl.norm(sfield.field, check_finite=False)

        # Call krylov and ensure it fails properly.
        solver.krylov(vmodel, sfield, efield, var)
        out, _ = capsys.readouterr()
        assert '* ERROR   :: Error in bicgstab' in out
Example #7
0
def test_krylov(capsys):

    # Everything should be tested just fine in `test_solver`.
    # Just check here for bicgstab-error.

    # Load any case.
    dat = REGRES['res'][()]
    grid = utils.TensorMesh(**dat['input_grid'])
    model = utils.Model(**dat['input_model'])
    sfield = utils.get_source_field(**dat['input_source'])
    vmodel = utils.VolumeModel(grid, model, sfield)
    efield = utils.Field(grid)  # Initiate e-field.

    # Get var-instance
    var = solver.MGParameters(
        cycle=None,
        sslsolver=True,
        semicoarsening=False,
        linerelaxation=False,
        vnC=grid.vnC,
        verb=3,
        maxit=-1,  # Set stupid input to make bicgstab fail.
    )
    var.l2_refe = njitted.l2norm(sfield)

    # Call krylov and ensure it fails properly.
    solver.krylov(grid, vmodel, sfield, efield, var)
    out, _ = capsys.readouterr()
    assert '* ERROR   :: Error in bicgstab' in out
Example #8
0
 def test_wrong_grid_size(self):
     with pytest.raises(ValueError, match='Nr. of cells must be at least'):
         solver.MGParameters(cycle='F',
                             sslsolver=False,
                             semicoarsening=False,
                             linerelaxation=False,
                             shape_cells=(1, 2, 3),
                             verb=0)
Example #9
0
def test_print_cycle_info(capsys):
    var = solver.MGParameters(verb=4,
                              cycle='F',
                              sslsolver=False,
                              linerelaxation=False,
                              semicoarsening=False,
                              shape_cells=(16, 8, 2))
    var.level_all = [0, 1, 2, 3, 2, 3]
    solver._print_cycle_info(var, 1.0, 2.0)
    out, _ = capsys.readouterr()

    assert "h_\n      2h_ \\    \n      4h_  \\   \n      8h_   \\/\\\n" in out
    assert "   1.000e+00  after   0 F-cycles   [1.000e+00, 0.500]   0 0" in out

    # Cuts at 70.
    var = solver.MGParameters(verb=5,
                              cycle='F',
                              sslsolver=True,
                              linerelaxation=False,
                              semicoarsening=False,
                              shape_cells=(16, 8, 2))
    var.level_all = list(np.r_[0,
                               np.array(50 * [
                                   [1, 2, 3, 2, 1],
                               ]).ravel()])
    var.it = 123
    solver._print_cycle_info(var, 1.0, 2.0)
    out, _ = capsys.readouterr()
    assert "restricted to first 70 steps" in out
    assert 21 * " " + "123" in out
    assert out[-1] == "\n"

    # One-liner.
    var = solver.MGParameters(verb=3,
                              cycle='F',
                              sslsolver=False,
                              linerelaxation=False,
                              semicoarsening=False,
                              shape_cells=(16, 8, 2))
    var.level_all = [0, 1, 2, 3, 2, 3]
    solver._print_cycle_info(var, 1.0, 2.0)
    out, _ = capsys.readouterr()
    assert ":: emg3d :: 1.0e+00; 0; 0:00:0" in out
Example #10
0
def test_print_gs_info(capsys):
    var = solver.MGParameters(verb=5,
                              cycle='F',
                              sslsolver=False,
                              linerelaxation=False,
                              semicoarsening=False,
                              shape_cells=(16, 8, 2))

    grid = emg3d.TensorMesh([[1, 1], [1, 1], [1, 1]], (0, 0, 0))
    solver._print_gs_info(var, 1, 2, 3, grid, 0.01, 'test')
    out, _ = capsys.readouterr()
    assert out == "      1 2 3 [  2,   2,   2]: 1.000e-02 test\n"
Example #11
0
 def test_sslsolver_and_cycle(self):
     with pytest.raises(ValueError, match='At least `cycle` or `sslsolve'):
         solver.MGParameters(cycle=None,
                             sslsolver=False,
                             semicoarsening=False,
                             linerelaxation=False,
                             shape_cells=self.shape_cells,
                             verb=0)
     var = solver.MGParameters(cycle='F',
                               sslsolver=True,
                               semicoarsening=True,
                               linerelaxation=False,
                               shape_cells=self.shape_cells,
                               verb=0,
                               maxit=33)
     assert "sslsolver : 'bicgstab'" in var.__repr__()
     assert var.ssl_maxit == 33
     assert var.maxit == 3
     with pytest.raises(ValueError, match='`sslsolver` must be True'):
         solver.MGParameters(cycle='F',
                             sslsolver='abcd',
                             semicoarsening=0,
                             linerelaxation=False,
                             shape_cells=self.shape_cells,
                             verb=0)
     with pytest.raises(ValueError, match='`sslsolver` must be True'):
         solver.MGParameters(cycle='F',
                             sslsolver=4,
                             semicoarsening=0,
                             linerelaxation=False,
                             shape_cells=self.shape_cells,
                             verb=0)
     with pytest.raises(ValueError, match='`cycle` must be one of'):
         solver.MGParameters(cycle='G',
                             sslsolver=False,
                             semicoarsening=False,
                             linerelaxation=False,
                             shape_cells=self.shape_cells,
                             verb=0)
Example #12
0
    def test_cprint(self, capsys):
        var = solver.MGParameters(cycle='F',
                                  sslsolver=False,
                                  semicoarsening=True,
                                  log=1,
                                  linerelaxation=False,
                                  shape_cells=self.shape_cells,
                                  verb=2)
        var.cprint('test', 3)
        out, _ = capsys.readouterr()
        assert out == ""
        assert var.log_message == ""

        var.cprint('test', 1)
        out, _ = capsys.readouterr()
        assert out == "test\n"
        assert var.log_message == "test\n"
Example #13
0
def test_print_one_liner(capsys):
    var = solver.MGParameters(verb=5,
                              cycle='F',
                              sslsolver=False,
                              linerelaxation=False,
                              semicoarsening=False,
                              shape_cells=(16, 8, 2))

    solver._print_one_liner(var, 1e-2, False)
    out, _ = capsys.readouterr()
    assert ":: emg3d :: 1.0e-02; 0; 0:00:0" in out

    var = solver.MGParameters(verb=5,
                              cycle='F',
                              sslsolver=True,
                              linerelaxation=False,
                              semicoarsening=False,
                              shape_cells=(16, 8, 2))
    var.exit_message = "TEST"

    solver._print_one_liner(var, 1e-2, True)
    out, _ = capsys.readouterr()
    assert ":: emg3d :: 1.0e-02; 0(0); 0:00:0" in out
    assert "TEST" in out

    grid = emg3d.TensorMesh(
        [np.ones(8), np.ones(8), np.ones(8)], origin=np.array([0, 0, 0]))
    model = emg3d.Model(grid, property_x=1.5, property_y=1.8, property_z=3.3)
    sfield = emg3d.get_source_field(grid,
                                    source=[4, 4, 4, 0, 0],
                                    frequency=10.0)

    # Dynamic one-liner.
    out, _ = capsys.readouterr()
    _ = solver.solve(model,
                     sfield,
                     sslsolver=False,
                     semicoarsening=False,
                     linerelaxation=False,
                     verb=1)
    out, _ = capsys.readouterr()
    assert '6; 0:00:' in out
    assert '; CONVERGED' in out

    out, _ = capsys.readouterr()
    _ = solver.solve(model,
                     sfield,
                     sslsolver=True,
                     semicoarsening=False,
                     linerelaxation=False,
                     verb=1)
    out, _ = capsys.readouterr()
    assert '3(5); 0:00:' in out
    assert '; CONVERGED' in out

    # One-liner.
    out, _ = capsys.readouterr()
    _ = solver.solve(model,
                     sfield,
                     sslsolver=True,
                     semicoarsening=False,
                     linerelaxation=False,
                     verb=1)
    out, _ = capsys.readouterr()
    assert '3(5); 0:00:' in out
    assert '; CONVERGED' in out
Example #14
0
def test_mgparameters():
    vnC = (2**3, 2**5, 2**4)

    # 1. semicoarsening
    var = solver.MGParameters(cycle='F',
                              sslsolver=False,
                              semicoarsening=True,
                              linerelaxation=False,
                              vnC=vnC,
                              verb=1)
    assert 'semicoarsening : True [1 2 3]' in var.__repr__()
    var = solver.MGParameters(cycle='V',
                              sslsolver=False,
                              semicoarsening=1213,
                              linerelaxation=False,
                              vnC=vnC,
                              verb=1)
    assert 'semicoarsening : True [1 2 1 3]' in var.__repr__()
    var = solver.MGParameters(cycle='F',
                              sslsolver=False,
                              semicoarsening=2,
                              linerelaxation=False,
                              vnC=vnC,
                              verb=1)
    assert 'semicoarsening : True [2]' in var.__repr__()
    with pytest.raises(ValueError):
        solver.MGParameters(cycle='F',
                            sslsolver=False,
                            semicoarsening=5,
                            linerelaxation=False,
                            vnC=vnC,
                            verb=1)

    # 2. linerelaxation
    var = solver.MGParameters(cycle='F',
                              sslsolver=False,
                              semicoarsening=False,
                              linerelaxation=True,
                              vnC=vnC,
                              verb=1)
    assert 'linerelaxation : True [4 5 6]' in var.__repr__()
    var = solver.MGParameters(cycle='F',
                              sslsolver=False,
                              semicoarsening=False,
                              linerelaxation=1247,
                              vnC=vnC,
                              verb=1)
    assert 'linerelaxation : True [1 2 4 7]' in var.__repr__()
    var = solver.MGParameters(cycle='F',
                              sslsolver=False,
                              semicoarsening=False,
                              linerelaxation=1,
                              vnC=vnC,
                              verb=1,
                              clevel=1)
    assert 'linerelaxation : True [1]' in var.__repr__()
    assert_allclose(var.clevel, 1)
    with pytest.raises(ValueError):
        solver.MGParameters(cycle='F',
                            sslsolver=False,
                            semicoarsening=False,
                            linerelaxation=-9,
                            vnC=vnC,
                            verb=1)

    # 3. sslsolver and cycle
    with pytest.raises(ValueError):
        solver.MGParameters(cycle=None,
                            sslsolver=False,
                            semicoarsening=False,
                            linerelaxation=False,
                            vnC=vnC,
                            verb=1)
    var = solver.MGParameters(cycle='F',
                              sslsolver=True,
                              semicoarsening=True,
                              linerelaxation=False,
                              vnC=vnC,
                              verb=1,
                              maxit=33)
    assert "sslsolver : 'bicgstab'" in var.__repr__()
    assert var.ssl_maxit == 33
    assert var.maxit == 3
    with pytest.raises(ValueError):
        solver.MGParameters(cycle='F',
                            sslsolver='abcd',
                            semicoarsening=0,
                            linerelaxation=False,
                            vnC=vnC,
                            verb=1)
    with pytest.raises(ValueError):
        solver.MGParameters(cycle='F',
                            sslsolver=4,
                            semicoarsening=0,
                            linerelaxation=False,
                            vnC=vnC,
                            verb=1)
    with pytest.raises(ValueError):
        solver.MGParameters(cycle='G',
                            sslsolver=False,
                            semicoarsening=False,
                            linerelaxation=False,
                            vnC=vnC,
                            verb=1)

    # 4. Wrong grid size
    with pytest.raises(ValueError):
        solver.MGParameters(cycle='F',
                            sslsolver=False,
                            semicoarsening=False,
                            linerelaxation=False,
                            vnC=(1, 2, 3),
                            verb=1)