% Shared R717 PH diagram used by the student and teacher versions.
% The thin drivers select whether the cycle and reading aids are visible.

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

\definecolor{RSevenTemperature}{RGB}{175,45,45}
\definecolor{RSevenVolume}{RGB}{35,75,175}
\definecolor{RSevenCycle}{RGB}{220,80,20}
\definecolor{RSevenGuide}{RGB}{75,90,115}

\tikzset{
  r717 state/.style={
    circle,fill=white,draw=RSevenCycle,very thick,
    inner sep=1.2pt,font=\sffamily\bfseries\small
  },
  r717 process label/.style={
    fill=white,fill opacity=.88,text opacity=1,
    rounded corners=1pt,inner sep=1.5pt,
    font=\sffamily\scriptsize,text=black
  }
}

\begin{document}
\LCPSetup{fluid=R717}
\begin{tikzpicture}
\begin{axis}[
  lcp fluid=R717,
  width=24cm,
  height=15cm,
  xmin=100,
  xmax=2200,
  ymin=0.07,
  ymax=140,
  ymode=log,
  xlabel={Specific enthalpy $h$ ($\mathrm{kJ\,kg^{-1}}$)},
  ylabel={Pressure $p$ (bar)},
  title={R717 pressure--enthalpy diagram},
  grid=both,
  major grid style={black!32},
  minor grid style={black!13},
  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,
  auto node border margin=2pt,
  auto node candidates=41
]

  % Use explicit grids so the worksheet remains pedagogically stable when the
  % package presets evolve.  Every value is expressed in the displayed unit.
  \LCPAddPHQuality[
    pressure min=7000,
    pressure max=14000000,
    quality values={0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1},
    quality color=black,
    quality boundary color=black,
    interior style={line width=.32pt},
    boundary style={line width=.9pt},
    labels=true,
    quality label every=1,
    quality label pos=.18,
    quality label sloped=true,
    quality label node style={font=\scriptsize,fill=white,inner sep=.8pt}
  ]
  \LCPAddPHIsotherms[
    pressure min=7000,
    pressure max=14000000,
    temperature values={-60,-40,-20,0,20,40,60,80,100,120,140,160,180,200,220,240},
    isotherm color=RSevenTemperature,
    isotherm style={line width=.34pt,densely dotted},
    labels=true,
    isotherm label every=1,
    isotherm label pos=.72,
    isotherm label sloped=true,
    isotherm label node style={font=\scriptsize,fill=white,inner sep=.8pt}
  ]
  \LCPAddPHIsentropes[
    pressure min=7000,
    pressure max=14000000,
    entropy values={1,2,3,4,5,6,7,8},
    isentrope color=black,
    isentrope style={line width=.32pt},
    labels=true,
    isentrope label every=1,
    isentrope label pos=.22,
    isentrope label sloped=true,
    isentrope label node style={font=\scriptsize,fill=white,inner sep=.8pt}
  ]
  \LCPAddPHIsochores[
    pressure min=7000,
    pressure max=14000000,
    specific volume values={0.01,0.02,0.05,0.1,0.2,0.5,1,2,10,20},
    isochore color=RSevenVolume,
    isochore style={line width=.36pt,dashed},
    labels=true,
    isochore label every=1,
    isochore label pos=.55,
    isochore label sloped=true,
    isochore label node style={font=\scriptsize,fill=white,inner sep=.8pt}
  ]

\ifRSevenSolution
  % State data are calculated by CoolProp.  Stable path names let TikZ recover
  % all five states as intersections, while export coordinates publishes the
  % exact plotted endpoint values for annotations and reproducible logs.
  \LCPAddPHProcess[
    type=isentrope,
    from={pressure=3bar,quality=1},
    to={pressure=10bar},
    color=RSevenCycle,
    style={line width=1.35pt,-{Latex[length=2.2mm]}},
    mark endpoints=false,
    name=r717-ab,
    export coordinates=RSevenAB,
    log coordinates=true
  ]
  \LCPAddPHProcess[
    type=isobar,
    pressure=10bar,
    from={entropy=6.2276889kJkgK},
    to={quality=1},
    color=RSevenCycle,
    style={line width=1.35pt,-{Latex[length=2.2mm]}},
    mark endpoints=false,
    name=r717-bc,
    export coordinates=RSevenBC
  ]
  \LCPAddPHProcess[
    type=isobar,
    pressure=10bar,
    from={quality=1},
    to={quality=0},
    color=RSevenCycle,
    style={line width=1.35pt,-{Latex[length=2.2mm]}},
    mark endpoints=false,
    name=r717-cd,
    export coordinates=RSevenCD
  ]
  \LCPAddPHProcess[
    type=isenthalp,
    from={pressure=10bar,quality=0},
    to={pressure=3bar},
    color=RSevenCycle,
    style={line width=1.35pt,-{Latex[length=2.2mm]}},
    mark endpoints=false,
    name=r717-de,
    export coordinates=RSevenDE
  ]
  \LCPAddPHProcess[
    type=isobar,
    pressure=3bar,
    from={enthalpy=462.75654kJkg},
    to={quality=1},
    color=RSevenCycle,
    style={line width=1.35pt,-{Latex[length=2.2mm]}},
    mark endpoints=false,
    name=r717-ea,
    export coordinates=RSevenEA
  ]

  % Each state is the intersection of the two physical processes that meet
  % there.  No duplicated state coordinate is maintained by hand.
  \path[name intersections={of=r717-ea and r717-ab,by=RSevenA}];
  \path[name intersections={of=r717-ab and r717-bc,by=RSevenB}];
  \path[name intersections={of=r717-bc and r717-cd,by=RSevenC}];
  \path[name intersections={of=r717-cd and r717-de,by=RSevenD}];
  \path[name intersections={of=r717-de and r717-ea,by=RSevenE}];

  % Projection coordinates combine the state intersections with axis-edge
  % anchors.  The |- and -| operations remain correct for a resized axis and
  % for logarithmic pressure because TikZ performs them in canvas space.
  \coordinate (RSevenAxisBottom) at (axis cs:100,.07);
  \coordinate (RSevenLabelBottom) at (axis cs:100,.075);
  \path (RSevenD |- RSevenAxisBottom) coordinate (RSevenDBottom);
  \path (RSevenA |- RSevenAxisBottom) coordinate (RSevenABottom);
  \path (RSevenC |- RSevenAxisBottom) coordinate (RSevenCBottom);
  \path (RSevenB |- RSevenAxisBottom) coordinate (RSevenBBottom);
  \path (RSevenD |- RSevenLabelBottom) coordinate (RSevenDLabel);
  \path (RSevenA |- RSevenLabelBottom) coordinate (RSevenALabel);
  \path (RSevenC |- RSevenLabelBottom) coordinate (RSevenCLabel);
  \path (RSevenB |- RSevenLabelBottom) coordinate (RSevenBLabel);

  % Projection lines turn the teacher copy into a model graphical reading.
  \draw[RSevenGuide,densely dashed,line width=.55pt]
    (RSevenDBottom) -- (RSevenD);
  \draw[RSevenGuide,densely dashed,line width=.55pt]
    (RSevenABottom) -- (RSevenA);
  \draw[RSevenGuide,densely dashed,line width=.55pt]
    (RSevenCBottom) -- (RSevenC);
  \draw[RSevenGuide,densely dashed,line width=.55pt]
    (RSevenBBottom) -- (RSevenB);
  \draw[RSevenGuide,densely dashed,line width=.55pt]
    (RSevenA -| RSevenAxisBottom) -- (RSevenA);
  \draw[RSevenGuide,densely dashed,line width=.55pt]
    (RSevenB -| RSevenAxisBottom) -- (RSevenB);

  % Spell out the five marker commands because PGFPlots defers axis paths;
  % an ordinary \foreach loop variable would be out of scope at visualization.
  \filldraw[draw=RSevenCycle,fill=white,line width=1pt]
    (RSevenA) circle[radius=2pt];
  \filldraw[draw=RSevenCycle,fill=white,line width=1pt]
    (RSevenB) circle[radius=2pt];
  \filldraw[draw=RSevenCycle,fill=white,line width=1pt]
    (RSevenC) circle[radius=2pt];
  \filldraw[draw=RSevenCycle,fill=white,line width=1pt]
    (RSevenD) circle[radius=2pt];
  \filldraw[draw=RSevenCycle,fill=white,line width=1pt]
    (RSevenE) circle[radius=2pt];

  \node[r717 state,anchor=south east] at (RSevenA) {A};
  \node[r717 state,anchor=south west] at (RSevenB) {B};
  \node[r717 state,anchor=south east] at (RSevenC) {C};
  \node[r717 state,anchor=south east] at (RSevenD) {D};
  \node[r717 state,anchor=north east] at (RSevenE) {E};

  \node[r717 process label,rotate=56] at (axis cs:1664,5.4)
    {isentropic compression};
  \node[r717 process label] at (axis cs:1695,11.6) {desuperheating};
  \node[r717 process label] at (axis cs:1040,11.6) {condensation};
  \node[r717 process label,rotate=90] at (axis cs:438,5.4) {expansion};
  \node[r717 process label] at (axis cs:1030,2.55) {evaporation};

  \node[font=\sffamily\scriptsize,anchor=west,rotate=90,text=RSevenGuide]
    at (RSevenDLabel)
    {$h_D=h_E=\pgfmathprintnumber[fixed,precision=0]{\RSevenCDToX}$};
  \node[font=\sffamily\scriptsize,anchor=west,rotate=90,text=RSevenGuide]
    at (RSevenALabel)
    {$h_A=\pgfmathprintnumber[fixed,precision=0]{\RSevenABFromX}$};
  \node[font=\sffamily\scriptsize,anchor=west,rotate=90,text=RSevenGuide]
    at (RSevenCLabel)
    {$h_C=\pgfmathprintnumber[fixed,precision=0]{\RSevenBCToX}$};
  \node[font=\sffamily\scriptsize,anchor=west,rotate=90,text=RSevenGuide]
    at (RSevenBLabel)
    {$h_B=\pgfmathprintnumber[fixed,precision=0]{\RSevenABToX}$};
\fi

\end{axis}
\end{tikzpicture}
\end{document}
