% Advanced R134a PH air-conditioner tutorial; all states are recomputed.
% Six pictures reproduce the correction's progressive graphical reasoning.

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

\definecolor{RAirTemperature}{RGB}{35,90,185}
\definecolor{RAirEntropy}{RGB}{35,125,65}
\definecolor{RAirCycle}{RGB}{205,55,35}
\definecolor{RAirGuide}{RGB}{80,88,105}

\tikzset{
  r134a state/.style={
    circle,draw=RAirCycle,fill=white,very thick,
    inner sep=1.2pt,font=\sffamily\bfseries\small
  },
  r134a note/.style={
    fill=white,fill opacity=.9,text opacity=1,rounded corners=1pt,
    inner sep=2pt,font=\sffamily\scriptsize,align=left
  }
}

\newcommand{\RAirBaseQuality}[1]{%
  \LCPAddPHQuality[
    pressure min=70000,pressure max=5500000,
    quality values={#1},
    quality color=black,quality boundary color=black,
    interior style={line width=.35pt},boundary style={line width=1pt},
    labels=false
  ]%
}

\newcommand{\RAirLowPressureGuide}[1]{%
  \LCPAddPHProcess[
    type=isobar,pressure=3bar,
    from={temperature=-10C},to={temperature=20C},
    name=#1,color=RAirGuide,style={line width=.8pt}
  ]%
}

\newcommand{\RAirHighPressureGuide}[1]{%
  \LCPAddPHProcess[
    type=isobar,pressure=18bar,
    from={temperature=40C},to={temperature=100C},
    name=#1,color=RAirGuide,style={line width=.8pt}
  ]%
}

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

\newcommand{\RAirDiagram}[1]{%
  \def\RAirTitle{Phase regions}%
  \ifcase#1\relax
  \or\def\RAirTitle{Where the ideal-gas approximation becomes plausible}%
  \or\def\RAirTitle{Locating state 1 from pressure and temperature}%
  \or\def\RAirTitle{Following the isentropic compression to state 2}%
  \or\def\RAirTitle{Locating state 3 after isobaric cooling}%
  \or\def\RAirTitle{Complete R134a air-conditioning cycle}%
  \fi
  \begin{tikzpicture}
  \begin{axis}[
    lcp fluid=R134a,
    width=17cm,height=11cm,
    xmin=140,xmax=520,
    ymin=.7,ymax=55,ymode=log,
    xlabel={Specific enthalpy $h$ ($\mathrm{kJ\,kg^{-1}}$)},
    ylabel={Pressure $p$ (bar)},
    title={\RAirTitle},
    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
  ]

  \ifcase#1\relax
    \RAirBaseQuality{0,1}
    \node[r134a note] at (axis cs:190,20) {subcooled\\liquid};
    \node[r134a note] at (axis cs:310,5) {liquid--vapour\\equilibrium};
    \node[r134a note] at (axis cs:455,12) {superheated\\vapour};

  \or
    \RAirBaseQuality{0,1}
    \LCPAddPHIsotherms[
      pressure min=70000,pressure max=5500000,
      temperature values={-40,-20,0,20,40,60,80,100,120,140,160},
      isotherm color=RAirTemperature,
      isotherm style={line width=.45pt},labels=false
    ]
    \draw[RAirCycle,dashed,line width=.9pt,rounded corners]
      (axis cs:390,.85) rectangle (axis cs:510,4.5);
    \node[r134a note,anchor=south east,text=RAirCycle]
      at (axis cs:507,4.7)
      {low-pressure superheated vapour:\\isotherms approach isenthalps};

  \or
    \RAirBaseQuality{0,1}
    \LCPAddPHIsotherms[
      pressure min=70000,pressure max=5500000,
      temperature values={0,10},
      isotherm color=RAirTemperature,
      isotherm style={line width=.9pt},labels=false
    ]
    \RAirLowPressureGuide{r134a-stage-low}
    \LCPAddPHProcess[
      type=isotherm,temperature=5C,
      from={pressure=2bar},to={pressure=5bar},
      name=r134a-stage-t-one,color=black,style={draw=none}
    ]
    \path[name intersections={of=r134a-stage-low and r134a-stage-t-one,
      by=RAirStageOne}];
    \RAirStateMarker{RAirStageOne}{anchor=south west}
    \node[r134a note,anchor=south west] at (RAirStageOne)
      {state 1: $p_1=3\,\mathrm{bar}$, $T_1=5\,{}^\circ\mathrm C$};
    \node[r134a note,text=RAirTemperature] at (axis cs:245,2.45)
      {$0\,{}^\circ\mathrm C$};
    \node[r134a note,text=RAirTemperature] at (axis cs:245,3.75)
      {$10\,{}^\circ\mathrm C$};

  \or
    \RAirBaseQuality{0,1}
    \LCPAddPHIsotherms[
      pressure min=70000,pressure max=5500000,
      temperature values={70,80},
      isotherm color=RAirTemperature,
      isotherm style={line width=.75pt},labels=false
    ]
    \LCPAddPHIsentropes[
      pressure min=70000,pressure max=5500000,
      entropy values={1.70,1.75},
      isentrope color=RAirEntropy,
      isentrope style={line width=.75pt},labels=false
    ]
    \RAirLowPressureGuide{r134a-stage-four-low}
    \RAirHighPressureGuide{r134a-stage-four-high}
    \LCPAddPHProcess[
      type=isentrope,
      from={pressure=3bar,temperature=5C},to={pressure=18bar},
      name=r134a-stage-one-two,color=RAirCycle,
      style={line width=1.4pt,-{Latex[length=2.2mm]}}
    ]
    \path[name intersections={of=r134a-stage-four-low and
      r134a-stage-one-two,by=RAirStageFourOne}];
    \path[name intersections={of=r134a-stage-one-two and
      r134a-stage-four-high,by=RAirStageFourTwo}];
    \RAirStateMarker{RAirStageFourOne}{anchor=south east}
    \RAirStateMarker{RAirStageFourTwo}{anchor=south west}
    \node[r134a note,anchor=east] at (RAirStageFourOne) {1};
    \node[r134a note,anchor=west] at (RAirStageFourTwo) {2};
    \node[r134a note,text=RAirEntropy,rotate=75]
      at (axis cs:421,6.5) {$s=1.70$ and $1.75\,\mathrm{kJ\,kg^{-1}\,K^{-1}}$};

  \or
    \RAirBaseQuality{0,1}
    \LCPAddPHIsotherms[
      pressure min=70000,pressure max=5500000,
      temperature values={60},
      isotherm color=RAirTemperature,
      isotherm style={line width=.9pt},labels=false
    ]
    \LCPAddPHProcess[
      type=isentrope,
      from={pressure=3bar,temperature=5C},to={pressure=18bar},
      name=r134a-stage-five-one-two,color=RAirCycle,
      style={line width=1.2pt,-{Latex[length=2mm]}}
    ]
    \LCPAddPHProcess[
      type=isobar,pressure=18bar,
      from={entropy=1740.778583426JkgK},to={temperature=60C},
      name=r134a-stage-five-two-three,color=RAirCycle,
      style={line width=1.4pt,-{Latex[length=2.2mm]}}
    ]
    \LCPAddPHProcess[
      type=isotherm,temperature=60C,
      from={pressure=12bar},to={pressure=25bar},
      name=r134a-stage-five-t-three,color=black,style={draw=none}
    ]
    \path[name intersections={of=r134a-stage-five-one-two and
      r134a-stage-five-two-three,by=RAirStageFiveTwo}];
    \path[name intersections={of=r134a-stage-five-two-three and
      r134a-stage-five-t-three,by=RAirStageFiveThree}];
    \RAirStateMarker{RAirStageFiveTwo}{anchor=south west}
    \RAirStateMarker{RAirStageFiveThree}{anchor=south east}
    \node[r134a note,anchor=west] at (RAirStageFiveTwo) {2};
    \node[r134a note,anchor=east] at (RAirStageFiveThree)
      {3: $T_3=60\,{}^\circ\mathrm C$};

  \or
    \RAirBaseQuality{0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1}
    \LCPAddPHIsotherms[
      pressure min=70000,pressure max=5500000,
      temperature values={0,10,60,70,80},
      isotherm color=RAirTemperature,
      isotherm style={line width=.42pt,densely dotted},labels=false
    ]
    \LCPAddPHIsentropes[
      pressure min=70000,pressure max=5500000,
      entropy values={1.70,1.75},
      isentrope color=RAirEntropy,
      isentrope style={line width=.5pt,dashed},labels=false
    ]
    \LCPAddPHProcess[
      type=isentrope,
      from={pressure=3bar,temperature=5C},to={pressure=18bar},
      name=r134a-cycle-one-two,export coordinates=RAirOneTwo,
      log coordinates=true,color=RAirCycle,
      style={line width=1.45pt,-{Latex[length=2.2mm]}}
    ]
    \LCPAddPHProcess[
      type=isobar,pressure=18bar,
      from={entropy=1740.778583426JkgK},to={temperature=60C},
      name=r134a-cycle-two-three,export coordinates=RAirTwoThree,
      color=RAirCycle,
      style={line width=1.45pt,-{Latex[length=2.2mm]}}
    ]
    \LCPAddPHProcess[
      type=isenthalp,
      from={pressure=18bar,temperature=60C},to={pressure=3bar},
      name=r134a-cycle-three-four,export coordinates=RAirThreeFour,
      color=RAirCycle,
      style={line width=1.45pt,-{Latex[length=2.2mm]}}
    ]
    \LCPAddPHProcess[
      type=isobar,pressure=3bar,
      from={enthalpy=287.4107701858kJkg},to={temperature=5C},
      name=r134a-cycle-four-one,export coordinates=RAirFourOne,
      color=RAirCycle,
      style={line width=1.45pt,-{Latex[length=2.2mm]}}
    ]
    \path[name intersections={of=r134a-cycle-four-one and
      r134a-cycle-one-two,by=RAirOne}];
    \path[name intersections={of=r134a-cycle-one-two and
      r134a-cycle-two-three,by=RAirTwo}];
    \path[name intersections={of=r134a-cycle-two-three and
      r134a-cycle-three-four,by=RAirThree}];
    \path[name intersections={of=r134a-cycle-three-four and
      r134a-cycle-four-one,by=RAirFour}];

    \coordinate (RAirAxisBottom) at (axis cs:140,.7);
    \path (RAirOne |- RAirAxisBottom) coordinate (RAirOneBottom);
    \path (RAirTwo |- RAirAxisBottom) coordinate (RAirTwoBottom);
    \path (RAirThree |- RAirAxisBottom) coordinate (RAirThreeBottom);
    \draw[RAirGuide,densely dashed]
      (RAirOneBottom)--(RAirOne);
    \draw[RAirGuide,densely dashed]
      (RAirTwoBottom)--(RAirTwo);
    \draw[RAirGuide,densely dashed]
      (RAirThreeBottom)--(RAirThree);
    \draw[RAirGuide,densely dashed]
      (RAirOne -| RAirAxisBottom)--(RAirOne);
    \draw[RAirGuide,densely dashed]
      (RAirTwo -| RAirAxisBottom)--(RAirTwo);

    \RAirStateMarker{RAirOne}{anchor=south east}
    \RAirStateMarker{RAirTwo}{anchor=south west}
    \RAirStateMarker{RAirThree}{anchor=south east}
    \RAirStateMarker{RAirFour}{anchor=north east}
    \node[r134a note,anchor=east] at (RAirOne) {1};
    \node[r134a note,anchor=west] at (RAirTwo) {2};
    \node[r134a note,anchor=east] at (RAirThree) {3};
    \node[r134a note,anchor=east] at (RAirFour) {4};

    \pgfmathsetmacro{\RAirTTwoC}{\RAirOneTwoToTemperatureK-273.15}
    \pgfmathsetmacro{\RAirTFourC}{\RAirThreeFourToTemperatureK-273.15}
    \pgfmathsetmacro{\RAirCompressorPower}{
      .1*(\RAirOneTwoToX-\RAirOneTwoFromX)}
    \pgfmathsetmacro{\RAirEvaporatorPower}{
      .1*(\RAirFourOneToX-\RAirFourOneFromX)}
    \pgfmathsetmacro{\RAirCOP}{
      \RAirEvaporatorPower/\RAirCompressorPower}
    \node[r134a note,anchor=north west] at (rel axis cs:.015,.985) {%
      \begin{tabular}{@{}c@{\,: }r@{\,}l@{\quad}r@{\,}l@{}}
      1 & \pgfmathprintnumber[fixed,precision=1]{\RAirOneTwoFromX}
        & $\mathrm{kJ\,kg^{-1}}$ & $T_1=5.0$ & $^\circ\mathrm C$\\
      2 & \pgfmathprintnumber[fixed,precision=1]{\RAirOneTwoToX}
        & $\mathrm{kJ\,kg^{-1}}$ &
        $T_2=\pgfmathprintnumber[fixed,precision=1]{\RAirTTwoC}$
        & $^\circ\mathrm C$\\
      3 & \pgfmathprintnumber[fixed,precision=1]{\RAirTwoThreeToX}
        & $\mathrm{kJ\,kg^{-1}}$ & $T_3=60.0$ & $^\circ\mathrm C$\\
      4 & \pgfmathprintnumber[fixed,precision=1]{\RAirThreeFourToX}
        & $\mathrm{kJ\,kg^{-1}}$ &
        $Q_4=\pgfmathprintnumber[fixed,precision=3]{\RAirThreeFourToQuality}$
        & \\
      \end{tabular}\\[-1mm]
      $\dot W_c=\pgfmathprintnumber[fixed,precision=2]{\RAirCompressorPower}
       \,\mathrm{kW}$,\quad
      $\dot Q_e=\pgfmathprintnumber[fixed,precision=2]{\RAirEvaporatorPower}
       \,\mathrm{kW}$,\quad
      $\mathrm{COP}=\pgfmathprintnumber[fixed,precision=2]{\RAirCOP}$
    };
  \fi

  \end{axis}
  \end{tikzpicture}%
}

\begin{document}
\LCPSetup{fluid=R134a}
\RAirDiagram{0}
\RAirDiagram{1}
\RAirDiagram{2}
\RAirDiagram{3}
\RAirDiagram{4}
\RAirDiagram{5}
\end{document}
