Skip to content

Commit 186092b

Browse files
authored
Merge pull request QMCPACK#5346 from kayahans/feature/magnetization_density
Nexus: magnetization density estimator
2 parents fc10941 + 0e7a48a commit 186092b

File tree

3 files changed

+189
-6
lines changed

3 files changed

+189
-6
lines changed

nexus/examples/qmcpack/rsqmc_misc/estimators/iron_ldaU_dmc.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,17 @@
143143
from qmcpack_input import spindensity
144144
sdens = spindensity(
145145
dr = (0.05, 0.05, 0.05), # Bohr units
146-
#grid = (100,100,100), # Alternative to dr, an NxNxN grid can be specified
146+
#grid = (100,100,100), # Alternative to dr, an NxNxN grid can be specified
147147
)
148148

149+
#===== Magnetization density =====
150+
from qmcpack_input import magnetizationdensity
151+
magdens = magnetizationdensity(
152+
dr = (0.05, 0.05, 0.05), # Grid spacing in Bohr (matching spin density)
153+
integrator = 'simpsons', # Integration method
154+
samples = 9, # Number of samples for integration
155+
)
156+
149157
#===== Energy density =====
150158
from qmcpack_input import generate_energydensity
151159
edens = generate_energydensity(
@@ -188,7 +196,7 @@
188196
job = job(cores=16,threads=4,app='qmcpack'),
189197
system = system,
190198
twistnum = 0,
191-
estimators = [sdens, edens, mom_dist, dm_est], # Requested estimators. These will be run in all QMC series.
199+
estimators = [sdens, magdens, edens, mom_dist, dm_est], # Requested estimators. These will be run in all QMC series.
192200
calculations = [
193201
vmc(
194202
walkers_per_rank = 256,

nexus/lib/qmcpack_input.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@
7676
# coefficient, hamiltonian, coulomb, constant, pseudopotential,#
7777
# pseudo, mpc, localenergy, energydensity, reference_points, #
7878
# spacegrid, origin, axis, chiesa, density, nearestneighbors, #
79-
# neighbor_trace, dm1b, spindensity, structurefactor, init, #
80-
# scalar_traces, array_traces, particle_traces, traces, loop, #
81-
# linear, cslinear, vmc, dmc. #
79+
# neighbor_trace, dm1b, spindensity, magnetizationdensity, #
80+
# structurefactor, init, scalar_traces, array_traces, #
81+
# particle_traces, traces, loop, linear, cslinear, vmc, dmc. #
8282
# #
8383
# QIxmlFactory #
8484
# Class supports comprehension of XML elements that share the #
@@ -2225,6 +2225,14 @@ class spindensity(QIxml):
22252225
identifier = 'name'
22262226
#end class spindensity
22272227

2228+
class magnetizationdensity(QIxml):
2229+
tag = 'estimator'
2230+
attributes = ['type','name','report']
2231+
parameters = ['dr','grid','center','corner','integrator','samples']
2232+
write_types = obj(report=yesno)
2233+
identifier = 'name'
2234+
#end class magnetizationdensity
2235+
22282236
class structurefactor(QIxml):
22292237
tag = 'estimator'
22302238
attributes = ['type','name','report']
@@ -2349,6 +2357,7 @@ class back_propagation(QIxml):
23492357
nearestneighbors = nearestneighbors,
23502358
dm1b = dm1b,
23512359
spindensity = spindensity,
2360+
magnetizationdensity = magnetizationdensity,
23522361
structurefactor = structurefactor,
23532362
force = force,
23542363
forwardwalking = forwardwalking,
@@ -2709,7 +2718,7 @@ class gen(QIxml):
27092718
correlation,coefficients,loop,linear,cslinear,vmc,dmc,vmc_batch,dmc_batch,linear_batch,
27102719
atomicbasisset,basisgroup,init,var,traces,scalar_traces,particle_traces,array_traces,
27112720
reference_points,nearestneighbors,neighbor_trace,dm1b,
2712-
coefficient,radfunc,spindensity,structurefactor,
2721+
coefficient,radfunc,spindensity,structurefactor,magnetizationdensity,
27132722
sposet,bspline_builder,composite_builder,heg_builder,include,
27142723
multideterminant,detlist,ci,mcwalkerset,csf,det,
27152724
optimize,cg_optimizer,flex_optimizer,optimize_qmc,wftest,kspace_jastrow,
@@ -2915,6 +2924,9 @@ class gen(QIxml):
29152924
spindensity.defaults.set(
29162925
type='spindensity',name='SpinDensity'
29172926
)
2927+
magnetizationdensity.defaults.set(
2928+
type='magnetizationdensity',name='MagnetizationDensity'
2929+
)
29182930
skall.defaults.set(
29192931
type='skall',name='skall',source='ion0',target='e',hdf5=True
29202932
)

nexus/tests/unit/test_qmcpack_input.py

+163
Original file line numberDiff line numberDiff line change
@@ -1965,6 +1965,169 @@ def test_excited_state():
19651965

19661966

19671967

1968+
def test_magnetization_density():
1969+
"""Test magnetization density estimator functionality"""
1970+
from qmcpack_input import QmcpackInput
1971+
from qmcpack_input import simulation, meta, section
1972+
from generic import obj
1973+
import numpy as np
1974+
# Helper function to find pattern in text allowing for flexible whitespace
1975+
def pattern_in_text(pattern, text):
1976+
"""Check if pattern exists in text, allowing for flexible whitespace"""
1977+
# Convert multiple spaces to single space and strip
1978+
normalized_text = ' '.join(text.split())
1979+
normalized_pattern = ' '.join(pattern.split())
1980+
return normalized_pattern in normalized_text
1981+
1982+
# Test with grid specification
1983+
qi_grid = QmcpackInput(
1984+
simulation(
1985+
qmcsystem = section(
1986+
hamiltonian = section(
1987+
name = 'h0',
1988+
type = 'generic',
1989+
estimators = [
1990+
section(
1991+
name = 'magnetizationdensity',
1992+
type = 'magnetizationdensity',
1993+
report = 'yes',
1994+
grid = '16 16 16',
1995+
center = '0 0 0',
1996+
corner = '1 1 1',
1997+
integrator = 'simpsons',
1998+
samples = 9,
1999+
),
2000+
],
2001+
),
2002+
),
2003+
),
2004+
)
2005+
qi_grid.pluralize()
2006+
2007+
# Verify XML output structure for grid case
2008+
text = qi_grid.write()
2009+
expected_xml_patterns = [
2010+
'<estimator type="magnetizationdensity"',
2011+
'name="magnetizationdensity"',
2012+
'report="yes"',
2013+
'<parameter name="grid" > 16 16 16 </parameter>',
2014+
'<parameter name="center" > 0 0 0 </parameter>',
2015+
'<parameter name="corner" > 1 1 1 </parameter>',
2016+
'<parameter name="integrator" > simpsons </parameter>',
2017+
'<parameter name="samples" > 9 </parameter>',
2018+
'</estimator>'
2019+
]
2020+
for pattern in expected_xml_patterns:
2021+
assert pattern_in_text(pattern, text), f"Missing or incorrect pattern: {pattern}"
2022+
assert 'name="dr"' not in text, "dr parameter should not be present"
2023+
2024+
# Test with dr specification
2025+
qi_dr = QmcpackInput(
2026+
simulation(
2027+
qmcsystem = section(
2028+
hamiltonian = section(
2029+
name = 'h0',
2030+
type = 'generic',
2031+
estimators = [
2032+
section(
2033+
name = 'magnetizationdensity',
2034+
type = 'magnetizationdensity',
2035+
report = 'yes',
2036+
dr = '0.1 0.1 0.1',
2037+
center = '0 0 0',
2038+
corner = '1 1 1',
2039+
integrator = 'simpsons',
2040+
samples = 9,
2041+
),
2042+
],
2043+
),
2044+
),
2045+
),
2046+
)
2047+
qi_dr.pluralize()
2048+
2049+
# Verify XML output structure for dr case
2050+
text = qi_dr.write()
2051+
expected_xml_patterns = [
2052+
'<estimator type="magnetizationdensity"',
2053+
'name="magnetizationdensity"',
2054+
'report="yes"',
2055+
'<parameter name="dr" > 0.1 0.1 0.1 </parameter>',
2056+
'<parameter name="center" > 0 0 0 </parameter>',
2057+
'<parameter name="corner" > 1 1 1 </parameter>',
2058+
'<parameter name="integrator" > simpsons </parameter>',
2059+
'<parameter name="samples" > 9 </parameter>',
2060+
'</estimator>'
2061+
]
2062+
for pattern in expected_xml_patterns:
2063+
assert pattern_in_text(pattern, text), f"Missing or incorrect pattern: {pattern}"
2064+
assert 'name="grid"' not in text, "grid parameter should not be present"
2065+
2066+
# Test full system setup with grid
2067+
qi_full = QmcpackInput(
2068+
meta(
2069+
lattice = obj(units='bohr'),
2070+
position = obj(condition='0', datatype='posArray'),
2071+
),
2072+
simulation(
2073+
project = section(
2074+
id='qmc',
2075+
series=0,
2076+
),
2077+
qmcsystem = section(
2078+
simulationcell = section(
2079+
lattice = np.array([
2080+
[10.0, 0.0, 0.0],
2081+
[0.0, 10.0, 0.0],
2082+
[0.0, 0.0, 10.0]
2083+
]),
2084+
bconds = np.array(tuple('ppp')),
2085+
),
2086+
hamiltonian = section(
2087+
name = 'h0',
2088+
type = 'generic',
2089+
estimators = [
2090+
section(
2091+
name = 'magnetizationdensity',
2092+
type = 'magnetizationdensity',
2093+
report = 'yes',
2094+
grid = '32 32 32',
2095+
center = '0 0 0',
2096+
corner = '1 1 1',
2097+
integrator = 'simpsons',
2098+
samples = 9,
2099+
),
2100+
],
2101+
),
2102+
),
2103+
),
2104+
)
2105+
qi_full.pluralize()
2106+
2107+
# Verify full system XML output
2108+
text = qi_full.write()
2109+
expected_xml_patterns = [
2110+
'<project id="qmc" series="0"',
2111+
'<simulationcell>',
2112+
'<parameter name="lattice" units="bohr"> 10.00000000 0.00000000 0.00000000 0.00000000 10.00000000 0.00000000 0.00000000 0.00000000 10.00000000 </parameter>',
2113+
'<parameter name="bconds"> p p p </parameter>',
2114+
'<hamiltonian name="h0" type="generic">',
2115+
'<estimator type="magnetizationdensity"',
2116+
'name="magnetizationdensity"',
2117+
'report="yes"',
2118+
'<parameter name="grid" > 32 32 32 </parameter>',
2119+
'<parameter name="center" > 0 0 0 </parameter>',
2120+
'<parameter name="corner" > 1 1 1 </parameter>',
2121+
'<parameter name="integrator" > simpsons </parameter>',
2122+
'<parameter name="samples" > 9 </parameter>',
2123+
'</estimator>',
2124+
'</hamiltonian>'
2125+
]
2126+
for pattern in expected_xml_patterns:
2127+
assert pattern_in_text(pattern, text), f"Missing or incorrect pattern: {pattern}"
2128+
assert 'name="dr"' not in text, "dr parameter should not be present"
2129+
#end def test_magnetization_density
2130+
19682131
if versions.seekpath_available:
19692132
def test_symbolic_excited_state():
19702133
from nexus import generate_physical_system

0 commit comments

Comments
 (0)