Ejemplo n.º 1
0
    def setUp(self):
        # real-frequency grid and example spectrum
        w = np.linspace(-10., 10., num=501, endpoint=True)
        spec = 0.4 * np.exp(-0.5 * (w - 1.8)**2) + 0.6 * np.exp(-0.5 *
                                                                (w + 1.8)**2)
        spec /= np.trapz(spec, w)

        # Matsubara frequency grid and transformation of data
        beta = 10.
        niw = 20
        iw = np.pi / beta * (2. * np.arange(niw) + 1.)
        kernel = 1. / (1j * iw[:, None] - w[None, :])
        giw = np.trapz(kernel * spec[None, :], w, axis=1)

        # add noise to the data
        rng = np.random.RandomState(4713)
        noise_ampl = 0.0001
        giw += noise_ampl * (rng.randn(niw) + 1j * rng.randn(niw))

        # error bars and default model for analytic continuation
        err = np.ones_like(iw) * noise_ampl
        model = np.ones_like(w)
        model /= np.trapz(model, w)

        # specify the analytic continuation problem
        probl = cont.AnalyticContinuationProblem(im_axis=iw,
                                                 re_axis=w,
                                                 im_data=giw,
                                                 kernel_mode='freq_fermionic')

        # solve the problem
        self.sol, _ = probl.solve(method='maxent_svd',
                                  alpha_determination='chi2kink',
                                  optimizer='newton',
                                  model=model,
                                  stdev=err,
                                  interactive=False,
                                  alpha_start=1e12,
                                  alpha_end=1e-2,
                                  preblur=True,
                                  blur_width=0.5,
                                  verbose=False)

        self.A_opt_reference = np.load("tests/A_opt_maxent_fermionic.npy")
        self.backtransform_reference = np.load(
            "tests/backtransform_maxent_fermionic.npy")
        self.chi2_reference = np.load("tests/chi2_maxent_fermionic.npy")
Ejemplo n.º 2
0
    def setUp(self):
        w_real = np.linspace(0., 5., num=2001, endpoint=True)
        spec_real = np.exp(-(w_real)**2 / (2. * 0.2**2))
        spec_real += 0.3 * np.exp(-(w_real - 1.5)**2 / (2. * 0.8**2))
        spec_real += 0.3 * np.exp(-(w_real + 1.5)**2 /
                                  (2. * 0.8**2))  # must be symmetric around 0!
        spec_real /= np.trapz(spec_real, w_real)  # normalization

        beta = 10.
        iw = 2. * np.pi / beta * np.arange(10)

        noise_amplitude = 1e-4  # create gaussian noise
        rng = np.random.RandomState(1234)
        noise = rng.normal(0., noise_amplitude, iw.shape[0])

        kernel = (w_real**2)[None, :] / ((iw**2)[:, None] +
                                         (w_real**2)[None, :])
        kernel[0, 0] = 1.
        gf_bos = np.trapz(kernel * spec_real[None, :], w_real, axis=1) + noise
        norm = gf_bos[0]
        gf_bos /= norm

        w = np.linspace(0., 5., num=501, endpoint=True)
        probl = cont.AnalyticContinuationProblem(im_axis=iw,
                                                 re_axis=w,
                                                 im_data=gf_bos,
                                                 kernel_mode='freq_bosonic')

        err = np.ones_like(iw) * noise_amplitude / norm
        model = np.ones_like(w)
        model /= np.trapz(model, w)
        self.sol, _ = probl.solve(method='maxent_svd',
                                  alpha_determination='chi2kink',
                                  optimizer='newton',
                                  stdev=err,
                                  model=model,
                                  interactive=False,
                                  verbose=False)

        self.A_opt_reference = np.load("tests/A_opt_maxent_bosonic.npy")
        self.backtransform_reference = np.load(
            "tests/backtransform_maxent_bosonic.npy")
        self.chi2_reference = np.load("tests/chi2_maxent_bosonic.npy")
Ejemplo n.º 3
0
    def setUp(self):
        # real-frequency grid and example spectrum
        w = np.linspace(-10., 10., num=501, endpoint=True)
        spec = 0.4 * np.exp(-0.5 * (w - 1.8)**2) + 0.6 * np.exp(-0.5 *
                                                                (w + 1.8)**2)
        spec /= np.trapz(spec, w)

        # Matsubara frequency grid and transformation of data
        beta = 10.
        niw = 20
        iw = np.pi / beta * (2. * np.arange(niw) + 1.)
        kernel = 1. / (1j * iw[:, None] - w[None, :])
        giw = np.trapz(kernel * spec[None, :], w, axis=1)

        # add noise to the data
        rng = np.random.RandomState(4713)
        noise_ampl = 0.0001
        giw += noise_ampl * (rng.randn(niw) + 1j * rng.randn(niw))

        # error bars and default model for analytic continuation
        err = np.ones_like(iw) * noise_ampl

        # specify the analytic continuation problem
        mats_ind = [0, 1, 2, 3, 4, 5, 6, 8,
                    9]  # Matsubara indices of data points to use in Pade
        probl = cont.AnalyticContinuationProblem(im_axis=iw[mats_ind],
                                                 re_axis=w,
                                                 im_data=giw[mats_ind],
                                                 kernel_mode='freq_fermionic')

        # solve the problem
        self.sol = probl.solve(method='pade')
        check_axis = np.linspace(0., 1.25 * iw[mats_ind[-1]], num=500)
        self.check = probl.solver.check(im_axis_fine=check_axis)

        self.A_opt_reference = np.load("tests/A_opt_pade.npy")
        self.check_reference = np.load("tests/check_pade.npy")
Ejemplo n.º 4
0
    def main_function(self):
        """Main function for the analytic continuation procedure.

        This function is called when the "Do it" button is clicked.
        It performs an analytical continuation for the present settings
        and shows a plot.
        """

        mats_ind = self.parse_mats_ind()
        self.ana_cont_probl = cont.AnalyticContinuationProblem(
            im_axis=self.input_data.mats[mats_ind],
            im_data=self.input_data.value[mats_ind],
            re_axis=self.realgrid.grid,
            kernel_mode='freq_fermionic')

        sol = self.ana_cont_probl.solve(method='pade')
        check_axis = np.linspace(0.,
                                 1.25 * self.input_data.mats[mats_ind[-1]],
                                 num=500)
        check = self.ana_cont_probl.solver.check(im_axis_fine=check_axis)

        self.output_data.update(self.realgrid.grid, sol.A_opt, self.input_data)

        fig, ax = plt.subplots(ncols=2, nrows=2,
                               figsize=(11.75, 8.25))  # A4 paper size

        ax[0, 0].plot(self.realgrid.grid, sol.A_opt)
        ax[0, 0].set_xlabel(r'$\omega$')
        ax[0, 0].set_ylabel('spectrum')

        ax[0, 1].plot(self.input_data.mats[mats_ind],
                      self.input_data.value.real[mats_ind],
                      color='red',
                      ls='None',
                      marker='.',
                      markersize=12,
                      alpha=0.33,
                      label='Re[selected data]')
        ax[0, 1].plot(self.input_data.mats[mats_ind],
                      self.input_data.value.imag[mats_ind],
                      color='red',
                      ls='None',
                      marker='.',
                      markersize=12,
                      alpha=0.33,
                      label='Im[selected data]')
        ax[0, 1].plot(self.input_data.mats,
                      self.input_data.value.real,
                      color='blue',
                      ls=':',
                      marker='x',
                      markersize=5,
                      label='Re[full data]')
        ax[0, 1].plot(self.input_data.mats,
                      self.input_data.value.imag,
                      color='green',
                      ls=':',
                      marker='+',
                      markersize=5,
                      label='Im[full data]')

        ax[1, 0].plot(self.input_data.mats[mats_ind],
                      self.input_data.value.real[mats_ind],
                      color='red',
                      ls='None',
                      marker='.',
                      markersize=12,
                      alpha=0.33,
                      label='Re[selected data]')
        ax[1, 0].plot(self.input_data.mats[mats_ind],
                      self.input_data.value.imag[mats_ind],
                      color='red',
                      ls='None',
                      marker='.',
                      markersize=12,
                      alpha=0.33,
                      label='Im[selected data]')
        # ax[1, 0].plot(self.input_data.mats, self.input_data.value.real,
        #               color='blue', ls=':', marker='x', markersize=5,
        #               label='Re[full data]')
        # ax[1, 0].plot(self.input_data.mats, self.input_data.value.imag,
        #               color='green', ls=':', marker='+', markersize=5,
        #               label='Im[full data]')
        ax[1, 0].plot(check_axis,
                      check.real,
                      ls='--',
                      color='gray',
                      label='Re[Pade interpolation]')
        ax[1, 0].plot(check_axis,
                      check.imag,
                      color='gray',
                      label='Im[Pade interpolation]')
        ax[1, 0].set_xlabel(r'$\nu_n$')
        ax[1, 0].set_ylabel(self.input_data.data_type)
        ax[1, 0].legend()
        ax[1, 0].set_xlim(0., 1.05 * check_axis[-1])

        # ax[1, 1].plot(self.input_data.mats, (self.input_data.value - sol[0].backtransform).real,
        #               ls='--', label='real part')
        # ax[1, 1].plot(self.input_data.mats, (self.input_data.value - sol[0].backtransform).imag,
        #               label='imaginary part')
        # ax[1, 1].set_xlabel(r'$\nu_n$')
        # ax[1, 1].set_ylabel('data $-$ fit')
        # ax[1, 1].legend()
        plt.tight_layout()
        plt.show()
Ejemplo n.º 5
0
    def main_function(self):
        """Main function for the analytic continuation procedure.

        This function is called when the "Do it" button is clicked.
        It performs an analytical continuation for the present settings
        and shows a plot.
        """
        self.ana_cont_probl = cont.AnalyticContinuationProblem(
            im_axis=self.input_data.mats,
            im_data=self.input_data.value,
            re_axis=self.realgrid.grid,
            kernel_mode='freq_fermionic')
        model = np.ones_like(self.realgrid.grid)
        model /= np.trapz(model, self.realgrid.grid)

        preblur, bw = self.get_preblur()

        sol = self.ana_cont_probl.solve(method='maxent_svd',
                                        optimizer='newton',
                                        alpha_determination='chi2kink',
                                        model=model,
                                        stdev=self.input_data.error,
                                        interactive=False,
                                        alpha_start=1e14,
                                        alpha_end=1e-3,
                                        preblur=preblur,
                                        blur_width=bw)

        inp_str = 'atom {}, orb {}, spin {}, blur {}: '.format(
            self.input_data.atom, self.input_data.orbital,
            self.input_data.spin, bw)
        all_chis = np.isfinite(np.array([s.chi2 for s in sol[1]]))
        res_str = 'alpha_opt={:3.2f}, chi2(alpha_opt)={:3.2f}, min(chi2)={:3.2f}'.format(
            sol[0].alpha, sol[0].chi2, np.amin(all_chis))
        self.text_output.append(inp_str + res_str)
        alphas = [s.alpha for s in sol[1]]
        chis = [s.chi2 for s in sol[1]]

        self.output_data.update(self.realgrid.grid, sol[0].A_opt,
                                self.input_data)

        fig, ax = plt.subplots(ncols=2, nrows=2,
                               figsize=(11.75, 8.25))  # A4 paper size
        ax[0, 0].loglog(alphas, chis, marker='s', color='black')
        ax[0, 0].loglog(sol[0].alpha,
                        sol[0].chi2,
                        marker='*',
                        color='red',
                        markersize=15)
        ax[0, 0].set_xlabel(r'$\alpha$')
        ax[0, 0].set_ylabel(r'$\chi^2(\alpha)$')

        ax[1, 0].plot(self.realgrid.grid, sol[0].A_opt)
        ax[1, 0].set_xlabel(r'$\omega$')
        ax[1, 0].set_ylabel('spectrum')

        ax[0, 1].plot(self.input_data.mats,
                      self.input_data.value.real,
                      color='blue',
                      ls=':',
                      marker='x',
                      markersize=5,
                      label='Re[data]')
        ax[0, 1].plot(self.input_data.mats,
                      self.input_data.value.imag,
                      color='green',
                      ls=':',
                      marker='+',
                      markersize=5,
                      label='Im[data]')
        ax[0, 1].plot(self.input_data.mats,
                      sol[0].backtransform.real,
                      ls='--',
                      color='gray',
                      label='Re[fit]')
        ax[0, 1].plot(self.input_data.mats,
                      sol[0].backtransform.imag,
                      color='gray',
                      label='Im[fit]')
        ax[0, 1].set_xlabel(r'$\nu_n$')
        ax[0, 1].set_ylabel(self.input_data.data_type)
        ax[0, 1].legend()

        ax[1, 1].plot(self.input_data.mats,
                      (self.input_data.value - sol[0].backtransform).real,
                      ls='--',
                      label='real part')
        ax[1, 1].plot(self.input_data.mats,
                      (self.input_data.value - sol[0].backtransform).imag,
                      label='imaginary part')
        ax[1, 1].set_xlabel(r'$\nu_n$')
        ax[1, 1].set_ylabel('data $-$ fit')
        ax[1, 1].legend()
        plt.tight_layout()
        plt.show()
Ejemplo n.º 6
0
gf_bos /= norm

fig,ax = plt.subplots(ncols=2, nrows=1, figsize=(12,4))
ax[0].plot(w_real, spec_real, label='spectrum')
ax[0].plot(w_real, w_real*spec_real, label='response function')
ax[0].legend()
ax[0].set_xlabel('real frequency')
ax[0].set_title('Spectrum')
ax[1].plot(iw, gf_bos.real, marker='+')
ax[1].set_xlabel('Matsubara frequency')
ax[1].set_title('Matsubara Greensfunction')
plt.show()


w = np.linspace(0., 5., num=501, endpoint=True)
probl = cont.AnalyticContinuationProblem(im_axis=iw, re_axis=w,
                                        im_data=gf_bos, kernel_mode='freq_bosonic')

err = np.ones_like(iw) * noise_amplitude / norm
model = np.ones_like(w)
model /= np.trapz(model, w)
sol,_ = probl.solve(method='maxent_svd',
                    alpha_determination='chi2kink',
                    optimizer='newton',
                    stdev=err, model=model,
                    interactive=True)

np.save("A_opt_maxent_bosonic", sol.A_opt)
np.save("backtransform_maxent_bosonic", sol.backtransform)
np.save("chi2_maxent_bosonic", sol.chi2)

fig, ax = plt.subplots(ncols=3, nrows=1, figsize=(15,4))
Ejemplo n.º 7
0
kernel = 1. / (1j * iw[:, None] - w[None, :])
giw = np.trapz(kernel * spec[None, :], w, axis=1)

# add noise to the data
rng = np.random.RandomState(4713)
noise_ampl = 0.0001
giw += noise_ampl * (rng.randn(niw) + 1j * rng.randn(niw))

# error bars and default model for analytic continuation
err = np.ones_like(iw) * noise_ampl

# specify the analytic continuation problem
mats_ind = [0, 1, 2, 3, 4, 5, 6, 8,
            9]  # Matsubara indices of data points to use in Pade
probl = cont.AnalyticContinuationProblem(im_axis=iw[mats_ind],
                                         re_axis=w,
                                         im_data=giw[mats_ind],
                                         kernel_mode='freq_fermionic')

# solve the problem
sol = probl.solve(method='pade')
check_axis = np.linspace(0., 1.25 * iw[mats_ind[-1]], num=500)
check = probl.solver.check(im_axis_fine=check_axis)

np.save("A_opt_pade", sol.A_opt)
np.save("check_pade", check)

# plot the results
fig, ax = plt.subplots(ncols=2, nrows=1, figsize=(12, 5))
ax[0].plot(w, spec, color='gray', label='real spectrum')
ax[0].plot(w, sol.A_opt, color='red', label='analytic continuation')
ax[0].set_xlabel('real frequency')