Skip to content

Commit a136c0b

Browse files
committed
add recp codes
1 parent e234e7a commit a136c0b

File tree

4 files changed

+166
-1
lines changed

4 files changed

+166
-1
lines changed

pyxtal/miscellaneous/RECP/dia.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from pyxtal.xtal import RECP
2+
from pyxtal import pyxtal
3+
import numpy as np
4+
5+
xtal1 = pyxtal(); xtal1.from_prototype('diamond') #; xtal.to_file('dia.cif'); print(xtal)
6+
xtal2 = pyxtal(); xtal2.from_prototype('cBN');
7+
xtal3 = pyxtal(); xtal3.from_prototype('h-diamond') #; xtal.to_file('dia.cif'); print(xtal)
8+
data = [(xtal1, 'dia'), (xtal2, 'cBN'), (xtal3, 'h-dia')]
9+
for i in range(3, 5):
10+
xtal = pyxtal()
11+
xtal.from_seed(f'sub{i}.cif')
12+
data.append((xtal, f'sub{i}'))
13+
14+
data0 = []
15+
recp = RECP(dmax=10.0, nmax=20, lmax=8)
16+
for i, (xtal, label) in enumerate(data):
17+
d, rdf = recp.compute(xtal.to_ase(), norm=True)
18+
if i > 0:
19+
print(f"{label:10s} : {np.linalg.norm(d - data0[0][0]):12.4f}")
20+
data0.append((d, rdf, label))
21+
recp.plot(data0, filename='reciprocal_dia.png')

pyxtal/miscellaneous/RECP/test0.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from pyxtal.xtal import RECP
2+
from pyxtal import pyxtal
3+
import matplotlib.pyplot as plt
4+
5+
# Use a modern style
6+
plt.style.use('seaborn-v0_8-darkgrid')
7+
8+
# Customize plot appearance
9+
plt.rcParams.update({
10+
#'font.family': 'serif',
11+
'font.size': 14,
12+
'axes.labelsize': 15,
13+
'axes.titlesize': 15,
14+
'legend.fontsize': 14,
15+
'axes.grid': True,
16+
'grid.alpha': 0.3,
17+
'lines.linewidth': 2,
18+
'figure.dpi': 300
19+
})
20+
21+
recp = RECP(dmax=10.0, nmax=10, lmax=10, rbasis='Bessel')
22+
prototypes = ['diamond',
23+
'h-diamond',
24+
#'graphite',
25+
#'$\alpha$-boron',
26+
'a-quartz',
27+
'b-quartz']
28+
29+
fig, axs = plt.subplots(len(prototypes), 1, figsize=(6, 1.6*len(prototypes)))
30+
data = []
31+
for row, prototype in enumerate(prototypes):
32+
xtal = pyxtal()
33+
xtal.from_prototype(prototype)
34+
p, rdf = recp.compute(xtal.to_ase(), norm=True)
35+
# Plot p values
36+
if prototype == 'a-quartz':
37+
label = r'$\alpha$-quartz'
38+
elif prototype == 'b-quartz':
39+
label = r'$\beta$-quartz'
40+
else:
41+
label = prototype
42+
label += f" ({xtal.group.number}, {xtal.group.symbol})"
43+
axs[row].plot(p, label=label)
44+
axs[row].legend(loc=1)
45+
axs[row].set_ylabel('$P_{nl}$')
46+
47+
if row == len(prototypes) - 1:
48+
axs[row].set_xlabel('Power Spectrum Index')
49+
else:
50+
axs[row].set_xticklabels([])
51+
axs[row].set_ylim(0, 1.0)
52+
plt.tight_layout()
53+
plt.savefig('p-demo.png', dpi=300)
54+
plt.close()

pyxtal/miscellaneous/RECP/test1.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from pyxtal import pyxtal
2+
from pyxtal.reciprocal import RECP
3+
import matplotlib.pyplot as plt
4+
import numpy as np
5+
np.random.seed(42)
6+
7+
# Use a modern style
8+
plt.style.use('seaborn-v0_8-darkgrid')
9+
10+
# Customize plot appearance
11+
plt.rcParams.update({
12+
#'font.family': 'serif',
13+
'font.size': 12,
14+
'axes.labelsize': 14,
15+
'axes.titlesize': 14,
16+
'legend.fontsize': 12,
17+
'axes.grid': True,
18+
'grid.alpha': 0.3,
19+
'lines.linewidth': 2,
20+
'figure.dpi': 300
21+
})
22+
23+
data = [(227, ['8a'], [3.567]),
24+
(216, ['4a', '4d'], [3.567]),
25+
(210, ['8b'], [3.567]),
26+
(203, ['8a'], [3.567]),
27+
(166, ['6c'], [2.522, 6.178, 0.125]),
28+
(141, ['4a'], [2.522, 3.567]),
29+
(122, ['4b'], [2.522, 3.567]),
30+
(119, ['2b', '2d'], [2.522, 3.567]),
31+
(109, ['4a'], [2.522, 3.567, 0.875]),
32+
(98, ['4b'], [2.522, 3.567]),
33+
(88, ['4a'], [2.522, 3.567], ),
34+
#(74, ['4e'], [2.522, 2.522, 3.567, 0.875]),
35+
(70, ['8a'], [3.567, 3.567, 3.567])]
36+
37+
data0 = []
38+
recp = RECP(dmax=10.0, nmax=10, lmax=10, rbasis='Bessel')
39+
40+
41+
xtal = pyxtal()
42+
xtal.from_prototype('diamond')
43+
xtal_sub = xtal.subgroup_once(H=141, eps=0)
44+
45+
fig, axs = plt.subplots(3, 2, figsize=(12, 7.5))
46+
for row, eps in enumerate([0, 0.02, 0.05]):
47+
data1 = []
48+
for i, d in enumerate(data):
49+
if i == 0:
50+
xtal0 = xtal.copy()
51+
else:
52+
xtal0 = xtal.subgroup_once(H=d[0], eps=eps)
53+
if xtal0 is not None:
54+
p, rdf = recp.compute(xtal0.to_ase(), norm=True)
55+
else:
56+
xtal0 = xtal_sub.subgroup_once(H=d[0], eps=eps)
57+
if xtal0 is None:
58+
print('problem', d[0]); import sys; sys.exit()
59+
p, rdf = recp.compute(xtal0.to_ase(), norm=True)
60+
data1.append((p, rdf, f'{xtal0.group.number}'))
61+
62+
# Plot p values
63+
axs[row, 0].set_title(f'$P$ (noise={eps})', y=0.80, x=0.05, loc='left')
64+
for p, _, label in data1:
65+
axs[row, 0].plot(p, label=label, alpha=0.5, lw=1.0)
66+
67+
68+
axs[row, 0].set_ylabel('$P_{nl}$')
69+
if row == 2:
70+
axs[row, 0].set_xlabel('Power Spectrum Index')
71+
72+
# Plot rdf values
73+
axs[row, 1].set_title(f'$G$ (noise={eps})', y=0.80, x=0.05, loc='left')
74+
for _, rdf, label in data1:
75+
axs[row, 1].plot(rdf, label=label, alpha=0.5, lw=1.0)
76+
if row == 2:
77+
axs[row, 1].set_xlabel(r'$d$ ($\mathrm{\AA}^{-1}$)')
78+
else:
79+
axs[row, 0].set_xticklabels([])
80+
axs[row, 1].set_xticklabels([])
81+
82+
axs[row, 1].set_ylabel('$G(d)$')
83+
if row == 0: axs[row, 1].legend(ncol=2, loc='upper right')
84+
85+
plt.tight_layout()
86+
plt.savefig('test1-diamond.png')
87+
plt.close()
88+
89+
90+

pyxtal/reciprocal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def apply_bounds(tensor):
348348
print(f"Step {step}, {loss_sum:.6f}, LR={scheduler.get_last_lr()[0]:.6f}")
349349
if step + 1 == num_steps:
350350
print(f"stopping at last iteration")
351-
xtal =
351+
#xtal =
352352
return rep.detach(), losses.detach()
353353

354354
def loss(self, spg, wps, elements, P_ref, RDF_ref):

0 commit comments

Comments
 (0)