Beispiel #1
0
    def test_get_mode_quantity(self):
        sum_obj = self.sum_class(num1=1, num2=2)
        self.assertEqual(
            pmutt._get_mode_quantity(mode=sum_obj,
                                     method_name='get_sum3',
                                     num3=3), 6)

        with self.assertRaises(AttributeError):
            pmutt._get_mode_quantity(mode=sum_obj, method_name='get_prod')

        self.assertEqual(
            pmutt._get_mode_quantity(mode=sum_obj,
                                     method_name='get_prod',
                                     raise_error=False,
                                     raise_warning=False,
                                     default_value=0), 0)
Beispiel #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
Beispiel #3
0
    def get_EoRT(self,
                 T=c.T0('K'),
                 include_ZPE=False,
                 raise_error=True,
                 raise_warning=True,
                 **kwargs):
        """Dimensionless electronic energy

        Parameters
        ----------
            T : float, optional
                Temperature in K. If the electronic mode is
                :class:`~pmutt.statmech.elec.GroundStateElec`, then the output is
                insensitive to this input. Default is 298.15 K
            include_ZPE : bool, optional
                If True, includes the zero point energy. Default is False
            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
            kwargs : key-word arguments
                Parameters passed to electronic mode
        Returns
        -------
            EoRT : float
                Dimensionless electronic energy
        """
        kwargs['T'] = T
        EoRT = _get_mode_quantity(mode=self.elec_model,
                                  method_name='get_UoRT',
                                  raise_error=raise_error,
                                  raise_warning=raise_warning,
                                  default_value=0.,
                                  **kwargs)
        if include_ZPE:
            EoRT += _get_mode_quantity(mode=self.vib_model,
                                       method_name='get_ZPE',
                                       raise_error=raise_error,
                                       raise_warning=raise_warning,
                                       default_value=0.,
                                       **kwargs) / c.R('eV/K') / T
        return EoRT
Beispiel #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