Skip to content

Commit c4312c3

Browse files
committed
Merge pull request #111 from APerezFadon:main
PiperOrigin-RevId: 918436052 Change-Id: Id8689a75dd9a1146d6331fa2f2963d0280e6cf82
2 parents 0ce3411 + a3e2cac commit c4312c3

2 files changed

Lines changed: 36 additions & 35 deletions

File tree

ferminet/hamiltonian.py

Lines changed: 17 additions & 9 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,14 +337,15 @@ 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.
340-
laplacian_method: Laplacian calculation method. One of:
341-
'default': take jvp(grad), looping over inputs
342-
'folx': use Microsoft's implementation of forward laplacian
342+
laplacian_method: Laplacian calculation method. One of: 'default': take
343+
jvp(grad), looping over inputs 'folx': use Microsoft's implementation of
344+
forward laplacian
343345
states: Number of excited states to compute. If 0, compute ground state with
344346
default machinery. If 1, compute ground state with excited state machinery
345-
state_specific: Only used for excited states (states > 0). If true, then
346-
the local energy is computed separately for each output from the network,
347+
state_specific: Only used for excited states (states > 0). If true, then the
348+
local energy is computed separately for each output from the network,
347349
instead of the local energy matrix being computed.
348350
pp_type: type of pseudopotential to use. Only used if ecp_symbols is
349351
provided.
@@ -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,11 @@ 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)
395+
)
390396
positions = jnp.reshape(data.positions, [states, -1])
391-
ae, _, r_ae, r_ee = vmap_features(positions, data.atoms)
397+
ae, _, r_ae, r_ee = vmap_features(positions, data.atoms, ndim)
392398

393399
# Compute potential energy
394400
vmap_pot = jax.vmap(potential_energy, (0, 0, None, None))
@@ -442,7 +448,9 @@ def _e_l(
442448
complex_output=complex_output,
443449
laplacian_method=laplacian_method)
444450
ae, _, r_ae, r_ee = networks.construct_input_features(
445-
data.positions, data.atoms
451+
data.positions,
452+
data.atoms,
453+
ndim,
446454
)
447455
potential = (potential_energy(r_ae, r_ee, data.atoms, effective_charges) +
448456
pp_local(r_ae) +

ferminet/train.py

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -739,40 +739,33 @@ 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,
768+
)
776769

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

0 commit comments

Comments
 (0)