Skip to content

Commit

Permalink
add mode to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
stkroe committed Jul 30, 2024
1 parent ecf90a2 commit df234b6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
19 changes: 16 additions & 3 deletions PQAnalysis/analysis/thermal_expansion/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from PQAnalysis.io import BoxFileReader
from PQAnalysis.traj import MDEngineFormat
from PQAnalysis.type_checking import runtime_type_checking
from PQAnalysis.io.formats import FileWritingMode

from .thermal_expansion import ThermalExpansion
from .thermal_expansion_input_file_reader import ThermalExpansionInputFileReader
Expand All @@ -16,7 +17,10 @@


@runtime_type_checking
def thermal_expansion(input_file: str, md_format: MDEngineFormat | str = MDEngineFormat.PQ):
def thermal_expansion(
input_file: str,
md_format: MDEngineFormat | str = MDEngineFormat.PQ,
mode: FileWritingMode | str = "w"):
"""
Calculates the thermal expansion coefficient using a given input file.
Expand All @@ -38,6 +42,13 @@ def thermal_expansion(input_file: str, md_format: MDEngineFormat | str = MDEngin
the format of the input trajectory. Default is "PQ".
For more information on the supported formats please visit
:py:class:`~PQAnalysis.traj.formats.MDEngineFormat`.
mode : FileWritingMode | str, optional
The writing mode. Default is "w".
The writing mode can be either a string or a FileWritingMode enum value.
Possible values are:
- "w" or FileWritingMode.WRITE: write mode (default, no overwrite)
- "a" or FileWritingMode.APPEND: append mode
- "o" or FileWritingMode.OVERWRITE: overwrite mode
"""
md_format = MDEngineFormat(md_format)

Expand Down Expand Up @@ -69,10 +80,12 @@ def thermal_expansion(input_file: str, md_format: MDEngineFormat | str = MDEngin
)

data_writer = ThermalExpansionDataWriter(
filename=input_reader.out_file_key
filename=input_reader.out_file_key,
mode=mode
)

log_writer = ThermalExpansionLogWriter(filename=input_reader.log_file)
log_writer = ThermalExpansionLogWriter(
filename=input_reader.log_file, mode=mode)

log_writer.write_before_run(_thermal_expansion)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def read(self):
* - Key
- Value
* - {Reader.unit_key}
- The unit of the box dimensions. Default is Å.
- The unit of the box dimensions.
* - {Reader.log_file_key}
- The log file to write the log information to.
Note
Expand All @@ -93,7 +93,7 @@ def read(self):
They are optional in the input file, but they might be required for
the analysis. This means that if an optional keyword is specified
other keywords might be required.
- :code:`{Reader.unit_key}` is optional for the analysis.
- :code:`{Reader.log_file_key}` is optional for the analysis.
(for more information see
:py:class:`~PQAnalysis.io.input_file_reader.pq_analysis.thermal_expansion.thermal_expansion.ThermalExpansion`).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from PQAnalysis.io import BaseWriter
from PQAnalysis.utils import __header__
from PQAnalysis.type_checking import runtime_type_checking
from PQAnalysis.io.formats import FileWritingMode

from .thermal_expansion import ThermalExpansion

Expand All @@ -22,15 +23,25 @@ class ThermalExpansionDataWriter(BaseWriter):
"""

@runtime_type_checking
def __init__(self, filename: str) -> None:
def __init__(
self,
filename: str,
mode: str | FileWritingMode = "w") -> None:
"""
Parameters
----------
filename : str
the filename to write to
mode : str | FileWritingMode, optional
The writing mode. Default is "w".
The writing mode can be either a string or a FileWritingMode enum value.
Possible values are:
- "w" or FileWritingMode.WRITE: write mode (default, no overwrite)
- "a" or FileWritingMode.APPEND: append mode
- "o" or FileWritingMode.OVERWRITE: overwrite mode
"""
self.filename = filename
super().__init__(filename)
super().__init__(filename, mode=mode)

@runtime_type_checking
def write(
Expand Down Expand Up @@ -59,14 +70,15 @@ def write(
thermal_expansion_data_mega = thermal_expansion_data * 1e6
super().open()
angstrom = '\u212B'.encode('utf-8')
self.file.write(
print(
f"T / K"
f"a_avg in {angstrom} a_std in {angstrom}"
f"b_avg in {angstrom} b_std in {angstrom}"
f"c_avg in {angstrom} c_std in {angstrom}"
f"volume in {angstrom}³ volume_std in {angstrom}³"
f"thermal_expansion_a in (M/K) thermal_expansion_b in (M/K)"
f"thermal_expansion_c in (M/K) volumetric expansion in (M/K)\n"
f"thermal_expansion_c in (M/K) volumetric expansion in (M/K)",
file=self.file
)
for i, temperature_point in enumerate(temperature_points):
print(
Expand All @@ -91,15 +103,25 @@ class ThermalExpansionLogWriter(BaseWriter):
"""

@runtime_type_checking
def __init__(self, filename: str) -> None:
def __init__(
self,
filename: str,
mode: str | FileWritingMode = "w") -> None:
"""
Parameters
----------
filename : str
the filename to write to
mode : str | FileWritingMode, optional
The writing mode. Default is "w".
The writing mode can be either a string or a FileWritingMode enum value.
Possible values are:
- "w" or FileWritingMode.WRITE: write mode (default, no overwrite)
- "a" or FileWritingMode.APPEND: append mode
- "o" or FileWritingMode.OVERWRITE: overwrite mode
"""
self.filename = filename
super().__init__(filename)
super().__init__(filename, mode=mode)

@runtime_type_checking
def write_before_run(self, thermal_expansion: ThermalExpansion) -> None:
Expand Down
3 changes: 2 additions & 1 deletion PQAnalysis/cli/thermal_expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def add_arguments(cls, parser: _ArgumentParser) -> None:
"""
parser.parse_input_file()
parser.parse_engine()
parser.parse_mode()

@classmethod
def run(cls, args):
Expand All @@ -75,7 +76,7 @@ def run(cls, args):
args : argparse.Namespace
The arguments parsed by the parser.
"""
thermal_expansion(args.input_file, args.engine)
thermal_expansion(args.input_file, args.engine, args.mode)


def main():
Expand Down

0 comments on commit df234b6

Please sign in to comment.