예제 #1
0
    def add_restricted_insertions(self, species_topologies, restricted_type,
                                  restricted_value):
        """Add restricted insertions for specific species and boxes

        Parameters
        ----------
        species_topologies : list
            list of ``parmed.Structures`` containing one list per box of species
        restricted_type : list
            list of restricted insertion types containing one list per box of species
        restricted_value : list
            list of restricted insertion values (unyt arrays) containing one list per box of species
        """
        if self._restricted_type and self._restricted_value:
            warnings.warn("Restricted insertion has been previously"
                          " added and will be replaced.")
        if self.ensemble not in ["gcmc", "gemc", "gemc_npt"]:
            raise ValueError("Restricted insertions are only valid for"
                             " 'gcmc', 'gemc', and 'gemc_npt' ensembles.")
        if len(restricted_type) != len(restricted_value):
            raise ValueError("Length of 'restricted_type' and "
                             " 'restricted_value' must match.")
        for box in restricted_type:
            if isinstance(box, (str, int, float)):
                raise TypeError("Restricted type must be passed as a list"
                                " of lists corresponding to each box.")
            if len(box) != len(species_topologies):
                raise ValueError(
                    "Length of 'species' and "
                    " length of box list in 'restricted_type'"
                    " must match.  `species` has a length of {}"
                    " and the box list in 'restricted_type' has a "
                    " length of {}".format(len(species_topologies), len(box)))
        for box in restricted_value:
            if isinstance(box, (str, int, float)):
                raise TypeError("Restricted value must be passed as a list"
                                " of lists corresponding to each box.")
            if len(box) != len(species_topologies):
                raise ValueError(
                    "Length of 'species' and "
                    " length of species list in 'restricted_value'"
                    " must match.  `species` has a length of {}"
                    " and the box list in 'restricted_value' has a "
                    " length of {}".format(len(species_topologies), len(box)))
        if self.ensemble == "gcmc" and len(restricted_type) != 1:
            raise ValueError("GCMC ensemble contains 1 box but"
                             " `restricted_type` of length {}"
                             " was passed.".format(len(restricted_type)))
        if self.ensemble in ["gemc", "gemc_npt"] and len(restricted_type) != 2:
            raise ValueError("GEMC ensembles contain 2 boxes but"
                             " `restricted_type` of length {}"
                             " was passed.".format(len(restricted_type)))

        for types, values in zip(restricted_type, restricted_value):
            for typ, val in zip(types, values):
                if not typ and not val:
                    pass
                elif typ and not val:
                    raise ValueError("`restricted_type` {} was passed"
                                     " but `restricted_value` is None.".format(
                                         typ, val))
                elif val and not typ:
                    raise ValueError("`restricted_value` {} was passed"
                                     " but `restricted_type` is None.".format(
                                         val, typ))
                else:
                    _check_restriction_type(typ, val)
                    # Check units of restricted value
                    if typ == "interface":
                        [validate_unit(i, dimensions.length) for i in val]
                    else:
                        validate_unit(val, dimensions.length)

        self._restricted_type = restricted_type
        self._restricted_value = restricted_value
예제 #2
0
 def test_unit_err_msg(self):
     with pytest.raises(TypeError, match="test must be a"):
         validate_unit(
             1 * u.nm, dimensions.temperature, argument_name="test"
         )
예제 #3
0
 def test_validate_unit_int_error(self):
     with pytest.raises(TypeError):
         validate_unit(1, dimensions.length)
예제 #4
0
 def test_invalid_dimension(self):
     with pytest.raises(TypeError, match="with dimensions of"):
         validate_unit(1 * u.nm, dimensions.temperature)
예제 #5
0
 def test_validate_unit(self, unit, dimension, name):
     validate_unit(1 * unit, dimension, argument_name=name)