Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ build/
**/PQAnalysis.egg-info/
**/__pycache*

# generated Cython artifacts (the .pyx sources stay tracked)
*.cpython-*.so
PQAnalysis/analysis/msd/_msd_kernel.c
PQAnalysis/analysis/vacf/_vacf_kernel.c
PQAnalysis/analysis/rdf/_rdf_kernel.c
PQAnalysis/io/traj_file/_slab_parser.c

_version.py
**/*.ipynb

Expand Down
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ include README.md
include CHANGELOG.md
recursive-include PQAnalysis/grammar *.lark
include PQAnalysis/io/traj_file/process_lines.pyx
include PQAnalysis/io/traj_file/_slab_parser.pyx
include PQAnalysis/analysis/msd/_msd_kernel.pyx
include PQAnalysis/analysis/vacf/_vacf_kernel.pyx
include PQAnalysis/analysis/rdf/_rdf_kernel.pyx
9 changes: 9 additions & 0 deletions PQAnalysis/analysis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,13 @@
This is a collection of analysis subpackages.
"""

from .momentum import Momentum, check_momentum
from .msd import MSD, MSDDiffusionFit, MSDInputFileReader, msd
from .rdf import RDF, RDFInputFileReader, RDFDataWriter, RDFLogWriter, rdf
from .spectrum_broadening import broaden, build_spectrum
from .vacf import VACF, VACFInputFileReader, vacf, vacf_spectrum
from .vibrational import (
VibrationalAnalysisInputFileReader,
VibrationalAnalysisResult,
vibrations,
)
28 changes: 28 additions & 0 deletions PQAnalysis/analysis/momentum/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
A package containing classes and functions to check the total linear
momentum of velocity trajectories.

Classes
-------
:py:class:`~PQAnalysis.analysis.momentum.momentum.Momentum`
A class to calculate the norm of the total linear momentum of a
selection of atoms for every frame of a velocity trajectory.
:py:class:`~PQAnalysis.analysis.momentum.momentum_output_file_writer.MomentumDataWriter`
A class to write momentum data to output files.

Functions
---------
:py:func:`~PQAnalysis.analysis.momentum.api.check_momentum`
A function to calculate and write the total linear momentum norm
per frame of a velocity trajectory.
"""

from .api import check_momentum
from .momentum import Momentum
from .momentum_output_file_writer import MomentumDataWriter

__all__ = [
"Momentum",
"MomentumDataWriter",
"check_momentum",
]
84 changes: 84 additions & 0 deletions PQAnalysis/analysis/momentum/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
API functions for the momentum analysis.
"""

from beartype.typing import List

from PQAnalysis.io import TrajectoryReader
from PQAnalysis.io.formats import FileWritingMode
from PQAnalysis.topology import SelectionCompatible
from PQAnalysis.traj import MDEngineFormat
from PQAnalysis.type_checking import runtime_type_checking
from PQAnalysis.types import Np1DNumberArray, PositiveReal

from .momentum import Momentum
from .momentum_output_file_writer import MomentumDataWriter



@runtime_type_checking
def check_momentum(
trajectory_files: str | List[str],
output: str | None = None,
selection: SelectionCompatible = None,
use_full_atom_info: bool = False,
scale: PositiveReal | None = None,
md_format: MDEngineFormat | str = MDEngineFormat.PQ,
mode: str | FileWritingMode = "w",
) -> Np1DNumberArray:
"""
Calculate the total linear momentum norm per frame and write it.

Reads the given velocity trajectory file(s) frame by frame,
calculates the norm of the total linear momentum
``P = sum_i m_i * v_i`` of the selected atoms for every frame and
writes one row per frame containing the one-based frame index and
the scaled momentum norm. This is a port of the legacy
``equipartition.jl`` tool.

Parameters
----------
trajectory_files : str | List[str]
The velocity trajectory file(s) to read.
output : str | None, optional
The output file. If None, the output is printed to stdout,
by default None.
selection : SelectionCompatible, optional
The selection of atoms to include in the total momentum,
by default None (all atoms).
use_full_atom_info : bool, optional
Whether to use the full atom information of the trajectory
for the selection or not, by default False.
scale : PositiveReal | None, optional
The scaling factor applied to the momentum norm before
output, by default None (1e-15, which converts
amu*Angstrom/s to amu*Angstrom/fs).
md_format : MDEngineFormat | str, optional
The format of the trajectory, by default MDEngineFormat.PQ.
Use ``qmcfc`` for legacy trajectories with a leading dummy
'X' atom.
mode : str | FileWritingMode, optional
The writing mode of the output file, by default "w".

Returns
-------
Np1DNumberArray
The scaled norms of the total linear momentum, one value
per frame.
"""
reader = TrajectoryReader(trajectory_files, md_format=md_format)

momentum = Momentum(
reader,
selection=selection,
use_full_atom_info=use_full_atom_info,
scale=scale,
)

data_writer = MomentumDataWriter(output, mode=mode)

momentum_norms = momentum.run()

data_writer.write(momentum_norms)

return momentum_norms
13 changes: 13 additions & 0 deletions PQAnalysis/analysis/momentum/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""
Exceptions for the momentum analysis.
"""

from PQAnalysis.exceptions import PQException



class MomentumError(PQException):

"""
Exception raised for momentum analysis errors.
"""
Loading
Loading