'''
Created on 01-Apr-2021
@author: rishi_priyansh
'''
import random
import inspect
import traceback
from sympy import (symbols, Symbol)

from xv.km.util import latex_fraction, latex_atom
from .....helper import BaseHelper

from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate
        
class SchrodingerEquationManager(BaseManager):
    '''
    SchrodingerEquationManager
    Example:
        ke = SchrodingerEquationManager()
        ke.getRandomProblem()
        ke.getRandomProblem(problem_type = 0)
        ...
        
        ke.printProblem()
        ke.printAnswer()
        ke.printSolution()
        
        ke.printProblemTypes()
        
    '''    
    _OPR_TYPE_COUNT = 10
    
    def __new__(cls, *args, **kwargs):
        instance = super(SchrodingerEquationManager, cls).__new__(cls, *args, **kwargs)
        for base_class in SchrodingerEquationManager.__bases__:
            base_class.__init__(instance, *args, **kwargs)
        return instance
    
    def __init__(self, *args, **kwargs):
        pass
    
    def _init_problem_types(self, *args, **kwargs):
        self._append_problem_types([
            '_problem_6_49',
            '_problem_6_50',
            '_problem_6_51',
            '_problem_6_52',
            '_problem_6_53', 
            '_problem_6_54',
            '_problem_6_55',
            '_problem_6_56',
            '_problem_6_57',
            '_problem_6_58', 
                      
            ])
        pass
            
    def _problem_6_49(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        6.49. Calculate the de Broglie wavelengths of an electron, proton,
        and uranium atom, all having the same kinetic energy 100 eV.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        6.49 The kinetic energy is nonrelativistic in all three cases. Now
        $$
        \lambda=\frac{{2 \pi \hbar}}{{p}}=\frac{{2 \pi \hbar}}{{\sqrt{{2 m T}}}}
        $$
        using
        $$
        \begin{{gathered}}
        T=1.602 \times 10^{{-17}} \text {{ Joules, we get }} \\
        \lambda_{{e}}=122.6 \mathrm{{pm}} \\
        \lambda_{{p}}=2.86 \mathrm{{pm}} \\
        \lambda_{{U}}=\frac{{\lambda_{{p}}}}{{\sqrt{{238}}}}=0.185 \mathrm{{pm}}
        \end{{gathered}}
        $$
        
        (where we have used a mass number of 238 for the $U$ nucleus).
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_50(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        6.50. What amount of energy should be added to an electron to
        reduce its de Broglie wavelength from 100 to 50 pm?
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        
        $6.50$ From $\lambda=\frac{{2 \pi \hbar}}{{p}}=\frac{{2 \pi \hbar}}{{\sqrt{{2 m T}}}}$
        we find
        $$
        T=\frac{{4 \pi^{{2}} \hbar^{{2}}}}{{2 m \lambda^{{2}}}}=\frac{{2 \pi^{{2}} \hbar^{{2}}}}{{m \lambda^{{2}}}}
        $$
        Thus
        $$
        T_{{2}}-T_{{1}}=\frac{{2 \pi^{{2}} h^{{2}}}}{{m}}\left(\frac{{1}}{{\lambda_{{2}}^{{2}}}}-\frac{{1}}{{\lambda_{{1}}^{{2}}}}\right)
        $$
        Substitution gives $\Delta T=451 \mathrm{{eV}}=0.451 \mathrm{{keV}}$

        
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_51(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        6.51. A neutron with kinetic energy 7' = 25 eV strikes a stationary deuteron (heavy hydrogen nucleus). Find the de Broglie
        wavelengths of both particles in the frame of their centre of inertia.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        6.51 We shall use $M_{{0}} \approx  2 M_{{n}} .$ The CM is moving with velocity
        $$
        V=\frac{{\sqrt{{2 M_{{n}} T}}}}{{3 M_{{n}}}}=\sqrt{{\frac{{2 T}}{{9 M_{{n}}}}}}
        $$
        with respect to the Lab frame. In the CM frame the velocity of neutron is
        $$
        \mathbf{{v}}_{{n}}^{{\prime}}=\mathrm{{v}}_{{\mathrm{{n}}}}-V=\sqrt{{\frac{{2 T}}{{M_{{n}}}}}}-\sqrt{{\frac{{2 T}}{{9 M_{{n}}}}}}=\sqrt{{\frac{{2 T}}{{M_{{n}}}}}} \cdot \frac{{2}}{{3}}
        $$
        and
        $$
        \lambda_{{n}}^{{\prime}}=\frac{{2 \pi \hbar}}{{M_{{n}} \mathbf{{v}}_{{n}}^{{\prime}}}}=\frac{{3 \pi \hbar}}{{\sqrt{{2 M_{{n}} T}}}}
        $$
        Substitution gives $\lambda_{{n}}^{{\prime}}=8.6 \mathrm{{pm}}$
        Since the momenta are equal in the CM frame the de Broglie wavelengths will also be equal. If we do not assume $M_{{d}} \approx  2 M_{{n}}$ we shall get
        $$
        \lambda_{{n}}^{{\prime}}=\frac{{2 \pi \hbar\left(1+M_{{n}} / M_{{d}}\right)}}{{\sqrt{{2 M_{{n}} T}}}}
        $$

        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_52(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        6.52. Two identical non-relativistic particles move at right
        angles to each other, possessing de Broglie wavelengths $\lambda_1$ and $\lambda_2$,.
        Find the de Broglie wavelength of each particle in the frame of
        their centre of inertia. 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        6.52 If $\overrightarrow{{p_{{1}}}}, \overrightarrow{{p_{{2}}}}$ are the momenta of the two particles then their momenta in the CM frame will be $\pm\left(\overrightarrow{{p_{{1}}}}-\overrightarrow{{p_{{2}}}}\right) / 2$ as the particle are identical.
        Hence their de Broglie wavelength will be
        $$
        \begin{{aligned}}
        \lambda &=\frac{{2 \pi \hbar}}{{\frac{{1}}{{2}}\left|\overrightarrow{{p_{{1}}}}-\overrightarrow{{p_{{2}}}}\right|}}=\frac{{4 \pi \hbar}}{{\sqrt{{p_{{1}}^{{2}}+p_{{2}}^{{2}}}}}}\left(\text {{ because }} \overrightarrow{{p_{{1}}}} \perp \overrightarrow{{p_{{2}}}}\right) \\
        &=\frac{{2}}{{\sqrt{{\frac{{1}}{{\frac{{1}}{{\lambda_{{1}}^{{2}}}}+\frac{{1}}{{\lambda_{{2}}^{{2}}}}}}}}}}=\frac{{2 \lambda_{{1}} \lambda_{{2}}}}{{\sqrt{{\lambda_{{1}}^{{2}}+\lambda_{{2}}^{{2}}}}}}
        \end{{aligned}}
        $$
        

        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_53(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        6.53. Find the de Broglie wavelength of hydrogen molecules,
        which corresponds to their most probable velocity at room temperature.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        6.53 In thermodynamic equilibrium, Maxwell's velocity distribution law holds :
        $$
        d N(\mathrm{{v}})=\Phi(\mathrm{{v}}) d \mathrm{{v}}=A \mathrm{{v}}^{{2}} e^{{-\mathrm{{mv}}^{{2}} / 2 \mathrm{{k}} \mathrm{{T}}}} d \mathrm{{v}}
        $$
        $\Phi(v)$ is maximum when
        $$
        \Phi^{{\prime}}(v)=\Phi(v)\left[\frac{{2}}{{v}}-\frac{{m v}}{{k T}}\right]=0
        $$
        The difines the most probable velocity.
        $$
        \mathrm{{v}}_{{\mathrm{{pr}}}}=\sqrt{{\frac{{2 k T}}{{m}}}}
        $$
        The de Broglie wavelength of $\mathrm{{H}}$ molecules with the most probable velocity is
        $$
        \lambda=\frac{{2 \pi \hbar}}{{m \mathrm{{v}}_{{p r}}}}=\frac{{2 \pi \hbar}}{{\sqrt{{2 m k T}}}}
        $$
        Substituting the appropriate value especially
        $$
        \begin{{gathered}}
        m=m_{{H_{{2}}}}=2 m_{{H}}, T=300 \mathrm{{~K}}, \text {{ we get }} \\
        \lambda=126 \mathrm{{pm}}
        \end{{gathered}}
        $$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_54(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        6.54. Calculate the most probable de Broglie wavelength of
        hydrogen molecules being in thermodynamic equilibrium at room
        temperature.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        6.54 To find the most probable de Broglie wavelength of a gas in thermodynamic equilibrium we determine the distribution is $\lambda$ corresponding to Maxwellian velocity distribution. It is given by
        $$
        \psi(\lambda) d \lambda=-\Phi(\mathrm{{v}}) d \mathrm{{v}}
        $$
        (where - sign takes account of the fact that $\lambda$ decreaes as $v$ increases). Now
        $$
        \begin{{aligned}}
        \lambda &=\frac{{2 \pi \hbar}}{{m \mathrm{{v}}}} \text {{ or }} \mathrm{{v}}=\frac{{2 \pi \hbar}}{{m \lambda}} \\
        d \mathrm{{v}} &=-\frac{{2 \pi \hbar}}{{m \lambda^{{2}}}} d \lambda \\
        \text{{Thus, }} \quad\Psi(\lambda) &=+A \mathrm{{v}}^{{2}} e^{{-m \mathrm{{v}}^{{2}} / 2 k T}}\left(-\frac{{d \mathrm{{v}}}}{{d \lambda}}\right)
        \end{{aligned}}
        $$
        
        $$
        \begin{{aligned}}
        &=A\left(\frac{{2 \pi \hbar}}{{m \lambda}}\right)^{{2}}\left(\frac{{2 \pi \hbar}}{{m \lambda^{{2}}}}\right) e^{{-\frac{{m}}{{2 k T}} \cdot\left(\frac{{2 \pi \hbar}}{{m \lambda}}\right)^{{2}}}} \\
        &=\text {{ Const }} \cdot \lambda^{{-4}} e^{{-a / \lambda^{{2}}}} \\
        &\text{{where, }} \quad  a=\frac{{2 \pi^{{2}} h^{{2}}}}{{m k T}}
        \end{{aligned}}
        $$
        
        This is maximum when
        $$
        \begin{{gathered}}
        \psi^{{\prime}}(\lambda)=0=\psi(\lambda)\left[\frac{{-4}}{{\lambda}}+\frac{{2 a}}{{\lambda^{{3}}}}\right] \\
        \text{{or, }}\quad\lambda_{{p r}}=\sqrt{{a / 2}}=\pi \hbar / \sqrt{{m k T}}
        \end{{gathered}}
        $$
        Using the result of the previous problem it is
        $$
        \lambda_{{p r}}=\frac{{126}}{{\sqrt{{2}}}} \mathrm{{pm}}=89.1 \mathrm{{pm}}
        $$


        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_55(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        6.55. Derive the expression for a de Broglie wavelength $lambda$ of a relativistic particle moving with kinetic energy $T$. At what values of $T$
        does the error in determining $lambda$ using the non-relativistic formula
        not exceed 1% for an electron and a proton? 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        6.55 For a relativistic particle
        $$
        T+m c^{{2}}=\text {{ total energy }}=\sqrt{{c^{{2}} p^{{2}}+m^{{2}} c^{{4}}}}
        $$
        Squaring
        $$
        \begin{{aligned}}
        &\sqrt{{T\left(T+2 m c^{{2}}\right)}}=c p \\
        \end{{aligned}}
        $$
        Hence
        $$
        \begin{{aligned}}
        &\lambda=\frac{{2 \pi \hbar c}}{{\sqrt{{T\left(T+2 m c^{{2}}\right)}}}} \\
        &=\frac{{2 \pi \hbar}}{{\sqrt{{2 m T\left(1+\frac{{T}}{{2 m c^{{2}}}}\right)}}}}
        \end{{aligned}}
        $$
        If we use nonrelativistic formula,
        $$
        \begin{{aligned}}
        &\lambda_{{N R}}=\frac{{2 \pi \hbar}}{{\sqrt{{2 m T}}}}
        &\frac{{\Delta \lambda}}{{\lambda}}=\frac{{\lambda_{{N R}}-\lambda}}{{\lambda_{{N R}}}}=\frac{{T}}{{4 m c^{{2}}}}
        \end{{aligned}}
        $$
        so
        $$
        \left( \text{{If }}\space T / 2 m c^{{2}} \ll 1, \text{{we can write }} \left(1+\frac{{T}}{{2 m c^{{2}}}}\right)^{{-1 / 2}} \quad =1-\frac{{T}}{{4 m c^{{2}}}}\right)
        $$
        Thus $T \leq \frac{{4 m c^{{2}} \Delta \lambda}}{{\lambda}}$ if the error is less than $\Delta \lambda$
        For electron the error is not more than $1 \%$ if
        $$
        \begin{{gathered}}
        T \leq 4 \times 0 \cdot 511 \times 01 \mathrm{{MeV}} \\
        \leq 20 \cdot 4 \mathrm{{keV}}
        \end{{gathered}}
        $$
        For a proton, the error is not more than $1 \%$ if
        $$
        \begin{{gathered}}
        T \leq 4 \times 938 \times 0.01 \mathrm{{MeV}} \\
        \text{{i.e. }}T \leq 37.5 \mathrm{{MeV}}
        \end{{gathered}}
        $$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_56(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        6.56. At what value of kinetic energy is the de Broglie wavelength
        of an electron equal to its Compton wavelength?
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        6.56 The de Broglie wavelength is
        $$
        \lambda_{{d B}}=\frac{{\frac{{2 \pi \hbar}}{{m_{{0}} v}}}}{{\sqrt{{1-v^{{2}} / c^{{2}}}}}}=\frac{{2 \pi \hbar}}{{m_{{0}} v}} \sqrt{{1-v^{{2}} / c^{{2}}}}
        $$
        and the Compton wavelength is
        $$
        \lambda_{{c}}=\frac{{2 \pi \hbar}}{{m_{{0}} c}}
        $$
        The two are equal if $\beta=\sqrt{{1-\beta^{{2}}}}$, where $\beta=\frac{{v}}{{c}}$
        or
        $$
        \beta=\frac{{1}}{{\sqrt{{2}}}}
        $$
        The corresponding kinetic energy is
        $$
        T=\frac{{m_{{0}} c^{{2}}}}{{\sqrt{{1-\beta^{{2}}}}}}-m_{{0}} c^{{2}}=(\sqrt{{2}}-1) m_{{0}} c^{{2}}
        $$
        Here $m_{{0}}$ is th rest mass of the particle (here an electron).

        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_57(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        6.57 Find the de Broglie wavelength of relativistic electrons
        reaching the anticathode of an X-ray tube if the short wavelength
        limit of the continuous X-ray spectrum is equal to $\lambda_{{sh}}$ = 10.0 pm?
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        6.57 For relativistic electrons, the formula for the short wavelength limit of $X$ - rays will be
        $$
        \frac{{2 \pi \hbar c}}{{\lambda_{{s h}}}}=m_{{0}} c^{{2}}\left(\frac{{1}}{{\sqrt{{1-\beta^{{2}}}}}}-1\right)-c \sqrt{{p^{{2}}+m^{{2}} c^{{2}}}}-m c^{{2}}
        $$
        or
        $$
        \left(\frac{{2 \pi h}}{{\lambda_{{s h}}}}+m c\right)^{{2}}=p^{{2}}+m^{{2}} c^{{2}}
        $$
        or
        $$
        \left(\frac{{2 \pi \hbar}}{{\lambda_{{s h}}}}\right)\left(\frac{{2 \pi \hbar}}{{\lambda_{{s h}}}}+2 m c\right)=p^{{2}}
        $$
        or
        $$
        p=\frac{{2 \pi \hbar}}{{\lambda_{{s h}}}} \sqrt{{1+\frac{{m c \lambda_{{s h}}}}{{\pi \hbar}}}}
        $$
        Hence
        $$
        \lambda_{{d B}}=\lambda_{{s h}} / \sqrt{{1+\frac{{m c \lambda_{{s h}}}}{{\pi \hbar}}}}=3.29 \mathrm{{pm}}
        $$

        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_6_58(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = fr"""
        6.58. A parallel stream of monoenergetic electrons falls normally
        on a diaphragm with narrow square slit of width b = 1.0 $\mu$m.
        Find the velocity of the electrons if the width of the central diffraction maximum formed on a screen located at a distance l = 50 cm
        from the slit is equal to $\Delta x$ = 0.36 mm. 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = fr"""
        6.58 The first minimum in a Fraunhofer diffraction is given by ( $b$ is the width of the slit) $b \sin \theta=\lambda$
        Here
        $$
        \sin \theta=\frac{{\Delta x / 2}}{{\sqrt{{1^{{2}}+\left(\frac{{\Delta x}}{{2}}\right)^{{2}}}}}} \approx \frac{{\Delta x}}{{2 l}}
        $$
        Thus
        $$
        ' \lambda=\frac{{b \Delta x}}{{2 l}}=\frac{{2 \pi \hbar}}{{m \mathrm{{v}}}}
        $$
        so
        $$
        v=\frac{{4 \pi \hbar l}}{{m b \Delta x}}=2.02 \times 10^{{6}} \mathrm{{~m}} / \mathrm{{s}}
        $$

        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    

    #https://courses.lumenlearning.com/austincc-physics2/chapter/31-4-nuclear-decay-and-conservation-laws/
    
    