Skip to content

Commit

Permalink
Check SpEC version when importing
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsvu committed Feb 1, 2025
1 parent 1956526 commit 8049ede
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
17 changes: 6 additions & 11 deletions support/Pipelines/EccentricityControl/EccentricityControlParams.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import pandas as pd
import yaml

from spectre.support.CheckSpecImport import check_spec_import
from spectre.Visualization.PlotTrajectories import import_A_and_B
from spectre.Visualization.ReadH5 import to_dataframe

Expand Down Expand Up @@ -83,17 +84,11 @@ def eccentricity_control_params(
new orbital parameters with the keys listed in 'OrbitalParams'.
"""
# Import functions from SpEC until we have ported them over
try:
from OmegaDotEccRemoval import (
ComputeOmegaAndDerivsFromFile,
performAllFits,
)
except ImportError:
raise ImportError(
"Importing from SpEC failed. Make sure you have pointed "
"'-D SPEC_ROOT' to a SpEC installation when configuring the build "
"with CMake."
)
check_spec_import(
contains_commit="ecfabf1ce78daeacbdd026625a02215c8e84af0e",
)
from OmegaDotEccRemoval import ComputeOmegaAndDerivsFromFile, performAllFits

assert (
target_eccentricity == 0.0
), "Only circular orbits are currently supported for eccentricity control."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import numpy as np
from scipy.optimize import minimize

from spectre.support.CheckSpecImport import check_spec_import

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -101,14 +103,8 @@ def initial_orbital_parameters(
# call old Fortran code (LSODA) through scipy.integrate.odeint, which leads
# to lots of noise in stdout. When porting these functions, we should
# modernize them to use scipy.integrate.solve_ivp.
try:
from ZeroEccParamsFromPN import nOrbitsAndTotalTime, omegaAndAdot
except ImportError:
raise ImportError(
"Importing from SpEC failed. Make sure you have pointed "
"'-D SPEC_ROOT' to a SpEC installation when configuring the build "
"with CMake."
)
check_spec_import()
from ZeroEccParamsFromPN import nOrbitsAndTotalTime, omegaAndAdot

# Find an omega0 that gives the right number of orbits or time to merger
if num_orbits is not None or time_to_merger is not None:
Expand Down
1 change: 1 addition & 0 deletions support/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ particular supercomputer" OFF)
spectre_python_add_module(
support
PYTHON_FILES
CheckSpecImport.py
CliExceptions.py
DirectoryStructure.py
Logging.py
Expand Down
36 changes: 36 additions & 0 deletions support/Python/CheckSpecImport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Distributed under the MIT License.
# See LICENSE.txt for details.

from pathlib import Path
from typing import Optional

import git


def check_spec_import(contains_commit: Optional[str] = None):
"""Check that the SpEC Python modules can be imported.
Arguments:
contains_commit: If specified, check that the SpEC repository contains
the specified commit. This can be used as a version check to ensure
that the installed SpEC version is compatible with the code that is
importing it.
"""
try:
import Utils as spec_utils
except ImportError:
raise ImportError(
"Importing from SpEC failed. Make sure you have pointed "
"'-D SPEC_ROOT' to a SpEC installation when configuring the build "
"with CMake."
)
if contains_commit:
spec_repo = git.Repo(Path(spec_utils.__file__).parent.parent.parent)
try:
assert spec_repo.is_ancestor(contains_commit, spec_repo.head.commit)
except (AssertionError, git.exc.GitCommandError):
raise ImportError(
"SpEC version mismatch: requested revision"
f" {contains_commit} is not in the SpEC repository at"
f" {spec_repo.working_dir}."
)
2 changes: 2 additions & 0 deletions support/Python/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# Click: to compose our Python CLI, load subcommands lazily, and for
# autocompletion
click
# GitPython: to interact with Git repositories, e.g. SpEC version check
GitPython
h5py >= 3.5.0
# Humanize: For human-readable output like "2 hours ago"
humanize
Expand Down

0 comments on commit 8049ede

Please sign in to comment.