All files / src/pages/flowsheet-page/pinch-analysis/hen-generation HENUnitOpCircle.tsx

93.05% Statements 67/72
54.16% Branches 26/48
87.5% Functions 7/8
95.58% Lines 65/68

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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                                        16641x 16641x       1849x 1849x 1849x 1849x 1849x     62x 11094x 29584x         1849x 1849x 1849x         1849x         1849x         1849x         1849x 1849x 1849x 1849x     62x                                   1221x   1221x     4x   4x 4x 4x 4x   2x 2x 2x   2x 2x   4x         1221x   867x 867x   867x 867x   867x 867x     982x 867x 867x     867x 982x     867x 867x     867x   867x                                 1221x                       3x   1x     2x 2x   2x 2x   2x 2x 2x 2x 2x   2x                                   4x 4x                              
import { useSearchParams } from "react-router-dom";
import { HenNodeRead, SegmentRead } from "@/api/apiStore.gen";
 
type UnitOpCircleProps = {
  stream: any;
  midX: number;
  colour: string;
  text: string;
  henNode: HenNodeRead;
  onShowHover?: any;
  onHideHover?: any;
  hoverBuilders?: any;
  hxKey?: string | undefined;
  selected?: boolean;
  lookup?: {
    sdeById: Map<number, any>;
    segmentsBySdeId: Map<number, any[]>;
  };
};
 
// helper used when hxKey isnt present
function normaliseHxGroup(label: string) {
  // "E-101A" | "E101A" -> "E-101" | "E101"
  const m = String(label).match(/^E-?\d+/i);
  return m?.[0] ?? String(label);
}
 
const num = (v: any, d = 0) => {
  const n = Number(v);
  return Number.isFinite(n) ? n : d;
};
 
const lmtd = (dT1: number, dT2: number) => {
  const a = num(dT1),
    b = num(dT2);
  Iif (a <= 0 && b <= 0) return 0;
  Iif (Math.abs(a - b) < 1e-9) return a;
  return (a - b) / Math.log(a / b);
};
 
const segField = (seg: SegmentRead, keys: string[], d = 0) =>
  num(
    keys.reduce((acc, k) => acc ?? seg?.[k], undefined),
    d,
  );
 
const computeSegmentQkW = (seg: SegmentRead, hotSde?: any, coldSde?: any) => {
  const U = segField(seg, ["overall_htc", "U", "htc"]); // W/m²·K
  const A = segField(seg, ["area"]); // m²
  const Th_in = segField(
    seg,
    ["hot_in", "t_hot_in", "Th_in"],
    hotSde?.temperatures?.inlet,
  );
  const Th_out = segField(
    seg,
    ["hot_out", "t_hot_out", "Th_out"],
    hotSde?.temperatures?.outlet,
  );
  const Tc_in = segField(
    seg,
    ["cold_in", "t_cold_in", "Tc_in"],
    coldSde?.temperatures?.inlet,
  );
  const Tc_out = segField(
    seg,
    ["cold_out", "t_cold_out", "Tc_out"],
    coldSde?.temperatures?.outlet,
  );
  const dT1 = Math.abs(Th_in - Tc_out);
  const dT2 = Math.abs(Th_out - Tc_in);
  const dTlm = lmtd(dT1, dT2);
  return (U * A * dTlm) / 1000; // kW
};
 
const delta = (a?: number, b?: number) =>
  Number.isFinite(Number(a)) && Number.isFinite(Number(b))
    ? Math.abs(Number(a) - Number(b))
    : undefined;
 
export default function UnitOpCircle({
  stream,
  midX,
  colour,
  text,
  henNode,
  onShowHover,
  onHideHover,
  hoverBuilders,
  hxKey,
  selected = false,
  lookup,
}: UnitOpCircleProps) {
  const [, setSearchParams] = useSearchParams();
 
  const isHX = /^E/i.test(String(text));
 
  function handleClick(event: React.MouseEvent) {
    event.stopPropagation();
 
    setSearchParams((prev) => {
      const p = new URLSearchParams(prev);
      p.delete("henStream");
      p.set("henNode", String(henNode?.id ?? ""));
      if (isHX) {
        p.set("selType", "HX");
        const group = hxKey ?? normaliseHxGroup(text);
        p.set("hxGroup", group);
      } else {
        p.set("selType", "UNIT");
        p.delete("hxGroup");
      }
      return p;
    });
  }
 
  // Build HX hover payload (hot/cold) if this node is an exchanger
  let hxHoverContent: any = null;
  if (isHX && lookup) {
    const hotId = Number(henNode?.hot_connection);
    const coldId = Number(henNode?.cold_connection);
 
    const hotSde = lookup.sdeById.get(hotId);
    const coldSde = lookup.sdeById.get(coldId);
 
    const hotSegments = lookup.segmentsBySdeId.get(hotId) ?? [];
    const coldSegments = lookup.segmentsBySdeId.get(coldId) ?? [];
 
    // Areas
    const hotArea = hotSegments.reduce((s, seg) => s + num(seg?.area), 0);
    const coldArea = coldSegments.reduce((s, seg) => s + num(seg?.area), 0);
    const totalArea = hotArea + coldArea || undefined;
 
    // Heat transfer (kW)
    const Qhot = hotSegments.reduce(
      (s, seg) => s + computeSegmentQkW(seg, hotSde, coldSde),
      0,
    );
    const Qcold = coldSegments.reduce(
      (s, seg) => s + computeSegmentQkW(seg, hotSde, coldSde),
      0,
    );
    const Qavg = Qhot && Qcold ? (Qhot + Qcold) / 2 : Qhot || Qcold || 0;
 
    hxHoverContent = hoverBuilders?.hxNode?.({
      title: text,
      hot: {
        before: hotSde?.temperatures?.inlet,
        after: hotSde?.temperatures?.outlet,
      },
      cold: {
        before: coldSde?.temperatures?.inlet,
        after: coldSde?.temperatures?.outlet,
      },
      overall: {
        transfer: Qavg,
        area: totalArea,
      },
    });
  }
 
  return (
    <>
      <circle
        cx={midX}
        cy="0"
        r="15"
        fill={`${selected ? "#2563ebbf" : colour}`}
        stroke="hsl(var(--hen-diagram-foreground))"
        className={`${selected ? "stroke-blue-600 stroke-[3] drop-shadow-[0_0_2px_rgba(37,99,235,0.9)] cursor-pointer" : "stroke-[1] cursor-pointer"}`}
        onClick={handleClick}
        aria-label={`hen-node-${text}`}
        onMouseEnter={(e) => {
          e.stopPropagation();
          if (isHX && hxHoverContent) {
            onShowHover?.(e, hxHoverContent, { width: 300 });
          } else {
            // Non-HX: use SDE just like the sidebar
            const sdeId = Number(henNode?.stream_data_entry);
            const sde = lookup?.sdeById?.get(sdeId);
 
            const inlet = sde?.temperatures?.inlet;
            const outlet = sde?.temperatures?.outlet;
            // const dT = delta(inlet, outlet);
            const duty = sde?.heat_flow;
            console.log("sde", sde);
            console.log("duty", duty);
            const unitopType = sde?.unitop_type;
            console.log("unitopType", unitopType);
 
            onShowHover?.(
              e,
              hoverBuilders?.nonHxNode?.({
                label: text,
                kind: unitopType,
                temps: { inlet, outlet },
                duty: Number(duty),
              }) ??
                hoverBuilders?.node?.(
                  text,
                  henNode?.type ?? "Unit operation",
                  `ID ${henNode?.id}, SDE ${sdeId}`,
                ), // safety
              { width: 300 },
            );
          }
        }}
        onMouseLeave={(e) => {
          e.stopPropagation();
          onHideHover?.(e);
        }}
      />
      <text
        x={midX}
        y="3.5"
        textAnchor="middle"
        fill="hsl(var(--hen-diagram-foreground))"
        className={`${selected ? "text-xs font-bold pointer-events-none" : "text-xs font-bold pointer-events-none"}`}
      >
        {text}
      </text>
    </>
  );
}