Skip to content
Open
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
4 changes: 4 additions & 0 deletions petsctools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# is not available then attempting to access these attributes will raise an
# informative error.
if PETSC4PY_INSTALLED:
from .citation import add_citation, cite, print_citations_at_exit # noqa: F401
from .config import get_blas_library # noqa: F401
from .init import ( # noqa: F401
InvalidEnvironmentException,
Expand All @@ -37,6 +38,9 @@

def __getattr__(name):
petsc4py_attrs = {
"add_citation",
"cite",
"print_citations_at_exit",
"get_blas_library",
"InvalidEnvironmentException",
"InvalidPetscVersionException",
Expand Down
70 changes: 70 additions & 0 deletions petsctools/citation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
"""Module containing functions for registering citations through PETSc.

The functions in this module may be used to record Bibtex citation
information and then register that a particular citation is
relevant for a particular computation. It hooks up with PETSc's
citation registration mechanism, so that running with
``-citations`` does the right thing.

Example usage::

petsctools.add_citation("key", "bibtex-entry-for-my-funky-method")

...

if using_funky_method:
petsctools.cite("key")

"""

from petsc4py import PETSc


_citations_database = {}


def add_citation(cite_key: str, entry: str) -> None:
"""Add a paper to the database of possible citations.

Parameters
----------
cite_key :
The key to use.
entry :
The bibtex entry.

"""
_citations_database[cite_key] = entry


def cite(cite_key: str) -> None:
"""Cite a paper.

The paper should already have been added to the citations database using
`add_citation`.

Parameters
----------
cite_key :
The key of the relevant citation.

Raises
------
KeyError :
If no such citation is found in the database.

"""
if cite_key in _citations_database:
citation = _citations_database[cite_key]
PETSc.Sys.registerCitation(citation)
else:
raise KeyError(
f"Did not find a citation for '{cite_key}', please add it to the "
"citations database"
)


def print_citations_at_exit() -> None:
"""Print citations at the end of the program."""
# We devolve to PETSc for actually printing citations.
PETSc.Options()["citations"] = None
15 changes: 15 additions & 0 deletions tests/test_citations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

import petsctools


@pytest.mark.skipnopetsc4py
def test_cannot_cite_nonexistent_citation():
with pytest.raises(KeyError):
petsctools.cite("nonexistent")


@pytest.mark.skipnopetsc4py
def test_cite_citation():
petsctools.add_citation("mykey", "myentry")
petsctools.cite("mykey")