diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml new file mode 100644 index 0000000000..134ae01e63 --- /dev/null +++ b/.github/workflows/security.yml @@ -0,0 +1,112 @@ +name: Security SAST + +# Blocking SAST gate added as CodeQL replacement (issue #2473). +# CodeQL remains disabled per maintainer direction. +# Runs Bandit full-repo scan (medium+ severity) and verifies the +# detect-secrets baseline has not drifted. + +on: + pull_request: + types: [opened, synchronize, reopened] + push: + branches: [main] + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + bandit-sast: + name: Bandit SAST (medium+ severity) + runs-on: d-sorg-fleet + timeout-minutes: 15 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Install Bandit + run: python -m pip install "bandit==1.7.7" + + - name: Run Bandit full-repo scan + # -r src/ — recursive scan of all source code + # -ll — report medium severity and above (LOW skipped) + # -ii — report medium confidence and above + # Exit code 1 when issues found — this step is blocking. + run: | + python -m bandit -r src/ -ll -ii \ + --exclude src/data_processing,src/document_processing,src/pendulum_simulator,src/scientific_modeling,src/tools/folder_tools \ + -f txt + + detect-secrets-baseline: + name: detect-secrets baseline check + runs-on: d-sorg-fleet + timeout-minutes: 10 + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Install detect-secrets + run: python -m pip install detect-secrets + + - name: Verify baseline is present + run: | + if [ ! -f .secrets.baseline ]; then + echo "::error::.secrets.baseline is missing. Run: detect-secrets scan > .secrets.baseline" + exit 1 + fi + + - name: Scan against baseline + run: | + cp .secrets.baseline .secrets.baseline.before + detect-secrets scan --baseline .secrets.baseline \ + --exclude-files '(^|/)(\.secrets\.baseline(\..*)?|src/document_processing/pdf_renamer/(API_KEY_SETUP|QUICK_START|README)\.md)$' + + - name: Fail if new secrets introduced + run: | + python - <<'PY' + import json + import pathlib + import sys + from collections import Counter + + def result_fingerprints(path: str) -> Counter: + data = json.loads(pathlib.Path(path).read_text()) + fingerprints = [] + for filename, findings in data.get("results", {}).items(): + normalized_name = filename.replace("\\", "/") + for finding in findings: + fingerprints.append( + ( + normalized_name, + finding.get("type", ""), + finding.get("hashed_secret", ""), + ) + ) + return Counter(fingerprints) + + before = result_fingerprints(".secrets.baseline.before") + after = result_fingerprints(".secrets.baseline") + added = after - before + removed = before - after + if added: + print("::error::detect-secrets baseline changed — new secret(s) detected") + for label, delta in (("added", added), ("removed", removed)): + summary = Counter((f, k) for f, k, _ in delta.elements()) + for (filename, kind), count in sorted(summary.items()): + print(f"{label}: {count} finding(s) in {filename} ({kind})") + sys.exit(1) + print("No new secrets detected.") + PY diff --git a/.gitignore b/.gitignore index cd2f1ba50e..83b73bb4bb 100644 --- a/.gitignore +++ b/.gitignore @@ -56,6 +56,9 @@ dmypy.json # Binary documents (should not be in VCS) *.docx +# PSA reference directory — contains overlong paths that break Windows checkout (Issue #2475) +src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/ + # Temp output files (explicitly listed for clarity) mypy_output.txt mypy_errors_temp.txt diff --git a/.secrets.baseline b/.secrets.baseline index df17bdf055..f6ca49370a 100644 --- a/.secrets.baseline +++ b/.secrets.baseline @@ -26,9 +26,6 @@ { "name": "GitHubTokenDetector" }, - { - "name": "GitLabTokenDetector" - }, { "name": "HexHighEntropyString", "limit": 3.0 @@ -39,9 +36,6 @@ { "name": "IbmCosHmacDetector" }, - { - "name": "IPPublicDetector" - }, { "name": "JwtTokenDetector" }, @@ -55,15 +49,9 @@ { "name": "NpmDetector" }, - { - "name": "OpenAIDetector" - }, { "name": "PrivateKeyDetector" }, - { - "name": "PypiTokenDetector" - }, { "name": "SendGridDetector" }, @@ -79,9 +67,6 @@ { "name": "StripeDetector" }, - { - "name": "TelegramBotTokenDetector" - }, { "name": "TwilioKeyDetector" } diff --git a/pyproject.toml b/pyproject.toml index 9b9b79666a..4dc4cde519 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -95,6 +95,8 @@ dev = [ "pytest-benchmark>=4.0.0", "ruff>=0.1.0", "mypy>=1.0.0", + "bandit>=1.7.7", + "detect-secrets>=1.5.0", ] [project.scripts] diff --git a/src/python/src/utils/integration_test_helpers.py b/src/python/src/utils/integration_test_helpers.py index fbbfa1df0a..ea54505c7a 100644 --- a/src/python/src/utils/integration_test_helpers.py +++ b/src/python/src/utils/integration_test_helpers.py @@ -94,7 +94,7 @@ def check_http_service( start = time.perf_counter() request = urllib.request.Request(url, method="GET") - with urllib.request.urlopen(request, timeout=timeout) as response: + with urllib.request.urlopen(request, timeout=timeout) as response: # nosec B310 latency = (time.perf_counter() - start) * 1000 status_code = response.getcode() diff --git a/src/shared/python/calc_backend/tests/test_calc_backend.py b/src/shared/python/calc_backend/tests/test_calc_backend.py index 7921a5d67e..1efa5885e8 100644 --- a/src/shared/python/calc_backend/tests/test_calc_backend.py +++ b/src/shared/python/calc_backend/tests/test_calc_backend.py @@ -734,7 +734,7 @@ def test_expression_evaluator_protocol(self) -> Any: class DummyEval: def evaluate(self, expression: str, namespace: dict) -> float: - return eval(expression, {}, namespace) + return eval(expression, {}, namespace) # nosec B307 def validate(self, expression: str) -> bool: return True diff --git a/src/shared/python/model_generation/library/_rate_limiter.py b/src/shared/python/model_generation/library/_rate_limiter.py index e30a5a44e6..fececbd33b 100644 --- a/src/shared/python/model_generation/library/_rate_limiter.py +++ b/src/shared/python/model_generation/library/_rate_limiter.py @@ -120,7 +120,7 @@ def make_request_with_backoff( for key, value in headers.items(): req.add_header(key, value) - response = urllib.request.urlopen(req, timeout=10) + response = urllib.request.urlopen(req, timeout=10) # nosec B310 # Log rate-limit status on success rate_limit_info = extract_rate_limit_info(response) diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041 - Xebec PSA Proposed Guarantee 20181205 - Detailed.xlsx b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041 - Xebec PSA Proposed Guarantee 20181205 - Detailed.xlsx deleted file mode 100644 index 5eb5ae3570..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041 - Xebec PSA Proposed Guarantee 20181205 - Detailed.xlsx and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041 PSA drawing.PDF b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041 PSA drawing.PDF deleted file mode 100644 index 2219f88c61..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041 PSA drawing.PDF and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041-PID-ALL-2024-03-21.pdf b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041-PID-ALL-2024-03-21.pdf deleted file mode 100644 index 26d3fe84ec..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041-PID-ALL-2024-03-21.pdf and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041-PID-ALL-2025-03-21 H2 Industry Recommendations.pdf b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041-PID-ALL-2025-03-21 H2 Industry Recommendations.pdf deleted file mode 100644 index 377bbcab23..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/3041-PID-ALL-2025-03-21 H2 Industry Recommendations.pdf and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/30784 IOM manual.pdf b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/30784 IOM manual.pdf deleted file mode 100644 index 964dc01df3..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/30784 IOM manual.pdf and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/Design Basis for PSA Unit.JPG b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/Design Basis for PSA Unit.JPG deleted file mode 100644 index 76213a21e5..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/Design Basis for PSA Unit.JPG and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/H16-1061 PSA upgrading proposal G4 rev 06 with Perez notes from 2019 01 17 9am call with Xebec - Internal Use Only.pdf b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/H16-1061 PSA upgrading proposal G4 rev 06 with Perez notes from 2019 01 17 9am call with Xebec - Internal Use Only.pdf deleted file mode 100644 index acb382b436..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/H16-1061 PSA upgrading proposal G4 rev 06 with Perez notes from 2019 01 17 9am call with Xebec - Internal Use Only.pdf and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/H16-1061 PSA upgrading proposal G4 rev 06.pdf b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/H16-1061 PSA upgrading proposal G4 rev 06.pdf deleted file mode 100644 index 7ddda51d13..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/H16-1061 PSA upgrading proposal G4 rev 06.pdf and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/H17-1016 PSA upgrading proposal G2 rev 01 (002).pdf b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/H17-1016 PSA upgrading proposal G2 rev 01 (002).pdf deleted file mode 100644 index 3f3e21d478..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/H17-1016 PSA upgrading proposal G2 rev 01 (002).pdf and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/ICR PSA Logic Summary.docx b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/ICR PSA Logic Summary.docx deleted file mode 100644 index da4f959f28..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/ICR PSA Logic Summary.docx and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/IMG_20180807_100123236.jpg b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/IMG_20180807_100123236.jpg deleted file mode 100644 index fe5cdd0735..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/IMG_20180807_100123236.jpg and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Control Narrative.docx b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Control Narrative.docx deleted file mode 100644 index a497735574..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Control Narrative.docx and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Pattern.xlsx b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Pattern.xlsx deleted file mode 100644 index 8e225ab117..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Pattern.xlsx and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Pressure Trace.xlsx b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Pressure Trace.xlsx deleted file mode 100644 index 932fb9be05..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Pressure Trace.xlsx and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Status Update July 2018.docx b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Status Update July 2018.docx deleted file mode 100644 index 7d82f6b147..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Status Update July 2018.docx and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA System PFD.jpg b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA System PFD.jpg deleted file mode 100644 index 762a1b9f4f..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA System PFD.jpg and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Testing Results.xlsx b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Testing Results.xlsx deleted file mode 100644 index f8ee6af4ea..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/PSA Testing Results.xlsx and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/Xebec PSA Manual.pdf b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/Xebec PSA Manual.pdf deleted file mode 100644 index 7ecab3931f..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/Xebec PSA Manual.pdf and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/en_AIGA_045_07_Gaseous_hydrogen_stations_.pdf b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/en_AIGA_045_07_Gaseous_hydrogen_stations_.pdf deleted file mode 100644 index 8bbb057199..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/en_AIGA_045_07_Gaseous_hydrogen_stations_.pdf and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/psa_email_lessons_learned_and_large_scale_plan.md b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/psa_email_lessons_learned_and_large_scale_plan.md deleted file mode 100644 index 3f7e516478..0000000000 --- a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/psa_email_lessons_learned_and_large_scale_plan.md +++ /dev/null @@ -1,278 +0,0 @@ -# PSA Email Summary - Lessons Learned and Large-Scale Plan - -## Scope - -- Source: PSA-related email threads in `Email Correspondence/` (92 .msg files) plus non-email docs in the folder -- Focus: lessons learned and performance implications for a larger-scale PSA unit -- Additional docs reviewed: `PSA Testing Results.xlsx`, `PSA Pressure Trace.xlsx`, `PSA Pattern.xlsx`, `3041 - Xebec PSA Proposed Guarantee 20181205 - Detailed.xlsx`, `PSA Control Narrative.docx`, `ICR PSA Logic Summary.docx`, `PSA Status Update July 2018.docx`, `Design Basis for PSA Unit.JPG` -- Note: PDFs and vector drawings could not be text-extracted with available tools in this environment; review pending for `Xebec PSA Manual.pdf`, `30784 IOM manual.pdf`, proposals, and P&IDs - -## Lessons Learned (Performance-Focused) - -### 1) Purity shortfalls observed during testing - -- Product gas from the first PSA test contained N2 at ~0.81%, O2 at ~786 ppm, and H2O >22 ppm after ~2 hours of operation following an N2 purge. -- Operators reported N2 and CO2 trending high, with CO and H2 sometimes low relative to expected performance. -- Impurity control appears sensitive to flow, valve speed, and upstream composition variability. -- Lab results in `PSA Testing Results.xlsx` show multiple H2 sample sets with total contaminants ranging from ~8,981 ppm (purity ~99.10%) to ~12,057 ppm (purity ~98.79%) in April 2018 samples, improving to ~106.6 ppm (purity ~99.989%) in a later May 2018 sample and ~274.6 ppm (purity ~99.973%) after air-correction. - -### 2) Recovery below expected levels in field testing - -- Performance spec expectations: ~99.999% purity and ~70% recovery (water saturated at 100% RH, no liquid water). -- Reported performance on one test case: - - PSA feed: 9.66 SCFM; product: 1.33 SCFM; recovery ~44%. - - Feed composition: H2 31.2%, CO 31.4%, CO2 17.7%, CH4 0.25%, N2 19.4%. - - Inlet pressure 160 psig; outlet pressure 155 psig; valve speed 29.9 RPH (0.5 RPM). -- A design basis table (low-pressure case) targets 99.999% H2 purity at 70% recovery with feed 14 scfm at 160 psig, product 3 scfm at 145 psig, and exhaust 11 scfm at 2 psig; nominal feed composition listed as H2 29.6%, CO 29.6%, CO2 21.2%, N2 19.0%, H2O 0.6% (by volume). - -### 3) Operating limits tie purity to valve speed and feed component flows - -- Minimum rotary valve speeds rise with feed flow. Lower speeds increase product flow but reduce purity and increase bed loading. -- Component-specific max flows at each valve speed can become limiting even if total flow appears acceptable. -- Example for low flows (1-5 SCFM): minimum speed ~0.17 RPM (10 RPH). Above 5 SCFM, minimum speed increases and component limits tighten. -- Control narrative calculations explicitly set minimum rotational speed using rolling 1-minute average feed SCFM and feed composition (CO and CO2 constraints) and use RPH derived from gear tooth timing (32-tooth gear). - -### 4) Turndown constraints are a major sizing risk - -- Xebec PSA turndown noted at ~50-60% of design flow. -- Large units cannot be run efficiently at very low flow (e.g., 7 SCFM on a 50-100 SCFM unit). -- A smaller unit is feasible for 7 SCFM but caps flow around ~22 SCFM and limits scalability. - -### 5) Feed composition variability requires modeling across a matrix, not a single normalized case - -- Syngas composition can shift rapidly with feed changes (e.g., CO from ~36% to <22%, CO2 from ~20% to ~38% in an hour). -- Operators requested model runs across max-concentration bounds (e.g., 15 SCFM with CO 45%, CO2 40%, N2 25%, CH4 5%, O2 2%). -- Normalizing a single composition risks under-sizing and purity loss during off-nominal operation. - -### 6) Adsorbent layering and water/CO2 management are critical - -- Observed/expected layering (from internal analysis): - - Layer 1: activated alumina or aluminosilicate desiccant (water protection) - - Layer 2: activated carbon (CO2, residual water, CH4) - - Layer 3: 5A zeolite (CO/CO2/CH4 removal) - - Layer 4: CaX zeolite (N2/O2 polishing; sensitive to water/CO2) -- CO2 and H2O can “clog” zeolites, reinforcing the need for front-end removal and proper packing. -- BTX noted above 10 ppm historically; ammonia also present and only partially removed by scrubbers. -- PSA Status Update (July 2018) explicitly notes that adding activated carbon above the desiccant was a warranty modification after initial tests failed to meet purity/recovery, indicating a strong sensitivity to CO2/H2O front-end removal. - -### 7) Mechanical / packing issues affect performance and reliability - -- On-site bedfill risks: slight vessel tilt (1-2 degrees) can force media into the O-ring groove, preventing seal and causing rework/scrap. -- Repacking and pressure trace diagnostics indicated possible restriction in product pressurization steps (slower pressurization, flattened curves). -- Pressure hold tests showed ~2 psig loss over 5 minutes, suggesting no large internal leak but possible localized restrictions. -- `PSA Pressure Trace.xlsx` captures a 12.3-hour trace on 2018-08-07 with pressure oscillating between ~0 and ~164 psig (mean ~19.25 psig, 95th percentile ~64.27 psig), consistent with rapid cycling and equalization events. - -### 8) Cycle behavior and control expectations - -- PSA uses 2 rotary valves and 9 beds; each revolution includes 18 half-steps. -- Typically 2-3 columns produce, 2-3 regenerate, the rest equalize at any moment. -- Product purity control often uses a 4-20 mA H2 purity signal to adjust rotary valve speed. -- `PSA Pattern.xlsx` documents 18-step sequences for 1-, 2-, and 3-step equalization patterns across 9 columns, reinforcing the 18-step rotary cycle model referenced in email correspondence. - -### 9) Pressure and compressor constraints - -- Proposed/expected PSA inlet pressures ranged from ~150-225 psig; 200 psig was a common baseline for guarantees. -- Small booster compressor behavior: - - Boosts from ~100 to 160 psig (system cycles 145-180 psig). - - Approximate flow at 120 psig ~4 SCFM; nominal design flow ~3 SCFM. -- PSA logic ranges and alarms include 0-300 psig transmitters with HH/H/L alarms on Stage 1 bottom pressure (HH 270 / H 255 / L 225 psig), showing high-pressure protection thresholds used in controls. - -### 10) CO2-removal configurations appear feasible at small scale - -- Small PSA repurpose target: 8 SCFM at 150 psig to reduce CO2 from ~20% to <4%. -- CO2 is easiest to remove; activated carbon is the preferred adsorbent for CO2 removal. -- A 3-stage CO2 PSA design was discussed, positioned before H2 PSA to reduce load and improve overall performance. - -## Performance Targets and Proposed Guarantees (from email guidance) - -- Purity spec: 99.999% (per performance specs email) -- Expected recovery: ~70% (performance specs email) -- Proposed guarantee (200 psig, with linear interpolation between cases): - - Case 1 (600 SCFM): H2 > 30%, CH4 < 2%, CO < 30%, CO2 < 30%, N2 < 8%, H2O saturated at 100 F - - Case 2 (1100 SCFM): H2 > 30%, CH4 < 2%, CO < 40%, CO2 < 23%, N2 < 5%, H2O saturated at 100 F -- Guarantees assume tail gas recycle from PSA2 to compressor inlet. -- Detailed guarantee table (`3041 - Xebec PSA Proposed Guarantee 20181205 - Detailed.xlsx`) shows interpolation from Case 1 (600 scfm) to Case 2 (1100 scfm) at 200 psig with recovery rising 70% to 75% and tightening impurity limits: CO 30% to 40%, CO2 ~30% to ~23%, CH4 2% to 1%, N2 8% to 5%. - -## Plan for the Larger-Scale Unit (Performance-Centric) - -### A) Confirm the feed envelope and performance targets - -- Lock in flow range with realistic turndown (50-60% of design flow), avoiding oversizing. -- Use a composition matrix across max and min constituent bounds (not a normalized nominal). -- Define acceptance criteria explicitly: product purity (N2/O2 ppm), H2 recovery, pressure drop, and steady-state stability after purge. -- Align test conditions with the design basis flow/pressure table (160 psig feed, 145 psig product, 2 psig exhaust at 14/3/11 scfm) and document deviations during testing. - -### B) Compare two-stage architectures using consistent performance criteria - -**Option 1: Two PSA units in series (Xebec approach)** - -- Stage 1: ~85% purity at high recovery. -- Stage 2: polishing to high purity at lower recovery. -- Tail gas from stage 2 recycled to compressor inlet. -- Expected overall recovery ~90% (from internal model based on small-scale data). - -**Option 2: Two PSA units in series (InEnTec approach)** - -- Stage 1: remove CO2/CH4/H2O with activated carbon; partial CO/N2 removal (~20% assumed). -- Stage 2: remove CO and N2 using zeolites; no tail gas recycle. -- Model assumption: ~75% removal per stage yields equivalent overall recovery ~86% after a chemical recycle step. -- Stage 1 inlet example (from internal model): 1013 SCFM with CO 37.8%, CO2 19.5%, H2 32.8%, H2O 5.6%, N2 3.7%. - -### C) Evaluate tail-gas recycle to process (TRC) for recovery gains - -- 50% tail-gas recycle: total PSA flow ~935 SCFM; recovery improvement from ~55% to ~70% (modeled). -- 75% tail-gas recycle: total PSA flow ~1260 SCFM; modeled recovery improvement toward ~97% (near compressor limits). -- Consider energy penalty (~550,000 BTU/hr) and potential 10-12% consumption of recycled H2/CO. - -### D) Design for impurity management and adsorbent protection - -- Front-load water and CO2 removal to protect downstream zeolites (especially CaX). -- Validate adsorbent layering and packing procedures to avoid CO2/water poisoning and performance drift. -- Consider dedicated CO2 PSA ahead of H2 PSA if CO2/H2O are dominant performance inhibitors. - -### E) Instrumentation, controls, and mechanical safeguards - -- Provide product purity measurement (H2 analyzer) with 4-20 mA output for valve speed control. -- Include thermal mass or ultrasonic flow meters for product flow. -- Add a surge tank on tail-gas line to dampen high-frequency pressure fluctuations from short cycle times. -- Avoid on-site bedfill when possible; enforce strict vessel alignment if on-site fill is unavoidable. -- Implement the control narrative formulas for minimum RPH based on rolling feed flow and CO/CO2 composition to prevent under-speed operation. -- Verify pressure transmitter scaling and alarm setpoints (Stage 1 bottom pressure HH/H/L = 270/255/225 psig) in commissioning to avoid nuisance trips or overpressure risk. - -### F) Performance validation test plan (pre-commission and commissioning) - -- Run tests across the full feed composition matrix at minimum, nominal, and maximum flow rates. -- Verify minimum valve speeds and component max flows per operating limit table. -- Track recovery, purity, O2/N2 ppm, H2O ppm, and pressure drop vs. valve speed. -- Include steady-state holds after purges to confirm stability and repeatability. - -## Key Open Questions for the Large-Scale Design - -- What purity/recovery tradeoffs are acceptable at the high-flow limit if composition shifts beyond the two guarantee cases? -- Can the compressor and recycle configuration consistently support 200-225 psig at the required flow without excessive cycling? -- Is 20% CO/N2 removal in activated carbon a realistic assumption, or should it be re-estimated with vendor data? -- What is the acceptable operating window for CH4 and BTX, given observed BTX >10 ppm and limited scrubber removal? -- Can the PSA be operated at the design-basis low-pressure case (14 scfm at 160 psig) without violating minimum RPH constraints derived from the control narrative? - -## Data Extract for Agents (Non-email docs) - -### PSA Testing Results (H2 Samples) - -Source: `PSA Testing Results.xlsx` (sheet: H2 Samples). -| Metric | 2018-04-19 | 2018-04-25 | Blank | 2018-05-25 Sample 1 | 2018-05-25 Sample 2 | 2018-05-25 Sample 1 No Air | 2018-05-25 Sample 2 No Air | Units | -| --- | --- | --- | --- | --- | --- | --- | --- | --- | -| Oxygen | 786.1 | 2092.0 | 0.23 | 78.4 | 9100 | 0 | 0 | ppm | -| H2O | 22.0 | 22.0 | 18.5 | 22 | 22 | 22 | 22 | ppm | -| THC | 21.11 | 58.8 | 0.01 | 1.51 | 50.7 | 1.51 | 50.7 | ppm | -| Nitrogen | 8100.000000000001 | 9300.000000000002 | 0.24199999999999997 | 2.647 | 21000 | 0 | 0 | ppm | -| Carbon Dioxide | 5.02 | 442.0 | 10.4 | 1 | 35 | 1 | 35 | ppm | -| Carbon Monoxide | 47.2 | 142.6 | 1.69 | 1 | 166.9 | 1 | 166.9 | ppm | -| Total Contaminants | 8981.430000000002 | 12057.400000000003 | 31.072000000000006 | 106.55700000000002 | 30374.600000000002 | 25.51 | 274.6 | ppm | -| Purity | 99.101857 | 98.79426 | 99.9968928 | 99.9893443 | 96.96253999999999 | 99.99744899999999 | 99.97254 | % purity | -| Time at Stable Operation | 1.5 | 3.5 | | | | | | hours | -| PSA Feed Flow | 9.66 | 9.4 | | 10.2 | 11 | 10.2 | 11 | SCFM | -| PSA H2 Flow | 1.33 | 2.0 | | 2 | 2.8 | 2 | 2.8 | SCFM | -| PSA Valve Speed | 30.0 | 24.9 | | 32 | 24 | 32 | 24 | RPH | - -### Design Basis (Flow Case 2: Low Pressure) - -Source: `Design Basis for PSA Unit.JPG`. - -- Targets: H2 purity 99.999%, expected recovery 70%. -- Stream properties: - -| Stream | Fresh feed | Product | Exhaust | -| --------------- | ---------- | ------- | ------- | -| Flow (NCMH) | 22 | 5 | 18 | -| Flow (scfm) | 14 | 3 | 11 | -| Pressure (psig) | 160 | 145 | 2 | -| Temperature (C) | 40 | 40 | 40 | - -- Feed composition (vol%): H2 29.6, CO 29.6, CO2 21.2, N2 19.0, H2O 0.6 (total 100). -- Product composition: H2 99.999, balance unspecified components. -- Exhaust composition (vol%): H2 11.2, CO 37.4, CO2 26.7, N2 23.9, H2O 0.8 (total 100). - -### Proposed Guarantee (Interpolated Cases) - -Source: `3041 - Xebec PSA Proposed Guarantee 20181205 - Detailed.xlsx` (pressure 200 psig). -| Flow scfm | H2 >= % | CO <= % | CO2 <= % | CH4 <= % | N2 <= % | Recovery % | -| --- | --- | --- | --- | --- | --- | --- | -| 600 | 30 | 30 | 30 | 2 | 8 | 70 | -| 700 | 30 | 32 | 28.721062618595823 | 1.8 | 7.4 | 71 | -| 800 | 30 | 34 | 27.371841155234655 | 1.6 | 6.8 | 72 | -| 900 | 30 | 36 | 25.962134251290884 | 1.4 | 6.2 | 73 | -| 1000 | 30 | 38 | 24.5 | 1.2 | 5.6 | 74 | -| 1100 | 30 | 40 | 22.99212598425197 | 1 | 5 | 75 | - -### PSA Control Narrative (Logic/Calculations) - -Source: `PSA Control Narrative.docx`. - -- RPH calculation from gear tooth timing: RPH = (Time2 - Time1) \* TeethNo; gear teeth = 32. -- 1-minute rolling average feed flow: AveFeedFlow = (T1Flow + ... + T60Flow) / 60 (lb/hr). -- Feed flow conversion to SCFM at 0 C, 1 atm: AveFeedSCFM = AveFeedFlow _ SyngasMW _ R _ Ps _ Ts (R=1.31443 ft^3\*atm/lbmol/K, Ps=1 atm, Ts=273.15 K). -- Minimum rotational speed constraints (by flow and by component): - - MinRPH,Flow = AveFeedSCFM \* NominalRPH / NominalFlow. - - MinRPH,CO = AveFeedSCFM _ CO%/100 _ NominalRPH / NominalCO. - - MinRPH,CO2 = AveFeedSCFM _ CO2%/100 _ NominalRPH / NominalCO2. - -### PSA Logic Summary (Alarms/Setpoints) - -Source: `ICR PSA Logic Summary.docx`. - -- Pressure transmitters (0-300 psig): Stage 1 bottom, Stage 2 inlet, Stage 2 bottom. -- Stage 1 bottom pressure alarm setpoints: HH 270 psig, H 255 psig, L 225 psig. -- Stage 2 inlet/bottom setpoints listed as HH/H/L/LL = 0 (placeholders in doc). - -### Pressure Trace Summary - -Source: `PSA Pressure Trace.xlsx` (2018-08-07). - -- Time span: 2018-08-07 00:00:00.123000 to 2018-08-07 12:18:21.401000; rows 173897. -- Pressure stats (psig): min 0.00, max 164.30, mean 19.25, P05 0.00, P95 64.27. - -### PSA Cycle Pattern - -Source: `PSA Pattern.xlsx`. - -- 18-step cycle patterns provided for 1-, 2-, and 3-step equalization across 9 columns (S/E percentages per step). - -### PSA Status Update (Operational Theory + Field Issues) - -Source: `PSA Status Update July 2018.docx`. - -- Field results: six processing days; samples did not meet vendor spec; warranty work in progress to add second stage. -- Adsorbent update: Xebec added activated carbon above desiccant after initial failures; later tests still failed to meet spec, prompting second-stage concept. -- Theory highlights: - - Multi-layer adsorption; strongly adsorbed species (CO2/H2O) can dominate capacity and are hard to desorb. - - Pressure equalization exposes upper zeolite layers to desorbed CO2/H2O, reducing N2 capacity and causing product contamination. - - Low H2 feed (30-40% vs typical 60-80%) increases tail-gas flow and contaminant partial pressures; worsens desorption and promotes re-adsorption in upper layers. - - Purity vs recovery tradeoff: higher valve speed improves purity but reduces recovery due to higher tail-gas fraction. -- Recommendations: two-stage approach to decouple H2O/CO2 removal; recycle second-stage tail gas to inlet to improve overall recovery (with N2 buildup caution). - -### PSA Manuals and Proposals (PDF Extracts) - -Source: `Xebec PSA Manual.pdf`, `30784 IOM manual.pdf`, `H16-1061 PSA upgrading proposal G4 rev 06.pdf`, `H16-1061 PSA upgrading proposal G4 rev 06 with Perez notes from 2019 01 17 9am call with Xebec - Internal Use Only.pdf`, `H17-1016 PSA upgrading proposal G2 rev 01 (002).pdf`. - -- Manual titles and models: - - Xebec Rapid-Cycle PSA IOM (Rev00, 31/03/2020), 2-stage PSA: 30" dia (G4) + 12" dia (G2), models `PSA30-90H-G4-250ASME-120VAC` and `PSA12-70H-G2-250ASME-120VAC`, Job 33090. - - Xebec Rapid-Cycle PSA IOM (Rev0, March 2018), model `PSA3H-G2-330A-480`, Job 30784. -- Proposal highlights (G4/G2 twin stage, Arlington OR): - - 9-bed fast-cycle PSA with triple equalization per stage; rotary valves (<1 CPM typical); pre-loaded adsorbents; adjustable cycle speed. - - Design basis flow cases: - - HIGH: Raw feed 1100 scfm; product 263 scfm; exhaust 837 scfm; feed pressure >200 psig; product ~180 psig; exhaust ~2 psig. Guaranteed recovery ~70.6% with product purity >99.999% H2. - - LOW: Raw feed 600 scfm; product 147 scfm; exhaust 453 scfm; feed pressure >200 psig; product ~180 psig; exhaust ~2 psig. Guaranteed recovery ~72.9% with product purity >99.999% H2. - - Product impurity targets (HIGH case, product stream): CO2 <1 ppmv, CO <0.2 ppmv, CH4 <2 ppmv; O2/N2 ~0. - - Process conditions: feed water at 100% RH, zero liquid water; zero particulates >0.5 micron; ±10 C product/exhaust temp from feed. - - Operating margin: +10% feed flow or contaminants handled by +10% valve speed with ~0.5% recovery reduction. -- Proposal highlights (G2 small unit): - - High-pressure case: feed 22 scfm at 290 psig; product 5 scfm at 275 psig; exhaust 17 scfm at 2 psig; expected recovery 73% at 99.999% purity. - - Low-pressure case: feed 14 scfm at 160 psig; product 3 scfm at 145 psig; exhaust 11 scfm at 2 psig; expected recovery 70% at 99.999% purity. - - Process conditions: 100% RH, zero liquid water; zero particulates >0.5 micron; stated nominal composition tolerance ±2.5%. - -### PDFs and Drawings Pending Extraction - -Remaining items not yet extracted: - -- `3041-PID-ALL-2024-03-21.pdf` -- `3041 PSA drawing.PDF` diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/psa_stage_removal_model.xlsx b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/psa_stage_removal_model.xlsx deleted file mode 100644 index 286075eb7f..0000000000 Binary files a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/psa_stage_removal_model.xlsx and /dev/null differ diff --git a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/psa_stage_removal_sensitivity.ipynb b/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/psa_stage_removal_sensitivity.ipynb deleted file mode 100644 index 2c5d70757f..0000000000 --- a/src/shared/python/upstream_drift_tools/process_calculators/psa_package/References/psa_stage_removal_sensitivity.ipynb +++ /dev/null @@ -1,935 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "424dabc9", - "metadata": {}, - "source": [ - "# PSA Two-Stage Removal Sensitivity (Multi-Component + Steady State)\n", - "\n", - "This notebook models a two-stage PSA system for **H2, CO, CO2, H2O, N2, O2, CH4**. It supports:\n", - "\n", - "- Different inlet compositions\n", - "- Different removal efficiencies per component in Stage 1 and Stage 2\n", - "- Optional recycle of Stage 2 tail gas back to Stage 1 feed\n", - "- **Steady-state** solution for recycle loops (fixed-point iteration)\n", - "\n", - "**Assumptions**\n", - "- Total fresh feed basis = 1.0 (fractional flow).\n", - "- Removal efficiencies represent the fraction of each component removed to that stage's **tail gas**.\n", - "- No removal of a component unless specified (0–1 range).\n", - "- Stage tail gas contains only removed fractions of each component.\n", - "- Stage product gas contains the remaining fractions of each component.\n", - "- Recycle is a fraction of Stage 2 tail gas; the rest vents.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9e004d9b", - "metadata": {}, - "outputs": [], - "source": [ - "import pandas as pd\n", - "\n", - "COMPONENTS = [\"H2\", \"CO\", \"CO2\", \"H2O\", \"N2\", \"O2\", \"CH4\"]\n", - "\n", - "\n", - "def normalize(comp):\n", - " total = sum(comp.values())\n", - " if total <= 0:\n", - " raise ValueError(\"Total composition must be > 0\")\n", - " return {k: v / total for k, v in comp.items()}\n", - "\n", - "\n", - "def apply_removal(stream, removal):\n", - " # stream/removal are dicts by component\n", - " tail = {k: stream[k] * removal[k] for k in COMPONENTS}\n", - " prod = {k: stream[k] - tail[k] for k in COMPONENTS}\n", - " return prod, tail\n", - "\n", - "\n", - "def comp_to_series(comp):\n", - " total = sum(comp.values())\n", - " if total == 0:\n", - " return pd.Series(dict.fromkeys(COMPONENTS, 0) | {\"Total\": 0.0})\n", - " return pd.Series({k: comp[k] / total for k in COMPONENTS} | {\"Total\": total})\n", - "\n", - "\n", - "def steady_state(fresh_feed, r1, r2, recycle_frac=0.0, tol=1e-8, max_iter=200):\n", - " # Normalize fresh feed\n", - " fresh = normalize(fresh_feed)\n", - "\n", - " # Initialize recycle stream\n", - " recycle = dict.fromkeys(COMPONENTS, 0.0)\n", - "\n", - " for _ in range(max_iter):\n", - " # Stage 1 feed = fresh + recycle\n", - " s1_feed = {k: fresh[k] + recycle[k] for k in COMPONENTS}\n", - " # Stage 1 removal\n", - " s1_prod, s1_tail = apply_removal(s1_feed, r1)\n", - " # Stage 2 feed = Stage 1 product\n", - " s2_feed = s1_prod\n", - " # Stage 2 removal\n", - " s2_prod, s2_tail = apply_removal(s2_feed, r2)\n", - "\n", - " # Recycle fraction of Stage 2 tail\n", - " new_recycle = {k: s2_tail[k] * recycle_frac for k in COMPONENTS}\n", - "\n", - " # Convergence check\n", - " diff = sum(abs(new_recycle[k] - recycle[k]) for k in COMPONENTS)\n", - " recycle = new_recycle\n", - " if diff < tol:\n", - " break\n", - "\n", - " # Compute vented (non-recycled) Stage 2 tail\n", - " s2_tail_vent = {k: s2_tail[k] * (1.0 - recycle_frac) for k in COMPONENTS}\n", - "\n", - " # Compose summary\n", - " streams = {\n", - " \"Fresh Feed\": fresh,\n", - " \"Stage 1 Feed\": s1_feed,\n", - " \"Stage 1 Product\": s1_prod,\n", - " \"Stage 1 Tail\": s1_tail,\n", - " \"Stage 2 Feed\": s2_feed,\n", - " \"Stage 2 Product\": s2_prod,\n", - " \"Stage 2 Tail (total)\": s2_tail,\n", - " \"Stage 2 Tail Vent\": s2_tail_vent,\n", - " \"Recycle to Stage 1\": recycle,\n", - " }\n", - "\n", - " df = pd.DataFrame({name: comp_to_series(comp) for name, comp in streams.items()}).T\n", - " return df" - ] - }, - { - "cell_type": "markdown", - "id": "3310297e", - "metadata": {}, - "source": [ - "## Example\n", - "\n", - "- Feed: H2=0.60, CO=0.10, CO2=0.10, H2O=0.02, N2=0.15, O2=0.02, CH4=0.01\n", - "- Stage 1 removals: CO2/H2O high; N2/O2 moderate; CO moderate; H2 minimal\n", - "- Stage 2 removals: N2/O2/CO high; CO2/H2O moderate; H2 minimal\n", - "- Recycle fraction: 0.5 of Stage 2 tail\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7dc5d8bc", - "metadata": {}, - "outputs": [], - "source": [ - "fresh_feed = {\n", - " \"H2\": 0.60,\n", - " \"CO\": 0.10,\n", - " \"CO2\": 0.10,\n", - " \"H2O\": 0.02,\n", - " \"N2\": 0.15,\n", - " \"O2\": 0.02,\n", - " \"CH4\": 0.01,\n", - "}\n", - "\n", - "r1 = {\n", - " \"H2\": 0.02,\n", - " \"CO\": 0.30,\n", - " \"CO2\": 0.80,\n", - " \"H2O\": 0.85,\n", - " \"N2\": 0.30,\n", - " \"O2\": 0.30,\n", - " \"CH4\": 0.20,\n", - "}\n", - "r2 = {\n", - " \"H2\": 0.01,\n", - " \"CO\": 0.80,\n", - " \"CO2\": 0.50,\n", - " \"H2O\": 0.50,\n", - " \"N2\": 0.90,\n", - " \"O2\": 0.90,\n", - " \"CH4\": 0.30,\n", - "}\n", - "\n", - "df = steady_state(fresh_feed, r1, r2, recycle_frac=0.5)\n", - "\n", - "df.style.format(dict.fromkeys(df.columns, \"{:.4f}\"))" - ] - }, - { - "cell_type": "markdown", - "id": "3025e794", - "metadata": {}, - "source": [ - "## Interactive Sliders (Simple UI)\n", - "\n", - "If `ipywidgets` is available, this will show sliders. If not, install it with:\n", - "\n", - "```bash\n", - "python3 -m pip install --user ipywidgets\n", - "```\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5b80b364", - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " import ipywidgets as widgets\n", - " from IPython.display import display\n", - "\n", - " # Feed sliders\n", - " feed_sliders = {\n", - " \"H2\": widgets.FloatSlider(\n", - " value=0.1, min=0.0, max=1.0, step=0.01, description=\"Feed H2\"\n", - " ),\n", - " \"CO\": widgets.FloatSlider(\n", - " value=0.1, min=0.0, max=1.0, step=0.01, description=\"Feed CO\"\n", - " ),\n", - " \"CO2\": widgets.FloatSlider(\n", - " value=0.1, min=0.0, max=1.0, step=0.01, description=\"Feed CO2\"\n", - " ),\n", - " \"H2O\": widgets.FloatSlider(\n", - " value=0.1, min=0.0, max=1.0, step=0.01, description=\"Feed H2O\"\n", - " ),\n", - " \"N2\": widgets.FloatSlider(\n", - " value=0.1, min=0.0, max=1.0, step=0.01, description=\"Feed N2\"\n", - " ),\n", - " \"O2\": widgets.FloatSlider(\n", - " value=0.1, min=0.0, max=1.0, step=0.01, description=\"Feed O2\"\n", - " ),\n", - " \"CH4\": widgets.FloatSlider(\n", - " value=0.1, min=0.0, max=1.0, step=0.01, description=\"Feed CH4\"\n", - " ),\n", - " }\n", - "\n", - " # Stage 1 removal sliders\n", - " r1_sliders = {\n", - " \"H2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem H2\"\n", - " ),\n", - " \"CO\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem CO\"\n", - " ),\n", - " \"CO2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem CO2\"\n", - " ),\n", - " \"H2O\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem H2O\"\n", - " ),\n", - " \"N2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem N2\"\n", - " ),\n", - " \"O2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem O2\"\n", - " ),\n", - " \"CH4\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem CH4\"\n", - " ),\n", - " }\n", - "\n", - " # Stage 2 removal sliders\n", - " r2_sliders = {\n", - " \"H2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem H2\"\n", - " ),\n", - " \"CO\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem CO\"\n", - " ),\n", - " \"CO2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem CO2\"\n", - " ),\n", - " \"H2O\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem H2O\"\n", - " ),\n", - " \"N2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem N2\"\n", - " ),\n", - " \"O2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem O2\"\n", - " ),\n", - " \"CH4\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem CH4\"\n", - " ),\n", - " }\n", - "\n", - " recycle = widgets.FloatSlider(\n", - " value=0.0, min=0.0, max=1.0, step=0.05, description=\"Recycle frac\"\n", - " )\n", - "\n", - " def update(*args):\n", - " feed = {k: v.value for k, v in feed_sliders.items()}\n", - " r1 = {k: v.value for k, v in r1_sliders.items()}\n", - " r2 = {k: v.value for k, v in r2_sliders.items()}\n", - " df = steady_state(feed, r1, r2, recycle_frac=recycle.value)\n", - " display(df.style.format(dict.fromkeys(df.columns, \"{:.4f}\")))\n", - "\n", - " for group in [feed_sliders, r1_sliders, r2_sliders]:\n", - " for w in group.values():\n", - " w.observe(lambda change: update(), names=\"value\")\n", - " recycle.observe(lambda change: update(), names=\"value\")\n", - "\n", - " feed_box = widgets.VBox(list(feed_sliders.values()))\n", - " r1_box = widgets.VBox(list(r1_sliders.values()))\n", - " r2_box = widgets.VBox(list(r2_sliders.values()))\n", - " tabs = widgets.Tab(children=[feed_box, r1_box, r2_box])\n", - " tabs.set_title(0, \"Feed\")\n", - " tabs.set_title(1, \"Stage 1 Removal\")\n", - " tabs.set_title(2, \"Stage 2 Removal\")\n", - " display(recycle)\n", - " display(tabs)\n", - " update()\n", - "\n", - "except Exception as e: # noqa: BLE001 — notebook widget init; graceful fallback on missing ipywidgets\n", - " print(\"ipywidgets not available or failed to load:\", e)\n", - " print(\"You can still run steady_state(...) manually.\")" - ] - }, - { - "cell_type": "markdown", - "id": "51077016", - "metadata": {}, - "source": [ - "## SCFM Mode (Absolute Flow Basis)\n", - "\n", - "This section lets you specify **total fresh feed flow (SCFM)** and inlet composition (%). It reports absolute SCFM for each stream (stage products and tails), plus composition.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "91a28991", - "metadata": {}, - "outputs": [], - "source": [ - "def steady_state_scfm(\n", - " total_feed_scfm,\n", - " feed_pct,\n", - " r1,\n", - " r2,\n", - " recycle_frac=0.0,\n", - " normalize_feed=True,\n", - " tol=1e-8,\n", - " max_iter=200,\n", - "):\n", - " # Convert % to fractional composition\n", - " feed_frac = {k: feed_pct[k] / 100.0 for k in COMPONENTS}\n", - " # Normalize in case % do not sum to 100 (optional)\n", - " if normalize_feed:\n", - " feed_frac = normalize(feed_frac)\n", - "\n", - " # Convert to absolute SCFM\n", - " fresh = {k: feed_frac[k] * total_feed_scfm for k in COMPONENTS}\n", - "\n", - " # Reuse steady-state solver logic on absolute flows\n", - " recycle = dict.fromkeys(COMPONENTS, 0.0)\n", - "\n", - " for _ in range(max_iter):\n", - " s1_feed = {k: fresh[k] + recycle[k] for k in COMPONENTS}\n", - " s1_prod, s1_tail = apply_removal(s1_feed, r1)\n", - " s2_feed = s1_prod\n", - " s2_prod, s2_tail = apply_removal(s2_feed, r2)\n", - "\n", - " new_recycle = {k: s2_tail[k] * recycle_frac for k in COMPONENTS}\n", - " diff = sum(abs(new_recycle[k] - recycle[k]) for k in COMPONENTS)\n", - " recycle = new_recycle\n", - " if diff < tol:\n", - " break\n", - "\n", - " s2_tail_vent = {k: s2_tail[k] * (1.0 - recycle_frac) for k in COMPONENTS}\n", - "\n", - " streams = {\n", - " \"Fresh Feed\": fresh,\n", - " \"Stage 1 Feed\": s1_feed,\n", - " \"Stage 1 Product\": s1_prod,\n", - " \"Stage 1 Tail\": s1_tail,\n", - " \"Stage 2 Feed\": s2_feed,\n", - " \"Stage 2 Product\": s2_prod,\n", - " \"Stage 2 Tail (total)\": s2_tail,\n", - " \"Stage 2 Tail Vent\": s2_tail_vent,\n", - " \"Recycle to Stage 1\": recycle,\n", - " }\n", - "\n", - " # Build two tables: composition (%) and absolute SCFM\n", - " comp_rows = {}\n", - " flow_rows = {}\n", - " for name, comp in streams.items():\n", - " total = sum(comp.values())\n", - " if total == 0:\n", - " comp_rows[name] = dict.fromkeys(COMPONENTS, 0.0) | {\"Total SCFM\": 0.0}\n", - " flow_rows[name] = dict.fromkeys(COMPONENTS, 0.0) | {\"Total SCFM\": 0.0}\n", - " else:\n", - " comp_rows[name] = {k: (comp[k] / total) * 100.0 for k in COMPONENTS} | {\n", - " \"Total SCFM\": total\n", - " }\n", - " flow_rows[name] = {k: comp[k] for k in COMPONENTS} | {\"Total SCFM\": total}\n", - "\n", - " comp_df = pd.DataFrame(comp_rows).T\n", - " flow_df = pd.DataFrame(flow_rows).T\n", - " return comp_df, flow_df" - ] - }, - { - "cell_type": "markdown", - "id": "1172725b", - "metadata": {}, - "source": [ - "### Example (SCFM)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4297d1dd", - "metadata": {}, - "outputs": [], - "source": [ - "total_feed_scfm = 100.0\n", - "feed_pct = {\"H2\": 60, \"CO\": 10, \"CO2\": 10, \"H2O\": 2, \"N2\": 15, \"O2\": 2, \"CH4\": 1}\n", - "\n", - "r1 = {\n", - " \"H2\": 0.02,\n", - " \"CO\": 0.30,\n", - " \"CO2\": 0.80,\n", - " \"H2O\": 0.85,\n", - " \"N2\": 0.30,\n", - " \"O2\": 0.30,\n", - " \"CH4\": 0.20,\n", - "}\n", - "r2 = {\n", - " \"H2\": 0.01,\n", - " \"CO\": 0.80,\n", - " \"CO2\": 0.50,\n", - " \"H2O\": 0.50,\n", - " \"N2\": 0.90,\n", - " \"O2\": 0.90,\n", - " \"CH4\": 0.30,\n", - "}\n", - "\n", - "comp_df, flow_df = steady_state_scfm(\n", - " total_feed_scfm, feed_pct, r1, r2, recycle_frac=0.5\n", - ")\n", - "\n", - "print(\"Composition (% vol) by stream\")\n", - "comp_df.style.format(\"{:.2f}\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "51202e5e", - "metadata": {}, - "outputs": [], - "source": [ - "print(\"Absolute flow (SCFM) by stream\")\n", - "flow_df.style.format(\"{:.2f}\")" - ] - }, - { - "cell_type": "markdown", - "id": "5cef650b", - "metadata": {}, - "source": [ - "### Interactive SCFM UI\n", - "\n", - "Sliders for total feed flow (SCFM), feed composition (%), per‑stage removals, and recycle fraction.\n" - ] - }, - { - "cell_type": "markdown", - "id": "aa772b43", - "metadata": {}, - "source": [ - "### Feed % Normalization\n", - "\n", - "The SCFM UI includes an **Auto-normalize feed %** checkbox.\n", - "- When enabled, feed % values are normalized to 100 automatically.\n", - "- When disabled, a warning appears if the sum is not 100; calculations still normalize to keep mass balance.\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6e4c3d24", - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " import ipywidgets as widgets\n", - " from IPython.display import display\n", - "\n", - " total_feed = widgets.FloatSlider(\n", - " value=100.0, min=1.0, max=2000.0, step=1.0, description=\"Feed SCFM\"\n", - " )\n", - " auto_normalize = widgets.Checkbox(value=True, description=\"Auto-normalize feed %\")\n", - " warn_out = widgets.Output()\n", - "\n", - " feed_pct_sliders = {\n", - " \"H2\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % H2\"\n", - " ),\n", - " \"CO\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % CO\"\n", - " ),\n", - " \"CO2\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % CO2\"\n", - " ),\n", - " \"H2O\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % H2O\"\n", - " ),\n", - " \"N2\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % N2\"\n", - " ),\n", - " \"O2\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % O2\"\n", - " ),\n", - " \"CH4\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % CH4\"\n", - " ),\n", - " }\n", - "\n", - " r1_sliders = {\n", - " \"H2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem H2\"\n", - " ),\n", - " \"CO\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem CO\"\n", - " ),\n", - " \"CO2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem CO2\"\n", - " ),\n", - " \"H2O\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem H2O\"\n", - " ),\n", - " \"N2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem N2\"\n", - " ),\n", - " \"O2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem O2\"\n", - " ),\n", - " \"CH4\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem CH4\"\n", - " ),\n", - " }\n", - "\n", - " r2_sliders = {\n", - " \"H2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem H2\"\n", - " ),\n", - " \"CO\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem CO\"\n", - " ),\n", - " \"CO2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem CO2\"\n", - " ),\n", - " \"H2O\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem H2O\"\n", - " ),\n", - " \"N2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem N2\"\n", - " ),\n", - " \"O2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem O2\"\n", - " ),\n", - " \"CH4\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem CH4\"\n", - " ),\n", - " }\n", - "\n", - " recycle = widgets.FloatSlider(\n", - " value=0.0, min=0.0, max=1.0, step=0.05, description=\"Recycle frac\"\n", - " )\n", - "\n", - " def update(*args):\n", - " feed_pct = {k: v.value for k, v in feed_pct_sliders.items()}\n", - " r1 = {k: v.value for k, v in r1_sliders.items()}\n", - " r2 = {k: v.value for k, v in r2_sliders.items()}\n", - " total_pct = sum(feed_pct.values())\n", - " with warn_out:\n", - " warn_out.clear_output()\n", - " if not auto_normalize.value and abs(total_pct - 100.0) > 1e-6:\n", - " print(\n", - " f\"Warning: feed % sums to {total_pct:.2f}, normalizing to 100 for calculations.\"\n", - " )\n", - " comp_df, flow_df = steady_state_scfm(\n", - " total_feed.value,\n", - " feed_pct,\n", - " r1,\n", - " r2,\n", - " recycle_frac=recycle.value,\n", - " normalize_feed=True,\n", - " )\n", - " display(comp_df.style.format(\"{:.2f}\"))\n", - " display(flow_df.style.format(\"{:.2f}\"))\n", - "\n", - " for group in [feed_pct_sliders, r1_sliders, r2_sliders]:\n", - " for w in group.values():\n", - " w.observe(lambda change: update(), names=\"value\")\n", - " total_feed.observe(lambda change: update(), names=\"value\")\n", - " recycle.observe(lambda change: update(), names=\"value\")\n", - " auto_normalize.observe(lambda change: update(), names=\"value\")\n", - "\n", - " feed_box = widgets.VBox(list(feed_pct_sliders.values()))\n", - " r1_box = widgets.VBox(list(r1_sliders.values()))\n", - " r2_box = widgets.VBox(list(r2_sliders.values()))\n", - " tabs = widgets.Tab(children=[feed_box, r1_box, r2_box])\n", - " tabs.set_title(0, \"Feed %\")\n", - " tabs.set_title(1, \"Stage 1 Removal\")\n", - " tabs.set_title(2, \"Stage 2 Removal\")\n", - " display(total_feed)\n", - " display(auto_normalize)\n", - " display(warn_out)\n", - " display(recycle)\n", - " display(tabs)\n", - " update()\n", - "\n", - "except Exception as e: # noqa: BLE001 — notebook widget init; graceful fallback on missing ipywidgets\n", - " print(\"ipywidgets not available or failed to load:\", e)\n", - " print(\"You can still run steady_state_scfm(...) manually.\")" - ] - }, - { - "cell_type": "markdown", - "id": "3c993799", - "metadata": {}, - "source": [ - "## Stream Map (per diagram)\n", - "\n", - "This section aligns model streams to the diagram:\n", - "\n", - "- **1**: Fresh feed\n", - "- **4**: PSA2 tail recycle to mixed feed\n", - "- **5A**: Mixed feed (fresh + recycle)\n", - "- **5B**: Compressor inlet (same as 5A in this simple model)\n", - "- **5C**: Compressor outlet (composition unchanged)\n", - "- **PSA1**: Stage 1\n", - "- **2**: Stage 1 exhaust (tail)\n", - "- **6**: Interstage (Stage 1 product to Stage 2 feed)\n", - "- **PSA2**: Stage 2\n", - "- **3G**: Gross product (Stage 2 product)\n", - "- **3R**: Product recycle (fraction of 3G)\n", - "- **3N**: Net product (3G minus 3R)\n" - ] - }, - { - "cell_type": "markdown", - "id": "f011733e", - "metadata": {}, - "source": [ - "**Diagram reference**: The stream numbering here follows the attached diagram from the prompt.\n", - "\n", - "If you save that image as `psa_streams.png` in this folder, you can embed it with:\n", - "\n", - "```markdown\n", - "![](psa_streams.png)\n", - "```\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "30ba8c7a", - "metadata": {}, - "outputs": [], - "source": [ - "def steady_state_scfm_with_product_recycle(\n", - " total_feed_scfm,\n", - " feed_pct,\n", - " r1,\n", - " r2,\n", - " tail_recycle_frac=0.0,\n", - " product_recycle_frac=0.0,\n", - " normalize_feed=True,\n", - " tol=1e-8,\n", - " max_iter=200,\n", - "):\n", - " # Convert % to fractional composition\n", - " feed_frac = {k: feed_pct[k] / 100.0 for k in COMPONENTS}\n", - " if normalize_feed:\n", - " feed_frac = normalize(feed_frac)\n", - "\n", - " fresh = {k: feed_frac[k] * total_feed_scfm for k in COMPONENTS}\n", - "\n", - " tail_recycle = dict.fromkeys(COMPONENTS, 0.0)\n", - " product_recycle = dict.fromkeys(COMPONENTS, 0.0)\n", - "\n", - " for _ in range(max_iter):\n", - " mixed_feed = {\n", - " k: fresh[k] + tail_recycle[k] + product_recycle[k] for k in COMPONENTS\n", - " } # 5A/5B\n", - " s1_prod, s1_tail = apply_removal(mixed_feed, r1) # s1_tail = stream 2\n", - " s2_feed = s1_prod # stream 6\n", - " s2_prod, s2_tail = apply_removal(s2_feed, r2) # s2_prod = 3G, s2_tail = tail\n", - "\n", - " new_tail_recycle = {\n", - " k: s2_tail[k] * tail_recycle_frac for k in COMPONENTS\n", - " } # stream 4\n", - " new_product_recycle = {\n", - " k: s2_prod[k] * product_recycle_frac for k in COMPONENTS\n", - " } # stream 3R\n", - "\n", - " diff = sum(\n", - " abs(new_tail_recycle[k] - tail_recycle[k])\n", - " + abs(new_product_recycle[k] - product_recycle[k])\n", - " for k in COMPONENTS\n", - " )\n", - " tail_recycle = new_tail_recycle\n", - " product_recycle = new_product_recycle\n", - " if diff < tol:\n", - " break\n", - "\n", - " s2_tail_vent = {k: s2_tail[k] * (1.0 - tail_recycle_frac) for k in COMPONENTS}\n", - " net_product = {k: s2_prod[k] * (1.0 - product_recycle_frac) for k in COMPONENTS}\n", - "\n", - " streams = {\n", - " \"1 Fresh Feed\": fresh,\n", - " \"4 Tail Recycle\": tail_recycle,\n", - " \"5A Mixed Feed\": mixed_feed,\n", - " \"5B Compressor Inlet\": mixed_feed,\n", - " \"5C Compressor Outlet\": mixed_feed,\n", - " \"2 Stage 1 Tail\": s1_tail,\n", - " \"6 Interstage\": s2_feed,\n", - " \"3G Gross Product\": s2_prod,\n", - " \"3R Product Recycle\": product_recycle,\n", - " \"3N Net Product\": net_product,\n", - " \"Stage 2 Tail (total)\": s2_tail,\n", - " \"Stage 2 Tail Vent\": s2_tail_vent,\n", - " }\n", - "\n", - " comp_rows = {}\n", - " flow_rows = {}\n", - " for name, comp in streams.items():\n", - " total = sum(comp.values())\n", - " if total == 0:\n", - " comp_rows[name] = dict.fromkeys(COMPONENTS, 0.0) | {\"Total SCFM\": 0.0}\n", - " flow_rows[name] = dict.fromkeys(COMPONENTS, 0.0) | {\"Total SCFM\": 0.0}\n", - " else:\n", - " comp_rows[name] = {k: (comp[k] / total) * 100.0 for k in COMPONENTS} | {\n", - " \"Total SCFM\": total\n", - " }\n", - " flow_rows[name] = {k: comp[k] for k in COMPONENTS} | {\"Total SCFM\": total}\n", - "\n", - " comp_df = pd.DataFrame(comp_rows).T\n", - " flow_df = pd.DataFrame(flow_rows).T\n", - " return comp_df, flow_df" - ] - }, - { - "cell_type": "markdown", - "id": "b4ed3de4", - "metadata": {}, - "source": [ - "### Example (SCFM, with product recycle)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "a0c1e5c3", - "metadata": {}, - "outputs": [], - "source": [ - "total_feed_scfm = 100.0\n", - "feed_pct = {\"H2\": 60, \"CO\": 10, \"CO2\": 10, \"H2O\": 2, \"N2\": 15, \"O2\": 2, \"CH4\": 1}\n", - "\n", - "r1 = {\n", - " \"H2\": 0.02,\n", - " \"CO\": 0.30,\n", - " \"CO2\": 0.80,\n", - " \"H2O\": 0.85,\n", - " \"N2\": 0.30,\n", - " \"O2\": 0.30,\n", - " \"CH4\": 0.20,\n", - "}\n", - "r2 = {\n", - " \"H2\": 0.01,\n", - " \"CO\": 0.80,\n", - " \"CO2\": 0.50,\n", - " \"H2O\": 0.50,\n", - " \"N2\": 0.90,\n", - " \"O2\": 0.90,\n", - " \"CH4\": 0.30,\n", - "}\n", - "\n", - "comp_df, flow_df = steady_state_scfm_with_product_recycle(\n", - " total_feed_scfm, feed_pct, r1, r2, tail_recycle_frac=0.5, product_recycle_frac=0.1\n", - ")\n", - "\n", - "print(\"Composition (% vol) by stream\")\n", - "comp_df.style.format(\"{:.2f}\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3a0c3c27", - "metadata": {}, - "outputs": [], - "source": [ - "print(\"Absolute flow (SCFM) by stream\")\n", - "flow_df.style.format(\"{:.2f}\")" - ] - }, - { - "cell_type": "markdown", - "id": "fa074cd6", - "metadata": {}, - "source": [ - "### Interactive SCFM UI (Tail + Product Recycle)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "64d08a42", - "metadata": {}, - "outputs": [], - "source": [ - "try:\n", - " import ipywidgets as widgets\n", - " from IPython.display import display\n", - "\n", - " total_feed = widgets.FloatSlider(\n", - " value=100.0, min=1.0, max=2000.0, step=1.0, description=\"Feed SCFM\"\n", - " )\n", - " auto_normalize = widgets.Checkbox(value=True, description=\"Auto-normalize feed %\")\n", - " warn_out = widgets.Output()\n", - "\n", - " feed_pct_sliders = {\n", - " \"H2\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % H2\"\n", - " ),\n", - " \"CO\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % CO\"\n", - " ),\n", - " \"CO2\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % CO2\"\n", - " ),\n", - " \"H2O\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % H2O\"\n", - " ),\n", - " \"N2\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % N2\"\n", - " ),\n", - " \"O2\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % O2\"\n", - " ),\n", - " \"CH4\": widgets.FloatSlider(\n", - " value=10.0, min=0.0, max=100.0, step=0.5, description=\"Feed % CH4\"\n", - " ),\n", - " }\n", - "\n", - " r1_sliders = {\n", - " \"H2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem H2\"\n", - " ),\n", - " \"CO\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem CO\"\n", - " ),\n", - " \"CO2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem CO2\"\n", - " ),\n", - " \"H2O\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem H2O\"\n", - " ),\n", - " \"N2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem N2\"\n", - " ),\n", - " \"O2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem O2\"\n", - " ),\n", - " \"CH4\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S1 rem CH4\"\n", - " ),\n", - " }\n", - "\n", - " r2_sliders = {\n", - " \"H2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem H2\"\n", - " ),\n", - " \"CO\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem CO\"\n", - " ),\n", - " \"CO2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem CO2\"\n", - " ),\n", - " \"H2O\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem H2O\"\n", - " ),\n", - " \"N2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem N2\"\n", - " ),\n", - " \"O2\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem O2\"\n", - " ),\n", - " \"CH4\": widgets.FloatSlider(\n", - " value=0.5, min=0.0, max=1.0, step=0.01, description=\"S2 rem CH4\"\n", - " ),\n", - " }\n", - "\n", - " tail_recycle = widgets.FloatSlider(\n", - " value=0.0, min=0.0, max=1.0, step=0.05, description=\"Tail recycle\"\n", - " )\n", - " product_recycle = widgets.FloatSlider(\n", - " value=0.0, min=0.0, max=1.0, step=0.05, description=\"Product recycle\"\n", - " )\n", - "\n", - " def update(*args):\n", - " feed_pct = {k: v.value for k, v in feed_pct_sliders.items()}\n", - " r1 = {k: v.value for k, v in r1_sliders.items()}\n", - " r2 = {k: v.value for k, v in r2_sliders.items()}\n", - " total_pct = sum(feed_pct.values())\n", - " with warn_out:\n", - " warn_out.clear_output()\n", - " if not auto_normalize.value and abs(total_pct - 100.0) > 1e-6:\n", - " print(\n", - " f\"Warning: feed % sums to {total_pct:.2f}, normalizing to 100 for calculations.\"\n", - " )\n", - " comp_df, flow_df = steady_state_scfm_with_product_recycle(\n", - " total_feed.value,\n", - " feed_pct,\n", - " r1,\n", - " r2,\n", - " tail_recycle_frac=tail_recycle.value,\n", - " product_recycle_frac=product_recycle.value,\n", - " normalize_feed=True,\n", - " )\n", - " display(comp_df.style.format(\"{:.2f}\"))\n", - " display(flow_df.style.format(\"{:.2f}\"))\n", - "\n", - " for group in [feed_pct_sliders, r1_sliders, r2_sliders]:\n", - " for w in group.values():\n", - " w.observe(lambda change: update(), names=\"value\")\n", - " total_feed.observe(lambda change: update(), names=\"value\")\n", - " auto_normalize.observe(lambda change: update(), names=\"value\")\n", - " tail_recycle.observe(lambda change: update(), names=\"value\")\n", - " product_recycle.observe(lambda change: update(), names=\"value\")\n", - "\n", - " feed_box = widgets.VBox(list(feed_pct_sliders.values()))\n", - " r1_box = widgets.VBox(list(r1_sliders.values()))\n", - " r2_box = widgets.VBox(list(r2_sliders.values()))\n", - " tabs = widgets.Tab(children=[feed_box, r1_box, r2_box])\n", - " tabs.set_title(0, \"Feed %\")\n", - " tabs.set_title(1, \"Stage 1 Removal\")\n", - " tabs.set_title(2, \"Stage 2 Removal\")\n", - " display(total_feed)\n", - " display(auto_normalize)\n", - " display(warn_out)\n", - " display(tail_recycle)\n", - " display(product_recycle)\n", - " display(tabs)\n", - " update()\n", - "\n", - "except Exception as e: # noqa: BLE001 — notebook widget init; graceful fallback on missing ipywidgets\n", - " print(\"ipywidgets not available or failed to load:\", e)\n", - " print(\"You can still run steady_state_scfm_with_product_recycle(...) manually.\")" - ] - } - ], - "metadata": {}, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/src/shared/python/upstream_drift_tools/tests/test_data_processing_io.py b/src/shared/python/upstream_drift_tools/tests/test_data_processing_io.py index 9ef4b4c574..410dc7e618 100644 --- a/src/shared/python/upstream_drift_tools/tests/test_data_processing_io.py +++ b/src/shared/python/upstream_drift_tools/tests/test_data_processing_io.py @@ -95,7 +95,7 @@ def test_read_unsupported_format_raises(self): from upstream_drift_tools.data_processing.io import DataReader with pytest.raises(ValueError, match="Unsupported or undetected"): - DataReader.read_file("/tmp/file.xyz_unknown_ext") + DataReader.read_file("/tmp/file.xyz_unknown_ext") # nosec B108 def test_read_parquet_no_pyarrow_raises(self): """If PYARROW_AVAILABLE is False, reading parquet should raise ImportError.""" @@ -106,7 +106,7 @@ def test_read_parquet_no_pyarrow_raises(self): try: io_mod.PYARROW_AVAILABLE = False with pytest.raises(ImportError, match="PyArrow"): - DataReader.read_file("/tmp/fake.parquet") + DataReader.read_file("/tmp/fake.parquet") # nosec B108 finally: io_mod.PYARROW_AVAILABLE = original @@ -189,7 +189,7 @@ def test_write_parquet_no_pyarrow_raises(self, sample_df: pd.DataFrame): try: io_mod.PYARROW_AVAILABLE = False with pytest.raises(ImportError, match="PyArrow"): - DataWriter.write_file(sample_df, "/tmp/fake.parquet") + DataWriter.write_file(sample_df, "/tmp/fake.parquet") # nosec B108 finally: io_mod.PYARROW_AVAILABLE = original @@ -321,7 +321,7 @@ def test_read_matlab_no_scipy_raises(self): try: io_mod.SCIPY_AVAILABLE = False with pytest.raises(ImportError, match="SciPy"): - DataReader.read_file("/tmp/fake.mat") + DataReader.read_file("/tmp/fake.mat") # nosec B108 finally: io_mod.SCIPY_AVAILABLE = original diff --git a/src/shared/python/upstream_drift_tools/tests/test_data_processor_engine.py b/src/shared/python/upstream_drift_tools/tests/test_data_processor_engine.py index 6d4a6b06f8..44b64a0bfd 100644 --- a/src/shared/python/upstream_drift_tools/tests/test_data_processor_engine.py +++ b/src/shared/python/upstream_drift_tools/tests/test_data_processor_engine.py @@ -153,14 +153,14 @@ class TestExportData: def test_no_data_raises(self): engine = DataProcessorEngine() with pytest.raises(DataNotLoadedError): - engine.export_data("/tmp/out.csv") + engine.export_data("/tmp/out.csv") # nosec B108 def test_export_success(self): engine = _make_engine_with_data() with patch( "upstream_drift_tools.data_processing.core.DataWriter.write_file" ) as mock_write: - result = engine.export_data("/tmp/out.csv", DataFormat.CSV) + result = engine.export_data("/tmp/out.csv", DataFormat.CSV) # nosec B108 mock_write.assert_called_once() assert result.success @@ -171,7 +171,7 @@ def test_export_oserror_raises(self): side_effect=OSError("disk full"), ): with pytest.raises(FileIOError, match="disk full"): - engine.export_data("/tmp/out.csv") + engine.export_data("/tmp/out.csv") # nosec B108 # --------------------------------------------------------------------------- diff --git a/src/shared/python/upstream_drift_tools/tests/test_data_processor_engine_errors.py b/src/shared/python/upstream_drift_tools/tests/test_data_processor_engine_errors.py index 7c75023637..addcdfe149 100644 --- a/src/shared/python/upstream_drift_tools/tests/test_data_processor_engine_errors.py +++ b/src/shared/python/upstream_drift_tools/tests/test_data_processor_engine_errors.py @@ -72,7 +72,7 @@ def test_load_file_with_invalid_format_raises(self): side_effect=ValueError("unsupported format"), ): with pytest.raises((FileIOError, ValueError)): - engine.load_file("/tmp/bad.xyz") + engine.load_file("/tmp/bad.xyz") # nosec B108 def test_load_file_empty_path_raises(self): from upstream_drift_tools.data_processing.core import DataProcessorEngine diff --git a/src/shared/python/upstream_drift_tools/tests/test_state_manager.py b/src/shared/python/upstream_drift_tools/tests/test_state_manager.py index b95f11c55d..b44365300f 100644 --- a/src/shared/python/upstream_drift_tools/tests/test_state_manager.py +++ b/src/shared/python/upstream_drift_tools/tests/test_state_manager.py @@ -134,7 +134,7 @@ def test_json_serializer(manager) -> Any: res = manager._json_serializer(dt) assert res == "2023-01-01T12:00:00" - p = Path("/tmp/foo") + p = Path("/tmp/foo") # nosec B108 res = manager._json_serializer(p) assert res == str(p) diff --git a/src/solar_system_model/solar_system/visualization/textures.py b/src/solar_system_model/solar_system/visualization/textures.py index 99497f0045..732990ae0e 100644 --- a/src/solar_system_model/solar_system/visualization/textures.py +++ b/src/solar_system_model/solar_system/visualization/textures.py @@ -80,7 +80,7 @@ def ensure_texture(self, body_name: str) -> pathlib.Path | None: path.parent.mkdir(parents=True, exist_ok=True) try: - urllib.request.urlretrieve(url, path) + urllib.request.urlretrieve(url, path) # nosec B310 except (ConnectionError, TimeoutError, OSError): return None diff --git a/src/urdf_builder_gui/tests/test_urdf_builder_gui.py b/src/urdf_builder_gui/tests/test_urdf_builder_gui.py index ec6f483e30..94530a25d9 100644 --- a/src/urdf_builder_gui/tests/test_urdf_builder_gui.py +++ b/src/urdf_builder_gui/tests/test_urdf_builder_gui.py @@ -349,7 +349,7 @@ def test_valid_xml_output(self) -> None: config = URDFConfig(robot_name="test_robot", height_m=1.75, mass_kg=70.0) xml = generate_urdf_xml(config) # Should parse without error - root = ET.fromstring(xml) + root = ET.fromstring(xml) # nosec B314 assert root.tag == "robot" assert root.attrib["name"] == "test_robot" @@ -357,7 +357,7 @@ def test_contains_expected_links(self) -> None: """Full Humanoid template should produce expected links.""" config = URDFConfig(template="Full Humanoid") xml = generate_urdf_xml(config) - root = ET.fromstring(xml) + root = ET.fromstring(xml) # nosec B314 link_names = {link.get("name") for link in root.findall("link")} assert "pelvis" in link_names assert "torso" in link_names @@ -368,7 +368,7 @@ def test_upper_body_only_excludes_legs(self) -> None: """Upper Body Only template should NOT include leg links.""" config = URDFConfig(template="Upper Body Only") xml = generate_urdf_xml(config) - root = ET.fromstring(xml) + root = ET.fromstring(xml) # nosec B314 link_names = {link.get("name") for link in root.findall("link")} assert "upper_arm_l" in link_names assert "thigh_l" not in link_names @@ -566,7 +566,7 @@ def test_preview_and_generate_same_template(self) -> None: assert seg in preview, f"{template}: {seg} not in preview" # Generated XML should have links for each segment - root = ET.fromstring(xml) + root = ET.fromstring(xml) # nosec B314 link_names = {link.get("name") for link in root.findall("link")} for seg in get_template_segments(template): assert seg in link_names, f"{template}: {seg} not in XML" @@ -580,7 +580,7 @@ def test_different_configs_produce_different_output(self) -> None: def test_all_inertia_values_positive(self) -> None: """Every inertia value in generated URDF must be positive.""" xml = generate_urdf_xml(URDFConfig()) - root = ET.fromstring(xml) + root = ET.fromstring(xml) # nosec B314 for inertia_el in root.iter("inertia"): for attr in ["ixx", "iyy", "izz"]: val = float(inertia_el.get(attr, "0"))