Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run PTCDA_single example with pytest #228

Merged
merged 13 commits into from
Nov 13, 2023
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.7', '3.9', '3.10', '3.11', '3.12']

steps:
- name: Check out repository
Expand All @@ -20,11 +20,11 @@ jobs:

- name: Install package and its dependencies
run: |
pip install .[dev,opencl]
pip install -e .[dev,opencl]
pip install pocl-binary-distribution

- name: Run pytest
run: pytest tests -v --cov
run: PPAFM_RECOMPILE=1 pytest tests examples -v --cov -s

- name: Save test images
uses: actions/upload-artifact@v3
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ dist/

# Sphinx documentation
doc/sphinx/build/

.coverage
File renamed without changes.
27 changes: 27 additions & 0 deletions examples/PTCDA_Hartree_dz2/example_ptcda_hartree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import subprocess
from pathlib import Path

from ppafm.cli.generateElFF import main as generate_elff
from ppafm.cli.generateLJFF import main as generate_ljff
from ppafm.cli.plot_results import main as plot_results
from ppafm.cli.relaxed_scan import main as relaxed_scan


def example_ptcda_hartree():
script_location = Path(__file__).absolute().parent

# Change directory to the location of this script
os.chdir(script_location)

subprocess.run(["wget", "--no-check-certificate", "https://www.dropbox.com/s/18eg89l89npll8x/LOCPOT.xsf.zip"])
subprocess.run(["unzip", "LOCPOT.xsf.zip"])

generate_ljff(["--input", "LOCPOT.xsf"])
generate_elff(["--input", "LOCPOT.xsf", "--tip", "dz2"])
relaxed_scan(["--klat", "0.5", "--charge", "-0.10"])
plot_results(["--klat", "0.5", "--charge", "-0.10", "--Amplitude", "0.5", "--df"])


if __name__ == "__main__":
example_ptcda_singe()
yakutovicha marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 23 additions & 0 deletions examples/PTCDA_single/example_ptcda.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import os
from pathlib import Path

from ppafm.cli.generateElFF_point_charges import main as generate_elff_point_charges
from ppafm.cli.generateLJFF import main as generate_ljff
from ppafm.cli.plot_results import main as plot_results
from ppafm.cli.relaxed_scan import main as relaxed_scan


def example_ptcda_singe():
script_location = Path(__file__).absolute().parent

# Change directory to the location of this script
os.chdir(script_location)

generate_ljff(["--input", "PTCDA.xyz"])
generate_elff_point_charges(["--input", "PTCDA.xyz", "--tip", "s"])
relaxed_scan(["--klat", "0.5", "--charge", "-0.10"])
plot_results(["--klat", "0.5", "--charge", "-0.10", "--arange", "0.5", "2.0", "2", "--df"])


if __name__ == "__main__":
example_ptcda_singe()
4 changes: 2 additions & 2 deletions ppafm/cli/generateElFF.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
)


def main():
def main(argv=None):
parser = common.CLIParser(
description="Generate electrostatic force field by cross-correlation of sample Hartree potential with tip charge density. "
"The generated force field is saved to FFel_{x,y,z}.[ext]."
Expand All @@ -29,7 +29,7 @@ def main():
parser.add_argument("--Vref", action="store", type=float, help="Field under the KPFM dens. and Vh was calculated in V/Ang")
parser.add_argument("--z0", action="store", type=float, default=0.0, help="Heigth of the topmost layer of metallic substrate for E to V conversion (Ang)")
# fmt: on
args = parser.parse_args()
args = parser.parse_args(argv)

# Load parameters.
params_path = Path("params.ini") if Path("params.ini").is_file() else cpp_utils.PACKAGE_PATH / "defaults" / "params.ini"
Expand Down
4 changes: 2 additions & 2 deletions ppafm/cli/generateElFF_point_charges.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from ..HighLevel import computeELFF_pointCharge


def main():
def main(argv=None):
parser = common.CLIParser(description="Generate electrostatic force field by Coulomb interaction of point charges. The generated force field is saved to FFel_{x,y,z}.[ext].")
parser.add_arguments(["input", "input_format", "output_format", "tip", "energy", "noPBC"])
args = parser.parse_args()
args = parser.parse_args(argv)

common.loadParams("params.ini")
common.apply_options(vars(args))
Expand Down
4 changes: 2 additions & 2 deletions ppafm/cli/generateLJFF.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
from ..HighLevel import computeLJ


def main():
def main(argv=None):
parser = common.CLIParser(description="Generate a Lennard-Jones, Morse, or vdW force field. The generated force field is saved to FFLJ_{x,y,z}.[ext].")

parser.add_arguments(["input", "input_format", "output_format", "ffModel", "energy", "noPBC"])
args = parser.parse_args()
args = parser.parse_args(argv)

try:
common.loadParams("params.ini")
Expand Down
4 changes: 2 additions & 2 deletions ppafm/cli/plot_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
atom_size = 0.15


def main():
def main(argv=None):
# fmt: off
parser = common.CLIParser( description="Plot results for a scan with a specified charge, amplitude, and spring constant.Images are saved in folder Q{charge}K{klat}/Amp{Amplitude}." )
parser.add_arguments(["output_format","Amplitude","arange","klat","krange","charge", "qrange", "Vbias", "Vrange", "noPBC", ])
Expand All @@ -36,7 +36,7 @@ def main():
parser.add_argument( "--bI", action="store_true", help="Plot images for Boltzmann current" )
# fmt: on

args = parser.parse_args()
args = parser.parse_args(argv)
opt_dict = vars(args)

common.loadParams("params.ini")
Expand Down
4 changes: 2 additions & 2 deletions ppafm/cli/relaxed_scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def rotate_ff(fx, fy, a):
return new_fx, new_fy


def main():
def main(argv=None):
parser = common.CLIParser(
description="Perform a scan, relaxing the probe particle in a precalculated force field. The generated force field is saved to Q{charge}K{klat}/OutFz.xsf."
)
Expand All @@ -46,7 +46,7 @@ def main():
parser.add_argument("--pol_t", action="store", type=float, default=1.0, help="Scaling factor for tip polarization")
parser.add_argument("--pol_s", action="store", type=float, default=1.0, help="Scaling factor for sample polarization")
# fmt: on
args = parser.parse_args()
args = parser.parse_args(argv)

opt_dict = vars(args)
common.loadParams("params.ini")
Expand Down
6 changes: 4 additions & 2 deletions ppafm/io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/python

import copy
import os
import re

Expand Down Expand Up @@ -459,7 +460,7 @@ def loadGeometry(fname=None, format=None, params=None):
if qs is None:
qs = np.zeros(len(Zs))
atoms = [list(Zs), list(xyzs[:, 0]), list(xyzs[:, 1]), list(xyzs[:, 2]), list(qs)]
nDim = params["gridN"].copy()
nDim = copy.copy(params["gridN"])

# Make sure lvec is a 4x3 array. Use default grid parameters if needed
if (lvec is None) or (len(lvec) == 0):
Expand Down Expand Up @@ -503,11 +504,12 @@ def loadGeometry(fname=None, format=None, params=None):

# Copy lattice vectors and grid dimensions to the global parameters
# so as to guarantee compatibility between the local variables and global parameters

for i in range(3):
params["gridA"][i] = lvec[1][i]
params["gridB"][i] = lvec[2][i]
params["gridC"][i] = lvec[3][i]
params["gridN"][i] = nDim[i]
params["gridN"] = nDim

return atoms, nDim, lvec

Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,8 @@ skip = ["pp*", "*-manylinux_i686", "*-musllinux*", '*-win32']
line-length = 180
target-version = ['py37', 'py38', 'py39', 'py310', 'py311']
extend-exclude = "ppafm/dev"

[tool.pytest.ini_options]
testpaths = ["tests", "examples"]
python_files = ["test_*.py", "example_*.py"]
python_functions = ["test_*", "example_*"]
File renamed without changes.
Loading