Esempio n. 1
0
    def test_npz(self, tmpdir, capsys):
        io.save(tmpdir+'/test.npz', **self.data)
        outstr, _ = capsys.readouterr()
        assert 'Data saved to «' in outstr
        assert emg3d.__version__ in outstr

        # Save it with other verbosity.
        _, _ = capsys.readouterr()
        io.save(tmpdir+'/test.npz', **self.data, verb=0)
        outstr, _ = capsys.readouterr()
        assert outstr == ""
        out = io.save(tmpdir+'/test.npz', **self.data, verb=-1)
        assert 'Data saved to «' in out

        # Load it.
        out_npz = io.load(str(tmpdir+'/test.npz'), allow_pickle=True)
        outstr, _ = capsys.readouterr()
        assert 'Data loaded from «' in outstr
        assert 'test.npz' in outstr
        assert emg3d.__version__ in outstr

        assert out_npz['Model'] == self.model
        assert_allclose(out_npz['a'], self.a)
        assert out_npz['b'] == self.b
        assert_allclose(self.field.fx, out_npz['Field'].fx)
        assert_allclose(self.grid.cell_volumes, out_npz['Grid'].cell_volumes)

        # Load it with other verbosity.
        _, _ = capsys.readouterr()
        out = io.load(tmpdir+'/test.npz', verb=0)
        outstr, _ = capsys.readouterr()
        assert outstr == ""
        out, out_str = io.load(tmpdir+'/test.npz', verb=-1)
        assert 'Data loaded from «' in out_str
Esempio n. 2
0
 def test_json(self, tmpdir):
     io.save(tmpdir+'/test.json', **self.data)
     out_json = io.load(str(tmpdir+'/test.json'))
     assert out_json['Model'] == self.model
     assert_allclose(out_json['a'], self.a)
     assert out_json['b'] == self.b
     assert_allclose(self.field.fx, out_json['Field'].fx)
     assert_allclose(self.grid.cell_volumes, out_json['Grid'].cell_volumes)
Esempio n. 3
0
    def test_h5(self, tmpdir):
        if h5py:
            io.save(tmpdir+'/test.h5', **self.data)
            out_h5 = io.load(str(tmpdir+'/test.h5'))
            assert out_h5['Model'] == self.model
            assert_allclose(out_h5['a'], self.a)
            assert out_h5['b'] == self.b
            assert_allclose(self.field.fx, out_h5['Field'].fx)
            assert_allclose(self.grid.cell_volumes,
                            out_h5['Grid'].cell_volumes)

        else:
            with pytest.warns(UserWarning, match='emg3d: This feature requir'):
                io.save(tmpdir+'/test.h5', grid=self.grid)
Esempio n. 4
0
    def test_warnings(self, tmpdir, capsys):
        # Check message from loading another file
        data = io._dict_serialize({'meshes': self.grid})
        fdata = io._dict_flatten(data)
        np.savez_compressed(tmpdir+'/test2.npz', **fdata)
        _ = io.load(str(tmpdir+'/test2.npz'), allow_pickle=True)
        outstr, _ = capsys.readouterr()
        assert "[version/format/date unknown; not created by emg" in outstr

        # Unknown keyword.
        with pytest.raises(TypeError, match="Unexpected "):
            io.load('ttt.npz', stupidkeyword='a')

        # Unknown extension.
        with pytest.raises(ValueError, match="Unknown extension '.abc'"):
            io.save(tmpdir+'/testwrongextension.abc', something=1)
        with pytest.raises(ValueError, match="Unknown extension '.abc'"):
            io.load(tmpdir+'/testwrongextension.abc')
Esempio n. 5
0
    def to_file(self, fname, name='survey', **kwargs):
        """Store Survey to a file.

        Parameters
        ----------
        fname : str
            Absolute or relative file name including ending, which defines the
            used data format. See :func:`emg3d.io.save` for the options.

        name : str, default: 'survey'
            Name with which the survey is stored in the file.

        kwargs : Keyword arguments, optional
            Passed through to :func:`emg3d.io.save`.

        """
        kwargs[name] = self  # Add survey to dict.
        return io.save(fname, **kwargs)
Esempio n. 6
0
}
model_l = models.Model(**input_model_l)

input_source_l = {
    'grid': grid_l,
    'source': [0, 0, 250., 30, 10],  # A rotated source to include all
    'frequency': freq
}

# Fields
sfield_l = fields.get_source_field(**input_source_l)

# F-cycle
fefield_l = solver.solve(model_l, sfield_l, plain=True)

# BiCGSTAB; F-cycle
bicefield_l = solver.solve(model_l, sfield_l, sslsolver='bicgstab', plain=True)

out_l = {
    'input_grid': input_grid_l,
    'input_model': input_model_l,
    'input_source': input_source_l,
    'grid': grid_l,
    'model': model_l,
    'sfield': sfield_l,
    'Fresult': fefield_l,
    'bicresult': bicefield_l,
}

io.save('../data/regression.npz', res=out, reg_2=reg_2, grid=mesh, lap=out_l)
Esempio n. 7
0
def simulation(args_dict):
    """Run `emg3d` invoked by CLI.

    Run and log ``emg3d`` given the settings stored in the config file,
    overruled by settings passed in ``args_dict`` (which correspond to
    command-line arguments).

    Results are saved to files according to provided settings.


    Parameters
    ----------
    args_dict : dict
        Arguments from terminal, see :func:`emg3d.cli.main`. Parameters passed
        in ``args_dict`` overrule parameters in the ``config``.

    """

    # Start timer.
    runtime = utils.Timer()

    # Parse configuration file.
    cfg, term = parser.parse_config_file(args_dict)
    check_files(cfg, term)  # Check all files and directories exist.
    function, verb = term['function'], term['verbosity']
    dry_run = term.get('dry_run', False)

    # Start this task: start timing.
    logger = initiate_logger(cfg, runtime, verb)

    # Log start info, python and emg3d version, and python path.
    logger.info(f":: emg3d CLI {function} START :: {time.asctime()} :: "
                f"v{utils.__version__}")
    logger.debug(f"{utils.Report()}")

    # Dump the configuration.
    paramdump = json.dumps(cfg, sort_keys=True, indent=4)
    logger.debug("\n    :: CONFIGURATION ::\n")
    logger.debug(f"{term['config_file']}\n{paramdump}")

    min_offset = cfg['simulation_options'].pop('min_offset', 0.0)

    if cfg['files']['load']:

        # Load input.
        logger.info("\n    :: LOAD SIMULATION ::\n")

        sim, sinfo = simulations.Simulation.from_file(
                cfg['files']['load'], verb=-1)
        logger.info(sinfo.split('\n')[0])
        logger.debug(sinfo.split('\n')[1])

    else:

        # Load input.
        logger.info("\n    :: LOAD SURVEY AND MODEL ::\n")
        sdata, sinfo = io.load(cfg['files']['survey'], verb=-1)
        survey = sdata['survey']
        logger.info(sinfo.split('\n')[0])
        logger.debug(sinfo.split('\n')[1])
        model, minfo = io.load(cfg['files']['model'], verb=-1)
        logger.info(minfo.split('\n')[0])
        logger.debug(minfo.split('\n')[1])

        # Select data.
        data = cfg['data']
        if data:
            survey = survey.select(
                sources=data.get('sources', None),
                receivers=data.get('receivers', None),
                frequencies=data.get('frequencies', None),
                remove_empty=data.get('remove_empty', False),
            )

        # Switch-off tqdm if verbosity is zero.
        if verb < 1:
            cfg['simulation_options']['tqdm_opts'] = {'disable': True}

        # Create simulation.
        sim = simulations.Simulation(
                survey=survey,
                model=model['model'],
                verb=-1,  # Only errors.
                **cfg['simulation_options']
        )

    # Print simulation info.
    logger.info("\n    :: SIMULATION ::")
    logger.info(f"\n{sim}\n")

    # Print meshes.
    logger.debug("    :: MESHES ::\n")
    logger.debug(sim.print_grid_info(return_info=True))

    # Initiate output dict, add configuration.
    output = {'configuration': cfg}

    # Compute forward model (all calls).
    logger.info("    :: FORWARD COMPUTATION ::\n")
    if dry_run:
        output['data'] = np.zeros(sim.survey.shape, dtype=complex)
    else:

        if function == 'forward':
            sim.compute(observed=True, min_offset=min_offset)
            output['data'] = sim.data.observed
        else:
            sim.compute()
            output['data'] = sim.data.synthetic

        # Print Solver Logs.
        if verb in [0, 1]:
            sim.print_solver_info('efield', 0)
        logger.debug(sim.print_solver_info('efield', 1, True))

    # Compute the misfit.
    if function in ['misfit', 'gradient']:
        if dry_run:
            output['misfit'] = 0.0
        else:
            output['misfit'] = sim.misfit
        output['n_observations'] = sim.survey.count

    # Compute the gradient.
    if function == 'gradient':
        logger.info("\n    :: BACKWARD COMPUTATION ::\n")

        if dry_run:
            output['gradient'] = np.zeros(sim.model.grid.shape_cells)
        else:
            output['gradient'] = sim.gradient

            # Print Solver Logs.
            if verb in [0, 1]:
                sim.print_solver_info('bfield', 0)
            logger.debug(sim.print_solver_info('bfield', 1, True))

    # Store output to disk.
    logger.info("    :: SAVE RESULTS ::\n")
    if cfg['files']['save']:
        oinfo = sim.to_file(cfg['files']['save'], verb=-1)
        logger.info(oinfo.split('\n')[0])
        logger.debug(oinfo.split('\n')[1])
    oinfo = io.save(cfg['files']['output'], **output, verb=-1)
    logger.info(oinfo.split('\n')[0])
    logger.debug(oinfo.split('\n')[1])

    # Goodbye
    logger.info(f"\n:: emg3d CLI {function} END   :: {time.asctime()} :: "
                f"runtime = {runtime.runtime}")