示例#1
0
 def test_get_specie_kwargs(self):
     # Tests function does not fail when species' name not present in dict
     in_dict = {'val': 1}
     self.assertDictEqual(pmutt._get_specie_kwargs('test', **in_dict),
                          in_dict)
     # Tests function correctly reads value when species' name is present
     in_dict = {'val1': 1, 'test_kwargs': {'val2': 2}}
     out_dict = {'val1': 1, 'val2': 2}
     self.assertDictEqual(pmutt._get_specie_kwargs('test', **in_dict),
                          out_dict)
     # Tests function correctly reads value when species' name is present
     # and ignores other names
     in_dict = {
         'val1': 1,
         'test_kwargs': {
             'val2': 2
         },
         'other_kwargs': {
             'val3': 3
         }
     }
     out_dict = {'val1': 1, 'val2': 2}
     self.assertDictEqual(pmutt._get_specie_kwargs('test', **in_dict),
                          out_dict)
     # Tests function overwrites default values if species' name is present
     in_dict = {'val1': 1, 'test_kwargs': {'val1': 2}}
     out_dict = {'val1': 2}
     self.assertDictEqual(pmutt._get_specie_kwargs('test', **in_dict),
                          out_dict)
示例#2
0
def _get_mix_quantity(misc_models,
                      method_name,
                      raise_error=True,
                      raise_warning=True,
                      default_value=0.,
                      **kwargs):
    """Calculate contribution from mixing models to desired quantity

    Parameters
    ----------
        misc_models : list (length N) of ``pmutt.mixture`` objects
            Mix models to calculate the property
        method_name : str
            Name of method to use to calculate quantity. Calculates any
            quantity as long as the relevant objects have the same method
            name
        raise_error : bool, optional
            If True, raises an error if any of the modes do not have the
            quantity of interest. Default is True
        raise_warning : bool, optional
            Only relevant if raise_error is False. Raises a warning if any
            of the modes do not have the quantity of interest. Default is
            True
        default_value : float, optional
            Default value if the object does not contain the method. Default is
            0
        kwargs : key-word arguments
            Arguments to calculate mixture model properties, if any
    Returns
    -------
        mix_quantity : (N,) `numpy.ndarray`_
            Mixing quantity of interest. If verbose is True, each element
            corresponds to the contribution of each mix_model

    .. _`numpy.ndarray`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html
    """
    # Return default value if no mixture models exist
    if misc_models is None:
        return np.array([default_value])

    # Calculate contribution from mixing models if any
    mix_quantity = np.full_like(a=misc_models,
                                fill_value=default_value,
                                dtype=np.double)
    for i, mix_model in enumerate(misc_models):
        if mix_model is None:
            continue
        try:
            specie_kwargs = _get_specie_kwargs(mix_model.name_j, **kwargs)
        except AttributeError:
            specie_kwargs = kwargs.copy()
        mix_quantity[i] = _get_mode_quantity(mode=mix_model,
                                             method_name=method_name,
                                             raise_error=raise_error,
                                             raise_warning=raise_warning,
                                             default_value=default_value,
                                             **specie_kwargs)
    return mix_quantity
示例#3
0
def get_state_quantity(species, stoich, method_name, **kwargs):
    if method_name == 'get_q':
        state_quantity = 1.
    else:
        state_quantity = 0.

    for specie, coeff in zip(species, stoich):
        # Process the inputs and methods for each specie
        specie_kwargs = _get_specie_kwargs(specie.name, **kwargs)

        method = getattr(specie, method_name)
        if method_name == 'get_q':
            state_quantity *= \
                    _force_pass_arguments(method, **specie_kwargs)**coeff
        else:
            state_quantity += \
                    _force_pass_arguments(method, **specie_kwargs)*coeff
    return state_quantity
示例#4
0
    def get_quantity(self,
                     method_name,
                     raise_error=True,
                     raise_warning=True,
                     operation='sum',
                     verbose=False,
                     use_references=True,
                     **kwargs):
        """Generic method to get any quantity from modes.

        Parameters
        ----------
            method_name : str
                Name of method to use to calculate quantity. Calculates any
                quantity as long as the relevant objects have the same method
                name
            raise_error : bool, optional
                If True, raises an error if any of the modes do not have the
                quantity of interest. Default is True
            raise_warning : bool, optional
                Only relevant if raise_error is False. Raises a warning if any
                of the modes do not have the quantity of interest. Default is
                True
            operation : str, optional
                Operation to apply when combining the modes. Supported options
                include:

                - sum (Default)
                - prod
            use_references : bool, optional
                If True, adds contribution from references. Default is True
            verbose : bool, optional
                If False, returns the total Gibbs energy. If True, returns
                contribution of each mode.
            kwargs : key-word arguments
                Parameters passed to each mode
        Returns
        -------
            quantity : float or (N+6,) `numpy.ndarray`_
                Desired quantity. N represents the number of misc models. If
                verbose is True, contribution to each mode are as follows:
                [trans, vib, rot, elec, nucl, references, misc_models (if any)]

        .. _`numpy.ndarray`: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html
        """
        # Get the default value
        operation = operation.lower()
        if operation == 'sum':
            default_value = 0.
        elif operation == 'prod':
            default_value = 1.
        else:
            err_msg = 'Operation: {} not supported'.format(operation)
            raise ValueError(err_msg)

        # Calculate the quantity for each mode
        specie_kwargs = _get_specie_kwargs(specie_name=self.name, **kwargs)
        quantity = np.array([
            _get_mode_quantity(mode=self.trans_model,
                               method_name=method_name,
                               raise_error=raise_error,
                               raise_warning=raise_warning,
                               default_value=default_value,
                               **specie_kwargs),
            _get_mode_quantity(mode=self.vib_model,
                               method_name=method_name,
                               raise_error=raise_error,
                               raise_warning=raise_warning,
                               default_value=default_value,
                               **specie_kwargs),
            _get_mode_quantity(mode=self.rot_model,
                               method_name=method_name,
                               raise_error=raise_error,
                               raise_warning=raise_warning,
                               default_value=default_value,
                               **specie_kwargs),
            _get_mode_quantity(mode=self.elec_model,
                               method_name=method_name,
                               raise_error=raise_error,
                               raise_warning=raise_warning,
                               default_value=default_value,
                               **specie_kwargs),
            _get_mode_quantity(mode=self.nucl_model,
                               method_name=method_name,
                               raise_error=raise_error,
                               raise_warning=raise_warning,
                               default_value=default_value,
                               **specie_kwargs)
        ])
        if use_references and self.references is not None:
            ref_kwargs = copy(specie_kwargs)
            ref_kwargs['descriptors'] = getattr(self,
                                                self.references.descriptor)
            refs_quantity = np.atleast_1d(
                _get_mode_quantity(mode=self.references,
                                   method_name=method_name,
                                   raise_error=raise_error,
                                   raise_warning=raise_warning,
                                   default_value=default_value,
                                   **ref_kwargs))
        else:
            refs_quantity = np.atleast_1d(default_value)
        # Calculate contribution from misc models if any
        misc_quantity = _get_mix_quantity(misc_models=self.misc_models,
                                          method_name=method_name,
                                          raise_error=raise_error,
                                          raise_warning=raise_warning,
                                          default_value=default_value,
                                          verbose=verbose,
                                          **kwargs)
        # Add misc quantities onto quantity
        quantity = np.concatenate([quantity, refs_quantity, misc_quantity])
        quantity = _apply_numpy_operation(quantity,
                                          verbose=verbose,
                                          operation=operation)
        return quantity