Skip to content

Commit a3e2cac

Browse files
committed
Fix for general ndim in OBC
1 parent 0ce3411 commit a3e2cac

2 files changed

Lines changed: 27 additions & 30 deletions

File tree

ferminet/hamiltonian.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ def local_energy(
321321
charges: jnp.ndarray,
322322
nspins: Sequence[int],
323323
use_scan: bool = False,
324+
ndim: int = 3,
324325
complex_output: bool = False,
325326
laplacian_method: str = 'default',
326327
states: int = 0,
@@ -336,6 +337,7 @@ def local_energy(
336337
charges: Shape (natoms). Nuclear charges of the atoms.
337338
nspins: Number of particles of each spin.
338339
use_scan: Whether to use a `lax.scan` for computing the laplacian.
340+
ndim: Number of dimensions.
339341
complex_output: If true, the output of f is complex-valued.
340342
laplacian_method: Laplacian calculation method. One of:
341343
'default': take jvp(grad), looping over inputs
@@ -360,7 +362,7 @@ def local_energy(
360362
if not pp_symbols:
361363
effective_charges = charges
362364
use_pp = False
363-
else:
365+
elif ndim == 3:
364366
effective_charges, pp_local, pp_nonlocal = pp.make_pp_potential(
365367
charges=charges,
366368
symbols=pp_symbols,
@@ -369,6 +371,8 @@ def local_energy(
369371
complex_output=complex_output
370372
)
371373
use_pp = not jnp.all(effective_charges == charges)
374+
else:
375+
raise NotImplementedError("Pseudopotentials only implemented for 3D")
372376

373377
if not use_pp:
374378
pp_local = lambda *args, **kwargs: 0.0
@@ -386,9 +390,10 @@ def _e_l(
386390
"""
387391
if states:
388392
# Compute features
389-
vmap_features = jax.vmap(networks.construct_input_features, (0, None))
393+
vmap_features = jax.vmap(
394+
networks.construct_input_features, (0, None, None))
390395
positions = jnp.reshape(data.positions, [states, -1])
391-
ae, _, r_ae, r_ee = vmap_features(positions, data.atoms)
396+
ae, _, r_ae, r_ee = vmap_features(positions, data.atoms, ndim)
392397

393398
# Compute potential energy
394399
vmap_pot = jax.vmap(potential_energy, (0, 0, None, None))
@@ -442,7 +447,7 @@ def _e_l(
442447
complex_output=complex_output,
443448
laplacian_method=laplacian_method)
444449
ae, _, r_ae, r_ee = networks.construct_input_features(
445-
data.positions, data.atoms
450+
data.positions, data.atoms, ndim,
446451
)
447452
potential = (potential_energy(r_ae, r_ee, data.atoms, effective_charges) +
448453
pp_local(r_ae) +

ferminet/train.py

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -739,40 +739,32 @@ def log_network(*args, **kwargs):
739739
steps=cfg.mcmc.steps,
740740
atoms=atoms_to_mcmc,
741741
blocks=cfg.mcmc.blocks * num_states,
742+
ndim=cfg.system.ndim,
742743
)
744+
743745
# Construct loss and optimizer
744-
laplacian_method = cfg.optim.get('laplacian', 'default')
745-
pp_symbols = cfg.system.get('pp', {'symbols': None}).get('symbols')
746746
if cfg.system.make_local_energy_fn:
747747
local_energy_module, local_energy_fn = (
748748
cfg.system.make_local_energy_fn.rsplit('.', maxsplit=1))
749749
local_energy_module = importlib.import_module(local_energy_module)
750750
make_local_energy = getattr(local_energy_module, local_energy_fn) # type: hamiltonian.MakeLocalEnergy
751-
local_energy_fn = make_local_energy(
752-
f=signed_network,
753-
charges=charges,
754-
nspins=nspins,
755-
use_scan=False,
756-
complex_output=use_complex,
757-
laplacian_method=laplacian_method,
758-
states=cfg.system.get('states', 0),
759-
state_specific=(cfg.optim.objective == 'vmc_overlap'),
760-
pp_type=cfg.system.get('pp', {'type': 'ccecp'}).get('type'),
761-
pp_symbols=pp_symbols if cfg.system.get('use_pp') else None,
762-
**cfg.system.make_local_energy_kwargs,
763-
)
764751
else:
765-
local_energy_fn = hamiltonian.local_energy(
766-
f=signed_network,
767-
charges=charges,
768-
nspins=nspins,
769-
use_scan=False,
770-
complex_output=use_complex,
771-
laplacian_method=laplacian_method,
772-
states=cfg.system.get('states', 0),
773-
state_specific=(cfg.optim.objective == 'vmc_overlap'),
774-
pp_type=cfg.system.get('pp', {'type': 'ccecp'}).get('type'),
775-
pp_symbols=pp_symbols if cfg.system.get('use_pp') else None)
752+
make_local_energy = hamiltonian.local_energy
753+
laplacian_method = cfg.optim.get('laplacian', 'default')
754+
pp_symbols = cfg.system.get('pp', {'symbols': None}).get('symbols')
755+
local_energy_fn = make_local_energy(
756+
f=signed_network,
757+
charges=charges,
758+
nspins=nspins,
759+
use_scan=False,
760+
ndim=cfg.system.ndim,
761+
complex_output=use_complex,
762+
laplacian_method=laplacian_method,
763+
states=cfg.system.get('states', 0),
764+
state_specific=(cfg.optim.objective == 'vmc_overlap'),
765+
pp_type=cfg.system.get('pp', {'type': 'ccecp'}).get('type'),
766+
pp_symbols=pp_symbols if cfg.system.get('use_pp') else None,
767+
**cfg.system.make_local_energy_kwargs)
776768

777769
if cfg.optim.get('spin_energy', 0.0) > 0.0:
778770
# Minimize <H + c * S^2> instead of just <H>

0 commit comments

Comments
 (0)