77from ase import Atoms
88from ase .io import read
99import pytest
10+ import torch
1011
1112from janus_core .calculations .descriptors import Descriptors
1213from janus_core .calculations .single_point import SinglePoint
1718MODEL_PATH = Path (__file__ ).parent / "models" / "mace_mp_small.model"
1819
1920
20- def test_calc_descriptors (tmp_path ):
21+ @pytest .mark .parametrize ("device" , ["cpu" , "cuda" ])
22+ def test_calc_descriptors (tmp_path , device ):
2123 """Test calculating equation of state from ASE atoms object."""
24+ if device == "cuda" and not torch .cuda .is_available ():
25+ pytest .skip ("CUDA not available" )
26+
2227 struct = read (DATA_PATH / "NaCl.cif" )
2328 log_file = tmp_path / "descriptors.log"
24- struct .calc = choose_calculator (arch = "mace_mp" , model = MODEL_PATH )
29+ struct .calc = choose_calculator (arch = "mace_mp" , model = MODEL_PATH , device = device )
2530
2631 descriptors = Descriptors (
2732 struct ,
@@ -41,13 +46,18 @@ def test_calc_descriptors(tmp_path):
4146 )
4247
4348
44- def test_calc_per_element (tmp_path ):
49+ @pytest .mark .parametrize ("device" , ["cpu" , "cuda" ])
50+ def test_calc_per_element (tmp_path , device ):
4551 """Test calculating descriptors for each element from SinglePoint object."""
52+ if device == "cuda" and not torch .cuda .is_available ():
53+ pytest .skip ("CUDA not available" )
54+
4655 log_file = tmp_path / "descriptors.log"
4756 single_point = SinglePoint (
4857 struct = DATA_PATH / "NaCl.cif" ,
4958 arch = "mace" ,
5059 model = MODEL_PATH ,
60+ device = device ,
5161 )
5262
5363 descriptors = Descriptors (
@@ -68,14 +78,19 @@ def test_calc_per_element(tmp_path):
6878 assert atoms .info ["mace_Na_descriptor" ] == pytest .approx (- 0.0020374985791535563 )
6979
7080
71- def test_logging (tmp_path ):
81+ @pytest .mark .parametrize ("device" , ["cpu" , "cuda" ])
82+ def test_logging (tmp_path , device ):
7283 """Test attaching logger to Descriptors and emissions are saved to info."""
84+ if device == "cuda" and not torch .cuda .is_available ():
85+ pytest .skip ("CUDA not available" )
86+
7387 log_file = tmp_path / "descriptors.log"
7488
7589 single_point = SinglePoint (
7690 struct = DATA_PATH / "NaCl.cif" ,
7791 arch = "mace_mp" ,
7892 model = MODEL_PATH ,
93+ device = device ,
7994 )
8095
8196 descriptors = Descriptors (
@@ -92,12 +107,17 @@ def test_logging(tmp_path):
92107 assert single_point .struct .info ["emissions" ] > 0
93108
94109
95- def test_dispersion ():
110+ @pytest .mark .parametrize ("device" , ["cpu" , "cuda" ])
111+ def test_dispersion (device ):
96112 """Test using mace_mp with dispersion."""
113+ if device == "cuda" and not torch .cuda .is_available ():
114+ pytest .skip ("CUDA not available" )
115+
97116 single_point = SinglePoint (
98117 struct = DATA_PATH / "NaCl.cif" ,
99118 arch = "mace_mp" ,
100119 calc_kwargs = {"dispersion" : False },
120+ device = device ,
101121 )
102122
103123 descriptors = Descriptors (
@@ -110,6 +130,7 @@ def test_dispersion():
110130 struct = DATA_PATH / "NaCl.cif" ,
111131 arch = "mace_mp" ,
112132 calc_kwargs = {"dispersion" : True },
133+ device = device ,
113134 )
114135
115136 descriptors_disp = Descriptors (
@@ -118,17 +139,24 @@ def test_dispersion():
118139 )
119140 descriptors_disp .run ()
120141
121- assert (
122- descriptors_disp .struct .info ["mace_mp_d3_descriptor" ]
123- == descriptors .struct .info ["mace_mp_descriptor" ]
142+ assert descriptors_disp .struct .info ["mace_mp_d3_descriptor" ] == pytest .approx (
143+ descriptors .struct .info ["mace_mp_descriptor" ]
124144 )
125145
126146
127- def test_not_implemented_error ():
128- """Test correct error raised if descriptors not implemented."""
147+ @pytest .mark .parametrize ("device" , ["cpu" , "cuda" ])
148+ def test_not_implemented_error (device ):
149+ """Test correct implemented error raised if descriptors not installed."""
150+ if device == "cuda" and not torch .cuda .is_available ():
151+ pytest .skip ("CUDA not available" )
152+
153+ from tests .utils import skip_extras
154+
155+ skip_extras ("chgnet" )
129156 single_point = SinglePoint (
130157 struct = DATA_PATH / "NaCl.cif" ,
131158 arch = "chgnet" ,
159+ device = device ,
132160 )
133161 with pytest .raises (NotImplementedError ):
134162 Descriptors (
0 commit comments