Commit 68da3443 authored by Zolnierczuk, Piotr's avatar Zolnierczuk, Piotr
Browse files

simulator bugfixes

parent f9fe4c36
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -60,6 +60,8 @@ class SimpleSimulator():
    def f_qt(self, tau):
        "return F(Q,t)"
        updn = self.coherent - 1/3*self.incoherent
        if updn == 0:
            return np.zeros_like(tau)
        return fqt(tau, self.coherent, self.tau_coh, self.incoherent, self.tau_inc)/updn

    def f_coh(self, tau):
+1 −1
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ PySEN revision module
import sys
__version__  = "2.1"
__release__  = "b4"
__date__     = "Apr 1, 2025"
__date__     = "Apr 2, 2025"

def version(full=False):
    "get pysen version number"
+42 −52
Original line number Diff line number Diff line
@@ -121,37 +121,31 @@ class EchoSimWidget(Ui_EchoSimWidget):

    def _init_canvas(self):
        "initialize canvas"
        #lbin  = np.linspace(self.lmin,self.lmax,self.ntbin+1)*ANGSTROM # neutron wavelength bins
        #djmax = 3*pi/(self.lmax*ANGSTROM)/OMEGA_N # field integral range (-3pi,+3pi)
        #self.lave  = (lbin[1:]+lbin[:-1])/2 # average wavelength
        #self.dlam  = (lbin[1:]-lbin[:-1])/2 # delta-lambda
        #self.flux  = (self.lave[0]/self.lave)**2
        #self.dj    = np.linspace(-djmax, djmax, 201)  # for plotting dj points
        #self.dj2   = np.linspace(-djmax, djmax,  11)  # for plotting dj points
        #self.xt    = xt

        xt = self.taus

        ax = self._ax[0]
        tau = self.taus
        self.lines = {}
        self.lines['sqt'], = ax.plot(xt, np.zeros_like(xt), 'r-',  lw=2, label='I(Q,t)')
        self.lines['coh'], = ax.plot(xt, np.zeros_like(xt), 'g--', lw=1, label=r'$f_{coh}$')
        self.lines['inc'], = ax.plot(xt, np.zeros_like(xt), 'b-.', lw=1, label=r'$f_{inc}$')
        # F(Q,t)
        ax = self._ax[0]
        self.lines['sqt'], = ax.plot(tau, np.zeros_like(tau), 'r-',  lw=2, label='I(Q,t)')
        self.lines['coh'], = ax.plot(tau, np.zeros_like(tau), 'g--', lw=1, label=r'$f_{coh}$')
        self.lines['inc'], = ax.plot(tau, np.zeros_like(tau), 'b-.', lw=1, label=r'$f_{inc}$')
        self.lines['tau']  = ax.axvline(0,          lw=1.0, ls='-', color='k')
        self.lines['1/e']  = ax.axhline(exp(-1), lw=0.5, ls='--', color='k')
        #
        ax.set_xscale('log')
        ax.set_xlim(min(xt), max(xt))
        ax.set_xlim(min(tau), max(tau))
        ax.set_xlabel(r'$\tau$ [ns]')
        ax.set_ylabel(r'$I(Q,\tau)$')
        ax.legend(loc=0)
        ax.grid(True) #, which='both')

        # echo
        ax = self._ax[1]
        self.lines['echo'], = ax.plot(self.esim.dj/MICRO, np.zeros_like(self.esim.dj),'g-', lw=3)
        self.lines['epts'], = ax.plot(self.esim.dj/MICRO, np.zeros_like(self.esim.dj),'ro', ms=5)
        self.lines['up']    = ax.axhline(2.0, lw=1, ls='--', color='red')
        self.lines['dn']    = ax.axhline(1.0, lw=1, ls='--', color='blue')
        self.lines['ave']   = ax.axhline(1.5, lw=1, ls='--',  color='k')
        #
        ax.set_xlabel(r'$\Delta J$ [$\mu Tm$]')
        ax.yaxis.tick_right()
        ax.grid(True)
@@ -168,57 +162,53 @@ class EchoSimWidget(Ui_EchoSimWidget):
        tinc  = 10**(self.hSliderTauInc.value()/1000.0)
        techo = 10**(self.hSliderTauEcho.value()/1000.0)

        # draw echo
        ax = self._ax[1]
        try:
            self.esim.set_intensities(coherent=coh, incoherent=inc, background=bgr,
                                     tau_coh=tcoh, tau_inc=tinc)
        except ArithmeticError as exc:
            print(exc)
            ax.set_title(f'!!! {exc} !!!')
            ax.figure.canvas.draw()
            return
            # check inputs
            if inc<0 or coh<0:
            ax.set_title('!!! Coherent or Incoherent cannot be negative !!!')
            ax.figure.canvas.draw()
            return
                raise RuntimeError("coherent or incoherent cannot be negative")
            dn  = self.esim.get('up', -1) # 2/3*(inc+bgr)
            up  = self.esim.get('dn', -1) # coh+1/3*(inc+bgr)
            if up<0 or dn<0:
            ax.set_title('!!! Up and Down counts must be positive')
            ax.figure.canvas.draw()
            return
        if (up-dn)==0:
            ax.set_title('!!! Invalid input')
                raise RuntimeError("up or dn cannot be negative")
            #if (up-dn)==0:
            #    raise RuntimeError("up and dn cannot be equal")
        except (RuntimeError, ArithmeticError) as exc:
            ax.set_title(f'!!! {exc} !!!')
            ax.figure.canvas.draw()
            return
        fr = self.esim.get('fr', -1) # up/dn
        ave= self.esim.get('ave', -1) # (up+dn)/2
        ax.set_title(rf'FR={fr:.3g} (U={up:.0f}/D={dn:.0f})')


        dj, yecho = self.esim.echo(techo)
        self.lines['echo'].set_data(dj/MICRO, yecho)
        self.lines['epts'].set_data(dj[::NFINE]/MICRO, yecho[::NFINE])

        self.lines['up'].set_data(dj/MICRO,up*np.ones_like(dj))
        self.lines['dn'].set_data(dj/MICRO,dn*np.ones_like(dj))
        self.lines['ave'].set_data(dj/MICRO,ave*np.ones_like(dj))

        try:
            bot = min(np.amin(yecho), up, dn)*0.67
            top = max(np.amax(yecho), up, dn)*1.2
            ax.set_ylim(bottom=bot, top=top)
        except ValueError:
            ax.set_ylim(bottom=None, top=None)

        xt   = self.taus
        ysqt = self.esim.f_qt(xt)
        ycoh = self.esim.f_coh(xt)
        yinc = self.esim.f_inc(xt)
        # draw F(Q,t)
        ax = self._ax[0]
        taus = self.taus
        ysqt = self.esim.f_qt(taus)
        ycoh = self.esim.f_coh(taus)
        yinc = self.esim.f_inc(taus)

        self.lines['sqt'].set_data(xt, ysqt)
        self.lines['coh'].set_data(xt, ycoh)
        self.lines['inc'].set_data(xt, yinc)
        self.lines['sqt'].set_data(taus, ysqt)
        self.lines['coh'].set_data(taus, ycoh)
        self.lines['inc'].set_data(taus, yinc)

        ax = self._ax[0]
        ymin, ymax = -abs(np.amin([ysqt,ycoh,yinc])), np.amax([ysqt,ycoh,yinc])
        self.lines['tau'].set_data([techo,techo],[ymin,ymax])
        try:
@@ -229,12 +219,12 @@ class EchoSimWidget(Ui_EchoSimWidget):

    def reset(self):
        "reset simulator values"
        self.dblSpinBoxCoh.setValue(104)
        self.dblSpinBoxInc.setValue(12)
        self.dblSpinBoxBgr.setValue(0)
        self.hSliderTauCoh.setValue( 2000)
        self.hSliderTauInc.setValue(-3000)
        self.hSliderTauEcho.setValue(1)
        self.dblSpinBoxCoh.setValue(104.0)
        self.dblSpinBoxInc.setValue(12.0)
        self.dblSpinBoxBgr.setValue(0.0)
        self.inpTauCoh.setText("100.0")
        self.inpTauInc.setText("0.001")
        self.inpTauEcho.setText("1.0")
        #
        self.update_canvas()
#EOF