Docs: Generate units.md from cp2k_input.xml

This commit is contained in:
Ole Schütt 2024-07-13 21:08:47 +02:00 committed by Ole Schütt
parent 549678dcdd
commit b40b0702d2
4 changed files with 54 additions and 189 deletions

View file

@ -27,6 +27,7 @@ def main() -> None:
locale.setlocale(locale.LC_ALL, locale="C.UTF8") # needed for strptime
root = ET.parse(cp2k_input_xml_fn).getroot()
build_bibliography(root, output_dir)
build_units_reference(root, output_dir)
build_input_reference(root, output_dir)
@ -73,6 +74,28 @@ def build_bibliography(root: ET.Element, output_dir: Path) -> None:
write_file(output_dir / "bibliography.md", "\n".join(output))
# ======================================================================================
def build_units_reference(root: ET.Element, output_dir: Path) -> None:
output = []
output += ["%", "% This file was created by generate_input_reference.py", "%"]
output += ["# Units", "", "Units of measurement available in CP2K's input.", ""]
for unit_kind in root.findall("UNIT_KIND"):
kind = unit_kind.attrib.get("name")
assert kind
article = "an" if kind[0] in "aeiou" else "a"
output += [f"## {kind.capitalize()}", ""]
output += [f"Possible units of measurement for {article} {kind}."]
output += [f"The `[{kind}]` entry acts like a dummy flag (assumes the"]
output += [f"unit of measurement of {kind} is in internal units),"]
output += [f"useful for dimensional analysis.", ""]
for unit in unit_kind.findall("UNIT"):
output += [f"- `{get_text(unit)}`"]
output += [""]
write_file(output_dir / "units.md", "\n".join(output))
# ======================================================================================
def build_input_reference(root: ET.Element, output_dir: Path) -> None:
num_files_written = process_section(root, ("CP2K_INPUT",), False, output_dir)

View file

@ -1,108 +0,0 @@
# Units
CP2K Available Units of Measurement
## Energy
Possible units of measurement for Energies. The `[energy]` entry acts like a dummy flag (assumes the
unit of measurement of energy is in internal units), useful for dimensional analysis.
- `hartree`
- `wavenumber_e`
- `joule`
- `kcalmol`
- `kjmol`
- `Ry`
- `eV`
- `keV`
- `K_e`
- `energy`
## Length
Possible units of measurement for Lengths. The `[length]` entry acts like a dummy flag (assumes the
unit of measurement of length is in internal units), useful for dimensional analysis.
- `bohr`
- `m`
- `pm`
- `nm`
- `angstrom`
- `length`
## Temperature
Possible units of measurement for Temperature. The `[temperature]` entry acts like a dummy flag
(assumes the unit of measurement of temperature is in internal units), useful for dimensional
analysis.
- `K`
- `au_temp`
- `temperature`
## Pressure
Possible units of measurement for Pressure. The `[pressure]` entry acts like a dummy flag (assumes
the unit of measurement of pressure is in internal units), useful for dimensional analysis.
- `bar`
- `atm`
- `kbar`
- `Pa`
- `MPa`
- `GPa`
- `au_p`
- `pressure`
## Angle
Possible units of measurement for Angles. The `[angle]` entry acts like a dummy flag (assumes the
unit of measurement of angle is in internal units), useful for dimensional analysis.
- `rad`
- `deg`
- `angle`
## Time
Possible units of measurement for Time. The `[time]` entry acts like a dummy flag (assumes the unit
of measurement of time is in internal units), useful for dimensional analysis.
- `s`
- `fs`
- `ps`
- `au_t`
- `wavenumber_t`
- `time`
## Mass
Possible units of measurement for Masses. The `[mass`\] entry acts like a dummy flag (assumes the
unit of measurement of mass is in internal units), useful for dimensional analysis.
- `kg`
- `amu`
- `m_e`
- `mass`
## Potential
Possible units of measurement for potentials. The `[potential]` entry acts like a dummy flag
(assumes the unit of measurement of potential is in internal units), useful for dimensional
analysis.
- `volt`
- `au_pot`
- `potential`
## Force
Possible units of measurement for forces. The `[force]` entry acts like a dummy flag (assumes the
unit of measurement of force is in internal units), useful for dimensional analysis.
- `N`
- `Newton`
- `mN`
- `mNewton`
- `au_f`
- `force`

View file

@ -121,7 +121,7 @@ MODULE cp_units
PUBLIC :: cp_unit_create, cp_unit_release, &
cp_unit_to_cp2k, cp_unit_from_cp2k, cp_unit_desc, &
cp_unit_set_create, cp_unit_set_release, &
cp_unit_to_cp2k1, cp_unit_from_cp2k1, cp_unit_compatible, print_all_units
cp_unit_to_cp2k1, cp_unit_from_cp2k1, cp_unit_compatible, export_units_as_xml
! **************************************************************************************************
!> \brief stores a unit
@ -1308,88 +1308,46 @@ CONTAINS
END SUBROUTINE cp_unit_set_release
! **************************************************************************************************
!> \brief Prints info on all available units in CP2K
!> \param unit_nr ...
!> \author Teodoro Laino [tlaino] - University of Zurich 10.2008
!> \brief Exports all available units as XML.
!> \param iw ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE print_all_units(unit_nr)
INTEGER, INTENT(IN) :: unit_nr
SUBROUTINE export_units_as_xml(iw)
INTEGER, INTENT(IN) :: iw
CALL print_section_unit(unit_label="Undefined", description="If the default unit "// &
"of a keyword is explicitly undefined, all possible units of measurement can "// &
"be used to define a proper value.", &
units_set=s2a("internal_cp2k"), unit_nr=unit_nr)
CALL format_units_as_xml("energy", s2a("hartree", "wavenumber_e", "joule", "kcalmol", &
"kjmol", "Ry", "eV", "keV", "K_e"), iw)
CALL format_units_as_xml("length", s2a("bohr", "m", "pm", "nm", "angstrom"), iw)
CALL format_units_as_xml("temperature", s2a("K", "au_temp"), iw)
CALL format_units_as_xml("pressure", s2a("bar", "atm", "kbar", "Pa", "MPa", "GPa", "au_p"), iw)
CALL format_units_as_xml("angle", s2a("rad", "deg"), iw)
CALL format_units_as_xml("time", s2a("s", "fs", "ps", "au_t", "wavenumber_t"), iw)
CALL format_units_as_xml("mass", s2a("kg", "amu", "m_e"), iw)
CALL format_units_as_xml("potential", s2a("volt", "au_pot"), iw)
CALL format_units_as_xml("force", s2a("N", "Newton", "mN", "mNewton", "au_f"), iw)
CALL print_section_unit(unit_label="Energy", description="Possible units of measurement "// &
"for Energies. The [energy] entry acts like a dummy flag (assumes the unit of "// &
"measurement of energy is in internal units), useful for dimensional analysis.", &
units_set=s2a("hartree", "wavenumber_e", "joule", "kcalmol", "kjmol", "Ry", &
"eV", "keV", "K_e", "energy"), unit_nr=unit_nr)
CALL print_section_unit(unit_label="Length", description="Possible units of measurement "// &
"for Lengths. The [length] entry acts like a dummy flag (assumes the unit of "// &
"measurement of length is in internal units), useful for dimensional analysis.", &
units_set=s2a("bohr", "m", "pm", "nm", "angstrom", "length"), unit_nr=unit_nr)
CALL print_section_unit(unit_label="Temperature", description="Possible units of measurement "// &
"for Temperature. The [temperature] entry acts like a dummy flag (assumes the unit of "// &
"measurement of temperature is in internal units), useful for dimensional analysis.", &
units_set=s2a("K", "au_temp", "temperature"), unit_nr=unit_nr)
CALL print_section_unit(unit_label="Pressure", description="Possible units of measurement "// &
"for Pressure. The [pressure] entry acts like a dummy flag (assumes the unit of "// &
"measurement of pressure is in internal units), useful for dimensional analysis.", &
units_set=s2a("bar", "atm", "kbar", "Pa", "MPa", "GPa", "au_p", "pressure"), &
unit_nr=unit_nr)
CALL print_section_unit(unit_label="Angle", description="Possible units of measurement "// &
"for Angles. The [angle] entry acts like a dummy flag (assumes the unit of "// &
"measurement of angle is in internal units), useful for dimensional analysis.", &
units_set=s2a("rad", "deg", "angle"), unit_nr=unit_nr)
CALL print_section_unit(unit_label="Time", description="Possible units of measurement "// &
"for Time. The [time] entry acts like a dummy flag (assumes the unit of "// &
"measurement of time is in internal units), useful for dimensional analysis.", &
units_set=s2a("s", "fs", "ps", "au_t", "wavenumber_t", "time"), unit_nr=unit_nr)
CALL print_section_unit(unit_label="Mass", description="Possible units of measurement "// &
"for Masses. The [mass] entry acts like a dummy flag (assumes the unit of "// &
"measurement of mass is in internal units), useful for dimensional analysis.", &
units_set=s2a("kg", "amu", "m_e", "mass"), unit_nr=unit_nr)
CALL print_section_unit(unit_label="Potential", description="Possible units of measurement "// &
"for potentials. The [potential] entry acts like a dummy flag (assumes the unit of "// &
"measurement of potential is in internal units), useful for dimensional analysis.", &
units_set=s2a("volt", "au_pot", "potential"), unit_nr=unit_nr)
CALL print_section_unit(unit_label="Force", description="Possible units of measurement "// &
"for forces. The [force] entry acts like a dummy flag (assumes the unit of "// &
"measurement of force is in internal units), useful for dimensional analysis.", &
units_set=s2a("N", "Newton", "mN", "mNewton", "au_f", "force"), unit_nr=unit_nr)
END SUBROUTINE print_all_units
END SUBROUTINE export_units_as_xml
! **************************************************************************************************
!> \brief Prints info on all available units in CP2K - Low level
!> \param unit_label ...
!> \param description ...
!> \brief Format units as xml.
!> \param unit_kind ...
!> \param units_set ...
!> \param unit_nr ...
!> \author Teodoro Laino [tlaino] - University of Zurich 10.2008
!> \param iw ...
!> \author Ole Schuett
! **************************************************************************************************
SUBROUTINE print_section_unit(unit_label, description, units_set, unit_nr)
CHARACTER(LEN=*), INTENT(IN) :: unit_label, description
SUBROUTINE format_units_as_xml(unit_kind, units_set, iw)
CHARACTER(LEN=*), INTENT(IN) :: unit_kind
CHARACTER(LEN=*), DIMENSION(:), INTENT(IN) :: units_set
INTEGER, INTENT(IN) :: unit_nr
INTEGER, INTENT(IN) :: iw
INTEGER :: i
WRITE (unit_nr, FMT='(A)') "<H2>"//TRIM(unit_label)//"</H2>"
WRITE (unit_nr, FMT='(A)') description//"<BR><DL>"
WRITE (iw, FMT='(T2,A)') '<UNIT_KIND name="'//TRIM(unit_kind)//'">'
DO i = 1, SIZE(units_set)
WRITE (unit_nr, FMT='(A)') "<DD><B>"//TRIM(units_set(i))//"</B></DD>"
WRITE (iw, FMT='(T3,A)') '<UNIT>'//TRIM(units_set(i))//'</UNIT>'
END DO
WRITE (unit_nr, FMT='(A)') "</DL><P>"
END SUBROUTINE print_section_unit
WRITE (iw, FMT='(T3,A)') '<UNIT>'//TRIM(unit_kind)//'</UNIT>' ! internal unit
WRITE (iw, FMT='(T2,A)') '</UNIT_KIND>'
END SUBROUTINE format_units_as_xml
END MODULE cp_units

View file

@ -43,7 +43,7 @@ MODULE cp2k_runs
USE cp_units, ONLY: cp_unit_set_create,&
cp_unit_set_release,&
cp_unit_set_type,&
print_all_units
export_units_as_xml
USE dbm_api, ONLY: dbm_library_print_stats
USE environment, ONLY: cp2k_finalize,&
cp2k_init,&
@ -914,6 +914,7 @@ CONTAINS
" <COMPILE_REVISION>"//TRIM(compile_revision)//"</COMPILE_REVISION>"
CALL export_references_as_xml(unit_number)
CALL export_units_as_xml(unit_number)
DO i = 1, root_section%n_subsections
CALL write_section_xml(root_section%subsections(i)%section, 1, unit_number)
@ -923,15 +924,6 @@ CONTAINS
CALL close_file(unit_number=unit_number)
CALL section_release(root_section)
! Units
CALL open_file(unit_number=unit_number, file_name="units.html", &
file_action="WRITE", file_status="REPLACE")
WRITE (unit_number, FMT='(A)') "<HTML><BODY><HEAD><TITLE>The cp2k units list</TITLE>"
WRITE (unit_number, FMT='(A)') "<H1>CP2K Available Units of Measurement</H1>"
CALL print_all_units(unit_nr=unit_number)
WRITE (unit_number, FMT='(A)') "</BODY></HTML>"
CALL close_file(unit_number=unit_number)
END SUBROUTINE write_xml_file
! **************************************************************************************************