'''
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

import os

from xv.km.managers import BaseManager
from xv.km.kelements import KProblemTemplate
        
class LiquidsManager(BaseManager):
    '''
    LiquidsManager
    Example:
        ke = LiquidsManager()
        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(LiquidsManager, cls).__new__(cls, *args, **kwargs)
        for base_class in LiquidsManager.__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_2_160',
            '_problem_2_161',
            '_problem_2_162',
            '_problem_2_163',
            '_problem_2_164',
            ])
        pass

    def _problem_2_160(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        problem_s = f"""
        Find the capillary pressure
        (a) in mercury droplets of diameter d = 1.5 $\\mu$m;
        (b) inside a soap bubble of diameter d = 3.0 mm if the surface tension of the soap water solution is a = 45 mN/m. 

        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        (a)
        $\\begin{{aligned}}
        \\Delta p &=\\alpha\\left(\\frac{{1}}{{d / 2}}+\\frac{{1}}{{d / 2}}\\right)=\\frac{{4 \\alpha}}{{d}} \\\\
        &=\\frac{{4 \\times 490 \\times 10^{{-3}}}}{{1 \\cdot 5 \\times 10^{{-6}}}} \\frac{{\\mathrm{{N}}}}{{\\mathrm{{m}}^{{2}}}}=1.307 \\times 10^{{6}} \\frac{{\\mathrm{{N}}}}{{\\mathrm{{m}}^{{2}}}}=13 \\text {{ atmosphere }}
        \\end{{aligned}}$
        (b) The soap bubble has two surfaces
        so
        $\\begin{{gathered}}
        \\Delta p=2 \\alpha\\left(\\frac{{1}}{{d / 2}}+\\frac{{1}}{{d / 2}}\\right)=\\frac{{8 \\alpha}}{{d}} \\\\
        =\\frac{{8 \\times 45}}{{3 \\times 10^{{-3}}}} \\times 10^{{-3}}=1.2 \\times 10^{{-3}} \\text {{ atomsphere }}
        \\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_2_161(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        In the bottom of a vessel with mercury there is a round hole of diameter d = 70 $\\mu$m. At what maximum thickness of the mercury layer will the liquid still not flow out through this hole? 
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        The pressure just inside the hole will be less than the outside pressure by $4 \\alpha / d$. This can support a height $h$ of $H g$ where
        $\\begin{{gathered}}
        \\rho g h=\\frac{{4 \\alpha}}{{d}} \\text {{ or }} h=\\frac{{4 \\alpha}}{{\\rho g d}} \\\\
        =\\frac{{4 \\times 490 \\times 10^{{-3}}}}{{13 \\cdot 6 \\times 10^{{3}} \\times 9.8 \\times 70 \\times 10^{{-6}}}}=\\frac{{200}}{{13 \\cdot 6 \\times 70}} \\approx \\cdot 21 \\mathrm{{~m}} \\text {{ of }} \\mathrm{{Hg}}
        \\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_2_162(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        problem_s = f"""
        A vessel filled with air under pressure $p_0$ contains a soap bubble of diameter d. The air pressure having been reduced isothermally n-fold, the bubble diameter increased $\\eta-$fold. Find the surface tension of the soap water solution.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html', add_html_line_break = False)
        
        soln_s = f"""
        By Boyle's law
        $\\left(p_{{0}}+\\frac{{8 \\alpha}}{{d}}\\right) \\frac{{4 \\pi}}{{3}}\\left(\\frac{{d}}{{2}}\\right)^{{3}}=\\left(\\frac{{p_{{0}}}}{{n}}+\\frac{{8 \\alpha}}{{\\eta d}}\\right) \\frac{{4 \\pi}}{{3}}\\left(\\frac{{\\eta d}}{{2}}\\right)^{{3}}$
        or $\\qquad p_{{0}}\\left(1-\\frac{{\\eta^{{3}}}}{{n}}\\right)=\\frac{{8 \\alpha}}{{d}}\\left(\\eta^{{2}}-1\\right) $
        Thus $\\qquad \\alpha=\\frac{{1}}{{8}} p_{{0}} d\\left(1-\\frac{{\\eta^{{3}}}}{{n}}\\right)\\left(\\eta^{{2}}-1\\right)$

        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_2_163(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        problem_s = f"""
        Find the pressure in an air bubble of diameter d = 4.0 $\\mu$m, located in water at a depth h = 5.0 m. The atmospheric pressure has the standard value $p_0$ .
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        The pressure has terms due to hydrostatic pressure and capillarity and they add
        $\\begin{{gathered}}
        p=p_{{0}}+\\rho g h+\\frac{{4 \\alpha}}{{d}} \\\\
        =\\left(1+\\frac{{5 \\times 9.8 \\times 10^{{3}}}}{{10^{{5}}}}+\\frac{{4 \\times \\cdot 73 \\times 10^{{-3}}}}{{4 \\times 10^{{-6}}}} \\times 10^{{-5}}\\right) \\text {{ atoms }}=2.22 \\text {{ atom }}
        \\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_2_164(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')
        
        
        problem_s = f"""
        The diameter of a gas bubble formed at the bottom of a pond is d = 4.0 $\\mu$m. When the bubble rises to the surface its diameter increases n = 1.1 times. Find how deep is the pond at that spot. The atmospheric pressure is standard, the gas expansion is assumed to be isothermal.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        By Boyle's law
        $\\left(p_{{0}}+h g \\rho+\\frac{{4 \\alpha}}{{d}}\\right) \\frac{{\\pi}}{{6}} d^{{3}}=\\left(p_{{0}}+\\frac{{4 \\alpha}}{{n d}}\\right) \\frac{{\\pi}}{{6}} n^{{3}} d^{{3}}$
        or $\\left[h g \\rho-p_{{0}}\\left(n^{{3}}-1\\right)\\right]=\\frac{{4 \\alpha}}{{d}}\\left(n^{{2}}-1\\right)$
        or $h=\\left[p_{{0}}\\left(n^{{3}}-1\\right)+\\frac{{4 \\alpha}}{{d}}\\left(n^{{2}}-1\\right)\\right] / g \\rho=4.98 \\text{{ meter of water}}$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate

    def _problem_2_165(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        Find the difference in height of mercury columns in two communicating vertical capillaries whose diameters are $d_1=$ $=0.50 \\mathrm{{~mm}}$ and $d_2=1.00 \\mathrm{{~mm}}$, if the contact angle $\\theta=138^{{\\circ}}$.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        Clearly
        $\\begin{{gathered}}
        \\Delta h \\rho g=4 \\alpha|\\cos \\theta|\\left(\\frac{{1}}{{d_1}}-\\frac{{1}}{{d_2}}\\right) \\\\
        \\Delta h=\\frac{{4 \\alpha|\\cos \\theta|\\left(d_2-d_1\\right)}}{{d_1 d_2 \\rho g}}=11 \\mathrm{{~mm}}
        \\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_2_166(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        A vertical capillary with inside diameter $0.50 \\mathrm{{~mm}}$ is submerged into water so that the length of its part protruding over the water surface is equal to $h=25 \\mathrm{{~mm}}$. Find the curvature radius of the meniscus.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        In a capillary with diameter $d=0.5 \\mathrm{{~mm}}$ water will rise to a height
        $\\begin{{gathered}}
        \\frac{{2 \\alpha}}{{\\rho g r}}=\\frac{{4 \\alpha}}{{\\rho g d}} \\\\
        =\\frac{{4 \\times 73 \\times 10^{{-3}}}}{{10^3 \\times 9.8 \\times 0.5 \\times 10^{{-3}}}}=59.6 \\mathrm{{~mm}}
        \\end{{gathered}}$
        Since this is greater than the height $(=25 \\mathrm{{~mm}})$ of the tube, a meniscus of radius $R$ will be formed at the top of the tube, where 
        $\\displaystyle R=\\frac{{2 \\alpha}}{{\\rho g h}}=\\frac{{2 \\times 73 \\times 10^{{-3}}}}{{10^3 \\times 9.8 \\times 25 \\times 10^{{-3}}}} \\approx 0.6 \\mathrm{{~mm}}$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_2_167(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        A glass capillary of length $l=110 \\mathrm{{~mm}}$ and inside diameter $d=20 \\mu \\mathrm{{m}}$ is submerged vertically into water. The upper end of the capillary is sealed. The outside pressure is standard. To what length $x$ has the capillary to be submorged to make the water levels inside and outside the capillary coincide?
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        Initially the pressure of air in the cppillary is $p_0$ and it's length is $l$. When submerged under water, the pressure of air in the portion above water must be $p_0+4 \\frac{{\\alpha}}{{d}}$, since the level of water inside the capillary is the same as the level outside. Thus by Boyle's law
        $\\displaystyle \\left(p_0+\\frac{{4 \\alpha}}{{d}}\\right)(l-x)=p_0 l$
        or $\\displaystyle \\frac{{4 \\alpha}}{{d}}(l-x)=p_0 x \\quad \\text {{ or }} x=\\frac{{l}}{{1+\\frac{{p_0 d}}{{4 \\alpha}}}}$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _problem_2_168(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        When a vertical capillary of length $l$ with the sealed upper end was brought in contact with the surface of a liquid, the level of this liquid rose to the height $h$. The liquid density is $\rho$, the inside diameter of the capillary is $d$, the contact angle is $\theta$, the atmospheric pressure is $p_0$. Find the surface tension of the liquid.
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        We have by Boyle's law
        $\\displaystyle \\left(p_0-\\rho g h+\\frac{{4 \\alpha \\cos \\theta}}{{d}}\\right)(l-h)=p_0 l$
        or, $\\displaystyle \\frac{{4 \\alpha \\cos \\theta}}{{d}}=\\rho g h+\\frac{{p_0 h}}{{l-h}}$
        Hence, $\\displaystyle \\alpha=\\left(\\rho g h+\\frac{{p_0 h}}{{l-h}}\\right) \\frac{{d}}{{4 \\cos \\theta}}$

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

        problem_s = f"""
        A glass rod of diameter $d_1=1.5 \\mathrm{{~mm}}$ is inserted symmetrically into a glass capillary with inside diameter $d_2=2.0 \\mathrm{{~mm}}$. Then the whole arrangement is vertically oriented and brought in contact with the surface of water. To what height will the water rise in the capillary?
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        Suppose the liquid rises to a height $h$. Then the total energy of the liquid in the capillary is
        $\\displaystyle E(h)=\\frac{{\\pi}}{{4}}\\left(d_2^2-d_1^2\\right) h \\times \\rho g \\times \\frac{{h}}{{2}}-\\pi\\left(d_2-d_1\\right) \\alpha h$
        Minimising $E$ we get
        $\\displaystyle h=\\frac{{4 \\alpha}}{{\\rho g\\left(d_2-d_1\\right)}}=6 \\mathrm{{~cm}}$
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _sample(self, *args, **kwargs):
        problemTemplate = KProblemTemplate()
        problemTemplate.problem_type = kwargs.get('problem_type')

        problem_s = f"""
        """
        problemTemplate.add_problem(problem_s.strip(), 'text/html')
        
        soln_s = f"""
        """
        problemTemplate.add_solution(soln_s.strip(), 'text/html')
        
        answer_s = f"""
        """
        problemTemplate.add_answer(answer_s.strip(), 'text/html')
        
        return problemTemplate
    
    def _init_file_path(self, file_name):
        import pathlib
        from xv.util import locatePath
        rootFolder = pathlib.Path(__file__).parent.absolute()
        file_path = locatePath(file_name, 
                               parent_depth = 6,
                               rootFolder = rootFolder,
                               part_match = False,
                               search_type = 'file',
                               verbose = self.verbose,
                               )
        return os.path.join(rootFolder, file_path)
    
