|
| 1 | +"""Module containing functions for registering citations through PETSc. |
| 2 | +
|
| 3 | +The functions in this module may be used to record Bibtex citation |
| 4 | +information and then register that a particular citation is |
| 5 | +relevant for a particular computation. It hooks up with PETSc's |
| 6 | +citation registration mechanism, so that running with |
| 7 | +``-citations`` does the right thing. |
| 8 | +
|
| 9 | +Example usage:: |
| 10 | +
|
| 11 | + petsctools.add_citation("key", "bibtex-entry-for-my-funky-method") |
| 12 | +
|
| 13 | + ... |
| 14 | +
|
| 15 | + if using_funky_method: |
| 16 | + petsctools.cite("key") |
| 17 | +
|
| 18 | +""" |
| 19 | + |
| 20 | +from petsc4py import PETSc |
| 21 | + |
| 22 | + |
| 23 | +_citations_database = {} |
| 24 | + |
| 25 | + |
| 26 | +def add_citation(cite_key: str, entry: str) -> None: |
| 27 | + """Add a paper to the database of possible citations. |
| 28 | +
|
| 29 | + Parameters |
| 30 | + ---------- |
| 31 | + cite_key : |
| 32 | + The key to use. |
| 33 | + entry : |
| 34 | + The bibtex entry. |
| 35 | +
|
| 36 | + """ |
| 37 | + _citations_database[cite_key] = entry |
| 38 | + |
| 39 | + |
| 40 | +def cite(cite_key: str) -> None: |
| 41 | + """Cite a paper. |
| 42 | +
|
| 43 | + The paper should already have been added to the citations database using |
| 44 | + `add_citation`. |
| 45 | +
|
| 46 | + Parameters |
| 47 | + ---------- |
| 48 | + cite_key : |
| 49 | + The key of the relevant citation. |
| 50 | +
|
| 51 | + Raises |
| 52 | + ------ |
| 53 | + KeyError : |
| 54 | + If no such citation is found in the database. |
| 55 | +
|
| 56 | + """ |
| 57 | + if cite_key in _citations_database: |
| 58 | + citation = _citations_database[cite_key] |
| 59 | + PETSc.Sys.registerCitation(citation) |
| 60 | + else: |
| 61 | + raise KeyError( |
| 62 | + f"Did not find a citation for '{cite_key}', please add it to the " |
| 63 | + "citations database" |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +def print_citations_at_exit() -> None: |
| 68 | + """Print citations at the end of the program.""" |
| 69 | + # We devolve to PETSc for actually printing citations. |
| 70 | + PETSc.Options()["citations"] = None |
0 commit comments