% Five-stage butane PV tutorial with computed phase and process diagrams.
% All curves and numerical values are regenerated from the external CoolProp
% library; no imported chart is used by this executable source.

\usepackage{luacoolprop}
\usetikzlibrary{arrows.meta,intersections}
\pgfplotsset{compat=1.18}

\definecolor{ButaneTemperature}{RGB}{190,45,42}
\definecolor{ButaneQuality}{RGB}{35,105,205}
\definecolor{ButaneIdeal}{RGB}{20,135,80}
\definecolor{ButaneHeating}{RGB}{225,115,20}
\definecolor{ButaneGuide}{RGB}{80,88,105}

\tikzset{
  butane state/.style={
    circle,draw=black,fill=white,very thick,
    inner sep=1.4pt,font=\sffamily\bfseries\small
  },
  butane note/.style={
    fill=white,fill opacity=.92,text opacity=1,rounded corners=1pt,
    inner sep=2.5pt,font=\sffamily\scriptsize,align=left
  }
}

\newcommand{\ButanePVBackground}{%
  \LCPAddPVQuality[
    fluid=Butane,
    pressure min=50000,pressure max=15000000,pressure scale=1,
    quality values={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1},
    quality symbol=x,
    quality color=ButaneQuality,quality boundary color=ButaneQuality,
    interior style={line width=.38pt},boundary style={line width=.9pt},
    labels=true,quality label every=1,quality label pos=.2,
    quality label sloped=true,
    initial intervals=28,max depth=9,tolerance=.1,log x weight=45
  ]%
  \LCPAddPVIsotherms[
    fluid=Butane,
    pressure min=50000,pressure max=15000000,pressure scale=1,
    temperature unit=K,
    temperature values={275,285,295,305,315,335,365,415,425.125,
      515,615,715},
    temperature symbol=T,
    isotherm color=ButaneTemperature,
    isotherm style={line width=.42pt},
    curve style for={lcp-pv-T-425p125-K}{densely dashed,line width=.9pt},
    labels=true,isotherm label every=1,isotherm label pos=.76,
    isotherm label sloped=true,
    initial intervals=28,max depth=9,tolerance=.1,log x weight=45
  ]%
}

\newcommand{\ButaneHeatingProcess}[1]{%
  \LCPAddPVProcess[
    fluid=Butane,type=isochore,pressure scale=1,
    from={temperature=305K,quality=0.3},
    to={temperature=335K},
    name=butane-isochoric-heating,
    export coordinates=ButaneHeatingState,
    coord digits=10,log coordinates=true,
    color=ButaneHeating,style={#1}
  ]%
}

\newcommand{\ButaneStateAPaths}{%
  \LCPAddPVProcess[
    fluid=Butane,type=isotherm,temperature=305K,pressure scale=1,
    from={pressure=1bar},to={pressure=10bar},
    name=butane-state-a-isotherm,color=black,style={draw=none}
  ]%
  \LCPAddPVProcess[
    fluid=Butane,type=quality,quality=0.3,pressure scale=1,
    from={temperature=275K},to={temperature=415K},
    name=butane-state-a-quality,color=black,style={draw=none}
  ]%
}

\newcommand{\ButaneStateA}{%
  \path[name intersections={of=butane-state-a-isotherm and
    butane-state-a-quality,
    by=ButaneA}];%
}

\newcommand{\ButaneStateMarker}[2]{%
  \filldraw[draw=black,fill=white,line width=1pt]
    (#1) circle[radius=2pt];%
  \node[butane state,#2] at (#1) {};%
}

\newcommand{\ButanePVDiagram}[1]{%
  \def\ButanePVTitle{Butane PV worksheet}%
  \ifcase#1\relax
  \or\def\ButanePVTitle{Phase regions and the critical isotherm}%
  \or\def\ButanePVTitle{Ideal-gas limit of the 515 K isotherm}%
  \or\def\ButanePVTitle{Tank state from two independent properties}%
  \or\def\ButanePVTitle{Heating a closed rigid butane tank}%
  \fi
  \begin{tikzpicture}
  \begin{loglogaxis}[
    lcp fluid=Butane,
    width=17cm,height=11cm,
    xmin=.001,xmax=1.4,
    ymin=50000,ymax=15000000,
    xlabel={Specific volume $v$ (\si{\cubic\metre\per\kilogram})},
    ylabel={Pressure $p$ (\si{\pascal})},
    title={\ButanePVTitle},
    grid=both,
    major grid style={black!28},minor grid style={black!10},
    tick label style={font=\small},label style={font=\small},
    title style={font=\sffamily\large},
    clip mode=individual,
    auto node placement,
    auto node algorithm=repair,
    auto node bbox mode=oriented,
    auto node failure mode=hide-low-priority
  ]
  \ButanePVBackground

  \ifcase#1\relax
    % The worksheet contains thermodynamic curves but no solution annotations.

  \or
    \node[butane note,anchor=west] at (axis cs:.00108,260000)
      {compressed\\liquid};
    \node[butane note] at (axis cs:.013,300000)
      {liquid--vapour\\equilibrium};
    \node[butane note] at (axis cs:.31,250000) {superheated\\vapour};
    \node[butane note] at (axis cs:.0033,9500000)
      {supercritical region:\\$T>T_c$ and $p>p_c$};
    \filldraw[draw=black,fill=white,line width=.8pt]
      (axis cs:\ButaneCriticalVolume,\LCPFluidPcritSI)
      circle[radius=1.7pt];
    \node[butane note,anchor=south west]
      at (axis cs:.0047,5100000)
      {$T_c=425.125\,\mathrm K$\\$p_c=3.796\,\mathrm{MPa}$};
    \node[butane note,text=ButaneTemperature,anchor=south west]
      at (axis cs:.011,4800000) {critical isotherm $\mathcal C$};

  \or
    % For an ideal gas, p=(R/M)T/v.  With M=58.12 g/mol and
    % T=515 K, (R/M)T is approximately 7.367e4 Pa m^3/kg.
    \addplot[ButaneIdeal,line width=1.15pt,domain=.025:1.25,samples=2]
      {(8.314462618/0.05812*515)/x};
    \node[butane note,text=ButaneIdeal,anchor=south west]
      at (axis cs:.12,850000)
      {$p=(R/M)T/v$\\slope $-1$ in log--log coordinates};
    \draw[ButaneIdeal,-{Latex[length=2mm]},line width=.8pt]
      (axis cs:.24,520000) -- (axis cs:.42,180000);
    \node[butane note,text=ButaneIdeal,anchor=north west]
      at (axis cs:.22,480000)
      {CoolProp and ideal-gas curves\\nearly coincide as $v$ increases};

  \or
    % The invisible process exists only to export the exact endpoint data used
    % by the numerical result box.  Point A itself is placed by intersection.
    \ButaneStateAPaths
    \ButaneHeatingProcess{draw=none}
    \ButaneStateA
    \coordinate (ButaneAxisOrigin) at (axis cs:.001,50000);
    \path (ButaneA |- ButaneAxisOrigin) coordinate (ButaneABottom);
    \path (ButaneA -| ButaneAxisOrigin) coordinate (ButaneALeft);
    \draw[ButaneGuide,densely dashed]
      (ButaneABottom)--(ButaneA)--(ButaneALeft);
    \ButaneStateMarker{ButaneA}{anchor=south west}
    \node[butane note,anchor=south west] at (ButaneA)
      {$A$: $T=305\,\mathrm K$, $x=0.30$};
    \pgfmathsetmacro{\ButaneMassGram}{
      0.004/\ButaneHeatingStateFromSpecificVolumeSI}
    \node[butane note,anchor=north east] at (rel axis cs:.985,.985) {%
      $p_A=\qty[round-mode=figures,round-precision=4]
        {\ButaneHeatingStateFromPressureSI}{\pascal}$\\
      $v_A=\qty[round-mode=figures,round-precision=5]
        {\ButaneHeatingStateFromSpecificVolumeSI}
        {\cubic\metre\per\kilogram}$\\
      $m=V/v_A=\qty[round-mode=figures,round-precision=3]
        {\ButaneMassGram}{\gram}$
    };

  \or
    \ButaneStateAPaths
    \ButaneHeatingProcess{line width=1.6pt,-{Latex[length=2.6mm]}}
    \ButaneStateA
    \ButaneStateMarker{ButaneA}{anchor=south east}
    \node[butane note,anchor=east] at (ButaneA) {$A$: $x=0.30$};
    \filldraw[draw=ButaneHeating,fill=white,line width=1pt]
      (axis cs:\ButaneHeatingStateToX,\ButaneHeatingStateToY)
      circle[radius=2pt];
    \node[butane note,anchor=south west,text=ButaneHeating]
      at (axis cs:\ButaneHeatingStateToX,\ButaneHeatingStateToY)
      {$T=335\,\mathrm K$\\
       $x=\num[round-mode=places,round-precision=3]
         {\ButaneHeatingStateToQuality}$};
    \node[butane note,anchor=south east,text=ButaneHeating]
      at (axis cs:.035,1150000)
      {$V$ and $m$ constant\\$\Rightarrow v=V/m$ constant};
    \typeout{Butane tutorial initial pressure (Pa):
      \ButaneHeatingStateFromPressureSI}
    \typeout{Butane tutorial initial specific volume (m3/kg):
      \ButaneHeatingStateFromSpecificVolumeSI}
    \typeout{Butane tutorial final quality:
      \ButaneHeatingStateToQuality}
  \fi

  \end{loglogaxis}
  \end{tikzpicture}%
}

\begin{document}
\LCPSetup{fluid=Butane}
\LCPDeclareFluid{Butane}
\pgfmathsetmacro{\ButaneCriticalVolume}{1/\LCPFluidRhocritSI}
\ButanePVDiagram{0}
\ButanePVDiagram{1}
\ButanePVDiagram{2}
\ButanePVDiagram{3}
\ButanePVDiagram{4}
\end{document}
