Exemplo n.º 1
0
    def test_constrain_with_unit(self, varunit, conunit):

        create = dict(value=3., unit=varunit, hasOld=True)
        name = 'MyVar'
        conval = PhysicalField(5, conunit)
        constraints = dict(top=conval, )

        v = ModelVariable(name=name, create=create, constraints=constraints)

        domain = SedimentDBLDomain()
        v.set_domain(domain)

        try:
            varval = conval.inUnitsOf(varunit)
        except TypeError:
            # incompatible units
            varval = None

        if varval is None:
            with pytest.raises(TypeError):
                v.setup()

        else:
            v.setup()
            v.var.updateOld()
            assert v.var.faceValue[0] == conval
Exemplo n.º 2
0
    def test_create_var(self, value, unit, err):

        domain = SedimentDBLDomain()
        if err:
            with pytest.raises(err):
                var = domain.create_var(name='myvar', value=value, unit=unit)
        else:
            var = domain.create_var(name='myvar', value=value, unit=unit)
            val = PhysicalField(value, unit)
            assert var.shape == domain.mesh.shape
            assert (var == val).all()
            if isinstance(value, PhysicalField):
                assert var.unit == value.unit
                # this overrides any supplied unit in creating cellvariables
            else:
                assert var.unit == val.unit
Exemplo n.º 3
0
    def test_constrain(self, constraints):
        # test that boundary conditions get applied
        create = dict(value=3.3, unit='mol/l')
        name = 'var'

        constraints = dict(constraints)

        v = ModelVariable(name=name, create=create, constraints=constraints)

        domain = SedimentDBLDomain()
        v.set_domain(domain)

        v.setup()
        assert v.var is not None
        assert v.var.name == v.name
        constraints_count = sum(
            [1 for c in constraints if c not in ('top', 'bottom')])
        assert len(v.var.constraints) == constraints_count, 'Var constraints: {} does not match ' \
                                                            'specified: {}'.format(
            (v.var.constraints), constraints
            )
        if 'top' in constraints:
            numerix.array_equal(v.var[0], constraints['top'])
        if 'bottom' in constraints:
            numerix.array_equal(v.var[-1], constraints['bottom'])
        if 'dbl' in constraints:
            assert (v.var[:domain.idx_surface] == constraints['dbl']).all()
        if 'sediment' in constraints:
            assert (
                v.var[domain.idx_surface:] == constraints['sediment']).all()
Exemplo n.º 4
0
    def test_setup(self, features, processes, err):
        # Test that for microbes, the feature variables are not stored on the domain
        # Also multiple features with same variable name will not raise an error

        domain = SedimentDBLDomain()

        m = MicrobialGroup(features=features, processes=processes)
        m.set_domain(domain)

        if err is None:
            m.setup()
            for feat in m.features.values():
                assert feat.name not in domain
                assert feat.name in m

            for fname, fdict in features.items():
                vname = fdict['init_params']['name']
                assert vname in m
                if fname != vname:
                    assert fname not in m
                    assert fname in m.features

        else:
            with pytest.raises(err):
                m.setup()
Exemplo n.º 5
0
    def test_snapshot(self, features, processes):

        domain = SedimentDBLDomain()
        domain.create_var('biomass', value=1, unit='mg/cm**3')

        m = MicrobialGroup(features=features, processes=processes)
        m.set_domain(domain)
        m.setup()

        state = m.snapshot()

        statekeys = ('metadata', 'features', 'processes')
        assert set(statekeys) == set(state.keys())

        featkeys = set(m.features)
        assert featkeys == set(state['features'])

        prockeys = set(m.processes)
        assert prockeys == set(state['processes'])
Exemplo n.º 6
0
    def test_snapshot(self):
        domain = SedimentDBLDomain()
        state = domain.snapshot()

        statekeys = ('metadata', 'depths', 'distances')
        assert set(statekeys) == set(state)

        metakeys = ('cell_size', 'sediment_length', 'DBL_length',
                    'sediment_cells', 'DBL_cells', 'sediment_porosity',
                    'idx_surface', 'total_cells', 'total_length')
        assert set(state['metadata']) == set(metakeys)

        assert len(state['depths']['data_static'][0]) == len(domain.mesh.x())
        assert len(state['distances']['data_static'][0]) == len(
            domain.mesh.scaledCellDistances) - 1

        for k in ('depths', 'distances'):
            # check that the units are that of distances
            p = PhysicalField(
                1, state[k]['data_static'][1]['unit']).inUnitsOf('m')
            assert p.value > 0
Exemplo n.º 7
0
    def test_create_entity_from(self):
        model = MicroBenthosModel()

        with pytest.raises(RuntimeError):
            entity = model.create_entity_from(VARDEF)
            # no domain available
        model.domain = SedimentDBLDomain()

        entity = model.create_entity_from(VARDEF)
        assert isinstance(entity, Entity)
        assert entity.check_domain()
        assert entity.is_setup
Exemplo n.º 8
0
    def test_create_var(self, value, unit, hasOld):
        # creation casts it into base units
        create = dict(value=value, unit=unit, hasOld=hasOld)
        name = 'myVar'
        v = ModelVariable(name=name, create=create)

        domain = SedimentDBLDomain()
        v.set_domain(domain)
        v.setup()
        assert v.var is domain[name]
        assert v.var.name == v.name
        assert v.var.shape == domain.mesh.shape
        assert (v.var() == PhysicalField(value, unit).inBaseUnits()).all()
Exemplo n.º 9
0
    def test_add_domain(self):
        e = DomainEntity()
        D = SedimentDBLDomain()

        # with pytest.raises(TypeError):
        #     e.domain = tuple()
        # This no longer raises a TypeError, but just issues a log warning

        e.domain = D

        assert e.domain is D

        with pytest.raises(RuntimeError):
            e.domain = D
Exemplo n.º 10
0
    def test_seed_normal(self, unit, loc, scale, coeff, error):

        create = dict(value=3., unit=unit, hasOld=True)
        name = 'MyVar'
        seed = dict(profile='normal',
                    params=dict(loc=loc, scale=scale, coeff=coeff))
        v = ModelVariable(name=name, create=create, seed=seed)

        domain = SedimentDBLDomain()
        v.domain = domain
        if error:
            with pytest.raises(error):
                v.setup()
            return

        else:

            v.setup()

            from scipy.stats import norm
            from fipy.tools import numerix

            C = 1.0 / numerix.sqrt(2 * numerix.pi)

            # loc and scale should be in units of the domain mesh
            if hasattr(loc, 'unit'):
                loc_ = loc.inUnitsOf(domain.depths.unit).value
            else:
                loc_ = loc

            if hasattr(scale, 'unit'):
                scale_ = scale.inUnitsOf(domain.depths.unit).value
            else:
                scale_ = scale

            if unit:
                coeff = PF(coeff, unit)

            normrv = norm(loc=loc_, scale=C**2 * scale_)
            val = coeff * normrv.pdf(domain.depths) * C * scale_

            # from pprint import pprint
            # pprint(zip(v.var, val))
            print(type(val), type(v.var))
            if unit:
                # array comparison between variable & physicalfield is problematic
                val = val.numericValue

            assert numerix.allclose(v.var.numericValue, val)
Exemplo n.º 11
0
    def test_init_float(self):
        # interpreted as millimeters
        cell_size = 0.3
        sediment_length = 30
        dbl_length = 3

        domain = SedimentDBLDomain(cell_size=cell_size,
                                   sediment_length=sediment_length,
                                   dbl_length=dbl_length)

        assert domain.cell_size.unit.name() == 'mm'

        assert domain.cell_size == PhysicalField(cell_size, 'mm')
        assert domain.sediment_length == PhysicalField(sediment_length, 'mm')
        assert domain.DBL_length == PhysicalField(dbl_length, 'mm')
Exemplo n.º 12
0
    def test_init_physical(self):
        cell_size = PhysicalField('20 mum')
        sediment_length = PhysicalField('1. cm')
        dbl_length = PhysicalField('1.0 mm')
        domain = SedimentDBLDomain(cell_size=cell_size,
                                   sediment_length=sediment_length,
                                   dbl_length=dbl_length)

        # internally converted to mm
        assert domain.cell_size.unit.name() == 'mm'

        assert domain.cell_size == cell_size
        assert domain.sediment_length == sediment_length
        assert domain.DBL_length == dbl_length
        assert 'sed_mask' in domain
Exemplo n.º 13
0
    def test_porosity(self):
        domain = SedimentDBLDomain()
        assert domain.sediment_porosity

        P = 0.35
        domain = SedimentDBLDomain(porosity=P)
        assert domain.sediment_porosity == P
        assert (domain.var_in_DBL('porosity') == 1.0).all()
        assert (domain.var_in_sediment('porosity') == P).all()

        # check that no new domain variable is created when just setting value
        Pvar = domain.VARS['porosity']
        newPvar = domain.set_porosity(0.66)
        assert newPvar is Pvar
Exemplo n.º 14
0
    def test_update_time(self):
        # test that updating time changes the surface_irrad value
        # test for inputs float, PhysicalField and Variable

        H = 4
        irrad = Irradiance(hours_total=H)
        irrad.set_domain(SedimentDBLDomain())
        irrad.setup()

        # irrad.setup(SedimentDBLDomain())
        assert irrad.surface_irrad is not None
        irrad.setup()
        old = irrad.surface_irrad.copy()
        irrad.on_time_updated(irrad.hours_total)
        assert irrad.surface_irrad() == old

        old = irrad.surface_irrad.copy()
        irrad.on_time_updated(H / 2.0 * 3600.0)
        assert irrad.surface_irrad() == irrad.zenith_level
Exemplo n.º 15
0
    def test_fractional_cell_size(self):
        """
        When the sediment or DBL lengths are not divisible by the cell size, the sediment_length
        and DBL_length attributes get updated after number of cells is calculateds
        """

        cell_size = 0.2
        sediment_length = 10.1
        dbl_length = 1.35
        Nsed = int(sediment_length / cell_size)
        Ndbl = int(dbl_length / cell_size)

        domain = SedimentDBLDomain(cell_size=cell_size,
                                   sediment_length=sediment_length,
                                   dbl_length=dbl_length)

        assert Nsed == domain.sediment_cells
        assert Ndbl == domain.DBL_cells
        assert domain.sediment_length.value != sediment_length
        assert domain.sediment_length.value == Nsed * cell_size
        assert domain.DBL_length.value != dbl_length
        assert domain.DBL_length.value == Ndbl * cell_size
Exemplo n.º 16
0
def domain():
    return SedimentDBLDomain()
Exemplo n.º 17
0
 def test_init_empty(self):
     domain = SedimentDBLDomain()
     assert domain.mesh
     assert 'porosity' in domain.VARS
     assert len(domain.VARS['porosity']) == domain.mesh.nx