'''
Created on 12-Jul-2021

@author: rishi_priyansh
'''
import inspect
import traceback
import random

from sympy import (latex, Symbol, UnevaluatedExpr as ueval,
                   )
import sympy as sp
from xv.km.util import SympyUnitSystem

# _lcr_circuit_helper
# LcrCircuitHelper

from ._base_circuit_helper import BaseCircuitHelper

class LcrCircuitHelper(BaseCircuitHelper):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        pass
    
    def get_problem_LCR_circuit(self):
   
        
        problem_s = f"""
        Write LCR circuit equation.
        """
        
        soln_s = rf"""
        $\displaystyle
        V_{{\text {{peak }}}} \sin (\omega * t)
        = L \frac{{d I}}{{d t}}
        + R I(t) + 
        \frac{{1}}{{C}} * \int I(t) d t
        $
        
        $\displaystyle
        L \frac{{d^2 I}}{{d t^2}}
        + R \frac{{d I}}{{d t}}
        + \frac{{1}}{{C}} I(t)
        = V_{{p e a k}} \omega \cos (\omega * t)
        $
        
        Where;
        - I is the circuit current,
        - $\mathrm{{V}}$ is the Voltage output from a solar panel system 
        which will be assumed to be constant,
        - $\mathrm{{L}}$ is the inductance of the inductor in the circuit 
        measured in Henrys, $\mathrm{{H}}$
        - $\mathrm{{R}}$ is the resistance of the resistor in the circuit 
        measured in ohms, $\Omega$, and
        - $\omega$ is the angular frequency of the AC signal 
        which is also expressed as $\omega=2 \pi f$
        
        Rearranging it,

        $\displaystyle
        d^2 I / d t^2
        = -R / L * d I / d t 
        - 1 /(C * L) * I(t)
        + \frac{{ V_{{p e a k}} \omega }}{{L}} \cos (\omega * t)
        $
        
        $\displaystyle
        x^{{\prime \prime}} 
        + 2 c x^{{\prime}}
        + \omega_o^2 x
        = A \cos (\omega t)
        $
        
        where 
        
        $\displaystyle
        \omega^2
        = \frac{{1}}{{C L}}
        $
         
        To solve for the homogeneous equation, the $\mathrm{{ODE}}(1)$ must have no driving force, therefore

        $\displaystyle
        I^{{\prime \prime}}+R / L * I^{{\prime}}+1 / C L * I(t)=0
        $
        
        the characteristic roots are found;
        
        $\displaystyle
        \lambda=-\frac{{R}}{{2 L}} \pm \frac{{\sqrt{{\left( \frac{{R}}{{L}} \right)^2-\frac{{4}}{{C L}}}}}}{{2}}
        $
        
        which equate to $\lambda_1=-5.6694 \times 10^3$ and $\lambda_2=-7.6917 \times 10^7$. The homogeneous part to the general solution is now known as
        
        $\displaystyle
        I_h=C_1 e^{{\lambda_1 * t}}+C_2 e^{{\lambda_2 * t}}
        $
        
        Where $C_1$ and $C_2$ are constants.
        
        However, before solving for $C_1$ and $C_2$, a particular solution for $I(t)=I_h+$ $I_p$ must be found. The complex method laid out on page 167 of "Differential Equations With Boundary Value Problems" [5] will be used in finding the particular solution $I_p$ where
        
        $\displaystyle
        I^{{\prime \prime}}+R / L * I^{{\prime}}+1 / C L * I(t)= V_{{p e a k}} \omega / L * \cos (\omega t)
        $

        will be put in the complex form of
        
        $\displaystyle
        z^{{\prime \prime}}+R / L * z^{{\prime}}+1 / C L * z(t)= V_{{p e a k}} \omega / L * e^{{\omega i t}}
        $
        
        Where 
        $\displaystyle
        i$ is the imaginary number $\sqrt{{-1}}
        $.
        
        In order to solve for $z(t)$, a guess must be made first. Let $z(t)=a e^{{\omega i t}}$ and substitute that guess in for $z(t)$. After taking a few derivatives and some algebra,
        
        $\displaystyle
        a=\frac{{V_{{p e a k}} \left(\frac{{1}}{{C \omega}}-\omega L-i R\right)}}{{\left(\frac{{1}}{{C \omega}}-\omega L\right)^2+R^2}}
        $


        
        
        In the complex method of solving particular solutions, it is known that the final particular solution to this initial problem will be equal to the real part of the $z(t)$ solution. Therefore,

        $\displaystyle
        I_p 
        =\operatorname{{Re}}(z(t))
        = -8.4229 \times 10^{{-8}} \sin (\omega t)-0.6 \cos (\omega t)
        $
        
        $\displaystyle
        The final step to solving for the explicit solution to equation1 is to let $
        I(t)=I_h+I_p$ and solve for the initial conditions $
        C_1$ and $C_2$. 
        
        By doing so, the closed form solution to the second order ODE is equated as,
        
        $\displaystyle
        \begin{{gathered}}
        I(t)
        = C_1 e^{{\lambda_1 t}}
        + C_2 e^{{\lambda_2 t}}
        + A * \cos (\omega t)+B * \sin (\omega t) \\
        
        C_1=0.0051 \\
        
        C_2=-0.0051 \\
        
        \lambda_1=-5.6694 \times 10^3 \\
        
        \lambda_2=-7.6917 \times 10^7 \\
        
        A=-8.4229 \times 10^{{-8}} \\
        
        B=0.6000 \\
        \omega=2 \pi 105.1 \times 10^3
        \end{{gathered}}
        $
        
                               
        """
        
        answer_s = f"""
        """
        
        result = {
            'problem_s': problem_s.strip(),
            'soln_s': soln_s.strip(),
            'answer_s': answer_s.strip(),
            }
        return result
        pass



    pass
#class LcrCircuitHelper:

    