Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refac #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions example/ho1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from torch import optim

from schrodinet.sampler.metropolis import Metropolis
from schrodinet.wavefunction.wf_potential import Potential
from schrodinet.solver.solver_potential import SolverPotential
from schrodinet.solver.plot_potential import plot_results_1d, plotter1d
from schrodinet.wavefunction.wave_function_1d import WaveFunction1D
from schrodinet.solver.solver import Solver
from schrodinet.solver.plot import plot_results_1d, plotter1d


def pot_func(pos):
Expand All @@ -21,12 +21,11 @@ def ho1d_sol(pos):
domain, ncenter = {'min': -5., 'max': 5.}, 11

# wavefunction
wf = Potential(pot_func, domain, ncenter, fcinit='random', nelec=1, sigma=1.)
wf = WaveFunction1D(pot_func, domain, ncenter, sigma=1.)

# sampler
sampler = Metropolis(nwalkers=1000, nstep=200,
step_size=1., nelec=wf.nelec,
ndim=wf.ndim, init={'min': -5, 'max': 5})
step_size=1., init=domain)

# optimizer
opt = optim.Adam(wf.parameters(), lr=0.05)
Expand All @@ -35,12 +34,13 @@ def ho1d_sol(pos):
scheduler = optim.lr_scheduler.StepLR(opt, step_size=100, gamma=0.75)

# define solver
solver = SolverPotential(wf=wf, sampler=sampler,
optimizer=opt, scheduler=scheduler)
solver = Solver(wf=wf, sampler=sampler,
optimizer=opt, scheduler=scheduler)

# train the wave function
#plotter = plotter1d(wf, domain, 100, sol=ho1d_sol) # , save='./image/')
solver.run(300, loss='energy-manual', plot=None, save='model.pth')
plotter = plotter1d(wf, domain, 100, sol=ho1d_sol)
solver.run(300, loss='energy-manual', plot=plotter, save='model.pth')

# plot the final wave function
plot_results_1d(solver, domain, 100, ho1d_sol, e0=0.5, load='model.pth')
plot_results_1d(solver, domain, 100, ho1d_sol,
e0=0.5, load='model.pth')
16 changes: 9 additions & 7 deletions example/morse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
from torch import optim

from schrodinet.sampler.metropolis import Metropolis
from schrodinet.wavefunction.wf_potential import Potential
from schrodinet.solver.solver_potential import SolverPotential
from schrodinet.solver.plot_potential import plot_results_1d, plotter1d
from schrodinet.wavefunction.wave_function_1d import WaveFunction1D
from schrodinet.solver.solver import Solver
from schrodinet.solver.plot import plot_results_1d


def pot_func(pos):
Expand All @@ -22,7 +22,8 @@ def ho1d_sol(pos):
domain, ncenter = {'min': -3., 'max': 8.}, 51

# wavefunction
wf = Potential(pot_func, domain, ncenter, fcinit='random', nelec=1, sigma=1)
wf = WaveFunction1D(pot_func, domain, ncenter,
fcinit='random', nelec=1, sigma=1)

# sampler
sampler = Metropolis(nwalkers=1000, nstep=500,
Expand All @@ -36,12 +37,13 @@ def ho1d_sol(pos):
scheduler = optim.lr_scheduler.StepLR(opt, step_size=100, gamma=0.75)

# define solver
solver = SolverPotential(wf=wf, sampler=sampler,
optimizer=opt, scheduler=scheduler)
solver = Solver(wf=wf, sampler=sampler,
optimizer=opt, scheduler=scheduler)

# train the wave function
#plotter = plotter1d(wf, domain, 100, sol=ho1d_sol)
solver.run(300, loss='variance', plot=None, save='model.pth')

# plot the final wave function
plot_results_1d(solver, domain, 100, ho1d_sol, e0=-0.125, load='model.pth')
plot_results_1d(solver, domain, 100, ho1d_sol,
e0=-0.125, load='model.pth')
3 changes: 2 additions & 1 deletion schrodinet/sampler/metropolis.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def generate(self, pdf, ntherm=10, ndecor=100, pos=None,
idecor += 1

if with_tqdm:
print("Acceptance rate %1.3f %%" % (rate/self.nstep*100))
print("Acceptance rate %1.3f %%" %
(rate/self.nstep*100))

return torch.cat(pos)

Expand Down
20 changes: 13 additions & 7 deletions schrodinet/solver/plot_potential.py → schrodinet/solver/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,15 @@ def drawNow(self):
self.lwf.set_ydata(vp)

if self.plot_weight:
self.pweight.set_xdata(self.wf.rbf.centers.detach().numpy())
self.pweight.set_ydata(self.wf.fc.weight.detach().numpy().T)
self.pweight.set_xdata(
self.wf.rbf.centers.detach().numpy())
self.pweight.set_ydata(
self.wf.fc.weight.detach().numpy().T)

if self.plot_grad:
if self.wf.fc.weight.requires_grad:
self.pgrad.set_xdata(self.wf.rbf.centers.detach().numpy())
self.pgrad.set_xdata(
self.wf.rbf.centers.detach().numpy())
data = (self.wf.fc.weight.grad.detach().numpy().T)**2
data /= np.linalg.norm(data)
self.pgrad.set_ydata(data)
Expand Down Expand Up @@ -167,7 +170,7 @@ def plot_wf_1d(net, domain, res, grad=False, hist=False, pot=True, sol=None,
vals = net.wf(X)
vn = vals.detach().numpy().flatten()
vn /= np.max(vn)
ax.plot(xn, vn, color='black', linewidth=2, label='DeepQMC')
ax.plot(xn, vn, color='black', linewidth=2, label='Schrodinet')

if pot:
pot = net.wf.nuclear_potential(X).detach().numpy()
Expand Down Expand Up @@ -213,7 +216,8 @@ def plot_results_1d(net, domain, res, sol=None, e0=None, load=None):
ax0 = fig.add_subplot(211)
ax1 = fig.add_subplot(212)

plot_wf_1d(net, domain, res, sol=sol, hist=False, ax=ax0, load=load)
plot_wf_1d(net, domain, res, sol=sol,
hist=False, ax=ax0, load=load)
plot_observable(net.obs_dict, e0=e0, ax=ax1)

plt.show()
Expand Down Expand Up @@ -291,7 +295,8 @@ def __init__(self, wf, domain, res, pot=False, kinetic=False, sol=None):
self.yy = pos[:, 1].reshape(res[0], res[1])

if callable(sol):
vs = sol(self.POS).view(self.res[0], self.res[1]).detach().numpy()
vs = sol(self.POS).view(
self.res[0], self.res[1]).detach().numpy()
vs /= np.linalg.norm(vs)
self.ax.plot_wireframe(self.xx, self.yy, vs,
color='black', linewidth=1)
Expand Down Expand Up @@ -458,7 +463,8 @@ def plot_wf_3d(net, domain, res, sol=None,
if hist:
pos = net.sample().detach().numpy()
for ielec in range(net.wf.nelec):
ax.scatter(pos[:, ielec*3], pos[:, ielec*3+1], pos[:, ielec*3+2])
ax.scatter(pos[:, ielec*3],
pos[:, ielec*3+1], pos[:, ielec*3+2])

if callable(sol):

Expand Down
Loading