All files / src/pages/flowsheet-page/pinch-analysis/hen-generation/utils pinchLine.ts

100% Statements 15/15
91.66% Branches 11/12
100% Functions 3/3
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                    9x             3x   6x 6x 4x 2x                 9x 9x 9x           9x                                 9x 9x 9x    
import * as d3 from "d3";
import { PinchSection } from "../henTypes";
 
// depending on where the pinch temperature lies in the entire temperature domain,
// classify whether it should be placed closer to the LEFT, RIGHT, or in the MIDDLE of the HEN diagram.
export function getPinchSection(
  pinch?: number,
  minT?: number,
  maxT?: number,
): PinchSection {
  if (
    pinch == null ||
    !isFinite(pinch) ||
    !isFinite(minT!) ||
    !isFinite(maxT!) ||
    maxT! <= minT!
  ) {
    return "MIDDLE";
  }
  const t = (pinch - minT!) / (maxT! - minT!); // 0..1
  if (t < 1 / 3) return "RIGHT";
  if (t > 2 / 3) return "LEFT";
  return "MIDDLE";
}
 
// calcaulate actual x the pinch line should render at
export function getPinchX(
  section: PinchSection,
  svgWidth: number,
  marginX: number,
): number {
  const left = marginX;
  const right = Math.max(left, svgWidth - marginX);
  const band = d3
    .scaleBand<PinchSection>()
    .domain(["LEFT", "MIDDLE", "RIGHT"])
    .range([left, right])
    .padding(1)
    .align(0.5);
  return (band(section) ?? left) + band.bandwidth() / 2;
}
 
// convenience method to compute the pinch line now.
export function computePinchPlacement({
  pinch,
  minT,
  maxT,
  svgWidth,
  marginX,
}: {
  pinch?: number;
  minT: number;
  maxT: number;
  svgWidth?: number;
  marginX: number;
}) {
  const pinchSection = getPinchSection(pinch, minT, maxT);
  const pinchX = getPinchX(pinchSection, svgWidth || 0, marginX);
  return { pinchX };
}