diff --git a/CITATION.cff b/CITATION.cff index e4e03dda..27a2e9d6 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -15,8 +15,8 @@ authors: email: "imranfadhil@gmail.com" repository-code: "https://github.com/imranfadhil/quick_pp" url: "https://quick-pp.readthedocs.io" -version: 0.3.3 -date-released: "2026-10-31" +version: 0.3.4 +date-released: "2026-11-03" license: MIT keywords: - petrophysics @@ -44,4 +44,4 @@ preferred-citation: orcid: "https://orcid.org/0000-0001-9392-8083" year: 2026 url: "https://github.com/imranfadhil/quick_pp" - version: 0.3.3 + version: 0.3.4 diff --git a/README.md b/README.md index 56bbf997..cdd1e711 100644 --- a/README.md +++ b/README.md @@ -331,6 +331,6 @@ If you use `quick_pp` in your academic research, coursework, publications, or te publisher = {GitHub}, journal = {GitHub repository}, howpublished = {\url{https://github.com/imranfadhil/quick_pp}}, - version = {0.3.3} + version = {0.3.4} } ``` diff --git a/docs/community/changelog.md b/docs/community/changelog.md index 921c2528..cffc6605 100644 --- a/docs/community/changelog.md +++ b/docs/community/changelog.md @@ -1,5 +1,15 @@ # Changelog +## 0.3.4 (2026-11-03) +- Added **Multi-Well Probabilistic Volumetric Assessment & Subsurface Risk Analysis** (`quick_pp/app/backend/services/workspace.py`, `MultiWellVolumetrics.svelte`, `VolumetricsCanvas.svelte`, `WorkflowStepper.svelte`) implementing Monte Carlo Latin Hypercube Sampling (LHS, $N \ge 10{,}000$) for STOIIP and GIIP across project wells and reservoir zones, supporting dual $S_w$-based and SHF-based (saturation-height function) modes, flexible parametric distributions (Normal, Log-Normal, Triangular, Uniform) for Area, Gross Thickness, NTG, $\phi_e$, $S_w$, $B_o/B_g$, and Recovery Factor, interactive parameter bounding, cumulative exceedance (CDF), probability density (PDF), and Tornado sensitivity ranking charts for P90 (1P/Proven), P50 (2P/Probable), P10 (3P/Possible), Mean, and Standard Deviation, and automated injection into Section 8 of FDP and Reservoir Summary reports. +- Added **Petrophysical Handover Package Import & Bi-Directional Exchange** (`quick_pp/handover.py`, `quick_pp/app/backend/services/database.py`, `HandoverImporter.svelte`, `BulkImporterModal.svelte`, `TopContextBar.svelte`, `tests/test_handover.py`) enabling end-to-end ingestion and project re-hydration from structured handover `.zip` archives (Techlog, Geolog, quick_pp), including pre-import archive inspection (`inspect_handover_archive`), manifest validation, well inventory reconciliation, and comprehensive database reconstruction of well headers, deviation surveys, continuous log curves (LAS 2.0/3.0), formation tops, fluid contacts, perforation intervals, core RCA sample point-sets (`RAW_DEPTH`, `SHIFT_M`, `CPORE`, `CPERM`), MDT/FPT formation pressure tests, and SCAL capillary pressure / relative permeability tables. +- Added **Field-Wide Fluid Contact Delineation & Contact Optimization** (`quick_pp/saturation.py`, `quick_pp/database/objects.py`, `quick_pp/app/backend/services/workspace.py`, `MultiWellShf.svelte`, `tests/test_fluid_contacts_and_limits.py`) with `extract_fluid_contacts_and_limits` to automatically identify and classify petrophysical fluid limits and contacts across project wells and zones: Gas limits (GUT, GDT, GOC, GWC), Oil limits (OUT, ODT, OWC), and Water limits (WUT, FWL) with explicit exclusion of non-physical WDT, robust Water-Up-To (WUT) assignment using net reservoir sand cutoffs (`phit_cutoff`, `vsh_cutoff`, `min_sand_thickness`), per-zone Free Water Level (`FWL`) and fluid contact persistence in `WellFluidContact` tables with automatic TVDSS-to-MD interpolation, and integrated contact table generation in Section 6 of the FDP report pipeline. +- Added **Multi-Well Parameters & Fits Management Workbench** (`quick_pp/app/backend/services/ancillary.py`, `quick_pp/app/backend/services/workspace.py`, `ParametersManagementPanel.svelte`, `SavedParamsIndicator.svelte`, `moduleParams.svelte.ts`, `projectParams.svelte.ts`, `tests/test_params_and_contacts_reset.py`) providing a unified interactive manager to inspect, modify, zone-override, and reset model parameters and regression fits across Lithology/Porosity, Rock Typing, SHF, Permeability transforms, and Swirr, complete with real-time dirty state tracking and zone-level parameter isolation. +- Added **Petrophysical Single-Source-of-Truth (SSOT) Reference & AI Assistant Grounding** (`docs/workflows/index.md`, `single_well.md`, `multi_well.md`, `quick_pp/app/backend/services/ssot_reference.py`, `quick_pp/app/backend/services/chat.py`, `WorkflowGuideSheet.svelte`) establishing authoritative petrophysical equations, curve contracts, parameter bounds, and diagnostic guidelines across sequential steps (1–7) and standalone modules, backed by `SSOTReferenceRegistry` and MCP server tool `get_workflow_technical_reference` for context-grounded AI assistant responses, paired with the interactive in-app `WorkflowGuideSheet` reference panel. +- Enhanced **Lithology Inversion & Multi-Mineral Optimization** (`quick_pp/lithology.py`, `quick_pp/plotter/report_plots.py`, `LogTrackCanvas.svelte`, `CrossplotCanvas.svelte`) to cater to essential secondary and accessory minerals (Siderite, Pyrite, Anhydrite, Coal) alongside quartz, clay, calcite, and dolomite, refined silt line angle geometric constraints in Sand-Silt-Clay (SSC) inversion models, refactored Thomas-Stieber thin-bed analysis, enforced non-reservoir rock flag filtering in rock typing and permeability modules, and added Spectral Gamma Ray (SGR) track components (K, U, Th). +- Refined **SCAL Water Saturation Normalization, Swirr & Permeability Transforms** (`quick_pp/saturation.py`, `quick_pp/scal.py`, `MultiWellSwirr.svelte`, `InspectorPerm.svelte`) correcting $S_w$ normalization bounds in SCAL relative permeability models, improving multi-well irreducible water saturation (`SWIRR`) fitting and continuous curve propagation across zones, and enforcing rigorous outlier filtering on core data points. +- Enhanced **Automated FDP & Single-Well Report Generation** (`quick_pp/plotter/report_plots.py`, `quick_pp/app/backend/services/report.py`, `InspectorReport.svelte`) with upgraded headless high-DPI Matplotlib figure rendering, composite CPI well logs with strictly monotonic depth indexing, auto-zoomed target reservoir depth windows, core calibration overlays (`CPORE`, `CPERM`), and zonal contact boundary lines. + ## 0.3.3 (2026) - Added **Formation Pressure Testing (FPT) Fluid Gradient & Contact Analysis** (`quick_pp/fluid_type.py`, `quick_pp/app/backend/services/fpt.py`, `MultiWellFpt.svelte`, `tests/test_fluid_type_fpt.py`) supporting multi-well pressure-depth regression (OLS, Huber, RANSAC), automatic mobility (`TIGHT`) and supercharge residual quality flagging, unsupervised fluid phase clustering, in-situ fluid density conversion (`gradient_to_density`, `density_to_gradient`), Monte Carlo P10/P50/P90 fluid contact intersections (`GOC`, `OWC`/`FWL`, `GWC`), excess pressure profiling, per-zone analysis, and one-click TVDSS-to-MD contact writing into well fluid contact tables. - Added **Multi-Well Irreducible Water Saturation (`Swirr`) Module** (`quick_pp/saturation.py`, `MultiWellSwirr.svelte`, `InspectorPerm.svelte`) with multi-well `Swirr` correlation fitting (`fit_swirr_correlation`) and continuous curve prediction using **RQI (`swirr_rqi`)**, **Semi-log Permeability (`swirr_semilog_k`)**, and **Timur (`swirr_timur`)** models, plus direct `SWIRR` integration in Single-Well Timur, Coates, and Tixier permeability calculations. diff --git a/docs/workflows/index.md b/docs/workflows/index.md index 428dfdc9..2d995eee 100644 --- a/docs/workflows/index.md +++ b/docs/workflows/index.md @@ -85,7 +85,7 @@ The tables below define the 1-to-1 correspondence between the SvelteKit workbenc | **Step 2** | `qaqc` | `MultiWellQaqc.svelte` | [`#mw-step-2-qaqc`](multi_well.md#mw-step-2-qaqc) | Key-well designation & stratigraphic quantile normalization | | **Step 3** | `rock-typing` | `MultiWellRockTyping.svelte` | [`#mw-step-3-rock-typing`](multi_well.md#mw-step-3-rock-typing) | Core Hydraulic Flow Units (FZI), Winland $r_{35}$ & Lucia RFN | | **Step 4** | `perm-transform` | `MultiWellPermTransform.svelte` | [`#mw-step-4-perm-transform`](multi_well.md#mw-step-4-perm-transform) | Rock-type partitioned poro-perm regression ($k = a \cdot e^{b\phi}$) | -| **Step 5** | `shf` | `MultiWellShf.svelte` | [`#mw-step-5-shf`](multi_well.md#mw-step-5-shf) | Saturation-Height Modeling (Leverett $J$ / Skelt-Harrison) & log reconciliation | +| **Step 5** | `shf` | `MultiWellShf.svelte` | [`#mw-step-5-shf`](multi_well.md#mw-step-5-shf) | Saturation-Height Modeling (Leverett $J$ / Skelt-Harrison), fluid contacts & log reconciliation | | **Step 6** | `volumetrics` | `MultiWellVolumetrics.svelte` | [`#mw-step-6-volumetrics`](multi_well.md#mw-step-6-volumetrics) | Probabilistic Monte Carlo LHS STOIIP/GIIP (P90/P50/P10) & Tornado | | **Step 7** | `report` | `InspectorReport.svelte` | [`#mw-step-7-report`](multi_well.md#mw-step-7-report) | Field Development Plan (FDP) comprehensive report generation | | **Standalone** | `swirr-corr` | `MultiWellSwirr.svelte` | [`#mw-standalone-scal-relperm`](multi_well.md#mw-standalone-scal-relperm) | **SCAL & Rel-Perm**: Field Swirr fit, Brooks-Corey, LET & SWOF decks | diff --git a/docs/workflows/multi_well.md b/docs/workflows/multi_well.md index 3e94b015..c6877d9f 100644 --- a/docs/workflows/multi_well.md +++ b/docs/workflows/multi_well.md @@ -281,6 +281,13 @@ flowchart TD $$\min_{z_{\text{FWL}}, \mathbf{\theta}_{\text{SHF}}} \sum_{w=1}^{N_w} \sum_{i=1}^{M_w} \left( S_{w,\text{SHF}}(z_{w,i}; z_{\text{FWL}}, \mathbf{\theta}) - S_{w,\text{log},i} \right)^2$$ + * **Fluid Contact & Transition Limits Delineation (`extract_fluid_contacts_and_limits`):** + Automatically establishes petrophysical fluid boundaries across all wells and zones: + * **Gas Limits & Contacts:** GUT (Gas Up To), GDT (Gas Down To), GOC (Gas-Oil Contact), GWC (Gas-Water Contact) + * **Oil Limits & Contacts:** OUT (Oil Up To), ODT (Oil Down To), OWC (Oil-Water Contact) + * **Water Limits & Contacts:** WUT (Water Up To), FWL (Free Water Level) — *Note: WDT (Water Down To) is explicitly excluded as it carries no petrophysical utility.* + * **Net Sand & Pay Conditioning:** Declared contacts require contiguous reservoir sand samples ($V_{\text{sh}} \le V_{\text{sh,cutoff}}$, $\phi_e \ge \phi_{\text{cutoff}}$, $h_{\text{sand}} \ge 1.0\,\text{m}$) and saturation discrimination ($S_w \le S_{w,\text{pay}}$ for hydrocarbon, $S_w \ge S_{w,\text{water}}$ for water). + * **Input Data:** `TVDSS`, `PERM_FIELD`, `PHIE`, $z_{\text{FWL}}$, In-situ fluid densities ($\rho_w, \rho_{\text{hc}}$). * **Output Data:** `SW_SHF`, Reconciliation crossplots, Residual error statistics ($\text{RMSE}, R^2$). diff --git a/pyproject.toml b/pyproject.toml index 0d81add2..cfb88927 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "quick-pp" -version = "0.3.3" +version = "0.3.4" description = "Comprehensive Python package and web platform for deterministic and probabilistic petrophysical evaluation, SCAL & relative permeability modeling, multi-well FWL optimization, stratigraphic normalization, and geomodel log upscaling." readme = { file = "README.md", content-type = "text/markdown" } license = "MIT" @@ -187,7 +187,13 @@ quick_pp = [ "app/frontend/.svelte-kit", "app/frontend/.svelte-kit/**/*", "app/backend/static/reports", - "app/backend/static/reports/**/*" + "app/backend/static/reports/**/*", + "**/*.db", + "**/*.db*", + "**/*.sqlite", + "**/*.sqlite3", + "**/.env", + "**/.env.*" ] # Development tools configuration diff --git a/quick_pp/__init__.py b/quick_pp/__init__.py index b806d322..be173261 100644 --- a/quick_pp/__init__.py +++ b/quick_pp/__init__.py @@ -48,7 +48,7 @@ __author__ = """Imran Fadhil""" __email__ = "imranfadhil@gmail.com" -__version__ = "0.3.3" # Need to be updated manually when releasing a new version/ change in pyproject.toml +__version__ = "0.3.4" # Need to be updated manually when releasing a new version/ change in pyproject.toml # Expose core petrophysical submodules and key public functions at top-level package namespace from quick_pp import ( # noqa: E402 @@ -71,6 +71,8 @@ from quick_pp.handover import ( # noqa: E402 build_handover_archive_bytes, collect_well_handover_dict, + import_handover_archive, + inspect_handover_archive, ) from quick_pp.qaqc import ( # noqa: E402 apply_environmental_corrections, @@ -87,6 +89,7 @@ ContactOptimizer, analyze_transition_zone, estimate_mdt_fwl, + extract_fluid_contacts_and_limits, fit_swirr_correlation, optimize_fwl, plot_fwl_reconciliation, @@ -182,6 +185,7 @@ "enforce_physical_constraints", "estimate_cbw_from_qv", "estimate_mdt_fwl", + "extract_fluid_contacts_and_limits", "expand_blocked_logs", "fit_relperm", "fit_swirr_correlation", @@ -192,6 +196,8 @@ "handover", "harm_upscale", "humidity_to_total_porosity", + "import_handover_archive", + "inspect_handover_archive", "klinkenberg_permeability_correction", "let_relperm", "logger", diff --git a/quick_pp/app/backend/mcp_server.py b/quick_pp/app/backend/mcp_server.py index 939b7d93..2171fd32 100644 --- a/quick_pp/app/backend/mcp_server.py +++ b/quick_pp/app/backend/mcp_server.py @@ -1673,7 +1673,15 @@ def tool_get_workflow_technical_reference( "primary_match": matches[0], "reference_content": matches[0]["content"], "source": f"docs/workflows/{'multi_well.md' if 'multi' in matches[0]['scope'] else 'single_well.md'}", + "found": True, } + return { + "tool": "get_workflow_technical_reference", + "query": query, + "matches_count": 0, + "reference_content": f"No workflow technical reference found matching query '{query}'.", + "found": False, + } if step: content = get_step_reference(effective_scope, step) diff --git a/quick_pp/app/backend/services/ancillary.py b/quick_pp/app/backend/services/ancillary.py index cae82de9..17a1ae9f 100644 --- a/quick_pp/app/backend/services/ancillary.py +++ b/quick_pp/app/backend/services/ancillary.py @@ -9,7 +9,7 @@ import numpy as np import pandas as pd from fastapi import APIRouter, Body, File, Form, HTTPException, Query, UploadFile -from sqlalchemy import select +from sqlalchemy import or_, select from quick_pp.app.backend.schemas.well_metadata import ( AncillaryColumnMapping, @@ -188,6 +188,10 @@ "depth_m", "depth_ft", ], + "ZONE": ["zone", "formation", "reservoir", "unit", "compartment"], + "FAULT_BLOCK": ["fault_block", "faultblock", "block", "compartment_block"], + "SOURCE": ["source", "method", "determination_method", "lineage", "basis"], + "REMARKS": ["remarks", "remark", "comment", "comments", "description"], }, "perforations": { "WELL_NAME": ["well", "well_name", "wellname", "uwi", "wellbore", "well_id"], @@ -1330,6 +1334,79 @@ def delete_fluid_contact_project( ) from e +@router.delete( + "/fluid_contacts", + summary="Delete fluid contacts across project or for a well (optionally auto-generated only)", + tags=["Fluid Contacts"], +) +@router.delete( + "/wells/{well_name}/fluid_contacts", + include_in_schema=False, +) +def delete_fluid_contacts_project( + project_id: int, + well_name: Optional[str] = None, + auto_only: bool = True, + zone: Optional[str] = None, +): + """Delete fluid contacts. If `auto_only=True` (default), only auto-generated/optimized contacts are deleted.""" + try: + deleted_count = 0 + wells_affected = set() + with get_db() as session: + proj = db_objects.Project.load(session, project_id=project_id) + if well_name: + wells = [proj.get_well(well_name)] + else: + wells = [proj.get_well(wn) for wn in proj.get_well_names()] + + for well in wells: + query = session.query(db_objects.ORMFluidContact).filter_by(well_id=well.well_id) + if zone: + query = query.filter(db_objects.ORMFluidContact.zone == str(zone).strip()) + + if auto_only: + auto_conditions = [ + db_objects.ORMFluidContact.source.ilike("%Log Evaluation%"), + db_objects.ORMFluidContact.source.ilike("%Calculated%"), + db_objects.ORMFluidContact.source.ilike("%Optimizer%"), + db_objects.ORMFluidContact.source.ilike("%FPT%"), + db_objects.ORMFluidContact.source.ilike("%Auto%"), + db_objects.ORMFluidContact.name.in_(["GUT", "GDT", "OUT", "ODT", "WUT"]), + db_objects.ORMFluidContact.remarks.ilike("%Proved Gas%"), + db_objects.ORMFluidContact.remarks.ilike("%Proved Oil%"), + db_objects.ORMFluidContact.remarks.ilike("%Proved Water%"), + db_objects.ORMFluidContact.remarks.ilike("%bracketed%"), + db_objects.ORMFluidContact.remarks.ilike("%Optimized Free Water Level%"), + db_objects.ORMFluidContact.remarks.ilike("%Formation Pressure Test%"), + ] + query = query.filter(or_(*auto_conditions)) + + count = query.delete(synchronize_session="fetch") + if count > 0: + deleted_count += count + wells_affected.add(well.name) + try: + session.expire(well._orm_well, ["fluid_contacts"]) + except Exception: + pass + + session.flush() + + for w_name in wells_affected: + invalidate_well_cache(project_id=project_id, well_name=str(w_name)) + + return { + "deleted_count": deleted_count, + "wells_affected": list(wells_affected), + "auto_only": auto_only, + } + except Exception as e: + raise HTTPException( + status_code=500, detail=f"Failed to delete fluid contacts: {e}" + ) from e + + @router.get( "/pressure_tests", summary="List pressure tests for a project (optional well_name)", @@ -2564,12 +2641,24 @@ def _opt_str(row: Dict[str, Any], col: Optional[str]) -> Optional[str]: elif dtype == "contacts": c_col, _ = col_map.get("CONTACT_TYPE", ("contact_type", None)) d_col, d_unit = col_map.get("DEPTH", ("depth", None)) + z_col, _ = col_map.get("ZONE", ("zone", None)) + fb_col, _ = col_map.get("FAULT_BLOCK", ("fault_block", None)) + src_col, _ = col_map.get("SOURCE", ("source", None)) + rem_col, _ = col_map.get("REMARKS", ("remarks", None)) contacts_payload = [] for r in w_rows: c_type = str(r.get(c_col, "")).strip().upper() d_v = _convert_ancillary_numeric(r.get(d_col), d_unit, "DEPTH") if c_type and d_v is not None: - contacts_payload.append({"name": c_type, "depth": d_v}) + contacts_payload.append({ + "name": str(r.get("name") or c_type).strip(), + "contact_type": c_type, + "depth": d_v, + "zone": str(r.get(z_col, "")).strip() or None if z_col in r else None, + "fault_block": str(r.get(fb_col, "")).strip() or None if fb_col in r else None, + "source": str(r.get(src_col, "")).strip() or None if src_col in r else None, + "remarks": str(r.get(rem_col, "")).strip() or None if rem_col in r else None, + }) if contacts_payload: well.add_fluid_contacts(contacts_payload, replace=True) summary_by_well[wn] = len(contacts_payload) diff --git a/quick_pp/app/backend/services/chat.py b/quick_pp/app/backend/services/chat.py index e19c8f83..037333f3 100644 --- a/quick_pp/app/backend/services/chat.py +++ b/quick_pp/app/backend/services/chat.py @@ -81,6 +81,45 @@ def _get_env_key(name: str, default: str = "") -> str: }, ] + +PRESET_QUICK_ACTIONS: List[Dict[str, str]] = [ + { + "label": "Inspect Well", + "prompt": "Inspect this well and summarize available curves, zones, and core coverage.", + "description": "Inventory well curves, depth intervals, formation tops, and core coverage.", + }, + { + "label": "Run QAQC", + "prompt": "Run qaqc.py on this well (badhole flagging and NPHI/RHOB hydrocarbon correction).", + "description": "Identify out-of-limit spikes, borehole washouts (BADHOLE), and hydrocarbon effects.", + }, + { + "label": "Multi-Mineral", + "prompt": "Execute multi_mineral.py lithology and porosity optimization on this well.", + "description": "Simultaneous solver for mineral fractions (Quartz, Calcite, Clay) and effective porosity.", + }, + { + "label": "Run SHF", + "prompt": "Execute shf.py saturation height function and reconcile Sw vs SHF.", + "description": "FZI rock typing, Leverett-J saturation height modeling, and Sw vs SHF reconciliation.", + }, + { + "label": "Conformal Bounds", + "prompt": "Calibrate conformal prediction on the fly and explain the interpretation with P10-P90 bounds.", + "description": "Locally weighted split conformal prediction for distribution-free P10–P90 uncertainty intervals.", + }, + { + "label": "Full Evaluation", + "prompt": "Run a full petrophysical evaluation (QAQC, multi-mineral, SHF, and conformal prediction bounds) and explain the results.", + "description": "End-to-end automated petrophysical pipeline combining QAQC, lithology, SHF, and conformal intervals.", + }, + { + "label": "FDP Report", + "prompt": "Generate a full FDP Petrophysical Evaluation Report for this project with AI executive summary and figures.", + "description": "Compile an FDP Petrophysical Evaluation Report with high-DPI figures and export options.", + }, +] + SYSTEM_PROMPT = """You are the quick_pp Petrophysical AI Assistant embedded inside the Svelte Subsurface Workbench. You have direct access to the Petrophysical MCP Server tools: - `inspect_well`: Inspect well curves, depth range, formation tops, fluid contacts, core count, and zonal stats. @@ -95,7 +134,34 @@ def _get_env_key(name: str, default: str = "") -> str: - `export_report_to_pdf`: Compile the active report into a downloadable A4 PDF document. - `get_workflow_technical_reference`: Retrieve authoritative petrophysical equations, curve contracts, parameter bounds, and diagnostic guidelines from the quick_pp SSOT technical foundation in docs/workflows/. -Always ground your interpretation in the quantitative tool outputs and the quick_pp SSOT technical foundations: cite depth intervals, zonal means, P10–P90 conformal interval widths, physical constraints (volumetric closure, washout impact, FWL transition zones), and reference governing equations from docs/workflows/ when explaining petrophysical methods. +CRITICAL DIRECTIVE — SELF-ASSESSED ANSWER CONFIDENCE & EPISTEMIC HUMILITY: +You must critically assess and explicitly state your level of confidence in the answer you provide. +Subsurface and petrophysical evaluations involve high-stakes technical and financial decisions. Unwarranted certainty, guessing, or hallucinating numbers can cause severe errors. + +1. Explicit Confidence Assessment: + In EVERY response, you must explicitly evaluate your own confidence in your answer: + - **High Confidence**: The answer is directly proven by executed quantitative tool outputs or authoritative quick_pp SSOT documentation. + - **Medium Confidence**: The answer is based on general petrophysical rules-of-thumb or empirical correlations, but you lack local calibration, key logs are missing, or the prompt has minor ambiguities. + - **Low Confidence**: You are making assumptions, extrapolating beyond available project data, recalling regional literature/analogues from general pre-training without local well verification, or estimating numbers without direct measurements. + +2. Mandatory Callout When NOT Confident: + Whenever your confidence in the answer is **Medium** or **Low**, you MUST highlight this prominently at the start of your response or in a dedicated callout block: + ```markdown + > ⚠️ **Answer Confidence: [Low / Medium]** + > - **Why I am not confident in this answer**: [Explain specifically what information is unverified, missing, or approximate in what you are saying]. + > - **Knowledge Gaps & Uncertainties in this Answer**: [Detail what you do not know for sure, what assumptions you made, and what might differ in reality]. + > - **How to Verify**: [State what specific tool or action the user should run, e.g. "Run `Inspect Well` or `Multi-Mineral` on the active well to compute the true local porosity."]. + ``` + +3. Regional Literature Queries (e.g., "what is the average porosity in balmoral sandstone"): + - If the user asks about an external formation, regional analogue, or literature property, state clearly that your confidence regarding the active well is **Low** because regional values are external generalizations. + - Provide the known literature context (e.g. Paleocene Balmoral turbidite sandstones in the UK North Sea typically have high average porosities between 28% and 35%, with permeabilities ranging from 500 to 3000 mD), BUT immediately highlight: + - The gap in the answer: This is an uncalibrated regional reference only. Actual local porosity is governed by burial compaction, cementation (calcite/quartz), and clay content. + - You cannot confirm whether the currently loaded well penetrates this formation without running inspection tools. + - Instruct the user to run the relevant quick action (`Inspect Well` or `Multi-Mineral`) to evaluate their loaded well logs directly. + +4. Never Fake Confidence: + Never present speculative estimates as authoritative facts. If you do not have enough data to give a confident answer, openly state your uncertainty and guide the user on how to resolve it. """ @@ -173,16 +239,26 @@ def _plan_native_tool_calls(req: AgentChatRequest) -> List[Dict[str, Any]]: ) # Workflow Theory, Equations & SSOT Reference Queries (independent of active project) - wants_theory = any( + # Require explicit theory/methodology keywords combined with petrophysical terms + has_theory_kw = any( kw in q for kw in ( - "equation", "formula", "theory", "foundation", "how is", "how do you calculate", - "what is the model", "cutoffs", "definition", "ssot", "reference", - "explain", "how does", "what model", "which model", "cite formula", - "what are the cutoffs", "best practice", "parameter tuning", "guideline", - "tell me about", "what is", "how to" + "equation", "formula", "governing equation", "cite formula", + "how do you calculate", "how is", "mathematical model", + "ssot reference", "technical reference", "workflow reference", + "cutoffs definition", "parameter bounds", "theoretical foundation" + ) + ) + has_petro_topic = any( + topic in q + for topic in ( + "qaqc", "badhole", "washout", "multimineral", "multi-mineral", "lithology", + "porosity", "shf", "leverett", "j-function", "capillary", "conformal", + "uncertainty", "archie", "simandoux", "permeability", "fzi", "vshale", + "clay", "fwl", "fluid contact", "water saturation" ) ) + wants_theory = has_theory_kw and has_petro_topic if wants_theory: calls.append( { @@ -534,23 +610,64 @@ def _plan_native_tool_calls(req: AgentChatRequest) -> List[Dict[str, Any]]: ) return calls - # Default fallback if user asks a general question on an active well: inspect + conformal bounds - if not calls and well: - calls.append({"name": "inspect_well", "arguments": common_filter}) - calls.append( - { - "name": "evaluate_with_conformal_bounds", - "arguments": { - **common_filter, - "alpha": req.alpha, - "save": True, - }, - } + # Do not execute arbitrary tools if no specific petrophysical intent was recognized. + # Return empty list so the caller handles unrecognized prompts gracefully with preset quick actions. + return calls + + + +def _handle_unrecognized_native_prompt( + req: AgentChatRequest, +) -> tuple[str, List[Dict[str, str]]]: + """Gracefully handle prompts that the deterministic built-in agent cannot answer directly.""" + q = req.message.strip() + well = req.well_name or "active well" + + # Check if user is asking about external formation/analogue or regional literature + is_regional_or_literature = any( + term in q.lower() + for term in ( + "sandstone", "formation", "group", "basin", "field", "balmoral", "brent", + "statfjord", "forties", "ekofisk", "clifton", "average porosity", "typical", + "analogue", "analog", "literature", "benchmark", "regional" ) - elif not calls and pid is not None: - calls.append({"name": "inspect_well", "arguments": {"project_id": int(pid)}}) + ) - return calls + if is_regional_or_literature: + confidence_details = ( + f"> ⚠️ **Answer Confidence: Low / Cannot Determine**\n" + f"> The Built-in Petrophysical Agent cannot reliably answer this question from the loaded project data.\n" + f"> \n" + f"> **Why the agent is not confident in this answer:**\n" + f"> - **Regional / Literature Gap**: The prompt asks about regional formation properties (*{q}*), which are not present in the active well logs (`{well}`).\n" + f"> - **Deterministic Boundary**: The Built-in Agent operates strictly on deterministic quantitative tools (`inspect_well`, `run_well_qaqc`, `run_multi_mineral`, `run_shf_evaluation`, `evaluate_with_conformal_bounds`). It does not have an open-domain neural memory of worldwide formation literature.\n" + f"> - **How to Get an Answer**: Switch to an external LLM provider (**OpenRouter** or **Ollama**) in **Stack Settings (⚙)**. With an LLM enabled, the assistant can provide published regional literature benchmarks (such as North Sea Paleocene Balmoral turbidite porosities of ~28–35%) while explicitly highlighting any uncertainties." + ) + else: + confidence_details = ( + f"> ⚠️ **Answer Confidence: Low / Unrecognized Intent**\n" + f"> The Built-in Agent cannot determine the intended subsurface workflow from this prompt.\n" + f"> \n" + f"> **Why the agent is not confident in this answer:**\n" + f"> - **Unrecognized Command**: The prompt (*{q}*) does not match a supported petrophysical tool command on `{well}`.\n" + f"> - **Deterministic Boundary**: The Built-in Agent only executes verified petrophysical algorithms (Log QAQC, multi-mineral lithology/porosity inversion, saturation height modeling, conformal P10–P90 bounds, and FDP reports).\n" + f"> - **Recommendation**: Choose from the preset quick actions below, or switch to an external LLM provider (**OpenRouter** or **Ollama**) in **Stack Settings (⚙)** for open-ended queries." + ) + + actions_md = "\n".join( + f"- ⚡ **{act['label']}**: {act['description']}" + for act in PRESET_QUICK_ACTIONS + ) + + reply = ( + f"### ℹ️ Built-in Agent Notice\n\n" + f"{confidence_details}\n\n" + f"---\n\n" + f"#### ⚡ Preset Quick Actions for `{well}`\n" + f"Instead of guessing or executing unrequested operations, you can run any of these verified workflows:\n\n" + f"{actions_md}\n" + ) + return reply, PRESET_QUICK_ACTIONS def _synthesize_petrophysical_explanation( @@ -566,6 +683,30 @@ def _synthesize_petrophysical_explanation( sections: List[str] = [] + # Check for confidence degraders across tool outputs + has_washout = any( + (rec.get("result") or {}).get("badhole", {}).get("flagged_count", 0) > 0 + for rec in tool_records + ) + no_core = any( + rec.get("name") == "inspect_well" and (rec.get("result") or {}).get("core_sample_count", 0) == 0 + for rec in tool_records + ) + + if has_washout or no_core: + reasons: List[str] = [] + if has_washout: + reasons.append("washout intervals detected where density/neutron log measurements are degraded by pad standoff") + if no_core: + reasons.append("lack of routine core calibration points (CPORE/CPERM) to anchor porosity and permeability transforms") + confidence_badge = ( + f"> ⚠️ **Answer Confidence: Medium** — Quantitative results computed from active well logs, but note: " + f"{'; '.join(reasons)}.\n" + ) + sections.append(confidence_badge) + else: + sections.append("> ✅ **Answer Confidence: High** — Grounded directly in quantitative MCP tool execution on active well logs.\n") + for rec in tool_records: t_name = rec["name"] res = rec.get("result") or {} @@ -751,6 +892,69 @@ def _synthesize_petrophysical_explanation( f"{ref_md}\n" ) + # Synthesize comprehensive Gaps & Uncertainty Analysis section across all executed tools + gaps: List[str] = [] + uncerts: List[str] = [] + + for rec in tool_records: + t_name = rec["name"] + res = rec.get("result") or {} + if t_name == "inspect_well": + core_pts = res.get("core_sample_count", 0) + if core_pts == 0: + gaps.append( + "**Core Calibration Gap**: No core samples (`CPORE` / `CPERM`) available in this well. " + "Porosity, permeability, and saturation height models rely on uncalibrated empirical log transforms." + ) + curves = set(res.get("available_curves", [])) + missing_essential = [c for c in ("RHOB", "NPHI", "GR", "RT", "CALI") if c not in curves] + if missing_essential: + gaps.append( + f"**Missing Essential Curves**: Curves {', '.join(f'`{c}`' for c in missing_essential)} " + f"are not present in the well log dataset." + ) + elif t_name == "run_well_qaqc": + bh = res.get("badhole") or {} + if bh.get("flagged_count", 0) > 0: + gaps.append( + f"**Borehole Washout Gap**: `{bh.get('flagged_pct')}%` (`{bh.get('flagged_count')}` samples) " + f"flagged as badhole. Density (`RHOB`) and neutron (`NPHI`) readings in washout intervals " + f"are degraded by pad standoff." + ) + elif t_name == "run_shf_evaluation": + rec_cmp = res.get("sw_vs_shf_reconciliation") or {} + if rec_cmp: + uncerts.append( + f"**Sw vs. SHF Reconciliation Spread**: Transition zone uncertainty indicated by RMSE of " + f"`{rec_cmp.get('rmse_sw_vs_shf')} v/v` and mean bias of `{rec_cmp.get('mean_bias_sw_minus_shf')} v/v`." + ) + elif t_name == "evaluate_with_conformal_bounds": + iw = res.get("interval_width_summary") or {} + if iw: + uncerts.append( + f"**Conformal P10–P90 Interval Spreads**: Porosity uncertainty is `±{round(iw.get('phit_mean_width_vv', 0)/2, 4)} v/v`, " + f"Sw uncertainty is `±{round(iw.get('sw_mean_width_vv', 0)/2, 4)} v/v`, and Permeability spread is " + f"`{iw.get('perm_median_p90_p10_factor')}×` between P10 and P90." + ) + z_conf = res.get("zonal_conformal_summary") or [] + if z_conf: + cert_pay = [f"{z['zone']} ({round(z['certifiable_pay_fraction']*100, 1)}% P10 vs {round(z['upside_pay_fraction']*100, 1)}% P90)" for z in z_conf[:3]] + uncerts.append( + f"**Pay Confidence Disparity**: Certifiable pay (P10) vs upside pay (P90): {', '.join(cert_pay)}." + ) + + if gaps or uncerts: + unc_lines = ["### ⚠️ Gaps & Uncertainty Analysis"] + if gaps: + unc_lines.append("**Identified Data & Coverage Gaps**:") + for g in gaps: + unc_lines.append(f"- {g}") + if uncerts: + unc_lines.append("**Petrophysical Model Uncertainties**:") + for u in uncerts: + unc_lines.append(f"- {u}") + sections.append("\n".join(unc_lines)) + return "\n\n".join(sections) @@ -955,6 +1159,7 @@ async def list_assistant_providers(): return { "providers": providers_out, "mcp_tools": petro_mcp_server.list_tools(), + "preset_quick_actions": PRESET_QUICK_ACTIONS, } @@ -982,6 +1187,20 @@ async def assistant_chat(req: AgentChatRequest): else: # Built-in Petrophysical MCP Agent planned = _plan_native_tool_calls(req) + if not planned: + # Built-in agent cannot answer this prompt; handle gracefully with preset quick actions + reply_text, suggested_actions = _handle_unrecognized_native_prompt(req) + return { + "reply": reply_text, + "provider": provider, + "used_fallback": False, + "tool_calls": [], + "computed_curves": {}, + "stats": {}, + "report_data": None, + "ui_actions": [], + "quick_actions": suggested_actions, + } for call_spec in planned: try: t_out = petro_mcp_server.call_tool( @@ -1077,6 +1296,7 @@ async def assistant_chat(req: AgentChatRequest): "stats": merged_stats, "report_data": latest_report_data, "ui_actions": ui_actions, + "quick_actions": PRESET_QUICK_ACTIONS if not executed_tools else [], } diff --git a/quick_pp/app/backend/services/fpt.py b/quick_pp/app/backend/services/fpt.py index b36f4570..2217d462 100644 --- a/quick_pp/app/backend/services/fpt.py +++ b/quick_pp/app/backend/services/fpt.py @@ -569,7 +569,13 @@ def fpt_apply_contacts(project_id: int, req: FPTApplyContactsRequest) -> Dict[st for name, z in contacts.items(): if tvd_ok[0] <= z <= tvd_ok[-1]: md_c = float(np.interp(z, tvd_ok, md_ok)) - payload.append({"name": name, "depth": round(md_c, 3)}) + payload.append({ + "name": name, + "depth": round(md_c, 3), + "contact_type": name, + "source": "FPT Analysis", + "remarks": "Formation Pressure Test gradient intersection", + }) written[name] = round(md_c, 3) else: outside.append(name) diff --git a/quick_pp/app/backend/services/report.py b/quick_pp/app/backend/services/report.py index 0eb02134..408004ad 100644 --- a/quick_pp/app/backend/services/report.py +++ b/quick_pp/app/backend/services/report.py @@ -590,8 +590,27 @@ def build_report_context( raw_upper_map[k] for k in priority_order if k in raw_upper_map ] + [c for c in raw_curves if str(c).upper() not in priority_order] - w_df = _ensure_evaluated_curves(w_df, w_name) - unfiltered_dfs.append(w_df) + has_real_logs = any( + str(c).upper() + in ( + "GR", + "RT", + "RD", + "RDEP", + "RILD", + "LLD", + "NPHI", + "RHOB", + "PHIT", + "PHIE", + "PERM", + "SWT", + ) + for c in raw_curves + ) + if has_real_logs: + w_df = _ensure_evaluated_curves(w_df, w_name) + unfiltered_dfs.append(w_df) d_s = pd.to_numeric(w_df["DEPTH"], errors="coerce").dropna() d_min = float(d_s.min()) if not d_s.empty else 0.0 @@ -1264,12 +1283,41 @@ def build_report_context( break # Spec 2.2: Dynamic Rock Typing & Permeability Regression via `scipy.stats.linregress` - rt_Specs = [ - (1, "RT1 — Nano/Micro-Porous Argillaceous", "DRT 1 (FZI < 0.5)", "< 0.50", 12.4, -1.85, 0.79), - (2, "RT2 — Meso-Porous Lower Shoreface", "DRT 2 (0.5–1.5)", "0.50 – 1.50", 14.2, -1.35, 0.85), - (3, "RT3 — Macro-Porous Upper Shoreface", "DRT 3 (1.5–3.0)", "1.50 – 3.00", 15.6, -0.92, 0.89), - (4, "RT4 — Mega-Porous Clean Channel Sand", "DRT 4 (FZI ≥ 3.0)", "≥ 3.00", 16.8, -0.45, 0.92), - ] + cfg_fzi_cutoffs = ( + (model_params.get("fzi_cutoffs") or model_params.get("fziCutoffs")) + if isinstance(model_params, dict) + else None + ) or merged_configs.get("fzi_cutoffs") or merged_configs.get("fziCutoffs") + if cfg_fzi_cutoffs and isinstance(cfg_fzi_cutoffs, (list, tuple)): + resolved_fzi_cutoffs = sorted([float(c) for c in cfg_fzi_cutoffs if float(c) > 0]) + else: + resolved_fzi_cutoffs = [0.5, 1.5, 3.0] + + rt_Specs = [] + n_cuts = len(resolved_fzi_cutoffs) + for i in range(n_cuts + 1): + idx = i + 1 + if i == 0: + c_high = resolved_fzi_cutoffs[0] + name = "RT1 — Nano/Micro-Porous Argillaceous" + code = f"DRT 1 (FZI < {c_high:.2f})" + fzi_str = f"< {c_high:.2f}" + def_m, def_c, def_r2 = 12.4, -1.85, 0.79 + elif i == n_cuts: + c_low = resolved_fzi_cutoffs[-1] + name = f"RT{idx} — Mega-Porous Clean Channel Sand" + code = f"DRT {idx} (FZI ≥ {c_low:.2f})" + fzi_str = f"≥ {c_low:.2f}" + def_m, def_c, def_r2 = 16.8, -0.45, 0.92 + else: + c_low = resolved_fzi_cutoffs[i - 1] + c_high = resolved_fzi_cutoffs[i] + name = f"RT{idx} — Meso/Macro-Porous Shoreface" + code = f"DRT {idx} ({c_low:.2f}–{c_high:.2f})" + fzi_str = f"{c_low:.2f} – {c_high:.2f}" + def_m, def_c, def_r2 = 14.2 + (i - 1) * 1.4, -1.35 + (i - 1) * 0.43, 0.85 + rt_Specs.append((idx, name, code, fzi_str, def_m, def_c, def_r2)) + rock_types: List[Dict[str, Any]] = [] rt34_share_pct = 43.5 rt34_kh_pct = 82.0 @@ -1319,7 +1367,7 @@ def build_report_context( rqi = 0.0314 * np.sqrt(k_arr / np.clip(p_arr, 1e-4, 0.99)) phiz = p_arr / np.clip(1.0 - p_arr, 1e-4, 1.0) fzi = rqi / np.clip(phiz, 1e-4, 100.0) - rt_bins = np.digitize(fzi, bins=[0.5, 1.5, 3.0]) + 1 + rt_bins = np.digitize(fzi, bins=resolved_fzi_cutoffs) + 1 total_n = max(len(p_arr), 1) total_kh = max(float(np.sum(k_arr)), 1e-9) @@ -1422,6 +1470,7 @@ def build_report_context( inferred_fwl_tvdss = round(median_target_tvdss + 15.0, 1) explicit_fwl = fwl_tvdss if fwl_tvdss is not None else merged_configs.get("fwl") + fwl_basis_str = "Target-Zone Hydrocarbon-Water Transition Zone (TVDss)" if explicit_fwl is not None: cand_fwl = _safe_float(explicit_fwl, inferred_fwl_tvdss) if abs(cand_fwl - median_target_tvdss) > 300.0: @@ -1434,10 +1483,13 @@ def build_report_context( } ) fwl_val = inferred_fwl_tvdss + fwl_basis_str = "Overridden to Target-Zone Transition Zone (TVDss)" else: fwl_val = round(cand_fwl, 1) + fwl_basis_str = "Configured / Project Parameters (TVDss)" else: fwl_val = inferred_fwl_tvdss + fwl_basis_str = "Inferred from Hydrocarbon-Water Transition Zone (TVDss)" # Spec 3.2, CHK-04, CHK-05: True Probabilistic Monte Carlo / Latin Hypercube Uncertainty Engine (N=1000) uncertainty_summary: List[Dict[str, Any]] = [] @@ -1737,6 +1789,12 @@ def _has_positive_col(col_name: str, threshold: float = 0.005) -> bool: "hi_hc": _safe_float(merged_configs.get("hi_hc"), 0.90), "norm_method": "Stratigraphic Zonal P10–P90 Quantile Alignment", } + dry_sand_pt = merged_configs.get("dry_sand_point") or (-0.02, 2.65) + dry_clay_pt = merged_configs.get("dry_clay_point") or (round(nphi_clay_cfg, 2), round(rho_clay_cfg, 2)) + dry_silt_pt = merged_configs.get("dry_silt_point") + silt_angle_cfg = _safe_float(merged_configs.get("silt_line_angle"), 119.0) + fluid_pt = merged_configs.get("fluid_point") or (1.0, 1.0) + litho_params = { "model_key": resolved_litho_key, "model_name": litho_model_display, @@ -1744,6 +1802,11 @@ def _has_positive_col(col_name: str, threshold: float = 0.005) -> bool: "nphi_clay": round(nphi_clay_cfg, 2), "gr_clean": round(gr_clean_cfg, 1), "gr_shale": round(gr_clay_cfg, 1), + "dry_sand_point": dry_sand_pt, + "dry_clay_point": dry_clay_pt, + "dry_silt_point": dry_silt_pt, + "silt_line_angle": silt_angle_cfg, + "fluid_point": fluid_pt, } scal_cfg = ( merged_configs.get("scal_params") @@ -1887,7 +1950,7 @@ def _has_positive_col(col_name: str, threshold: float = 0.005) -> bool: "model_key": raw_sw_key, "model_name": sw_model_display, "fwl": fwl_val, - "fwl_basis": "Target-Zone Hydrocarbon-Water Transition Zone (TVDss)", + "fwl_basis": fwl_basis_str, "median_target_tvdss": median_target_tvdss, "salinity": int(_safe_float(merged_configs.get("water_salinity"), 35000.0)), "rw": round(_safe_float(merged_configs.get("rw_val"), 0.080), 3), @@ -1983,6 +2046,113 @@ def _has_positive_col(col_name: str, threshold: float = 0.005) -> bool: "omega": 0.33, } + # Query all committed fluid contacts for selected project wells + well_name_by_id = {w["id"]: w["name"] for w in selected_wells_meta} + fluid_contacts_rows: List[Dict[str, Any]] = [] + with get_db() as session: + fc_res = session.execute( + select( + db_objects.ORMFluidContact.well_id, + db_objects.ORMFluidContact.name, + db_objects.ORMFluidContact.depth, + db_objects.ORMFluidContact.zone, + db_objects.ORMFluidContact.contact_type, + db_objects.ORMFluidContact.fault_block, + db_objects.ORMFluidContact.source, + db_objects.ORMFluidContact.remarks, + ) + .where(db_objects.ORMFluidContact.well_id.in_(list(well_name_by_id.keys()))) + .order_by(db_objects.ORMFluidContact.depth) + ).all() + for r in fc_res: + c_type = str(r.contact_type or r.name or "CONTACT").upper() + if c_type == "WDT": # Excluded per user instruction + continue + wn = well_name_by_id.get(r.well_id, f"Well-{r.well_id}") + fluid_contacts_rows.append({ + "well_name": wn, + "name": str(r.name), + "depth": round(float(r.depth), 1) if r.depth is not None else None, + "zone": str(r.zone) if r.zone else "All Zones", + "contact_type": c_type, + "fault_block": str(r.fault_block) if r.fault_block else None, + "source": str(r.source) if r.source else "Petrophysical Log Evaluation", + "remarks": str(r.remarks) if r.remarks else "", + }) + + # If no stored contacts in DB, dynamically extract them from full_df + if not fluid_contacts_rows and not full_df.empty: + try: + from quick_pp.saturation import extract_fluid_contacts_and_limits + extracted_df = extract_fluid_contacts_and_limits( + df=full_df, + fwl_dict={"default": fwl_val}, + sw_pay_cutoff=sw_cut, + phit_cutoff=phie_cut, + vsh_cutoff=vsh_cut, + ) + if not extracted_df.empty: + for r in extracted_df.to_dict(orient="records"): + c_type = str(r.get("contact_type") or "").upper() + if c_type == "WDT": + continue + fluid_contacts_rows.append({ + "well_name": str(r.get("well_name") or "Field"), + "name": str(r.get("name") or c_type), + "depth": round(float(r["depth"]), 1) if r.get("depth") is not None else None, + "zone": str(r.get("zone")) if r.get("zone") else "All Zones", + "contact_type": c_type, + "fault_block": str(r.get("fault_block")) if r.get("fault_block") else None, + "source": str(r.get("source") or "Petrophysical Log Interpretation"), + "remarks": str(r.get("remarks") or ""), + }) + except Exception: + pass + + # Summarize proved column and uncertainty brackets by (zone, fault_block) + zonal_contact_groups: Dict[Tuple[str, Optional[str]], Dict[str, Any]] = {} + for r in fluid_contacts_rows: + key = (r["zone"], r.get("fault_block")) + if key not in zonal_contact_groups: + zonal_contact_groups[key] = { + "zone": r["zone"], + "fault_block": r.get("fault_block"), + "gut": None, + "gdt": None, + "out": None, + "odt": None, + "wut": None, + "goc": None, + "gwc": None, + "owc": None, + "fwl": None, + } + g = zonal_contact_groups[key] + ctype = r["contact_type"] + d = r["depth"] + if d is None: + continue + if ctype == "GUT": + g["gut"] = min(g["gut"], d) if g["gut"] is not None else d + elif ctype == "GDT": + g["gdt"] = max(g["gdt"], d) if g["gdt"] is not None else d + elif ctype == "OUT": + g["out"] = min(g["out"], d) if g["out"] is not None else d + elif ctype == "ODT": + g["odt"] = max(g["odt"], d) if g["odt"] is not None else d + elif ctype == "WUT": + g["wut"] = min(g["wut"], d) if g["wut"] is not None else d + elif ctype == "GOC": + g["goc"] = d + elif ctype == "GWC": + g["gwc"] = d + elif ctype == "OWC": + g["owc"] = d + elif ctype == "FWL": + g["fwl"] = d + + fluid_contacts_summary = list(zonal_contact_groups.values()) + ctx_payload: Dict[str, Any] = { "project_id": pid, "project_name": project_name, @@ -1997,6 +2167,7 @@ def _has_positive_col(col_name: str, threshold: float = 0.005) -> bool: "litho_params": litho_params, "poro_params": poro_params, "rock_types": rock_types, + "fzi_cutoffs": resolved_fzi_cutoffs, "rt_summary": { "basis": rt_basis_source, "rt34_share_pct": rt34_share_pct, @@ -2008,6 +2179,8 @@ def _has_positive_col(col_name: str, threshold: float = 0.005) -> bool: "zonal_summary": zonal_summary, "well_zonal_summary": well_zonal_rows, "ressum_markdown_table": ressum_markdown_table, + "fluid_contacts_rows": fluid_contacts_rows, + "fluid_contacts_summary": fluid_contacts_summary, "uncertainty_summary": uncertainty_summary, "volumetric_summary": merged_configs.get("volumetric_summary"), "volumetric_params": merged_configs.get("volumetric_params"), @@ -2648,12 +2821,19 @@ def _generate_all_report_plots( assets["norm_plot"] = report_plots.generate_qaqc_norm_plot( full_df, well_names, output_dir=out_dir, url_prefix=url_prefix ) + litho_p = context.get("litho_params", {}) if "04_lithology_porosity" in enabled_section_ids: assets["litho_poro_crossplot"] = report_plots.generate_nd_crossplot( full_df, well_names, color_by="GR", target_zones=target_zones, + litho_model=litho_p.get("model_key", "ssc"), + dry_sand_point=litho_p.get("dry_sand_point", (-0.02, 2.65)), + dry_silt_point=litho_p.get("dry_silt_point"), + dry_clay_point=litho_p.get("dry_clay_point", (0.33, 2.70)), + fluid_point=litho_p.get("fluid_point", (1.0, 1.0)), + silt_line_angle=_safe_float(litho_p.get("silt_line_angle"), 119.0), output_dir=out_dir, url_prefix=url_prefix, ) @@ -2662,6 +2842,8 @@ def _generate_all_report_plots( full_df, well_names, rock_types=context.get("rock_types"), + fzi_cutoffs=context.get("fzi_cutoffs"), + target_zones=target_zones, output_dir=out_dir, url_prefix=url_prefix, ) @@ -2674,6 +2856,8 @@ def _generate_all_report_plots( m=_safe_float(sw_p.get("m"), 2.0), n=_safe_float(sw_p.get("n"), 2.0), fwl=_safe_float(sw_p.get("fwl"), 2500.0), + fwl_basis=sw_p.get("fwl_basis"), + target_zones=target_zones, output_dir=out_dir, url_prefix=url_prefix, ) @@ -2683,6 +2867,9 @@ def _generate_all_report_plots( rock_types=context.get("rock_types"), fwl=_safe_float(sw_p.get("fwl"), 2500.0), is_analogue_scal=is_analogue_scal, + scal_params=scal_p, + shf_params=context.get("shf_params", {}), + target_zones=target_zones, output_dir=out_dir, url_prefix=url_prefix, ) @@ -2707,7 +2894,88 @@ def _generate_all_report_plots( ) if "10_appendix_cpi" in enabled_section_ids: cpi_list = [] - for wn in well_names[:4]: + cpi_candidate_wells = [] + candidate_scores = [] + if not full_df.empty and "WELL_NAME" in full_df.columns: + for wn in well_names: + w_sub = full_df[full_df["WELL_NAME"].astype(str) == str(wn)] + if w_sub.empty or "DEPTH" not in w_sub.columns: + continue + + # Target reservoir interval subset + d_min, d_max = report_plots._resolve_target_depth_window( + w_sub, target_zones=target_zones, max_span=480.0 + ) + d_col = pd.to_numeric(w_sub["DEPTH"], errors="coerce") + w_win = w_sub[(d_col >= d_min) & (d_col <= d_max)] + if w_win.empty: + w_win = w_sub + + # Count valid evaluated samples in target window + phi_cnt = int( + pd.to_numeric( + w_win.get("PHIT", w_win.get("PHIE", pd.Series(dtype=float))), + errors="coerce", + ) + .notna() + .sum() + ) + swt_cnt = int( + pd.to_numeric( + w_win.get("SWT", pd.Series(dtype=float)), errors="coerce" + ) + .notna() + .sum() + ) + perm_cnt = int( + pd.to_numeric( + w_win.get("PERM", pd.Series(dtype=float)), errors="coerce" + ) + .notna() + .sum() + ) + gr_cnt = int( + pd.to_numeric( + w_win.get("GR", pd.Series(dtype=float)), errors="coerce" + ) + .notna() + .sum() + ) + rt_cnt = int( + pd.to_numeric( + w_win.get("RT", pd.Series(dtype=float)), errors="coerce" + ) + .notna() + .sum() + ) + + # Strict requirement: well must have evaluated porosity and at least one other key curve (RT, SWT, or PERM) + if phi_cnt < 10 or (swt_cnt < 5 and perm_cnt < 5 and rt_cnt < 5): + # Empty or uninterpreted in target window -> skip! + continue + + has_core = ( + "CPORE" in w_win.columns + and pd.to_numeric(w_win["CPORE"], errors="coerce").notna().sum() > 0 + ) or ( + "PORO_CORE" in w_win.columns + and pd.to_numeric(w_win["PORO_CORE"], errors="coerce").notna().sum() + > 0 + ) + score = ( + (10000 if has_core else 0) + + phi_cnt + + swt_cnt + + perm_cnt + + gr_cnt + + rt_cnt + ) + candidate_scores.append((score, wn)) + + candidate_scores.sort(key=lambda x: x[0], reverse=True) + cpi_candidate_wells = [wn for _, wn in candidate_scores] + + for wn in cpi_candidate_wells[:4]: cpi_url = report_plots.generate_well_cpi_plot( full_df, wn, @@ -2716,7 +2984,8 @@ def _generate_all_report_plots( output_dir=out_dir, url_prefix=url_prefix, ) - cpi_list.append({"well_name": wn, "url": cpi_url}) + if cpi_url: + cpi_list.append({"well_name": wn, "plot_url": cpi_url, "url": cpi_url}) assets["cpi_plots"] = cpi_list return assets diff --git a/quick_pp/app/backend/services/ssot_reference.py b/quick_pp/app/backend/services/ssot_reference.py index 1a2c8995..0e239c1f 100644 --- a/quick_pp/app/backend/services/ssot_reference.py +++ b/quick_pp/app/backend/services/ssot_reference.py @@ -232,8 +232,24 @@ def query_reference( ) -> List[Dict[str, Any]]: """Search SSOT documentation for keywords or formulas.""" self._ensure_loaded() - q_tokens = query.lower().split() - if not q_tokens: + stopwords = { + "a", "about", "above", "after", "again", "against", "all", "am", "an", "and", + "any", "are", "as", "at", "be", "because", "been", "before", "being", "below", + "between", "both", "but", "by", "can", "could", "did", "do", "does", "doing", + "down", "during", "each", "few", "for", "from", "further", "had", "has", "have", + "having", "he", "her", "here", "hers", "herself", "him", "himself", "his", "how", + "i", "if", "in", "into", "is", "it", "its", "itself", "just", "me", "more", "most", + "my", "myself", "no", "nor", "not", "now", "of", "off", "on", "once", "only", + "or", "other", "our", "ours", "ourselves", "out", "over", "own", "same", "she", + "should", "so", "some", "such", "than", "that", "the", "their", "theirs", "them", + "themselves", "then", "there", "these", "they", "this", "those", "through", "to", + "too", "under", "until", "up", "very", "was", "we", "were", "what", "when", + "where", "which", "while", "who", "whom", "why", "with", "would", "you", "your", + "yours", "yourself", "yourselves", "tell", "please", "average", "value" + } + raw_tokens = re.findall(r"\b[a-z0-9_-]{2,}\b", query.lower()) + meaningful_tokens = [t for t in raw_tokens if t not in stopwords] + if not meaningful_tokens: return [] results: List[Dict[str, Any]] = [] @@ -245,12 +261,14 @@ def query_reference( for anchor_id, content in anchors.items(): content_lower = content.lower() - # Score based on token frequency - score = sum(content_lower.count(token) for token in q_tokens) - if score > 0: - # Extract header - first_line = content.splitlines()[0] if content else anchor_id - clean_header = re.sub(r"\{#[\w\-]+\}", "", first_line).lstrip("#").strip() + first_line = content.splitlines()[0] if content else anchor_id + clean_header = re.sub(r"\{#[\w\-]+\}", "", first_line).lstrip("#").strip() + header_lower = clean_header.lower() + + title_matches = sum(1 for token in meaningful_tokens if token in header_lower or token in anchor_id.lower()) + body_matches = sum(content_lower.count(token) for token in meaningful_tokens) + if title_matches > 0 or body_matches > 0: + score = title_matches * 5 + body_matches results.append( { "scope": sc, diff --git a/quick_pp/app/backend/services/workspace.py b/quick_pp/app/backend/services/workspace.py index cd88b509..0aee8163 100644 --- a/quick_pp/app/backend/services/workspace.py +++ b/quick_pp/app/backend/services/workspace.py @@ -102,6 +102,7 @@ estimate_qv, estimate_qvn, estimate_rw_temperature_salinity, + extract_fluid_contacts_and_limits, fit_swirr_correlation, optimize_fwl, swirr_rqi, @@ -1160,9 +1161,70 @@ class WorkspaceShfRequest(BaseModel): None, description="Optional SwirrModelConfig dict used when swirr_source == 'poro_perm'", ) + zones: Optional[List[str]] = Field( + None, + description="Optional subset of zones to filter SCAL core data", + ) + min_depth: Optional[float] = Field( + None, + description="Optional minimum depth filter for SCAL core data", + ) + max_depth: Optional[float] = Field( + None, + description="Optional maximum depth filter for SCAL core data", + ) save: bool = Field(False) +def _resolve_scal_sample_zone( + s_depth: float, + well_df: Optional[pd.DataFrame], + anc: Optional[Dict[str, Any]], +) -> str: + """Resolve the geological zone name for a SCAL core sample at `s_depth`.""" + if s_depth > 0: + if ( + well_df is not None + and not well_df.empty + and "DEPTH" in well_df.columns + and "ZONES" in well_df.columns + ): + try: + depth_diff = (well_df["DEPTH"] - s_depth).abs() + idx_near = depth_diff.idxmin() + if float(depth_diff.loc[idx_near]) <= 5.0: + z_val = well_df.loc[idx_near, "ZONES"] + if pd.notna(z_val): + z_str = str(z_val).strip() + if z_str and z_str.lower() not in ("unknown", "none", "nan"): + return z_str + except Exception: + pass + tops = (anc or {}).get("formation_tops") + if isinstance(tops, list) and tops: + valid_tops = [] + for t in tops: + if isinstance(t, dict): + td = t.get("depth") + tn = str(t.get("name") or t.get("zone") or "").strip() + if td is not None and tn: + try: + valid_tops.append((float(td), tn)) + except (TypeError, ValueError): + pass + if valid_tops: + valid_tops.sort(key=lambda x: x[0]) + chosen = "Unknown" + for td, tn in valid_tops: + if s_depth >= td: + chosen = tn + else: + break + if chosen != "Unknown": + return chosen + return "Unknown" + + def _filter_fzi_data_by_wells( fzi_data: Dict[str, List[Any]], well_names: Optional[List[str]] ) -> Dict[str, List[Any]]: @@ -5937,15 +5999,38 @@ def workspace_shf_compute(project_id: int, req: WorkspaceShfRequest) -> Dict[str ) if str(pid).strip() } - excl_pt_set = { - str(ptid).strip() - for ptid in ( - swirr_cfg_dict.get("excludedScalPointIds") - or swirr_cfg_dict.get("excluded_scal_point_ids") - or [] - ) - if str(ptid).strip() - } + excl_plug_set = set() + for pid in ( + swirr_cfg_dict.get("excludedPlugIds") + or swirr_cfg_dict.get("excluded_plug_ids") + or [] + ): + p_str = str(pid).strip() + if p_str: + excl_plug_set.add(p_str) + if "::" in p_str: + excl_plug_set.add(p_str.replace("::", ":", 1)) + elif ":" in p_str: + excl_plug_set.add(p_str.replace(":", "::", 1)) + excl_pt_set = set() + for ptid in ( + swirr_cfg_dict.get("excludedScalPointIds") + or swirr_cfg_dict.get("excluded_scal_point_ids") + or [] + ): + pt_str = str(ptid).strip() + if pt_str: + excl_pt_set.add(pt_str) + if pt_str.count("::") == 1 and ":" in pt_str.split("::")[0]: + p_part, idx_part = pt_str.split("::", 1) + excl_pt_set.add(f"{p_part.replace(':', '::', 1)}::{idx_part}") + allowed_zones_set = ( + {str(z).strip() for z in req.zones if str(z).strip()} + if req.zones + else None + ) + min_d_filter = float(req.min_depth) if req.min_depth is not None else None + max_d_filter = float(req.max_depth) if req.max_depth is not None else None core_rows: List[Dict[str, Any]] = [] with get_db() as session: proj = db_objects.Project.load(session, project_id=project_id) @@ -5959,12 +6044,16 @@ def workspace_shf_compute(project_id: int, req: WorkspaceShfRequest) -> Dict[str core_data = (anc or {}).get("core_data") or {} well_df_cache: Optional[pd.DataFrame] = None for sname, sample in core_data.items(): - if f"{wn}::{sname}" in excl_plug_set: + if f"{wn}::{sname}" in excl_plug_set or f"{wn}:{sname}" in excl_plug_set: continue pc_df = sample.get("pc") if pc_df is None or pc_df.empty: continue s_depth = float(sample.get("depth") or 0.0) + if min_d_filter is not None and s_depth > 0 and s_depth < min_d_filter: + continue + if max_d_filter is not None and s_depth > 0 and s_depth > max_d_filter: + continue meas = sample.get("measurements") cperm = None cpore = None @@ -6087,6 +6176,15 @@ def _norm_plug_stem(val: Any) -> str: # Fallback 2: lookup well's merged CPORE/CPERM (within ±0.50m) or log PHIT & PERM_TRANS/PERM at nearest depth near_rock_flag: Optional[int] = None + if s_depth > 0 or allowed_zones_set is not None: + try: + if well_df_cache is None: + well_df_cache = _get_filtered_well_df(project_id, wn) + except Exception: + pass + s_zone = _resolve_scal_sample_zone(s_depth, well_df_cache, anc) + if allowed_zones_set is not None and s_zone not in allowed_zones_set: + continue if s_depth > 0: try: if well_df_cache is None: @@ -6220,39 +6318,30 @@ def _norm_plug_stem(val: Any) -> str: cpore, cperm, rf_val, getattr(req, "swirr_config", None) ) if swirr_src_mode == "poro_perm": - raw_swn = (sw_vals - swirr_calc_plug) / max( - 0.05, 1.0 - swirr_calc_plug + eff_swirr = float(np.clip(swirr_calc_plug, 0.01, 0.85)) + raw_swn = (sw_vals - eff_swirr) / max( + 0.05, 1.0 - eff_swirr ) swn_vals = np.clip(raw_swn, 0.005, 0.99) - elif ( - len(sw_vals) >= 4 - and (np.nanmax(sw_vals) - np.nanmin(sw_vals)) > 0.25 - ): - # Use a poro-perm bounded Swirr estimate so incomplete drainage curves - # (e.g., tight plugs terminating at Sw=0.41) are not artificially stretched to Swn=0.030 - sw_min_plug = float(np.nanmin(sw_vals)) - swirr_est = float( - np.clip( - 0.08 - * ( - (max(float(cperm), 0.01) / max(float(cpore), 0.01)) - ** -0.12 - ), - 0.04, - 0.22, - ) - ) - eff_swirr = min(sw_min_plug * 0.92, swirr_est) - raw_swn = (sw_vals - eff_swirr) / max(1.0 - eff_swirr, 1e-3) - swn_vals = np.clip(raw_swn, 0.02, 0.98) else: - swn_vals = np.clip(sw_vals, 0.02, 0.98) + # Per-plug Lab Terminal Sw normalization: each sample is normalized by its own + # minimum/terminal lab Sw (with a 0.96 asymptote factor so the terminal point remains > 0 on log-log J fits) + sw_min_plug = float(np.nanmin(sw_vals)) + eff_swirr = float(np.clip(sw_min_plug * 0.96, 0.01, 0.85)) + raw_swn = (sw_vals - eff_swirr) / max(1.0 - eff_swirr, 0.05) + swn_vals = np.clip(raw_swn, 0.005, 0.99) + swirr_used_plug = eff_swirr + order_pc = np.argsort(pc_vals) + pc_vals = pc_vals[order_pc] + sw_vals = sw_vals[order_pc] + swn_vals = swn_vals[order_pc] for pt_idx, (pc_v, sw_v, swn_v) in enumerate(zip(pc_vals, sw_vals, swn_vals, strict=False)): - if ( + is_pt_excl = ( f"{wn}::{sname}::{pt_idx}" in excl_pt_set + or f"{wn}:{sname}::{pt_idx}" in excl_pt_set or f"{wn}::{sname}::{round(float(pc_v), 2)}" in excl_pt_set - ): - continue + or f"{wn}:{sname}::{round(float(pc_v), 2)}" in excl_pt_set + ) if not (0.004 <= swn_v <= 0.995 and pc_v > 0): continue j_v = float(leverett_j(pc_v, req.ift, req.theta, cperm, cpore)) @@ -6271,7 +6360,7 @@ def _norm_plug_stem(val: Any) -> str: "PC": round(float(pc_v), 4), "SW": round(float(sw_v), 4), "SWN": round(float(swn_v), 4), - "SWIRR_CALC": round(float(swirr_calc_plug), 4), + "SWIRR_CALC": round(float(swirr_used_plug), 4), "J": round(float(j_v), 4), "H": round(float(h_v), 4), "BVW": round(float(bvw_v), 4), @@ -6280,74 +6369,93 @@ def _norm_plug_stem(val: Any) -> str: "Well": wn, "Sample": str(sname), "DEPTH": round(float(s_depth), 2), + "ZONE": s_zone, + "POINT_ID": f"{wn}::{sname}::{pt_idx}", + "PLUG_ID": f"{wn}::{sname}", + "EXCLUDED": bool(is_pt_excl), } ) - # Synthesize representative SCAL capillary pressure curves for reservoir rock types with < 3 plugs + # Synthesize representative SCAL capillary pressure curves only if project has < 3 real SCAL points rf_arr_full = np.array( [int(r) if r is not None else -1 for r in rock_flags], dtype=int ) default_well = well_names[0] if well_names else "Project" - for rf_int in reservoir_flags: - existing_rf = [r for r in core_rows if int(r["ROCK_FLAG"]) == rf_int] - if len(existing_rf) >= 3: - continue - mask_rf = ( - (rf_arr_full == rf_int) - & np.isfinite(phit_arr) - & np.isfinite(perm_arr) - & (phit_arr > 0) - & (perm_arr > 0) - ) - p_med = ( - float(np.nanmedian(phit_arr[mask_rf])) - if np.any(mask_rf) - else max(0.24 - 0.04 * (rf_int - 1), 0.06) - ) - k_med = ( - float(np.nanmedian(perm_arr[mask_rf])) - if np.any(mask_rf) - else max(250.0 / (4.0 ** (rf_int - 1)), 0.5) - ) - swirr = float(np.clip(0.06 + 0.05 * (rf_int - 1), 0.05, 0.35)) - j_a = float( - np.clip( - 0.12 - * ((max(k_med, 0.01) / max(p_med, 0.01)) ** -0.06) - * (1.0 + 0.22 * (rf_int - 1)), - 0.05, - 0.65, + if len(core_rows) < 3: + for rf_int in reservoir_flags: + existing_rf = [r for r in core_rows if int(r["ROCK_FLAG"]) == rf_int] + if len(existing_rf) >= 3: + continue + mask_rf = ( + (rf_arr_full == rf_int) + & np.isfinite(phit_arr) + & np.isfinite(perm_arr) + & (phit_arr > 0) + & (perm_arr > 0) ) - ) - j_b = float(np.clip(0.24 + 0.06 * (rf_int - 1), 0.15, 0.65)) - cos_t = max(abs(math.cos(math.radians(req.theta))), 0.1) - sqrt_kp = max(math.sqrt(max(k_med, 1e-4) / max(p_med, 1e-4)), 1e-3) - for idx_pt, swn_v in enumerate( - (0.08, 0.14, 0.22, 0.32, 0.45, 0.60, 0.76, 0.90) - ): - noise = 1.0 + 0.03 * ((-1.0) ** idx_pt) - j_v = float(j_a * (swn_v ** (-j_b)) * noise) - pc_v = float(max((j_v * req.ift * cos_t) / (0.2166 * sqrt_kp), 0.05)) - sw_v = float(np.clip(swirr + swn_v * (1.0 - swirr), 0.02, 0.99)) - h_v = float(pc_v / grad_psi) - bvw_v = float(p_med * sw_v) - core_rows.append( - { - "ROCK_FLAG": rf_int, - "PC": round(pc_v, 4), - "SW": round(sw_v, 4), - "SWN": round(float(swn_v), 4), - "J": round(j_v, 4), - "H": round(h_v, 4), - "BVW": round(bvw_v, 4), - "PHIT": round(p_med, 4), - "PERM": round(k_med, 4), - "Well": default_well, - "Sample": f"SYN_RT{rf_int}_{idx_pt + 1}", - } + p_med = ( + float(np.nanmedian(phit_arr[mask_rf])) + if np.any(mask_rf) + else max(0.24 - 0.04 * (rf_int - 1), 0.06) + ) + k_med = ( + float(np.nanmedian(perm_arr[mask_rf])) + if np.any(mask_rf) + else max(250.0 / (4.0 ** (rf_int - 1)), 0.5) ) + swirr = float(np.clip(0.06 + 0.05 * (rf_int - 1), 0.05, 0.35)) + j_a = float( + np.clip( + 0.12 + * ((max(k_med, 0.01) / max(p_med, 0.01)) ** -0.06) + * (1.0 + 0.22 * (rf_int - 1)), + 0.05, + 0.65, + ) + ) + j_b = float(np.clip(0.24 + 0.06 * (rf_int - 1), 0.15, 0.65)) + cos_t = max(abs(math.cos(math.radians(req.theta))), 0.1) + sqrt_kp = max(math.sqrt(max(k_med, 1e-4) / max(p_med, 1e-4)), 1e-3) + for idx_pt, swn_v in enumerate( + (0.08, 0.14, 0.22, 0.32, 0.45, 0.60, 0.76, 0.90) + ): + noise = 1.0 + 0.03 * ((-1.0) ** idx_pt) + j_v = float(j_a * (swn_v ** (-j_b)) * noise) + pc_v = float(max((j_v * req.ift * cos_t) / (0.2166 * sqrt_kp), 0.05)) + sw_v = float(np.clip(swirr + swn_v * (1.0 - swirr), 0.02, 0.99)) + h_v = float(pc_v / grad_psi) + bvw_v = float(p_med * sw_v) + sname_syn = f"SYN_RT{rf_int}_{idx_pt + 1}" + core_rows.append( + { + "ROCK_FLAG": rf_int, + "PC": round(pc_v, 4), + "SW": round(sw_v, 4), + "SWN": round(float(swn_v), 4), + "SWIRR_CALC": round(float(swirr), 4), + "J": round(j_v, 4), + "H": round(h_v, 4), + "BVW": round(bvw_v, 4), + "PHIT": round(p_med, 4), + "PERM": round(k_med, 4), + "Well": default_well, + "Sample": sname_syn, + "DEPTH": round(1500.0 + rf_int * 25.0 + idx_pt * 2.0, 2), + "ZONE": "Unknown", + "POINT_ID": f"{default_well}::{sname_syn}::{idx_pt}", + "PLUG_ID": f"{default_well}::{sname_syn}", + "EXCLUDED": False, + } + ) scal_df = pd.DataFrame(core_rows) + scal_df_fit = ( + scal_df[~scal_df["EXCLUDED"].astype(bool)] + if (not scal_df.empty and "EXCLUDED" in scal_df.columns) + else scal_df + ) + if scal_df_fit.empty and not scal_df.empty: + scal_df_fit = scal_df j_sat_mode = ( "sw" if str(getattr(req, "j_sat_mode", "swn") or "swn").strip().lower() == "sw" @@ -6355,8 +6463,8 @@ def _norm_plug_stem(val: Any) -> str: ) fits_swn: Dict[str, Dict[str, float]] = {} fits_sw: Dict[str, Dict[str, float]] = {} - if model == "leverett_j" and not scal_df.empty: - for param in auto_j_params(scal_df): + if model == "leverett_j" and not scal_df_fit.empty: + for param in auto_j_params(scal_df_fit): fits_swn[str(param["ROCK_FLAG"])] = { "a": float(param["a"]), "b": float(param["b"]), @@ -6364,7 +6472,7 @@ def _norm_plug_stem(val: Any) -> str: "r2": float(param.get("r2", 0.0)), "count": int(param.get("count", 0)), } - scal_df_sw = scal_df.assign(SWN=scal_df["SW"]) + scal_df_sw = scal_df_fit.assign(SWN=scal_df_fit["SW"]) for param in auto_j_params(scal_df_sw): fits_sw[str(param["ROCK_FLAG"])] = { "a": float(param["a"]), @@ -6396,9 +6504,9 @@ def _norm_plug_stem(val: Any) -> str: "r2": 1.0, "count": 0, } - elif not scal_df.empty: + elif not scal_df_fit.empty: if model == "skelt_harrison": - for param in auto_skelt_harrison_params(scal_df): + for param in auto_skelt_harrison_params(scal_df_fit): fits[str(param["ROCK_FLAG"])] = { "a": float(param["a"]), "b": float(param["b"]), @@ -6409,7 +6517,7 @@ def _norm_plug_stem(val: Any) -> str: "count": int(param.get("count", 0)), } elif model == "cuddy": - for param in auto_cuddy_params(scal_df): + for param in auto_cuddy_params(scal_df_fit): fits[str(param["ROCK_FLAG"])] = { "a": float(param["a"]), "b": float(param["b"]), @@ -6423,14 +6531,14 @@ def _norm_plug_stem(val: Any) -> str: fits[rf_k] = dict(f_val) # Recompute fit diagnostics (rmse, r2, count) against SCAL points for all entries in fits - if not scal_df.empty and fits: + if not scal_df_fit.empty and fits: j_sat_col = "SW" if j_sat_mode == "sw" else "SWN" for rf_key, fit_entry in list(fits.items()): try: rf_int = int(rf_key) except ValueError: continue - sub_scal = scal_df[scal_df["ROCK_FLAG"] == rf_int] + sub_scal = scal_df_fit[scal_df_fit["ROCK_FLAG"] == rf_int] if sub_scal.empty: continue a_f = float(fit_entry.get("a", 0.2)) @@ -6960,6 +7068,31 @@ def _norm_plug_stem(val: Any) -> str: else [], "well_names": scal_df["Well"].tolist() if not scal_df.empty else [], "samples": scal_df["Sample"].tolist() if not scal_df.empty else [], + "depths": ( + scal_df["DEPTH"].tolist() + if (not scal_df.empty and "DEPTH" in scal_df.columns) + else [] + ), + "zones": ( + scal_df["ZONE"].tolist() + if (not scal_df.empty and "ZONE" in scal_df.columns) + else [] + ), + "point_ids": ( + scal_df["POINT_ID"].tolist() + if (not scal_df.empty and "POINT_ID" in scal_df.columns) + else [] + ), + "plug_ids": ( + scal_df["PLUG_ID"].tolist() + if (not scal_df.empty and "PLUG_ID" in scal_df.columns) + else [] + ), + "excluded": ( + [bool(x) for x in scal_df["EXCLUDED"].tolist()] + if (not scal_df.empty and "EXCLUDED" in scal_df.columns) + else [] + ), } return { @@ -7915,6 +8048,7 @@ class WorkspaceOptimizeFwlRequest(BaseModel): mdt_weight: Optional[float] = Field(None) solve_per_well: bool = Field(True) solve_per_zone: bool = Field(True) + pay_weighted: bool = Field(True) optimize_shm_params: bool = Field(False) unit: str = Field("metric") save: bool = Field(False) @@ -8884,20 +9018,38 @@ def workspace_optimize_fwl( mdt_penalty_weight=penalty_w, solve_per_well=req.solve_per_well, solve_per_zone=req.solve_per_zone, + pay_weighted=req.pay_weighted, zone_col="ZONES", optimize_shm_params=req.optimize_shm_params, unit=req.unit, ) saved_wells = 0 - if req.save and "SW_SHM" in res["df"].columns: - for wn, grp in res["df"].groupby("WELL_NAME"): - _save_curves_to_well( - project_id, - str(wn), - grp[["DEPTH", "SW_SHM"]].rename(columns={"SW_SHM": "SHF"}), - ) - saved_wells += 1 + contacts_records = [] + if "fluid_contacts" in res and isinstance(res["fluid_contacts"], pd.DataFrame) and not res["fluid_contacts"].empty: + contacts_records = dataframe_to_records(res["fluid_contacts"]) + + if req.save: + if "SW_SHM" in res["df"].columns: + for wn, grp in res["df"].groupby("WELL_NAME"): + _save_curves_to_well( + project_id, + str(wn), + grp[["DEPTH", "SW_SHM"]].rename(columns={"SW_SHM": "SHF"}), + ) + saved_wells += 1 + if "fluid_contacts" in res and isinstance(res["fluid_contacts"], pd.DataFrame) and not res["fluid_contacts"].empty: + try: + with get_db() as session: + proj_obj = db_objects.Project.load(session, project_id=project_id) + for wn, grp in res["fluid_contacts"].groupby("well_name"): + try: + well_obj = proj_obj.get_well(str(wn)) + well_obj.add_fluid_contacts(grp.to_dict(orient="records"), replace=False) + except Exception: + continue + except Exception: + pass zone_metrics_df = res.get("zone_metrics") zone_metrics_records = ( @@ -8926,6 +9078,7 @@ def workspace_optimize_fwl( "well_metrics": dataframe_to_records(res["well_metrics"].round(4)), "zone_metrics": zone_metrics_records, "grid_profile": dataframe_to_records(res["grid_profile"].round(4)), + "fluid_contacts": contacts_records, "saved": req.save, "saved_wells": saved_wells, } @@ -8935,6 +9088,125 @@ def workspace_optimize_fwl( ) from e +class WorkspaceExtractContactsRequest(BaseModel): + well_names: Optional[List[str]] = Field(None, description="Subset of wells to extract contacts for") + zones: Optional[List[str]] = Field(None, description="Subset of zones") + save: bool = Field(True, description="Save extracted contacts into each well's ancillary data table") + replace: bool = Field(False, description="Replace existing contacts for target wells") + clean_slate: bool = Field(True, description="Clean slate: delete existing contacts for the zones under purview before inserting new ones") + sw_pay_cutoff: float = Field(0.65, description="Max Sw for hydrocarbon pay") + sw_water_cutoff: float = Field(0.90, description="Min Sw for 100% water leg") + phit_cutoff: float = Field(0.05, description="Min porosity for reservoir rock") + vsh_cutoff: float = Field(0.50, description="Max Vshale for reservoir rock") + min_sand_thickness: float = Field(1.0, description="Min cumulative sand thickness (m) to consider a zone reservoir-bearing") + require_pay: bool = Field(True, description="Only extract contacts for zones containing confirmed hydrocarbon pay") + + +@router.post( + "/contacts/extract_and_save", + summary="Extract and save fluid limits & contacts (GUT, GDT, GOC, GWC, OUT, ODT, OWC, WUT, FWL) per well and zone", +) +def workspace_extract_and_save_contacts( + project_id: int, req: WorkspaceExtractContactsRequest +) -> Dict[str, Any]: + """Extract petrophysical fluid limits and contacts across project wells with full data lineage.""" + try: + proj_summary = get_project_summary(project_id) + all_well_names = [w["name"] for w in proj_summary.get("wells", [])] + target_wells = req.well_names if req.well_names else all_well_names + dfs = _load_multi_well_dfs(project_id, well_names=target_wells) + if not dfs: + raise ValueError("No well log data available for contact extraction.") + comb = pd.concat(dfs, ignore_index=True) + if req.zones and "ZONES" in comb.columns: + comb = comb[comb["ZONES"].isin(req.zones)] + + contacts_df = extract_fluid_contacts_and_limits( + df=comb, + sw_pay_cutoff=req.sw_pay_cutoff, + sw_water_cutoff=req.sw_water_cutoff, + phit_cutoff=req.phit_cutoff, + vsh_cutoff=req.vsh_cutoff, + min_sand_thickness=req.min_sand_thickness, + require_pay=req.require_pay, + ) + + saved_count = 0 + if req.save: + with get_db() as session: + proj = db_objects.Project.load(session, project_id=project_id) + zones_purview = [str(z).strip() for z in req.zones if str(z).strip()] if req.zones else None + + # Clean slate for target wells in the zones under purview + if req.clean_slate or req.replace: + for wn in target_wells: + try: + w = proj.get_well(str(wn)) + if req.replace or not zones_purview: + w.delete_fluid_contacts() + else: + w.delete_fluid_contacts(zones=zones_purview) + except Exception: + continue + + if not contacts_df.empty: + for wn, grp in contacts_df.groupby("well_name"): + try: + w = proj.get_well(str(wn)) + w.add_fluid_contacts( + grp.to_dict(orient="records"), + replace=False, + ) + saved_count += len(grp) + except Exception: + continue + + records = dataframe_to_records(contacts_df) + return { + "project_id": project_id, + "contacts_count": len(records), + "saved_count": saved_count, + "contacts": records, + } + except Exception as exc: + raise HTTPException( + status_code=400, detail=f"Failed to extract fluid contacts: {exc}" + ) from exc + + +@router.get( + "/contacts/summary", + summary="Get multi-well & zonal fluid contacts and limits summary for project", +) +def workspace_contacts_summary( + project_id: int, well_name: Optional[str] = None +) -> Dict[str, Any]: + """Retrieve all fluid contacts across project wells structured with lineage.""" + try: + with get_db() as session: + proj = db_objects.Project.load(session, project_id=project_id) + target_wells = [well_name] if well_name else proj.get_well_names() + all_rows = [] + for wn in target_wells: + try: + w = proj.get_well(wn) + df = w.get_fluid_contacts() + for r in df.to_dict(orient="records"): + r["well_name"] = wn + all_rows.append(r) + except Exception: + continue + return { + "project_id": project_id, + "contacts_count": len(all_rows), + "contacts": all_rows, + } + except Exception as exc: + raise HTTPException( + status_code=500, detail=f"Failed to get fluid contacts summary: {exc}" + ) from exc + + class WorkspaceMlInferenceRequest(WorkspaceFilterBase): model_config_key: str = Field( "clastic", @@ -9619,6 +9891,171 @@ def workspace_save_project_module_params( ) from e +@router.delete( + "/wells/{well_name}/module_params", + summary="Delete or reset single-well module parameters", +) +def workspace_delete_well_module_params( + project_id: int, + well_name: str, + module: Optional[str] = None, + key: Optional[str] = None, +) -> Dict[str, Any]: + """Delete saved single-well module parameters for a given module and/or selection key.""" + try: + well_keys = {well_name, resolve_db_well_name(project_id, well_name)} + merged: Dict[str, Any] = {} + persisted = False + if not is_demo_mode(): + with get_db() as session: + db_name = resolve_db_well_name(project_id, well_name) + well_orm = session.scalar( + select(db_models.Well).filter_by(project_id=project_id, name=db_name) + ) + if well_orm: + current_cfg = ( + dict(well_orm.config_data) + if isinstance(well_orm.config_data, dict) + else {} + ) + merged = dict(current_cfg.get(MODULE_PARAMS_CONFIG_KEY) or {}) + if module: + if key and module in merged: + mod_map = dict(merged[module]) + mod_map.pop(key, None) + if mod_map: + merged[module] = mod_map + else: + merged.pop(module, None) + else: + merged.pop(module, None) + else: + merged = {} + + current_cfg[MODULE_PARAMS_CONFIG_KEY] = merged + well_orm.config_data = _sanitize_for_json(current_cfg) + flag_modified(well_orm, "config_data") + session.add(well_orm) + persisted = True + + for w_key in well_keys: + eph = _ephemeral_well_configs.get((project_id, w_key)) + if eph and MODULE_PARAMS_CONFIG_KEY in eph: + eph = dict(eph) + eph[MODULE_PARAMS_CONFIG_KEY] = merged + _ephemeral_well_configs[(project_id, w_key)] = eph + + if not persisted: + base: Any = None + for w_key in well_keys: + eph = _ephemeral_well_configs.get((project_id, w_key)) or {} + if isinstance(eph.get(MODULE_PARAMS_CONFIG_KEY), dict): + base = eph[MODULE_PARAMS_CONFIG_KEY] + break + merged = dict(base or {}) + if module: + if key and module in merged: + mod_map = dict(merged[module]) + mod_map.pop(key, None) + if mod_map: + merged[module] = mod_map + else: + merged.pop(module, None) + else: + merged.pop(module, None) + else: + merged = {} + for w_key in well_keys: + eph = dict(_ephemeral_well_configs.get((project_id, w_key), {})) + eph[MODULE_PARAMS_CONFIG_KEY] = merged + _ephemeral_well_configs[(project_id, w_key)] = eph + + return _sanitize_for_json({ + "well_name": well_name, + "module_params": merged, + "deleted": True, + "persisted": persisted, + }) + except Exception as e: + raise HTTPException( + status_code=500, detail=f"Failed to delete module parameters: {e}" + ) from e + + +@router.delete( + "/module_params", + summary="Delete or reset project-level module parameters", +) +def workspace_delete_project_module_params( + project_id: int, + module: Optional[str] = None, + key: Optional[str] = None, +) -> Dict[str, Any]: + """Delete saved multi-well module parameters for a given module and/or zone key.""" + try: + merged: Dict[str, Any] = {} + persisted = False + if not is_demo_mode(): + try: + with get_db() as session: + proj = session.get(db_models.Project, project_id) + if proj is not None: + cfg = ( + dict(proj.config_data) + if isinstance(proj.config_data, dict) + else {} + ) + merged = dict(cfg.get(MODULE_PARAMS_CONFIG_KEY) or {}) + if module: + if key and module in merged: + mod_map = dict(merged[module]) + mod_map.pop(key, None) + if mod_map: + merged[module] = mod_map + else: + merged.pop(module, None) + else: + merged.pop(module, None) + else: + merged = {} + cfg[MODULE_PARAMS_CONFIG_KEY] = merged + proj.config_data = _sanitize_for_json(cfg) + flag_modified(proj, "config_data") + session.add(proj) + persisted = True + except Exception: + persisted = False + + if not persisted: + merged = dict(_ephemeral_project_module_params.get(project_id) or {}) + if module: + if key and module in merged: + mod_map = dict(merged[module]) + mod_map.pop(key, None) + if mod_map: + merged[module] = mod_map + else: + merged.pop(module, None) + else: + merged.pop(module, None) + else: + merged = {} + _ephemeral_project_module_params[project_id] = merged + else: + _ephemeral_project_module_params.pop(project_id, None) + + return _sanitize_for_json({ + "project_id": project_id, + "module_params": merged, + "deleted": True, + "persisted": persisted, + }) + except Exception as e: + raise HTTPException( + status_code=500, detail=f"Failed to delete project module parameters: {e}" + ) from e + + @router.post( "/demo/reset", summary="Reset ephemeral demo session edits back to pre-seeded Volve/Costa state", @@ -10736,6 +11173,9 @@ def workspace_clear_calculated_curves( class WorkspaceSwirrFitRequest(BaseModel): well_names: Optional[List[str]] = Field(None) + zones: Optional[List[str]] = Field(None) + min_depth: Optional[float] = Field(None) + max_depth: Optional[float] = Field(None) extraction_mode: Optional[str] = Field(None) extractionMode: Optional[str] = Field(None) reference_pc_psi: Optional[float] = Field(None) @@ -10821,6 +11261,9 @@ def _extract_project_scal_plugs_for_swirr( cutoffs: Optional[List[float]] = None, ift: float = 30.0, theta: float = 30.0, + zones: Optional[List[str]] = None, + min_depth: Optional[float] = None, + max_depth: Optional[float] = None, ) -> List[Dict[str, Any]]: """Extract plug-level SCAL drainage curves and matched CPORE/CPERM/ROCK_FLAG across project wells.""" cutoffs_list = sorted(float(c) for c in (cutoffs or [0.5, 1.2, 2.5, 5.0])) @@ -10850,6 +11293,14 @@ def _extract_project_scal_plugs_for_swirr( rf for rf in valid_flags if highest_flag is None or rf < highest_flag ] or ([1, 2, 3] if not valid_flags else valid_flags) + allowed_zones_set = ( + {str(z).strip() for z in zones if str(z).strip()} + if zones + else None + ) + min_d_filter = float(min_depth) if min_depth is not None else None + max_d_filter = float(max_depth) if max_depth is not None else None + plug_records: List[Dict[str, Any]] = [] with get_db() as session: proj = db_objects.Project.load(session, project_id=project_id) @@ -10867,6 +11318,10 @@ def _extract_project_scal_plugs_for_swirr( if pc_df is None or pc_df.empty: continue s_depth = float(sample.get("depth") or 0.0) + if min_d_filter is not None and s_depth > 0 and s_depth < min_d_filter: + continue + if max_d_filter is not None and s_depth > 0 and s_depth > max_d_filter: + continue meas = sample.get("measurements") cperm = None cpore = None @@ -10974,6 +11429,15 @@ def _norm_stem(val: Any) -> str: has_core_perm = True near_rock_flag: Optional[int] = None + if s_depth > 0 or allowed_zones_set is not None: + try: + if well_df_cache is None: + well_df_cache = _get_filtered_well_df(project_id, wn) + except Exception: + pass + s_zone = _resolve_scal_sample_zone(s_depth, well_df_cache, anc) + if allowed_zones_set is not None and s_zone not in allowed_zones_set: + continue if s_depth > 0: try: if well_df_cache is None: @@ -11078,10 +11542,11 @@ def _norm_stem(val: Any) -> str: plug_records.append( { - "plug_id": f"{wn}:{sname}", + "plug_id": f"{wn}::{sname}", "well": str(wn), "sample": str(sname), "depth": round(float(s_depth), 2), + "zone": s_zone, "rt": int(rf_val), "cpore": round(float(cpore), 4), "cperm": round(float(cperm), 4), @@ -11095,7 +11560,7 @@ def _norm_stem(val: Any) -> str: [int(r) if r is not None else -1 for r in rock_flags], dtype=int ) default_well = fzi_wells[0] if fzi_wells else "Project" - if len(plug_records) < 3: + if len(plug_records) < 3 and not allowed_zones_set: for rf_int in reservoir_flags: mask_rf = ( (rf_arr_full == rf_int) @@ -11138,10 +11603,11 @@ def _norm_stem(val: Any) -> str: sname_syn = f"SYN_RT{rf_int}_{p_idx + 1}" plug_records.append( { - "plug_id": f"{default_well}:{sname_syn}", + "plug_id": f"{default_well}::{sname_syn}", "well": str(default_well), "sample": sname_syn, "depth": round(1500.0 + rf_int * 25.0 + p_idx * 3.5, 2), + "zone": "Unknown", "rt": int(rf_int), "cpore": round(p_plug, 4), "cperm": round(k_plug, 4), @@ -11201,7 +11667,14 @@ def workspace_swirr_fit( if req.excluded_plug_ids is not None else (req.excludedPlugIds or []) ) - excluded_set = {str(x).strip() for x in excl_raw if str(x).strip()} + excluded_set = set() + for x in excl_raw: + x_s = str(x).strip() + if x_s: + if "::" not in x_s and ":" in x_s: + excluded_set.add(x_s.replace(":", "::", 1)) + else: + excluded_set.add(x_s) raw_plugs = _extract_project_scal_plugs_for_swirr( project_id, @@ -11210,6 +11683,9 @@ def workspace_swirr_fit( cutoffs=req.cutoffs, ift=req.ift, theta=req.theta, + zones=req.zones, + min_depth=req.min_depth, + max_depth=req.max_depth, ) plug_rows: List[Dict[str, Any]] = [] @@ -11224,7 +11700,7 @@ def workspace_swirr_fit( rqi_ratio = math.sqrt(cperm / max(0.001, cpore)) rqi_um = 0.0314 * rqi_ratio pid = str(p["plug_id"]) - is_excl = pid in excluded_set + is_excl = pid in excluded_set or pid.replace("::", ":", 1) in excluded_set is_incl = bool(valid_pc and not is_excl) plug_rows.append( diff --git a/quick_pp/app/backend/template/reports/sections/06_saturation_contacts.md.j2 b/quick_pp/app/backend/template/reports/sections/06_saturation_contacts.md.j2 index 131ae073..fe2cac4b 100644 --- a/quick_pp/app/backend/template/reports/sections/06_saturation_contacts.md.j2 +++ b/quick_pp/app/backend/template/reports/sections/06_saturation_contacts.md.j2 @@ -32,3 +32,41 @@ $$S_w^n + R_w \cdot B \cdot Q_v \cdot S_w^{n-1} = \frac{a \cdot R_w}{\phi_t^m \c ![Figure {{ sec_num }}.1: Pickett Plot & Water Saturation Profile vs. Free Water Level]({{ assets.pickett_shf_plot }}) {% endif %} + +### {{ sec_num }}.2 Multi-Well & Zonal Fluid Contacts, Proved Column & Uncertainty Brackets + +Accurate delineation of fluid contacts is fundamental to volumetric in-place estimation and dynamic reservoir simulation. The proved hydrocarbon column is bounded by the shallowest and deepest proved fluid limits, while discrete contacts (OWC, GOC, GWC) and Free Water Levels (FWL) define the hydrostatic equilibrium boundaries. + +{% if fluid_contacts_rows and fluid_contacts_rows|length > 0 %} +| Well Name | Zone | Fault Block | Contact / Limit Type | Depth (m TVDss) | Determination Method / Source | Geological Notes / Remarks | +| :--- | :--- | :--- | :---: | :---: | :--- | :--- | +{% for row in fluid_contacts_rows %} +| {{ row.well_name }} | {{ row.zone or "General" }} | {{ row.fault_block or "-" }} | **{{ row.contact_type }}** | {{ "%.1f"|format(row.depth) if row.depth is not none else "-" }} | {{ row.source or "Petrophysical Log Evaluation" }} | {{ row.remarks or "-" }} | +{% endfor %} + +{% if fluid_contacts_summary and fluid_contacts_summary|length > 0 %} +#### Hydrocarbon Column & Uncertainty Bracket Summary by Zone + +{% for z in fluid_contacts_summary %} +* **Zone: `{{ z.zone }}`**{% if z.fault_block %} (Fault Block: `{{ z.fault_block }}`){% endif %}: + {% if z.gut is not none and z.gdt is not none %} + - **Proved Gas Column:** ${{ "%.1f"|format(z.gut) }}\text{ m}$ (GUT) to ${{ "%.1f"|format(z.gdt) }}\text{ m TVDss}$ (GDT) [Proved Gross Gas Column: ${{ "%.1f"|format(z.gdt - z.gut) }}\text{ m}$]. + {% endif %} + {% if z.out is not none and z.odt is not none %} + - **Proved Oil Column:** ${{ "%.1f"|format(z.out) }}\text{ m}$ (OUT) to ${{ "%.1f"|format(z.odt) }}\text{ m TVDss}$ (ODT) [Proved Gross Oil Column: ${{ "%.1f"|format(z.odt - z.out) }}\text{ m}$]. + {% endif %} + {% if z.goc is not none %} + - **Gas-Oil Contact (GOC):** ${{ "%.1f"|format(z.goc) }}\text{ m TVDss}$ (Bracket: ${{ "%.1f"|format(z.gdt) if z.gdt else "N/A" }}\text{ m}$ to ${{ "%.1f"|format(z.out) if z.out else "N/A" }}\text{ m}$). + {% endif %} + {% if z.owc is not none %} + - **Oil-Water Contact (OWC):** ${{ "%.1f"|format(z.owc) }}\text{ m TVDss}$ (Bracket: ${{ "%.1f"|format(z.odt) if z.odt else "N/A" }}\text{ m}$ to ${{ "%.1f"|format(z.wut) if z.wut else "N/A" }}\text{ m}$). + {% endif %} + {% if z.fwl is not none %} + - **Free Water Level (FWL):** ${{ "%.1f"|format(z.fwl) }}\text{ m TVDss}$ (Capillary $P_c = 0$ Datum). + {% endif %} +{% endfor %} +{% endif %} +{% else %} +> [!NOTE] +> Discrete single-well fluid contact markers (GUT/GDT/OUT/ODT/WUT/OWC/GOC/FWL) have not yet been extracted or committed to ancillary data for this project. Global saturation model uses $FWL = {{ "%.1f"|format(sw_params.fwl) }}\text{ m TVDss}$. +{% endif %} diff --git a/quick_pp/app/frontend/package-lock.json b/quick_pp/app/frontend/package-lock.json index b65c4779..a00667c3 100644 --- a/quick_pp/app/frontend/package-lock.json +++ b/quick_pp/app/frontend/package-lock.json @@ -1,12 +1,12 @@ { "name": "frontend", - "version": "0.3.3", + "version": "0.3.4", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "frontend", - "version": "0.3.3", + "version": "0.3.4", "devDependencies": { "@dnd-kit-svelte/svelte": "^0.1.5", "@dnd-kit/abstract": "^0.1.21", diff --git a/quick_pp/app/frontend/package.json b/quick_pp/app/frontend/package.json index d9d0633a..8a35e46f 100644 --- a/quick_pp/app/frontend/package.json +++ b/quick_pp/app/frontend/package.json @@ -1,7 +1,7 @@ { "name": "frontend", "private": true, - "version": "0.3.3", + "version": "0.3.4", "type": "module", "scripts": { "dev": "vite dev", diff --git a/quick_pp/app/frontend/src/lib/api/client.ts b/quick_pp/app/frontend/src/lib/api/client.ts index 861a8158..2b2aafe3 100644 --- a/quick_pp/app/frontend/src/lib/api/client.ts +++ b/quick_pp/app/frontend/src/lib/api/client.ts @@ -323,6 +323,18 @@ export function describeApiEndpoint(method: string, rawUrl: string): EndpointSta done: 'Rock Type classifier saved' }; } + if (path.includes('/module_params') && m === 'DELETE') { + return { + active: 'Resetting saved module parameters...', + done: 'Saved module parameters reset' + }; + } + if (path.includes('/fluid_contacts') && m === 'DELETE') { + return { + active: 'Deleting fluid contacts...', + done: 'Fluid contacts deleted' + }; + } if (path.includes('/rock_typing/models/') && m === 'DELETE') { return { active: 'Deleting saved Rock Type classifier...', @@ -693,6 +705,58 @@ export const apiPost = postJson; export const apiPatch = patchJson; export const apiDelete = deleteJson; +/** + * Delete saved single-well module parameters (optionally for a specific module and/or selection key). + */ +export async function deleteWellModuleParams( + projectId: string | number, + wellName: string, + module?: string, + key?: string +): Promise<{ well_name: string; module_params: Record; deleted: boolean }> { + const params = new URLSearchParams(); + if (module) params.set('module', module); + if (key) params.set('key', key); + const qs = params.toString(); + return deleteJson( + `/quick_pp/workspace/projects/${encodeURIComponent(String(projectId))}/wells/${encodeURIComponent(wellName)}/module_params${qs ? `?${qs}` : ''}` + ); +} + +/** + * Delete saved project-level (multi-well) module parameters (optionally for a specific module and/or zone key). + */ +export async function deleteProjectModuleParams( + projectId: string | number, + module?: string, + key?: string +): Promise<{ project_id: number; module_params: Record; deleted: boolean }> { + const params = new URLSearchParams(); + if (module) params.set('module', module); + if (key) params.set('key', key); + const qs = params.toString(); + return deleteJson( + `/quick_pp/workspace/projects/${encodeURIComponent(String(projectId))}/module_params${qs ? `?${qs}` : ''}` + ); +} + +/** + * Delete fluid contacts across project or for a well (optionally auto-generated only). + */ +export async function deleteFluidContacts( + projectId: string | number, + options?: { wellName?: string; autoOnly?: boolean; zone?: string } +): Promise<{ deleted_count: number; wells_affected: string[]; auto_only: boolean }> { + const params = new URLSearchParams(); + if (options?.wellName) params.set('well_name', options.wellName); + if (options?.autoOnly !== undefined) params.set('auto_only', String(options.autoOnly)); + if (options?.zone) params.set('zone', options.zone); + const qs = params.toString(); + return deleteJson( + `/quick_pp/workspace/projects/${encodeURIComponent(String(projectId))}/fluid_contacts${qs ? `?${qs}` : ''}` + ); +} + /** * Download the complete Petrophysical Handover Package (.zip) for a project or well. */ diff --git a/quick_pp/app/frontend/src/lib/components/BulkAncillaryImporter.svelte b/quick_pp/app/frontend/src/lib/components/BulkAncillaryImporter.svelte index 4a0ef746..a04a19ce 100644 --- a/quick_pp/app/frontend/src/lib/components/BulkAncillaryImporter.svelte +++ b/quick_pp/app/frontend/src/lib/components/BulkAncillaryImporter.svelte @@ -87,8 +87,12 @@ ], fluid_contacts: [ { key: 'WELL_NAME', label: 'WELL_NAME — Well Name / UWI' }, - { key: 'CONTACT_TYPE', label: 'CONTACT_TYPE — Fluid Contact (OWC/GOC/FWL) *', required: true }, - { key: 'DEPTH', label: 'DEPTH — Contact Depth *', required: true } + { key: 'CONTACT_TYPE', label: 'CONTACT_TYPE — Fluid Contact (OWC/GOC/GWC/FWL/ODT/OUT/GDT/GUT/WUT) *', required: true }, + { key: 'DEPTH', label: 'DEPTH — Contact Depth *', required: true }, + { key: 'ZONE', label: 'ZONE — Reservoir Zone / Formation' }, + { key: 'FAULT_BLOCK', label: 'FAULT_BLOCK — Fault Block / Compartment' }, + { key: 'SOURCE', label: 'SOURCE — Method / Determination Lineage' }, + { key: 'REMARKS', label: 'REMARKS — Geological Notes / Comments' } ], perforations: [ { key: 'WELL_NAME', label: 'WELL_NAME — Well Name / UWI' }, diff --git a/quick_pp/app/frontend/src/lib/components/modules/InspectorReport.svelte b/quick_pp/app/frontend/src/lib/components/modules/InspectorReport.svelte index 46aa7bc2..aba0a276 100644 --- a/quick_pp/app/frontend/src/lib/components/modules/InspectorReport.svelte +++ b/quick_pp/app/frontend/src/lib/components/modules/InspectorReport.svelte @@ -46,17 +46,34 @@ []) as AnomalyFlagItem[] ); + let evaluatedWells = $derived( + projectData.wells.filter((w) => { + const c = w.curves || (w as any).available_curves || []; + return c.length > 0 && (w.row_count ?? (w as any).depth_count ?? 1) > 0; + }) + ); + + function getTargetWells(): string[] { + if (isSingleWell) { + return workbench.wellId + ? [workbench.wellId] + : (evaluatedWells[0]?.name ? [evaluatedWells[0].name] : projectData.wells.slice(0, 1).map((w) => w.name)); + } + if (projectData.selectedWellNames.length > 0) { + const evalSet = new Set(evaluatedWells.map((w) => w.name)); + const validSel = projectData.selectedWellNames.filter((name) => evalSet.has(name)); + return validSel.length > 0 ? validSel : evaluatedWells.map((w) => w.name); + } + return evaluatedWells.length > 0 + ? evaluatedWells.map((w) => w.name) + : projectData.wells.map((w) => w.name); + } + async function runPreRenderAudit() { if (!workbench.projectId) return; isRunningPreCheck = true; try { - const targetWells = isSingleWell - ? workbench.wellId - ? [workbench.wellId] - : projectData.wells.slice(0, 1).map((w) => w.name) - : projectData.selectedWellNames.length > 0 - ? projectData.selectedWellNames - : projectData.wells.map((w) => w.name); + const targetWells = getTargetWells(); const params = new URLSearchParams(); if (targetWells.length > 0) { params.set('wells', targetWells.join(',')); @@ -104,10 +121,14 @@ } function toggleWellSelection(wellName: string) { - if (projectData.selectedWellNames.includes(wellName)) { - projectData.selectedWellNames = projectData.selectedWellNames.filter((w) => w !== wellName); + const currentSel = + projectData.selectedWellNames.length > 0 + ? [...projectData.selectedWellNames] + : evaluatedWells.map((w) => w.name); + if (currentSel.includes(wellName)) { + projectData.selectedWellNames = currentSel.filter((w) => w !== wellName); } else { - projectData.selectedWellNames = [...projectData.selectedWellNames, wellName]; + projectData.selectedWellNames = [...currentSel, wellName]; } } @@ -119,13 +140,7 @@ const reportType: ReportType = isSingleWell ? 'SINGLE_WELL_CPI' : workbench.reportConfig.reportType; - const targetWells = isSingleWell - ? workbench.wellId - ? [workbench.wellId] - : projectData.wells.slice(0, 1).map((w) => w.name) - : projectData.selectedWellNames.length > 0 - ? projectData.selectedWellNames - : projectData.wells.map((w) => w.name); + const targetWells = getTargetWells(); const defaultTitle = isSingleWell ? `Well ${targetWells[0] || ''} — Petrophysical CPI & Evaluation Dossier` @@ -157,6 +172,7 @@ well_names: targetWells, zones: activeZones, target_zones: activeZones, + fwl_tvdss: workbench.saturationParams.fwl ?? projectData.shfParams.fwl ?? undefined, min_depth: workbench.depthFilter ? workbench.depthFilter.min : undefined, max_depth: workbench.depthFilter ? workbench.depthFilter.max : undefined, model_params: { @@ -170,6 +186,14 @@ nphi_clay: workbench.lithoParams.dryClayNphi, rho_clay: workbench.lithoParams.dryClayRhob, rho_fl: workbench.lithoParams.fluidRhob, + dry_sand_point: [-0.02, workbench.lithoParams.drySandRhob ?? 2.65], + dry_clay_point: [workbench.lithoParams.dryClayNphi ?? 0.33, workbench.lithoParams.dryClayRhob ?? 2.70], + dry_silt_point: (workbench.lithoParams as any).drySiltPoint ?? undefined, + silt_line_angle: (workbench.lithoParams as any).siltLineAngle ?? 119.0, + fluid_point: [1.0, workbench.lithoParams.fluidRhob ?? 1.0], + fzi_cutoffs: projectData.fziCutoffs, + fziCutoffs: projectData.fziCutoffs, + fwl: workbench.saturationParams.fwl ?? projectData.shfParams.fwl ?? undefined, badhole_treatment: workbench.lithoParams.badholeTreatment, cali_threshold: workbench.badholeParams.caliThreshold, bitsize_default: workbench.badholeParams.bitsizeDefault, @@ -369,7 +393,7 @@ title={isSingleWell ? 'Well Net Pay Cutoffs' : 'Evaluated Wells & Pay Cutoffs'} badge={isSingleWell ? 'Pay Flags' - : `${projectData.selectedWellNames.length || projectData.wells.length} wells`} + : `${projectData.selectedWellNames.length || evaluatedWells.length} wells`} > {#if !isSingleWell && projectData.wells.length > 0}
@@ -381,10 +405,10 @@ ·
-
+
+
diff --git a/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte b/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte index 030a13c4..5db8aa92 100644 --- a/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte +++ b/quick_pp/app/frontend/src/lib/components/modules/MultiWellShf.svelte @@ -4,6 +4,7 @@ import NumericScrubber from '$lib/components/workbench/NumericScrubber.svelte'; import ParameterSection from '$lib/components/workbench/ParameterSection.svelte'; import SavedParamsIndicator from '$lib/components/workbench/SavedParamsIndicator.svelte'; + import { appStatus } from '$lib/state/status.svelte'; import { projectData } from '$lib/state/projectData.svelte'; import { projectParams } from '$lib/state/projectParams.svelte'; import { wellData } from '$lib/state/wellData.svelte'; @@ -119,16 +120,12 @@ ); } } - if ( - !projectParams.status['shf']?.matched && - (projectData.swirrResult || - projectData.swirrApplyResult || - Object.keys(projectData.swirrConfig.rules ?? {}).length > 0) - ) { - projectData.shfParams.swirrSource = 'poro_perm'; - } - if (!projectData.shfResult && projectData.swirrResult) { - projectData.recomputeScalFitsFromPlugs(false); + if (!projectData.shfResult) { + if (projectData.swirrResult) { + projectData.recomputeScalFitsFromPlugs(false); + } else { + void projectData.ensureSwirrDataLoaded(); + } } }); @@ -166,9 +163,8 @@ try { const sp = projectData.shfParams; const jSatMode = sp.jSatMode ?? 'swn'; - // Restored SHF fits (from saved project params) are used once on the next non-reset run + // Restored SHF fits (from saved project params) are used on non-reset runs const saved = projectData.shfSavedFits; - projectData.shfSavedFits = null; const restoredFits = !resetFits && saved && @@ -192,6 +188,9 @@ `/quick_pp/workspace/projects/${encodeURIComponent(workbench.projectId)}/shf/compute`, { well_names: validTvdWells, + zones: workbench.selectedZones.length > 0 ? workbench.selectedZones : null, + min_depth: workbench.depthFilter?.min ?? null, + max_depth: workbench.depthFilter?.max ?? null, model: sp.model, j_sat_mode: jSatMode, unit: sp.unit ?? 'metric', @@ -221,6 +220,8 @@ }; if (markDirty) { projectData.markMultiWellComputed('shf'); + // Auto-persist SHF parameters & fits for the active zone selection + void projectData.persistProjectParams([], ['shf']); } // Live-merge SHF curve into active well so LogTrackCanvas Track 6 updates in real time! @@ -281,6 +282,7 @@ shm_params: projectData.shfResult?.fits ?? null, loss: so.optLoss, solve_per_zone: so.solvePerZone, + pay_weighted: so.payWeighted ?? true, ift: sp.ift, theta: sp.theta, gw: sp.gw, @@ -311,6 +313,33 @@ projectData.isComputingMultiWell = false; } } + + let isSavingContacts = $state(false); + async function saveContactsToAncillary() { + if (!workbench.projectId) return; + isSavingContacts = true; + try { + const res = await postJson( + `/quick_pp/workspace/projects/${encodeURIComponent(workbench.projectId)}/contacts/extract_and_save`, + { + well_names: projectData.wellsWithTvd.map((w) => w.name), + zones: workbench.selectedZones.length > 0 ? workbench.selectedZones : null, + save: true, + clean_slate: true, + replace: false + } + ); + const count = res.saved_count ?? res.contacts_count ?? 0; + appStatus.success(`Saved ${count} fluid contacts (ODT, OWC, FWL, etc.) to well ancillary data.`); + if (workbench.projectId) { + await projectData.selectProject(workbench.projectId); + } + } catch (e: any) { + projectData.error = e?.message || 'Failed to save fluid contacts'; + } finally { + isSavingContacts = false; + } + }
@@ -396,6 +425,14 @@ /> Φ-Weighted +
{/each}
diff --git a/quick_pp/app/frontend/src/lib/state/auth.svelte.ts b/quick_pp/app/frontend/src/lib/state/auth.svelte.ts index 1977d3aa..33c16e6f 100644 --- a/quick_pp/app/frontend/src/lib/state/auth.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/auth.svelte.ts @@ -63,7 +63,7 @@ export interface AuditLogItem { class AuthState { user = $state(null); token = $state(null); - appVersion = $state('0.3.3'); + appVersion = $state('0.3.4'); isInitializing = $state(true); uamModalOpen = $state(false); accountModalOpen = $state(false); diff --git a/quick_pp/app/frontend/src/lib/state/moduleParams.svelte.ts b/quick_pp/app/frontend/src/lib/state/moduleParams.svelte.ts index 874bc9c0..7c14c412 100644 --- a/quick_pp/app/frontend/src/lib/state/moduleParams.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/moduleParams.svelte.ts @@ -6,8 +6,89 @@ * window, falling back to `well` when no filter is active. Saved parameters are restored * automatically on exact selection-key match when the well is loaded or the selection changes. */ +import { deleteWellModuleParams } from '$lib/api/client'; import { workbench } from './workbench.svelte'; +export const SINGLE_WELL_REGIONAL_DEFAULTS: Record> = { + 'qaqc-badhole': { + useCali: true, + caliThreshold: 2.0, + autoBitsize: true, + useDrift: true, + driftWindowM: 50.0, + driftThresholdSigma: 2.5, + useDensityWashout: false, + caliSpikeThreshold: 0.5 + }, + 'qaqc-env': { + bitsizeDefault: 8.5, + mudWeight: 9.5, + surfaceTemp: 75.0, + tempGradient: 1.5, + salinityPpm: 30000.0 + }, + 'litho-poro': { + litho: { + model: 'ssc', + porosityMethod: 'den', + logCorrectionMode: 'none', + grClean: 20.0, + grClay: 120.0, + drySandNphi: -0.02, + drySandRhob: 2.65, + dryClayNphi: 0.44, + dryClayRhob: 2.78, + fluidNphi: 1.0, + fluidRhob: 1.0, + badholeTreatment: 'interpolate' + }, + ts: { + cleanSandPoint: [-0.02, 2.65], + pureShalePoint: [0.44, 2.78], + fluidPoint: [1.0, 1.0], + useInSaturation: false, + useInPerm: false + } + }, + perm: { + method: 'choo', + m: 2.0, + A: 1000000, + coatesA: 10000.0, + coatesB: 2.0, + coatesC: 2.0, + useTsInPerm: false + }, + saturation: { + method: 'waxman_smits', + rwMode: 'salinity_temp', + measSystem: 'metric', + a: 1.0, + m: 2.0, + n: 2.0, + rwVal: 0.05, + waterSalinity: 30000, + fwl: 0, + useTsInSaturation: false + }, + scal: { + model: 'let', + swirr: 0.15, + sor: 0.2, + l_w: 2.0, + e_w: 1.0, + t_w: 1.0, + l_o: 2.0, + e_o: 1.0, + t_o: 1.0 + }, + ressum: { + vclCutoff: 0.4, + phieCutoff: 0.08, + swCutoff: 0.6 + } +}; + export type ParamModuleKey = | 'qaqc-badhole' | 'qaqc-env' @@ -255,7 +336,7 @@ class ModuleParamsState { /** Number of saved selections per module for the loaded well. */ savedCounts = $state>>({}); - /** Apply exact-match saved params for the current selection to all registered modules. */ + /** Apply saved params (exact, bundle subset, or whole well fallback) for the current selection. */ applyForCurrentSelection(config: Record | null | undefined) { const cfg = (config || {}) as Record; const mp = (cfg.module_params || {}) as ModuleParamsMap; @@ -268,6 +349,25 @@ class ModuleParamsState { const modMap = mp[module] || {}; nextCounts[module] = Object.keys(modMap).length; let entry: SavedModuleParamsEntry | null = modMap[key] ?? null; + let matchType: 'exact' | 'bundle' | 'fallback_all' = 'exact'; + if (!entry && sel.zones && sel.zones.length > 0) { + const normSel = [...new Set(sel.zones.map((s) => String(s).trim()).filter(Boolean))].sort(); + const bundles = Object.entries(modMap) + .filter(([, e]) => { + const ez = (e.selection?.zones ?? []).map((s) => String(s).trim()).filter(Boolean); + const s = new Set(ez); + return normSel.every((z) => s.has(z)); + }) + .sort((a, b) => (a[1].selection?.zones?.length ?? 99) - (b[1].selection?.zones?.length ?? 99)); + if (bundles.length > 0) { + entry = bundles[0][1]; + matchType = 'bundle'; + } + } + if (!entry && key !== 'well' && modMap['well']) { + entry = modMap['well']; + matchType = 'fallback_all'; + } if (!entry && key === 'well' && Object.keys(modMap).length === 0) { entry = legacyWellEntry(module, cfg); } @@ -278,11 +378,17 @@ class ModuleParamsState { // Ignore malformed saved entry; keep current values } } + let displayLabel = entry?.label ?? null; + if (entry && matchType === 'bundle') { + displayLabel = `${entry.label || 'Saved bundle'} (bundle)`; + } else if (entry && matchType === 'fallback_all') { + displayLabel = `${entry.label || 'Whole well'} (default)`; + } nextStatus[module] = { key, label, matched: Boolean(entry), - savedLabel: entry?.label ?? null, + savedLabel: displayLabel, savedAt: entry?.saved_at || null }; } @@ -315,6 +421,89 @@ class ModuleParamsState { this.status = {}; this.savedCounts = {}; } + + getRegionalDefaults(module: ParamModuleKey): Record { + return clone(SINGLE_WELL_REGIONAL_DEFAULTS[module]); + } + + /** Reset the current active reactive parameters in the workbench to regional default presets. */ + resetToRegionalDefaults(module: ParamModuleKey) { + const defs = this.getRegionalDefaults(module); + REGISTRY[module].set(defs); + } + + /** Delete a specific saved preset for a module & selection key in the well config & backend. */ + async deleteSavedEntry( + projectId: string | number, + wellName: string, + module: ParamModuleKey, + key: string, + wellConfig?: Record + ): Promise { + try { + await deleteWellModuleParams(projectId, wellName, module, key); + if (wellConfig?.module_params?.[module]?.[key]) { + delete wellConfig.module_params[module][key]; + } + this.applyForCurrentSelection(wellConfig); + return true; + } catch { + return false; + } + } + + /** Delete all saved presets for a module across all intervals in the well. */ + async deleteAllForModule( + projectId: string | number, + wellName: string, + module: ParamModuleKey, + wellConfig?: Record + ): Promise { + try { + await deleteWellModuleParams(projectId, wellName, module); + if (wellConfig?.module_params?.[module]) { + delete wellConfig.module_params[module]; + } + this.applyForCurrentSelection(wellConfig); + return true; + } catch { + return false; + } + } + + /** Extract a flat list of all saved entries for convenient tabular presentation. */ + getAllSavedEntries(wellConfig?: Record): Array<{ + module: ParamModuleKey; + moduleLabel: string; + key: string; + label: string; + saved_at: string; + params: Record; + }> { + const mp = ((wellConfig?.module_params || {}) as ModuleParamsMap); + const rows: Array<{ + module: ParamModuleKey; + moduleLabel: string; + key: string; + label: string; + saved_at: string; + params: Record; + }> = []; + for (const m of PARAM_MODULE_KEYS) { + const modMap = mp[m] || {}; + for (const [k, e] of Object.entries(modMap)) { + rows.push({ + module: m, + moduleLabel: PARAM_MODULE_LABELS[m], + key: k, + label: e.label || k, + saved_at: e.saved_at || '', + params: e.params || {} + }); + } + } + return rows; + } } export const moduleParams = new ModuleParamsState(); diff --git a/quick_pp/app/frontend/src/lib/state/projectData.svelte.ts b/quick_pp/app/frontend/src/lib/state/projectData.svelte.ts index cb91336a..1dab053e 100644 --- a/quick_pp/app/frontend/src/lib/state/projectData.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/projectData.svelte.ts @@ -1,4 +1,4 @@ -import { API_BASE, api, authHeaders, deleteJson, getJson, patchJson, postJson } from '$lib/api/client'; +import { API_BASE, api, authHeaders, deleteFluidContacts, deleteJson, deleteProjectModuleParams, getJson, patchJson, postJson } from '$lib/api/client'; import type { ProjectSummaryResponse, WellSummaryItem } from '$lib/api/schema'; import { appStatus } from './status.svelte'; import { wellData } from './wellData.svelte'; @@ -8,6 +8,7 @@ import { isProjectParamModuleKey, PROJECT_PARAM_MODULE_KEYS, projectParams, + resolveMatchingProjectParamsEntry, zoneSelectionKey, zoneSelectionLabel, type ProjectModuleParamsMap, @@ -59,6 +60,7 @@ export interface ShfOptimizerParams { mdtWeight: number; poreVolumeWeighted: boolean; equalWellWeights: boolean; + payWeighted?: boolean; } export interface MlTrainerParams { @@ -155,6 +157,7 @@ export interface SwirrPlugRow { well: string; sample: string; depth: number; + zone?: string; rt: number; cpore: number; cperm: number; @@ -467,6 +470,11 @@ class ProjectDataState { selectedScalPointIds = $state([]); scalClickFilterTarget = $state<'point' | 'plug'>('point'); + scalGroupMode = $state<'rt' | 'sample'>('sample'); + scalConnectPlugs = $state(true); + scalShowSampleLabels = $state(false); + focusedScalPlugId = $state(null); + scalFilterRockType = $state('all'); swirrInputCols = $state<{ phieCol: string; permCol: string; rtCol: string }>({ phieCol: 'PHIT', @@ -529,7 +537,8 @@ class ProjectDataState { mdtFwl: 3925.0, mdtWeight: 1.0, poreVolumeWeighted: true, - equalWellWeights: true + equalWellWeights: true, + payWeighted: true }); mlTrainerParams = $state({ @@ -598,6 +607,7 @@ class ProjectDataState { pc: number[]; sw: number[]; swn: number[]; + swirr_calc?: number[]; j: number[]; h: number[]; bvw: number[]; @@ -606,6 +616,8 @@ class ProjectDataState { rock_flags: number[]; well_names: string[]; samples: string[]; + depths?: number[]; + zones?: string[]; point_ids?: string[]; plug_ids?: string[]; excluded?: boolean[]; @@ -1001,21 +1013,28 @@ class ProjectDataState { projectParams.loadGeneration += 1; } - /** Apply exact zone-key matches from saved project parameters to all multi-well modules. */ + /** Apply zone-key matches (exact, bundle subset, or fallback) from saved project parameters to all multi-well modules. */ applyProjectParamsForCurrentSelection() { const mp = this.projectModuleParams || {}; const key = zoneSelectionKey(); const label = zoneSelectionLabel(); for (const module of PROJECT_PARAM_MODULE_KEYS) { - const entry = mp[module]?.[key] ?? null; - if (entry?.params) { + const modMap = mp[module]; + const resolved = resolveMatchingProjectParamsEntry(modMap, workbench.selectedZones); + if (resolved?.entry?.params) { try { - this.applyProjectParams(module, entry.params); + this.applyProjectParams(module, resolved.entry.params); } catch { // Ignore malformed saved entry; keep current values } } - projectParams.setStatus(module, key, label, entry); + projectParams.setStatus( + module, + key, + label, + resolved?.entry ?? null, + resolved?.matchType ?? 'exact' + ); } projectParams.setCounts(mp); } @@ -1070,6 +1089,90 @@ class ProjectDataState { } } + /** Reset rock typing and classifier results and saved parameters to defaults. */ + async resetRockTyping(): Promise { + this.rockTypingResult = null; + if (workbench.projectId) { + await projectParams.deleteAllForModule(workbench.projectId, 'rock-typing', this.projectModuleParams); + await projectParams.deleteAllForModule(workbench.projectId, 'rt-classifier', this.projectModuleParams); + } + appStatus.success('Rock typing and classifier reset to regional defaults'); + } + + /** Reset SCAL plug fittings, Swirr correlation, and SWOF simulation deck. */ + async resetScalFits(): Promise { + this.swirrConfig.referencePcPsi = 100.0; + this.swirrConfig.extractionMode = 'cutoff_pc'; + this.swirrConfig.equationType = 'rqi_power'; + this.swirrConfig.fitScope = 'global'; + this.swirrConfig.excludedPlugIds = []; + this.swirrConfig.excludedScalPointIds = []; + this.swirrResult = null; + workbench.scalResult = null; + this.focusedScalPlugId = null; + this.selectedScalPointIds = []; + if (workbench.projectId) { + await projectParams.deleteAllForModule(workbench.projectId, 'swirr-corr', this.projectModuleParams); + } + appStatus.success('SCAL & Swirr fittings reset to regional defaults'); + } + + /** Reset SHF model parameters, Leverett-J fits, and FWL datum. */ + async resetShf(): Promise { + this.shfParams.model = 'leverett_j'; + this.shfParams.jSatMode = 'swn'; + this.shfParams.swirrSource = 'lab_min'; + this.shfParams.unit = 'metric'; + this.shfParams.fwl = 2500.0; + this.shfParams.fwlByZone = {}; + this.shfParams.ift = 30.0; + this.shfParams.theta = 30.0; + this.shfParams.gw = 1.05; + this.shfParams.ghc = 0.8; + this.shfOptParams.solvePerZone = false; + this.shfResult = null; + this.shfSavedFits = null; + if (workbench.projectId) { + await projectParams.deleteAllForModule(workbench.projectId, 'shf', this.projectModuleParams); + } + appStatus.success('SHF model and FWL reset to regional defaults'); + } + + /** Delete auto-generated / optimized fluid contacts across project wells. */ + async deleteAutoContacts(zone?: string): Promise { + const pid = workbench.projectId; + if (!pid) return 0; + try { + const res = await deleteFluidContacts(pid, { autoOnly: true, zone }); + const count = res?.deleted_count ?? 0; + const autoNames = new Set(['GUT', 'GDT', 'OUT', 'ODT', 'WUT']); + wellData.contacts = wellData.contacts.filter((c: any) => { + if (zone && c.zone && c.zone !== zone) return true; + const cName = c.name || c.type; + if (autoNames.has(cName)) return false; + const src = String(c.source || '').toLowerCase(); + if (src.includes('log evaluation') || src.includes('calculated') || src.includes('optimizer') || src.includes('fpt')) return false; + return true; + }); + for (const w of this.wells) { + if (Array.isArray(w.fluid_contacts)) { + w.fluid_contacts = w.fluid_contacts.filter((c: any) => { + const cName = c.name || c.type; + if (autoNames.has(cName)) return false; + const src = String(c.source || '').toLowerCase(); + if (src.includes('log evaluation') || src.includes('calculated') || src.includes('optimizer') || src.includes('fpt')) return false; + return true; + }); + } + } + appStatus.success(`Deleted ${count} auto-generated fluid contact(s) across project`); + return count; + } catch (e: any) { + appStatus.error(`Failed to delete fluid contacts: ${e?.message ?? e}`); + return 0; + } + } + private static readonly MULTI_WELL_PIPELINE: MultiWellSaveModule[] = [ 'rock-typing', 'perm-transform', @@ -1085,16 +1188,44 @@ class ProjectDataState { return ProjectDataState.MULTI_WELL_PIPELINE.some((m) => Boolean(this.pendingMultiWellModules[m])); } - async saveMultiWellComputed(moduleKey: MultiWellSaveModule): Promise { + async saveMultiWellComputed( + moduleKey: MultiWellSaveModule, + savePrecedingPending: boolean = true + ): Promise { const pid = workbench.projectId; - if (!pid || !this.hasPendingMultiWell(moduleKey)) return false; + if (!pid) return false; + + const pipeline = ProjectDataState.MULTI_WELL_PIPELINE; + const targetIdx = pipeline.indexOf(moduleKey); + const modulesToSave: MultiWellSaveModule[] = []; + + if (savePrecedingPending && targetIdx >= 0) { + for (let i = 0; i <= targetIdx; i++) { + const m = pipeline[i]; + if (this.hasPendingMultiWell(m)) { + modulesToSave.push(m); + } + } + } else if (this.hasPendingMultiWell(moduleKey)) { + modulesToSave.push(moduleKey); + } + + if (modulesToSave.length === 0) return false; - const res = await this.saveSingleMultiWellModule(pid, moduleKey); - if (!res) return false; + const savedLabels: string[] = []; + let lastWellsCount = 0; + for (const m of modulesToSave) { + const res = await this.saveSingleMultiWellModule(pid, m); + if (res) { + savedLabels.push(res.curveLabel); + lastWellsCount = res.savedWells; + } + } + if (savedLabels.length === 0) return false; - // Persist the saved module's parameters keyed by the zone selection active at compute time - await this.persistProjectParams([moduleKey]); - const msg = `Saved ${res.curveLabel} (${res.savedWells} wells) ✓`; + // Persist parameters for all saved pipeline modules + await this.persistProjectParams(modulesToSave); + const msg = `Saved ${savedLabels.join(', ')} (${lastWellsCount} wells) ✓`; wellData.lastSavedMessage = msg; appStatus.success(msg); void this.refreshSummary(pid); @@ -1509,6 +1640,103 @@ class ProjectDataState { return Math.min(sMax, Math.max(sMin, Number.isFinite(raw) ? raw : sMin)); } + normalizeScalPlugId(id: string | undefined | null, well?: string, sample?: string): string { + const raw = String(id || '').trim(); + if (raw.includes('::')) return raw; + if (raw.includes(':')) return raw.replace(':', '::'); + if (well && sample) return `${well}::${sample}`; + return raw; + } + + resolveScalSampleZone( + wellName: string | undefined | null, + depth: number | undefined | null, + rawZone?: string | null + ): string { + const rz = String(rawZone || '').trim(); + if (rz && rz !== 'Unknown' && rz !== 'None' && rz !== 'nan') { + return rz; + } + const wn = String(wellName || '').trim(); + const d = Number(depth); + if (!wn || !Number.isFinite(d) || d <= 0) return rz || 'Unknown'; + const wMeta = this.wells.find((w) => w.name === wn) as any; + const stickWell = this.stickPlotResult?.wells?.find((w) => w.well_name === wn); + const tops = + wMeta?.ancillary_data?.formation_tops ?? + stickWell?.tops ?? + (wn === workbench.wellId ? wellData.tops : null); + if (Array.isArray(tops) && tops.length > 0) { + const sorted = [...tops] + .filter((t) => Number.isFinite(Number(t?.depth)) && (t?.name || (t as any)?.zone)) + .sort((a, b) => Number(a.depth) - Number(b.depth)); + let chosen = 'Unknown'; + for (const t of sorted) { + if (d >= Number(t.depth)) { + chosen = String(t.name || (t as any).zone).trim(); + } else { + break; + } + } + if (chosen !== 'Unknown') return chosen; + } + const shf = this.shfResult; + if (shf?.well_names?.length && shf?.depths?.length && shf?.zones?.length) { + let bestDist = 5.0; + let bestZone = ''; + for (let i = 0; i < shf.well_names.length; i++) { + if (shf.well_names[i] !== wn) continue; + const dist = Math.abs((shf.depths[i] ?? 0) - d); + if (dist <= bestDist && shf.zones[i] && shf.zones[i] !== 'Unknown') { + bestDist = dist; + bestZone = shf.zones[i]; + } + } + if (bestZone) return bestZone; + } + return rz || 'Unknown'; + } + + isScalItemInActiveFilter( + wellName: string | undefined | null, + depth?: number | null, + rawZone?: string | null + ): boolean { + const wn = String(wellName || '').trim(); + if (wn) { + if (workbench.scope === 'single-well' && workbench.wellId) { + if (wn !== workbench.wellId) return false; + } else if ( + this.selectedWellNames.length > 0 && + !this.selectedWellNames.includes(wn) + ) { + return false; + } + } + const d = typeof depth === 'number' && Number.isFinite(depth) ? depth : null; + if (workbench.depthFilter && d !== null && d > 0) { + if (d < workbench.depthFilter.min || d > workbench.depthFilter.max) return false; + } + if (workbench.selectedZones.length > 0) { + const zn = this.resolveScalSampleZone(wn, d, rawZone); + if (zn && zn !== 'Unknown') { + if (!workbench.selectedZones.includes(zn)) return false; + } else if (d !== null && d > 0) { + return false; + } + } + return true; + } + + toggleFocusedScalPlug(plugId: string | null) { + if (!plugId) { + this.focusedScalPlugId = null; + return; + } + const norm = this.normalizeScalPlugId(plugId); + this.focusedScalPlugId = this.focusedScalPlugId === norm ? null : norm; + } + recomputeSwirrClientSide(autoFit: boolean = true) { const sr = this.swirrResult; if (!sr || !Array.isArray(sr.plugs) || sr.plugs.length === 0) return; @@ -1516,10 +1744,14 @@ class ProjectDataState { const refPc = Math.max(0.1, Number(cfg.referencePcPsi) || 50.0); const sMin = Number(cfg.swirrMin) || 0.03; const sMax = Math.max(sMin + 0.05, Number(cfg.swirrMax) || 0.85); - const exclSet = new Set(cfg.excludedPlugIds || []); + const exclSet = new Set( + (cfg.excludedPlugIds || []).map((id) => this.normalizeScalPlugId(id)) + ); - // 1. Re-extract Swirr per plug at referencePcPsi + // 1. Re-extract Swirr per plug at referencePcPsi and respect active Well/Zone/Depth filters const updatedPlugs: SwirrPlugRow[] = sr.plugs.map((p) => { + const normPid = this.normalizeScalPlugId(p.plug_id, p.well, p.sample); + const resolvedZone = this.resolveScalSampleZone(p.well, p.depth, p.zone); const pcArr = p.pc_curve || []; const swArr = p.sw_curve || []; const maxPc = pcArr.length > 0 ? Math.max(...pcArr) : p.max_pc; @@ -1547,24 +1779,30 @@ class ProjectDataState { } } } - const isExcl = exclSet.has(p.plug_id); + const isExcl = exclSet.has(normPid) || exclSet.has(p.plug_id); + const inFilter = this.isScalItemInActiveFilter(p.well, p.depth, resolvedZone); return { ...p, + plug_id: normPid, + zone: resolvedZone, max_pc: Number(maxPc.toFixed(2)), sw_at_max_pc: Number(minSw.toFixed(4)), extracted_swirr: Number(extSwirr.toFixed(4)), valid_pc: validPc, excluded: isExcl, - included: validPc && !isExcl + included: validPc && !isExcl && inFilter }; }); - let inclPlugs = updatedPlugs.filter((p) => p.included); + const filterMatchedPlugs = updatedPlugs.filter((p) => + this.isScalItemInActiveFilter(p.well, p.depth, p.zone) + ); + let inclPlugs = filterMatchedPlugs.filter((p) => p.included); if (inclPlugs.length === 0) { - inclPlugs = updatedPlugs.filter((p) => !p.excluded); + inclPlugs = filterMatchedPlugs.filter((p) => !p.excluded); } if (inclPlugs.length === 0) { - inclPlugs = updatedPlugs; + inclPlugs = filterMatchedPlugs.length > 0 ? filterMatchedPlugs : updatedPlugs; } const fitSubset = (subset: SwirrPlugRow[], scopeKey: string): SwirrCorrelationRule => { @@ -1719,6 +1957,51 @@ class ProjectDataState { this.recomputeScalFitsFromPlugs(true); } + /** Ensure multi-well Swirr & SCAL plug dataset is loaded and fitted, avoiding manual roundtrips to Swirr. */ + async ensureSwirrDataLoaded(force: boolean = false): Promise { + const pid = workbench.projectId; + if (!pid) return false; + if (!force && this.swirrResult && Array.isArray(this.swirrResult.plugs) && this.swirrResult.plugs.length > 0) { + return true; + } + try { + const cfg = this.swirrConfig; + const res = await postJson( + `/quick_pp/workspace/projects/${encodeURIComponent(pid)}/swirr/fit`, + { + well_names: this.selectedWellNames.length > 0 ? this.selectedWellNames : null, + zones: workbench.selectedZones.length > 0 ? workbench.selectedZones : null, + min_depth: workbench.depthFilter?.min ?? null, + max_depth: workbench.depthFilter?.max ?? null, + extraction_mode: cfg.extractionMode, + reference_pc_psi: cfg.referencePcPsi, + equation_type: cfg.equationType, + fit_scope: cfg.fitScope, + swirr_min: cfg.swirrMin, + swirr_max: cfg.swirrMax, + excluded_plug_ids: cfg.excludedPlugIds, + rules: cfg.rules, + auto_fit: !this.swirrManualRuleOverride, + drt_method: this.drtMethod, + cutoffs: this.activeDrtCutoffs, + ift: this.shfParams.ift, + theta: this.shfParams.theta + } + ); + if (res && Array.isArray(res.plugs) && res.plugs.length > 0) { + if (!this.swirrManualRuleOverride && res.rules) { + this.swirrConfig.rules = res.rules; + } + this.swirrResult = res; + this.recomputeScalFitsFromPlugs(!this.shfManualFitOverride); + return true; + } + } catch { + // Project might not have SCAL data + } + return false; + } + recomputeScalFitsFromPlugs(resetFits: boolean = true) { const sr = this.swirrResult; if (!sr || !Array.isArray(sr.plugs) || sr.plugs.length === 0) return; @@ -1726,7 +2009,7 @@ class ProjectDataState { const sp = this.shfParams; const activeModel = sp.model ?? 'leverett_j'; const jSatMode = sp.jSatMode ?? 'swn'; - const swirrSource = sp.swirrSource ?? 'poro_perm'; + const swirrSource = sp.swirrSource ?? 'lab_min'; const iftCos = Math.max( 1e-4, (sp.ift || 30.0) * Math.cos(((sp.theta ?? 30.0) * Math.PI) / 180.0) @@ -1737,6 +2020,7 @@ class ProjectDataState { const pcArr: number[] = []; const swArr: number[] = []; const swnArr: number[] = []; + const swirrCalcArr: number[] = []; const jArr: number[] = []; const hArr: number[] = []; const bvwArr: number[] = []; @@ -1745,6 +2029,8 @@ class ProjectDataState { const rfArr: number[] = []; const wellArr: string[] = []; const sampleArr: string[] = []; + const depthArr: number[] = []; + const zoneArr: string[] = []; const pointIdArr: string[] = []; const plugIdArr: string[] = []; const excludedArr: boolean[] = []; @@ -1753,27 +2039,30 @@ class ProjectDataState { (this.swirrConfig.excludedScalPointIds || []).map((id) => String(id).trim()) ); const excludedPlugsSet = new Set( - (this.swirrConfig.excludedPlugIds || []).map((id) => String(id).trim()) + (this.swirrConfig.excludedPlugIds || []).map((id) => this.normalizeScalPlugId(id)) ); for (const p of sr.plugs) { + const pZone = this.resolveScalSampleZone(p.well, p.depth, p.zone); + if (!this.isScalItemInActiveFilter(p.well, p.depth, pZone)) continue; const pCurve = p.pc_curve || []; const sCurve = p.sw_curve || []; const phi = Math.max(1e-4, p.cpore || 0.15); const k = Math.max(1e-4, p.cperm || 10.0); const rf = p.rt ?? 1; - const plugId = p.plug_id || `${p.well}::${p.sample}`; - const isPlugExcluded = Boolean(p.excluded) || excludedPlugsSet.has(plugId); + const plugId = this.normalizeScalPlugId(p.plug_id, p.well, p.sample); + const legacyPlugId = `${p.well}:${p.sample}`; + const isPlugExcluded = + Boolean(p.excluded) || + excludedPlugsSet.has(plugId) || + excludedPlugsSet.has(legacyPlugId); const swMinPlug = sCurve.length > 0 ? Math.min(...sCurve) : (p.sw_at_max_pc ?? 0.12); const swirrNorm = swirrSource === 'poro_perm' - ? this.evalSwirrValue(phi, k, rf) - : Math.min( - swMinPlug * 0.92, - Math.min(0.22, Math.max(0.04, 0.08 * Math.pow(k / phi, -0.12))) - ); + ? Math.min(0.85, Math.max(0.01, this.evalSwirrValue(phi, k, rf))) + : Math.min(0.85, Math.max(0.01, swMinPlug * 0.96)); for (let i = 0; i < Math.min(pCurve.length, sCurve.length); i++) { const pc = Number(pCurve[i]); @@ -1787,13 +2076,18 @@ class ProjectDataState { const h = pc / gradPsi; const bvw = phi * sw; const ptId = `${plugId}::${i}`; + const legacyPtId = `${legacyPlugId}::${i}`; const ptIdByPc = `${plugId}::${Number(pc.toFixed(2))}`; const isPtExcluded = - isPlugExcluded || excludedPtsSet.has(ptId) || excludedPtsSet.has(ptIdByPc); + isPlugExcluded || + excludedPtsSet.has(ptId) || + excludedPtsSet.has(legacyPtId) || + excludedPtsSet.has(ptIdByPc); pcArr.push(Number(pc.toFixed(3))); swArr.push(Number(sw.toFixed(4))); swnArr.push(Number(swn.toFixed(4))); + swirrCalcArr.push(Number(swirrNorm.toFixed(4))); jArr.push(Number(j.toFixed(4))); hArr.push(Number(h.toFixed(2))); bvwArr.push(Number(bvw.toFixed(4))); @@ -1802,6 +2096,8 @@ class ProjectDataState { rfArr.push(rf); wellArr.push(p.well); sampleArr.push(p.sample); + depthArr.push(Number((p.depth ?? 0).toFixed(2))); + zoneArr.push(pZone); pointIdArr.push(ptId); plugIdArr.push(plugId); excludedArr.push(isPtExcluded); @@ -1920,6 +2216,7 @@ class ProjectDataState { pc: pcArr, sw: swArr, swn: swnArr, + swirr_calc: swirrCalcArr, j: jArr, h: hArr, bvw: bvwArr, @@ -1928,6 +2225,8 @@ class ProjectDataState { rock_flags: rfArr, well_names: wellArr, samples: sampleArr, + depths: depthArr, + zones: zoneArr, point_ids: pointIdArr, plug_ids: plugIdArr, excluded: excludedArr @@ -1943,9 +2242,11 @@ class ProjectDataState { } toggleSwirrPlugExclusion(plugId: string) { - const id = String(plugId || '').trim(); + const id = this.normalizeScalPlugId(plugId); if (!id) return; - const cur = new Set(this.swirrConfig.excludedPlugIds || []); + const cur = new Set( + (this.swirrConfig.excludedPlugIds || []).map((x) => this.normalizeScalPlugId(x)) + ); if (cur.has(id)) { cur.delete(id); } else { @@ -1999,11 +2300,15 @@ class ProjectDataState { excludeSelectedScalPlugs() { if (this.selectedScalPointIds.length === 0) return; - const curPlugs = new Set(this.swirrConfig.excludedPlugIds || []); + const curPlugs = new Set( + (this.swirrConfig.excludedPlugIds || []).map((x) => this.normalizeScalPlugId(x)) + ); for (const ptId of this.selectedScalPointIds) { const parts = ptId.split('::'); - if (parts.length >= 2) { + if (parts.length >= 3) { curPlugs.add(`${parts[0]}::${parts[1]}`); + } else if (parts.length === 2) { + curPlugs.add(this.normalizeScalPlugId(parts[0])); } } this.swirrConfig.excludedPlugIds = Array.from(curPlugs); @@ -2013,11 +2318,15 @@ class ProjectDataState { includeSelectedScalPlugs() { if (this.selectedScalPointIds.length === 0) return; - const curPlugs = new Set(this.swirrConfig.excludedPlugIds || []); + const curPlugs = new Set( + (this.swirrConfig.excludedPlugIds || []).map((x) => this.normalizeScalPlugId(x)) + ); for (const ptId of this.selectedScalPointIds) { const parts = ptId.split('::'); - if (parts.length >= 2) { + if (parts.length >= 3) { curPlugs.delete(`${parts[0]}::${parts[1]}`); + } else if (parts.length === 2) { + curPlugs.delete(this.normalizeScalPlugId(parts[0])); } } this.swirrConfig.excludedPlugIds = Array.from(curPlugs); @@ -2134,6 +2443,10 @@ class ProjectDataState { setShfSwirrSource(source: 'lab_min' | 'poro_perm') { this.shfParams.swirrSource = source; + if (this.swirrResult?.plugs?.length) { + this.recomputeScalFitsFromPlugs(true); + return; + } const sr = this.shfResult; if (!sr || !sr.scal_data || !Array.isArray(sr.scal_data.sw)) return; const swArr = sr.scal_data.sw; @@ -2146,7 +2459,7 @@ class ProjectDataState { // Group by plug to compute lab_min per plug or poro_perm per point const plugMinSw = new Map(); for (let i = 0; i < swArr.length; i++) { - const pk = `${wellsArr[i] ?? 'W'}:${samplesArr[i] ?? i}`; + const pk = `${wellsArr[i] ?? 'W'}::${samplesArr[i] ?? i}`; const sv = swArr[i]; if (typeof sv === 'number' && Number.isFinite(sv)) { const prev = plugMinSw.get(pk); @@ -2155,25 +2468,24 @@ class ProjectDataState { } const nextSwn: number[] = []; + const nextSwirrCalc: number[] = []; for (let i = 0; i < swArr.length; i++) { const sw = swArr[i]; const phi = phitArr[i] ?? 0.18; const k = permArr[i] ?? 50.0; const rf = rfArr[i] ?? 1; if (source === 'poro_perm') { - const swirrCalc = this.evalSwirrValue(phi, k, rf); + const swirrCalc = Math.min(0.85, Math.max(0.01, this.evalSwirrValue(phi, k, rf))); const swn = Math.min(0.99, Math.max(0.005, (sw - swirrCalc) / Math.max(0.05, 1.0 - swirrCalc))); nextSwn.push(Number(swn.toFixed(4))); + nextSwirrCalc.push(Number(swirrCalc.toFixed(4))); } else { - const pk = `${wellsArr[i] ?? 'W'}:${samplesArr[i] ?? i}`; + const pk = `${wellsArr[i] ?? 'W'}::${samplesArr[i] ?? i}`; const swMinPlug = plugMinSw.get(pk) ?? 0.1; - const swirrEst = Math.min( - 0.22, - Math.max(0.04, 0.08 * Math.pow(Math.max(k, 0.01) / Math.max(phi, 0.01), -0.12)) - ); - const effSwirr = Math.min(swMinPlug * 0.92, swirrEst); - const swn = Math.min(0.98, Math.max(0.02, (sw - effSwirr) / Math.max(1.0 - effSwirr, 1e-3))); + const effSwirr = Math.min(0.85, Math.max(0.01, swMinPlug * 0.96)); + const swn = Math.min(0.99, Math.max(0.005, (sw - effSwirr) / Math.max(1.0 - effSwirr, 0.05))); nextSwn.push(Number(swn.toFixed(4))); + nextSwirrCalc.push(Number(effSwirr.toFixed(4))); } } @@ -2182,7 +2494,8 @@ class ProjectDataState { fits_swn: undefined, scal_data: { ...sr.scal_data, - swn: nextSwn + swn: nextSwn, + swirr_calc: nextSwirrCalc } }; // Force refit of Swn J-curves diff --git a/quick_pp/app/frontend/src/lib/state/projectParams.svelte.ts b/quick_pp/app/frontend/src/lib/state/projectParams.svelte.ts index c67b9a64..6c535601 100644 --- a/quick_pp/app/frontend/src/lib/state/projectParams.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/projectParams.svelte.ts @@ -3,11 +3,51 @@ * * Parameters are stored in `projects.config_data.module_params[module][zoneKey]`, keyed only by the * shared zone selection (`zone:A+B`, or `all` when no zone is selected). Each entry also records the - * wells it was computed on. Saved parameters are restored on exact zone-key match when the project - * loads or the zone selection changes while in multi-well scope. + * wells it was computed on. Saved parameters are restored on zone match (exact or hierarchical bundle match) + * when the project loads or the zone selection changes while in multi-well scope. */ +import { deleteProjectModuleParams } from '$lib/api/client'; import { workbench } from './workbench.svelte'; +export const MULTI_WELL_REGIONAL_DEFAULTS: Record> = { + 'mw-qaqc': { + normalizeCurves: ['GR', 'NPHI', 'RHOB'], + badholeThreshold: 2.0, + activeCurve: 'GR' + }, + 'rock-typing': { + method: 'rqi_fzi', + numClusters: 3, + cutoffs: [1.0, 3.0] + }, + 'perm-transform': { + modelType: 'power', + defaultA: 1000.0, + defaultB: 3.5 + }, + 'swirr-corr': { + referencePcPsi: 100.0, + method: 'power', + defaultA: 0.15, + defaultB: -0.5 + }, + shf: { + modelType: 'leverett_j', + iftDynesCm: 30.0, + contactAngleDeg: 0.0, + fwlTvdss: 2500.0 + }, + 'rt-classifier': { + classifierType: 'rf', + featureCurves: ['NPHI', 'RHOB', 'GR', 'RT'] + }, + 'ml-training': { + algorithm: 'gbm', + targetCurve: 'PHIT', + featureCurves: ['GR', 'RT', 'NPHI', 'RHOB'] + } +}; + export type ProjectParamModuleKey = | 'mw-qaqc' | 'rock-typing' @@ -64,7 +104,7 @@ export function isProjectParamModuleKey(k: string): k is ProjectParamModuleKey { return (PROJECT_PARAM_MODULE_KEYS as string[]).includes(k); } -function normZones(zones: string[] | null | undefined): string[] { +export function normZones(zones: string[] | null | undefined): string[] { return [...new Set((zones ?? []).map((s) => String(s).trim()).filter(Boolean))].sort(); } @@ -82,6 +122,92 @@ export function zoneSelectionLabel( return z.length === 1 ? `Zone ${z[0]}` : `Zones ${z.join(' + ')}`; } +export type ProjectParamMatchType = 'exact' | 'bundle' | 'fallback_all' | 'superset'; + +export interface ResolvedProjectParamsEntry { + entry: SavedProjectParamsEntry; + matchType: ProjectParamMatchType; + key: string; +} + +/** + * Resolve the best matching saved parameter entry for the active zone selection. + * Priority: + * 1. Exact zone match (e.g. `zone:A+B` or `zone:A` or `all`) + * 2. Bundle match (the selected zones are a subset of a saved bundle, e.g. selecting `Zone A` + * matches a saved `Zones A + B` bundle; most specific bundle wins) + * 3. Overlap / superset match (e.g. selecting `Zones A + B` matches a saved `Zone A`) + * 4. Project-wide fallback (`all`) + */ +export function resolveMatchingProjectParamsEntry( + modMap: Record | undefined, + selectedZones: string[] | null | undefined = workbench.selectedZones +): ResolvedProjectParamsEntry | null { + if (!modMap || Object.keys(modMap).length === 0) return null; + + const curKey = zoneSelectionKey(selectedZones); + // 1. Exact match + if (modMap[curKey]) { + return { entry: modMap[curKey], matchType: 'exact', key: curKey }; + } + + const normSelected = normZones(selectedZones); + + // 2. If specific zones selected, check if they are contained in a saved bundle + if (normSelected.length > 0) { + const candidateBundles: Array<{ key: string; entry: SavedProjectParamsEntry; zoneCount: number }> = []; + + for (const [k, e] of Object.entries(modMap)) { + const entryZones = normZones(e.selection?.zones); + if (entryZones.length === 0) continue; + const entryZoneSet = new Set(entryZones); + const containsAll = normSelected.every((z) => entryZoneSet.has(z)); + if (containsAll) { + candidateBundles.push({ key: k, entry: e, zoneCount: entryZones.length }); + } + } + + if (candidateBundles.length > 0) { + // Smallest bundle size first (most specific), then latest saved_at + candidateBundles.sort((a, b) => { + if (a.zoneCount !== b.zoneCount) return a.zoneCount - b.zoneCount; + const tA = new Date(a.entry.saved_at || 0).getTime(); + const tB = new Date(b.entry.saved_at || 0).getTime(); + return tB - tA; + }); + return { entry: candidateBundles[0].entry, matchType: 'bundle', key: candidateBundles[0].key }; + } + + // 3. Check for overlap/superset match + const candidateSubsets: Array<{ key: string; entry: SavedProjectParamsEntry; overlap: number }> = []; + for (const [k, e] of Object.entries(modMap)) { + const entryZones = normZones(e.selection?.zones); + if (entryZones.length === 0) continue; + const selSet = new Set(normSelected); + const overlapCount = entryZones.filter((z) => selSet.has(z)).length; + if (overlapCount > 0) { + candidateSubsets.push({ key: k, entry: e, overlap: overlapCount }); + } + } + if (candidateSubsets.length > 0) { + candidateSubsets.sort((a, b) => { + if (b.overlap !== a.overlap) return b.overlap - a.overlap; + const tA = new Date(a.entry.saved_at || 0).getTime(); + const tB = new Date(b.entry.saved_at || 0).getTime(); + return tB - tA; + }); + return { entry: candidateSubsets[0].entry, matchType: 'superset', key: candidateSubsets[0].key }; + } + } + + // 4. Fallback to 'all' + if (modMap['all']) { + return { entry: modMap['all'], matchType: 'fallback_all', key: 'all' }; + } + + return null; +} + export interface ProjectParamsStatus { key: string; label: string; @@ -89,6 +215,7 @@ export interface ProjectParamsStatus { savedLabel: string | null; savedAt: string | null; wellCount: number | null; + matchType?: ProjectParamMatchType | null; } class ProjectParamsState { @@ -101,19 +228,29 @@ class ProjectParamsState { module: ProjectParamModuleKey, key: string, label: string, - entry: SavedProjectParamsEntry | null + entry: SavedProjectParamsEntry | null, + matchType: ProjectParamMatchType = 'exact' ) { + let displayLabel = entry?.label ?? null; + if (entry && matchType === 'bundle') { + displayLabel = `${entry.label || 'Saved bundle'} (bundle)`; + } else if (entry && matchType === 'fallback_all' && key !== 'all') { + displayLabel = `${entry.label || 'All zones'} (default)`; + } else if (entry && matchType === 'superset') { + displayLabel = `${entry.label || 'Saved zone'}`; + } this.status = { ...this.status, [module]: { key, label, matched: Boolean(entry), - savedLabel: entry?.label ?? null, + savedLabel: displayLabel, savedAt: entry?.saved_at || null, wellCount: Array.isArray(entry?.selection?.well_names) ? entry!.selection.well_names!.length - : null + : null, + matchType: entry ? matchType : null } }; } @@ -128,7 +265,88 @@ class ProjectParamsState { this.status = {}; this.savedCounts = {}; } + + getRegionalDefaults(module: ProjectParamModuleKey): Record { + return JSON.parse(JSON.stringify(MULTI_WELL_REGIONAL_DEFAULTS[module] ?? {})); + } + + /** Delete a specific saved preset for a multi-well module & zone key. */ + async deleteSavedEntry( + projectId: string | number, + module: ProjectParamModuleKey, + key: string, + modMap?: ProjectModuleParamsMap + ): Promise { + try { + await deleteProjectModuleParams(projectId, module, key); + if (modMap?.[module]?.[key]) { + delete modMap[module]![key]; + } + this.setCounts(modMap ?? {}); + this.loadGeneration++; + return true; + } catch { + return false; + } + } + + /** Delete all saved presets for a multi-well module across all zones. */ + async deleteAllForModule( + projectId: string | number, + module: ProjectParamModuleKey, + modMap?: ProjectModuleParamsMap + ): Promise { + try { + await deleteProjectModuleParams(projectId, module); + if (modMap?.[module]) { + delete modMap[module]; + } + this.setCounts(modMap ?? {}); + this.loadGeneration++; + return true; + } catch { + return false; + } + } + + /** Extract a flat list of all saved multi-well entries for table presentation. */ + getAllSavedEntries(modMap?: ProjectModuleParamsMap): Array<{ + module: ProjectParamModuleKey; + moduleLabel: string; + key: string; + label: string; + saved_at: string; + wellCount: number | null; + params: Record; + }> { + const mp = modMap || {}; + const rows: Array<{ + module: ProjectParamModuleKey; + moduleLabel: string; + key: string; + label: string; + saved_at: string; + wellCount: number | null; + params: Record; + }> = []; + for (const m of PROJECT_PARAM_MODULE_KEYS) { + const mEntries = mp[m] || {}; + for (const [k, e] of Object.entries(mEntries)) { + rows.push({ + module: m, + moduleLabel: PROJECT_PARAM_MODULE_LABELS[m], + key: k, + label: e.label || k, + saved_at: e.saved_at || '', + wellCount: Array.isArray(e.selection?.well_names) + ? e.selection.well_names.length + : null, + params: e.params || {} + }); + } + } + return rows; + } } export const projectParams = new ProjectParamsState(); - diff --git a/quick_pp/app/frontend/src/lib/state/wellData.svelte.ts b/quick_pp/app/frontend/src/lib/state/wellData.svelte.ts index 45d36439..a0e8bc6d 100644 --- a/quick_pp/app/frontend/src/lib/state/wellData.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/wellData.svelte.ts @@ -1140,6 +1140,18 @@ class WellDataState { committedCopy['DEPT'] = [...numericCurves['DEPTH']]; } + // If a curve ends with '.' (common in LAS files with blank units, e.g. POTA.), also map the clean name + for (const col of Object.keys(numericCurves)) { + if (col.endsWith('.')) { + const clean = col.replace(/\.+$/, '').trim(); + if (clean && !numericCurves[clean]) { + numericCurves[clean] = numericCurves[col]; + origCopy[clean] = origCopy[col]; + committedCopy[clean] = committedCopy[col]; + } + } + } + // Detect carbonate / multi-mineral curves on well load and clean up legacy duplicate VSAND==VCALC / VSILT==VDOLO const hasPositiveCurve = (col: string) => Array.isArray(numericCurves[col]) && @@ -3873,12 +3885,21 @@ class WellDataState { 'PAY', 'BADHOLE' ]) { - if (this.curves[k]) { - const v = this.curves[k][lo]; + const arr = this.curves[k] ?? this.curves[k + "."]; + if (arr) { + const v = arr[lo]; out[k] = Number.isFinite(v) ? v : null; } } + // Normalize POTA to percent (%) if stored as decimal fraction (max <= 0.5 v/v) + if (out['POTA'] !== null && out['POTA'] !== undefined) { + const potaArr = this.curves['POTA'] ?? this.curves['POTA.']; + if (potaArr && !potaArr.some((v) => Number.isFinite(v) && v > 0.5)) { + out['POTA'] = out['POTA'] * 100.0; + } + } + // Fallback to discrete core plug samples when nearest continuous log curve cell is empty if (this.coreSamples.length > 0 && depth !== null) { let bestSample: any = null; diff --git a/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts b/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts index b70524cf..13e37eb0 100644 --- a/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts +++ b/quick_pp/app/frontend/src/lib/state/workbench.svelte.ts @@ -139,6 +139,7 @@ export interface ThomasStieberParams { } export interface SaturationModelParams { + fwl: number; method: 'waxman_smits' | 'archie'; rwMode: 'salinity_temp' | 'pickett'; measSystem: 'metric' | 'imperial'; @@ -548,6 +549,15 @@ class WorkbenchState { // Right pane view mode rightPaneTab = $state<'crossplot' | 'grid' | 'split'>('split'); + /** Sub-tab within the right bottom data dock: 'data' (Step Data / QC) or 'params' (Parameters & Fits). */ + rightGridSubTab = $state<'data' | 'params'>('data'); + + openParametersTab() { + this.rightGridSubTab = 'params'; + if (this.rightPaneTab === 'crossplot') { + this.rightPaneTab = 'split'; + } + } crossplotTemplateOverride = $state< | 'auto' | 'nphi-rhob' @@ -590,6 +600,7 @@ class WorkbenchState { stickPlotShowContacts = $state(true); stickPlotShowTops = $state(true); stickPlotShowPerfs = $state(true); + logShowContacts = $state(true); // Global modal / drawer flags importerOpen = $state(false); @@ -729,7 +740,8 @@ class WorkbenchState { bMethod: 'waxman_smits', qvMethod: 'cec', cecClay: 0.1, - rhoDryClay: 2.71 + rhoDryClay: 2.71, + fwl: 0 }); scalParams = $state({ diff --git a/quick_pp/app/frontend/src/routes/+page.svelte b/quick_pp/app/frontend/src/routes/+page.svelte index 01803992..c01ba9cb 100644 --- a/quick_pp/app/frontend/src/routes/+page.svelte +++ b/quick_pp/app/frontend/src/routes/+page.svelte @@ -10,6 +10,7 @@ import CompactDataGrid, { type GridColumn } from '$lib/components/workbench/CompactDataGrid.svelte'; + import ParametersManagementPanel from '$lib/components/workbench/ParametersManagementPanel.svelte'; import ReportPreviewCanvas from '$lib/components/workbench/ReportPreviewCanvas.svelte'; import InspectorQaqc from '$lib/components/modules/InspectorQaqc.svelte'; @@ -79,6 +80,25 @@ untrack(() => projectData.applyProjectParamsForCurrentSelection()); }); + // Keep SCAL Swirr & Leverett-J / Pc curve fits synchronized with active Well, Zone, and Depth filters + let lastScalFilterKey = ''; + $effect(() => { + const wellsKey = Array.from(projectData.selectedWellNames).sort().join(','); + const zonesKey = zoneSelectionKey(workbench.selectedZones); + const depthKey = workbench.depthFilter + ? `${workbench.depthFilter.min}:${workbench.depthFilter.max}` + : 'all'; + const scopeKey = `${workbench.scope}:${workbench.wellId ?? ''}`; + const hasSwirr = Boolean(projectData.swirrResult?.plugs?.length); + if (!hasSwirr) return; + const nextKey = `${scopeKey}|${wellsKey}|${zonesKey}|${depthKey}`; + if (nextKey === lastScalFilterKey) return; + lastScalFilterKey = nextKey; + untrack(() => { + projectData.recomputeSwirrClientSide(!projectData.swirrManualRuleOverride); + }); + }); + let initialized = $state(false); let isReportMode = $derived( (workbench.scope === 'single-well' && workbench.singleWellStep === 'report') || @@ -87,10 +107,14 @@ let isFptMode = $derived(workbench.scope === 'multi-well' && workbench.multiWellStep === 'fpt'); let isVolumetricsMode = $derived(workbench.scope === 'multi-well' && workbench.multiWellStep === 'volumetrics'); - /** FPT grid: clicking a row selects the point on the Pressure vs TVDSS plots. */ + /** FPT grid: clicking a row selects the point on the Pressure vs TVDSS plots. SCAL grids: clicking a row spotlights that sample plug. */ function handleRightGridRowClick(row: Record) { if (isFptMode && typeof row?._id === 'number') { fpt.selectedIds = [row._id]; + return; + } + if (typeof row?._scalPlugId === 'string' && row._scalPlugId) { + projectData.toggleFocusedScalPlug(row._scalPlugId); } } @@ -144,6 +168,12 @@ replaceState(url, {}); }); + let totalSavedParamsCount = $derived( + workbench.scope === 'multi-well' + ? Object.values(projectParams.savedCounts).reduce((a, b) => a + (b ?? 0), 0) + : Object.values(moduleParams.savedCounts).reduce((a, b) => a + (b ?? 0), 0) + ); + // Dynamic columns & rows for the Bottom-Right Quick Zonal / Context Data Grid let rightGridConfig = $derived.by( (): { title: string; columns: GridColumn[]; rows: Record[] } => { @@ -376,32 +406,78 @@ projectData.shfResult?.scal_data?.pc?.length ) { const scal = projectData.shfResult.scal_data; + const plugMeta = new Map(); + for (const p of projectData.swirrResult?.plugs ?? []) { + const pid = projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample); + plugMeta.set(pid, { + depth: p.depth ?? 0, + zone: projectData.resolveScalSampleZone(p.well, p.depth, p.zone) + }); + } + const focusedPid = projectData.focusedScalPlugId + ? projectData.normalizeScalPlugId(projectData.focusedScalPlugId) + : null; + const rtFilter = projectData.scalFilterRockType; + const rows = scal.pc + .map((pcVal, i) => { + const wn = scal.well_names?.[i] ?? ''; + const sname = scal.samples?.[i] ?? ''; + const pid = projectData.normalizeScalPlugId(scal.plug_ids?.[i], wn, sname); + const meta = plugMeta.get(pid); + const d = scal.depths?.[i] ?? meta?.depth ?? null; + const zn = projectData.resolveScalSampleZone( + wn, + d, + scal.zones?.[i] ?? meta?.zone + ); + const rf = scal.rock_flags?.[i] ?? null; + const isExcl = Boolean(scal.excluded?.[i]); + const isFocused = focusedPid === pid; + return { + _scalPlugId: pid, + Well: wn, + Sample: isFocused ? `★ ${sname}` : sname, + ZONE: zn, + DEPTH: d, + ROCK_FLAG: rf, + PC: pcVal, + SW: scal.sw?.[i] ?? null, + SWIRR_CALC: scal.swirr_calc?.[i] ?? null, + SWN: scal.swn?.[i] ?? null, + J: scal.j?.[i] ?? null, + H: scal.h?.[i] ?? null, + PHIT: scal.phit?.[i] ?? null, + PERM: scal.perm?.[i] ?? null, + STATUS: isExcl ? 'Excluded' : isFocused ? 'Focused' : 'Active' + }; + }) + .filter((r) => { + if (!projectData.isScalItemInActiveFilter(r.Well, r.DEPTH, r.ZONE)) return false; + if (rtFilter !== 'all' && r.ROCK_FLAG !== rtFilter) return false; + if (focusedPid && r._scalPlugId !== focusedPid) return false; + return true; + }); return { - title: 'Multi-Well SCAL Capillary Pressure & Leverett-J Table', + title: focusedPid + ? `SCAL Capillary Pressure & Leverett-J — Sample ${focusedPid.replace('::', ' · ')} (click row to clear focus)` + : 'Multi-Well SCAL Capillary Pressure & Leverett-J Table (click row to spotlight sample)', columns: [ { key: 'Well', label: 'Well' }, { key: 'Sample', label: 'Sample' }, + { key: 'ZONE', label: 'Zone' }, + { key: 'DEPTH', label: 'Depth', unit: 'm', numeric: true, decimals: 1 }, { key: 'ROCK_FLAG', label: 'RT', numeric: true, decimals: 0 }, { key: 'PC', label: 'Pc', unit: 'psi', numeric: true, decimals: 2 }, { key: 'SW', label: 'Sw', unit: 'v/v', numeric: true, decimals: 3 }, + { key: 'SWIRR_CALC', label: 'Swirr', unit: 'v/v', numeric: true, decimals: 3 }, { key: 'SWN', label: 'Swn', unit: 'v/v', numeric: true, decimals: 3 }, { key: 'J', label: 'J', numeric: true, decimals: 3 }, { key: 'H', label: 'Height', unit: 'm', numeric: true, decimals: 1 }, { key: 'PHIT', label: 'CPORE', unit: 'v/v', numeric: true, decimals: 3 }, - { key: 'PERM', label: 'CPERM', unit: 'mD', numeric: true, decimals: 2 } + { key: 'PERM', label: 'CPERM', unit: 'mD', numeric: true, decimals: 2 }, + { key: 'STATUS', label: 'Status' } ], - rows: scal.pc.map((pcVal, i) => ({ - Well: scal.well_names?.[i] ?? '', - Sample: scal.samples?.[i] ?? '', - ROCK_FLAG: scal.rock_flags?.[i] ?? null, - PC: pcVal, - SW: scal.sw?.[i] ?? null, - SWN: scal.swn?.[i] ?? null, - J: scal.j?.[i] ?? null, - H: scal.h?.[i] ?? null, - PHIT: scal.phit?.[i] ?? null, - PERM: scal.perm?.[i] ?? null - })) + rows }; } @@ -410,12 +486,22 @@ workbench.multiWellStep === 'swirr-corr' && projectData.swirrResult?.plugs?.length ) { - const plugs = projectData.swirrResult.plugs; + const focusedPid = projectData.focusedScalPlugId + ? projectData.normalizeScalPlugId(projectData.focusedScalPlugId) + : null; + const rtFilter = projectData.scalFilterRockType; + const plugs = projectData.swirrResult.plugs.filter((p) => { + const zn = projectData.resolveScalSampleZone(p.well, p.depth, p.zone); + if (!projectData.isScalItemInActiveFilter(p.well, p.depth, zn)) return false; + if (rtFilter !== 'all' && (p.rt ?? 1) !== rtFilter) return false; + return true; + }); return { - title: `Multi-Well SCAL Plug Swirr Summary (Ref Pc=${projectData.swirrConfig.referencePcPsi} psi | n=${plugs.filter((p) => p.included).length}/${plugs.length} included)`, + title: `Multi-Well SCAL Plug Swirr Summary (Ref Pc=${projectData.swirrConfig.referencePcPsi} psi | n=${plugs.filter((p) => p.included).length}/${plugs.length} in filter — click row to spotlight)`, columns: [ { key: 'WELL', label: 'Well' }, { key: 'SAMPLE', label: 'Sample' }, + { key: 'ZONE', label: 'Zone' }, { key: 'DEPTH', label: 'Depth', unit: 'm', numeric: true, decimals: 1 }, { key: 'RT', label: 'RT', numeric: true, decimals: 0 }, { key: 'CPORE', label: 'CPORE', unit: 'v/v', numeric: true, decimals: 3 }, @@ -428,21 +514,33 @@ { key: 'RESIDUAL', label: 'Residual', unit: 'v/v', numeric: true, decimals: 3 }, { key: 'STATUS', label: 'Status' } ], - rows: plugs.map((p) => ({ - WELL: p.well, - SAMPLE: p.sample, - DEPTH: p.depth, - RT: p.rt, - CPORE: p.cpore, - CPERM: p.cperm, - RQI: p.rqi, - MAX_PC: p.max_pc, - SW_AT_MAX_PC: p.sw_at_max_pc, - EXTRACTED_SWIRR: p.extracted_swirr, - MODELED_SWIRR: p.modeled_swirr, - RESIDUAL: p.residual, - STATUS: p.included ? 'Included' : !p.valid_pc ? 'Low Pc (< Ref)' : 'Excluded' - })) + rows: plugs.map((p) => { + const pid = projectData.normalizeScalPlugId(p.plug_id, p.well, p.sample); + const isFocused = focusedPid === pid; + return { + _scalPlugId: pid, + WELL: p.well, + SAMPLE: isFocused ? `★ ${p.sample}` : p.sample, + ZONE: projectData.resolveScalSampleZone(p.well, p.depth, p.zone), + DEPTH: p.depth, + RT: p.rt, + CPORE: p.cpore, + CPERM: p.cperm, + RQI: p.rqi, + MAX_PC: p.max_pc, + SW_AT_MAX_PC: p.sw_at_max_pc, + EXTRACTED_SWIRR: p.extracted_swirr, + MODELED_SWIRR: p.modeled_swirr, + RESIDUAL: p.residual, + STATUS: isFocused + ? 'Focused' + : p.included + ? 'Included' + : !p.valid_pc + ? 'Low Pc (< Ref)' + : 'Excluded' + }; + }) }; } @@ -879,16 +977,49 @@ {:else if workbench.rightPaneTab === 'grid'}
- {rightGridConfig.title} +
+ + +
- + {#if workbench.rightGridSubTab === 'data'} + + {:else} + + {/if}
{:else} @@ -912,21 +1043,49 @@
- {rightGridConfig.title} - - {rightGridConfig.rows.length} rows - +
+ + +
- + {#if workbench.rightGridSubTab === 'data'} + + {:else} + + {/if}
diff --git a/quick_pp/database/db_connector.py b/quick_pp/database/db_connector.py index 21ffe161..fbf5ea77 100644 --- a/quick_pp/database/db_connector.py +++ b/quick_pp/database/db_connector.py @@ -167,6 +167,7 @@ def setup_db(self): self._migrate_uam_columns() self._migrate_project_columns() self._migrate_pressure_test_columns() + self._migrate_fluid_contact_columns() except Exception as exc: if "readonly" in str(exc).lower() or os.environ.get("QPP_DEMO_MODE", "").lower() in ("1", "true", "yes"): pass @@ -228,6 +229,37 @@ def _migrate_project_columns(self): except Exception: pass + def _migrate_fluid_contact_columns(self): + """Ensure existing `fluid_contacts` tables have per-zone, fault block, + contact type, source lineage, and remarks columns.""" + if DBConnector._engine is None: + return + from sqlalchemy import inspect as sa_inspect + from sqlalchemy import text + + try: + inspector = sa_inspect(DBConnector._engine) + if not inspector.has_table("fluid_contacts"): + return + existing_cols = {c["name"] for c in inspector.get_columns("fluid_contacts")} + migrations = [ + ("zone", "VARCHAR(255)"), + ("contact_type", "VARCHAR(50)"), + ("fault_block", "VARCHAR(100)"), + ("source", "VARCHAR(100)"), + ("remarks", "TEXT"), + ] + with DBConnector._engine.begin() as conn: + for col_name, col_def in migrations: + if col_name not in existing_cols: + conn.execute( + text( + f'ALTER TABLE "fluid_contacts" ADD COLUMN "{col_name}" {col_def}' + ) + ) + except Exception: + pass + def _migrate_pressure_test_columns(self): """Ensure existing `pressure_tests` tables have formation pressure testing columns (mobility, permeability, temperature, temperature_uom, remarks) and FPT interpretation diff --git a/quick_pp/database/models.py b/quick_pp/database/models.py index 71bb2bd3..3f7e42ba 100644 --- a/quick_pp/database/models.py +++ b/quick_pp/database/models.py @@ -220,6 +220,11 @@ class FluidContact(Base): ) name: Mapped[str] = mapped_column(String(100), nullable=False) depth: Mapped[float] = mapped_column(REAL, nullable=False) + zone: Mapped[Optional[str]] = mapped_column(String(255), nullable=True) + contact_type: Mapped[Optional[str]] = mapped_column(String(50), nullable=True) + fault_block: Mapped[Optional[str]] = mapped_column(String(100), nullable=True) + source: Mapped[Optional[str]] = mapped_column(String(100), nullable=True) + remarks: Mapped[Optional[str]] = mapped_column(Text, nullable=True) well: Mapped["Well"] = relationship("Well", back_populates="fluid_contacts") @@ -228,7 +233,10 @@ class FluidContact(Base): ) def __repr__(self): - return f"" + return ( + f"" + ) class PressureTest(Base): diff --git a/quick_pp/database/objects.py b/quick_pp/database/objects.py index bdde0672..0c05f82f 100644 --- a/quick_pp/database/objects.py +++ b/quick_pp/database/objects.py @@ -100,7 +100,9 @@ def normalize_fpt_test_quality(value: Any) -> str: """Normalize a pre-test quality flag to VALID / TIGHT / SUPERCHARGED / LOST_SEAL / EXCLUDED.""" if value is None: return "VALID" - key = re.sub(r"[^A-Z_]", "", str(value).strip().upper().replace(" ", "_").replace("-", "_")) + key = re.sub( + r"[^A-Z_]", "", str(value).strip().upper().replace(" ", "_").replace("-", "_") + ) if not key: return "VALID" return _FPT_QUALITY_ALIASES.get(key, "VALID") @@ -1325,14 +1327,17 @@ def _flush_copy_buffer(buf: io.StringIO) -> None: dtype=np.float64 ) valid_mask = ( - np.isfinite(depths_arr) & unique_f32_mask & np.isfinite(vals_arr) + np.isfinite(depths_arr) + & unique_f32_mask + & np.isfinite(vals_arr) ) if not valid_mask.any(): continue v_depths = depths_arr[valid_mask].tolist() v_vals = vals_arr[valid_mask].tolist() sio.writelines( - f"{cid}\t{d}\t{v}\t\\N\n" for d, v in zip(v_depths, v_vals, strict=False) + f"{cid}\t{d}\t{v}\t\\N\n" + for d, v in zip(v_depths, v_vals, strict=False) ) buffered_rows += len(v_depths) else: @@ -1344,7 +1349,9 @@ def _flush_copy_buffer(buf: io.StringIO) -> None: if not valid_mask.any(): continue v_depths = depths_arr[valid_mask].tolist() - v_vals = values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + v_vals = ( + values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + ) for d, v in zip(v_depths, v_vals, strict=False): safe_v = ( v.replace("\\", "\\\\") @@ -1377,7 +1384,9 @@ def _flush_copy_buffer(buf: io.StringIO) -> None: dtype=np.float64 ) valid_mask = ( - np.isfinite(depths_arr) & unique_f32_mask & np.isfinite(vals_arr) + np.isfinite(depths_arr) + & unique_f32_mask + & np.isfinite(vals_arr) ) if not valid_mask.any(): continue @@ -1401,7 +1410,9 @@ def _flush_copy_buffer(buf: io.StringIO) -> None: if not valid_mask.any(): continue v_depths = depths_arr[valid_mask].tolist() - v_vals = values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + v_vals = ( + values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + ) batch_buffer.extend( { "curve_id": cid, @@ -1489,7 +1500,10 @@ def update_data(self, data: pd.DataFrame): new_curves.append(mnemonic) else: orm_curve.data_type = data_type - if str(mnemonic).upper() in sparse_core_mnemonics and len(data.index) > 0: + if ( + str(mnemonic).upper() in sparse_core_mnemonics + and len(data.index) > 0 + ): valid_idx = [float(d) for d in data.index if pd.notna(d)] if valid_idx: full_span_delete_curves[orm_curve.curve_id] = ( @@ -1565,14 +1579,17 @@ def update_data(self, data: pd.DataFrame): dtype=np.float64 ) valid_mask = ( - np.isfinite(depths_arr) & unique_f32_mask & np.isfinite(vals_arr) + np.isfinite(depths_arr) + & unique_f32_mask + & np.isfinite(vals_arr) ) if not valid_mask.any(): continue v_depths = depths_arr[valid_mask].tolist() v_vals = vals_arr[valid_mask].tolist() sio.writelines( - f"{cid}\t{d}\t{v}\t\\N\n" for d, v in zip(v_depths, v_vals, strict=False) + f"{cid}\t{d}\t{v}\t\\N\n" + for d, v in zip(v_depths, v_vals, strict=False) ) total_inserted += len(v_depths) else: @@ -1584,7 +1601,9 @@ def update_data(self, data: pd.DataFrame): if not valid_mask.any(): continue v_depths = depths_arr[valid_mask].tolist() - v_vals = values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + v_vals = ( + values.iloc[np.flatnonzero(valid_mask)].astype(str).tolist() + ) for d, v in zip(v_depths, v_vals, strict=False): safe_v = ( v.replace("\\", "\\\\") @@ -1742,9 +1761,7 @@ def add_curve_data( if mappings: self.db_session.bulk_insert_mappings(ORMCurveData, mappings) self.db_session.flush() - logger.debug( - f"Added {len(mappings)} data points for curve '{mnemonic}'" - ) + logger.debug(f"Added {len(mappings)} data points for curve '{mnemonic}'") return if is_numeric: @@ -2130,9 +2147,7 @@ def export_handover_package( def __str__(self): return f"Well: {self.name} (ID: {self.well_id}, UWI: {self.uwi})" - def add_formation_tops( - self, tops: List[Dict[str, Any]], replace: bool = False - ): + def add_formation_tops(self, tops: List[Dict[str, Any]], replace: bool = False): """Adds or updates a list of formation tops for the well. Args: @@ -2193,72 +2208,200 @@ def get_formation_tops(self) -> pd.DataFrame: logger.debug(f"Retrieved {len(df)} formation tops for well '{self.name}'.") return df + def delete_fluid_contacts(self, zones: Optional[List[str]] = None) -> int: + """Deletes fluid contacts for the well, optionally filtered by zone(s). + + Args: + zones (Optional[List[str]]): If provided, only deletes contacts belonging to these zones. + If None, deletes all fluid contacts for this well. + + Returns: + int: Number of deleted contact records. + """ + query = self.db_session.query(ORMFluidContact).filter_by(well_id=self.well_id) + if zones is not None: + clean_z = [str(z).strip() for z in zones if str(z).strip()] + if clean_z: + query = query.filter(ORMFluidContact.zone.in_(clean_z)) + deleted_count = query.delete(synchronize_session="fetch") + self.db_session.flush() + try: + self.db_session.expire(self._orm_well, ["fluid_contacts"]) + except Exception: + pass + logger.info( + f"Deleted {deleted_count} fluid contacts for well '{self.name}' (zones={zones})." + ) + return deleted_count + def add_fluid_contacts( - self, contacts: List[Dict[str, Any]], replace: bool = False + self, + contacts: List[Dict[str, Any]], + replace: bool = False, + clean_zones: Optional[List[str]] = None, ): """Adds or updates a list of fluid contacts for the well. + Provides a clean slate for the zones under purview to maintain consistency. + Args: contacts (List[Dict[str, Any]]): A list of dictionaries, where each - dictionary represents a contact and must - contain 'name' (or 'contact_type') and 'depth'. + dictionary represents a contact and contains + 'name' or 'contact_type', and 'depth'. + Optional keys: 'zone', 'fault_block', 'source', 'remarks'. replace (bool): If True, deletes all existing fluid contacts for this well before inserting. + clean_zones (Optional[List[str]]): Specific zones under purview to clear before inserting. + If None and replace=False, defaults to cleaning all zones + present in the incoming contacts payload. """ logger.info( - f"Adding/updating {len(contacts)} fluid contacts for well '{self.name}' (replace={replace})." + f"Adding/updating {len(contacts)} fluid contacts for well '{self.name}' (replace={replace}, clean_zones={clean_zones})." ) - dedup_contacts: Dict[str, float] = {} + parsed_contacts: Dict[str, Dict[str, Any]] = {} for contact_data in contacts or []: if not isinstance(contact_data, dict): continue - c_name = str( - contact_data.get("name") or contact_data.get("contact_type") or "" - ).strip() + raw_c_type = ( + str(contact_data.get("contact_type") or contact_data.get("type") or "") + .strip() + .upper() + ) + raw_name = str(contact_data.get("name") or "").strip() + zone = str(contact_data.get("zone") or "").strip() or None + fb = ( + str( + contact_data.get("fault_block") + or contact_data.get("faultblock") + or contact_data.get("block") + or "" + ).strip() + or None + ) + source = str(contact_data.get("source") or "").strip() or None + remarks = ( + str( + contact_data.get("remarks") + or contact_data.get("remark") + or contact_data.get("comment") + or "" + ).strip() + or None + ) + + c_type = raw_c_type + if not c_type and raw_name: + parts = [ + p.strip().upper() for p in raw_name.replace(":", " - ").split(" - ") + ] + c_type = parts[-1] if parts else raw_name.upper() + + c_name = raw_name + if not c_name: + c_name = ( + f"{zone} - {c_type}" if (zone and c_type) else (c_type or "CONTACT") + ) + elif zone and zone.lower() not in c_name.lower() and c_type: + c_name = f"{zone} - {c_type}" + d_raw = contact_data.get("depth") - if not c_name or d_raw is None: - logger.warning(f"Skipping invalid fluid contact data: {contact_data}") + if d_raw is None or not c_name: continue try: d_val = float(d_raw) except (TypeError, ValueError): continue - dedup_contacts[c_name] = d_val - if not dedup_contacts and not replace: - return + parsed_contacts[c_name] = { + "name": c_name, + "depth": d_val, + "zone": zone, + "contact_type": c_type or c_name, + "fault_block": fb, + "source": source, + "remarks": remarks, + } + # 1. Clean slate execution: if replace: - self.db_session.query(ORMFluidContact).filter_by( - well_id=self.well_id - ).delete(synchronize_session="fetch") + self.delete_fluid_contacts() + elif clean_zones is not None: + self.delete_fluid_contacts(zones=clean_zones) else: - self.db_session.query(ORMFluidContact).filter( - ORMFluidContact.well_id == self.well_id, - ORMFluidContact.name.in_(list(dedup_contacts.keys())), - ).delete(synchronize_session="fetch") - self.db_session.flush() + # Default clean-slate behavior: wipe existing contacts for the zones present in this payload + zones_to_clean = { + item["zone"] for item in parsed_contacts.values() if item.get("zone") + } + if zones_to_clean: + self.delete_fluid_contacts(zones=list(zones_to_clean)) + # Also clean matching names for any unassigned general contacts (zone is None) + unassigned_names = [ + name for name, item in parsed_contacts.items() if not item.get("zone") + ] + if unassigned_names: + self.db_session.query(ORMFluidContact).filter( + ORMFluidContact.well_id == self.well_id, + ORMFluidContact.name.in_(unassigned_names), + ).delete(synchronize_session="fetch") + self.db_session.flush() + + if not parsed_contacts: + return - for c_name, d_val in dedup_contacts.items(): + for c_name, item in parsed_contacts.items(): orm_contact = ORMFluidContact( - well_id=self.well_id, name=c_name, depth=d_val + well_id=self.well_id, + name=c_name, + depth=item["depth"], + zone=item["zone"], + contact_type=item["contact_type"], + fault_block=item["fault_block"], + source=item["source"], + remarks=item["remarks"], ) self.db_session.add(orm_contact) self.db_session.flush() + try: + self.db_session.expire(self._orm_well, ["fluid_contacts"]) + except Exception: + pass def get_fluid_contacts(self) -> pd.DataFrame: """Retrieves all fluid contacts for the well as a DataFrame.""" + try: + self.db_session.expire(self._orm_well, ["fluid_contacts"]) + except Exception: + pass contacts = self._orm_well.fluid_contacts + cols = [ + "name", + "depth", + "zone", + "contact_type", + "fault_block", + "source", + "remarks", + ] if not contacts: - return pd.DataFrame(columns=["name", "depth"]) + return pd.DataFrame(columns=cols) - data = [{"name": c.name, "depth": c.depth} for c in contacts] + data = [ + { + "contact_id": c.contact_id, + "name": c.name, + "depth": c.depth, + "zone": c.zone, + "contact_type": c.contact_type or c.name, + "fault_block": c.fault_block, + "source": c.source, + "remarks": c.remarks, + } + for c in contacts + ] df = pd.DataFrame(data).sort_values(by="depth").reset_index(drop=True) logger.debug(f"Retrieved {len(df)} fluid contacts for well '{self.name}'.") return df - def add_pressure_tests( - self, tests: List[Dict[str, Any]], replace: bool = False - ): + def add_pressure_tests(self, tests: List[Dict[str, Any]], replace: bool = False): """Adds or updates a list of formation pressure tests for the well. Args: @@ -2303,9 +2446,7 @@ def _opt_float(v: Any) -> Optional[float]: if not (np.isfinite(d_val) and np.isfinite(p_val)): continue uom = str( - test_data.get("pressure_uom") - or test_data.get("pressure_unit") - or "psi" + test_data.get("pressure_uom") or test_data.get("pressure_unit") or "psi" ).strip() t_uom = str( test_data.get("temperature_uom") @@ -2315,7 +2456,8 @@ def _opt_float(v: Any) -> Optional[float]: remarks_raw = test_data.get("remarks", test_data.get("remark")) remarks = ( str(remarks_raw).strip() - if remarks_raw is not None and str(remarks_raw).strip() not in ("", "nan", "None") + if remarks_raw is not None + and str(remarks_raw).strip() not in ("", "nan", "None") else None ) dedup_tests[d_val] = { @@ -2446,7 +2588,11 @@ def get_pressure_tests(self) -> pd.DataFrame: } for t in tests ] - df = pd.DataFrame(data, columns=cols).sort_values(by="depth").reset_index(drop=True) + df = ( + pd.DataFrame(data, columns=cols) + .sort_values(by="depth") + .reset_index(drop=True) + ) # Keep missing optional measurements as None (JSON-safe) rather than NaN for c in ("mobility", "permeability", "temperature", "tvdss"): df[c] = df[c].astype(object).where(pd.notna(df[c]), None) @@ -2529,7 +2675,9 @@ def add_perforations( continue top_raw = perf_data.get( "top", - perf_data.get("top_depth", perf_data.get("top_md", perf_data.get("depth"))), + perf_data.get( + "top_depth", perf_data.get("top_md", perf_data.get("depth")) + ), ) bot_raw = perf_data.get( "bottom", @@ -2551,14 +2699,18 @@ def add_perforations( if b_val < t_val: t_val, b_val = b_val, t_val p_name = str( - perf_data.get("name") - or perf_data.get("perf_name") - or f"Perf-{idx + 1}" + perf_data.get("name") or perf_data.get("perf_name") or f"Perf-{idx + 1}" ).strip() if not p_name: p_name = f"Perf-{idx + 1}" - desc_raw = perf_data.get("description", perf_data.get("remark", perf_data.get("comment"))) - p_desc = str(desc_raw).strip() if desc_raw is not None and str(desc_raw).strip() != "" else None + desc_raw = perf_data.get( + "description", perf_data.get("remark", perf_data.get("comment")) + ) + p_desc = ( + str(desc_raw).strip() + if desc_raw is not None and str(desc_raw).strip() != "" + else None + ) dedup_perfs[(p_name, t_val, b_val)] = { "name": p_name, "top": t_val, @@ -2691,9 +2843,7 @@ def add_core_sample_with_measurements( if not orm_sample: # Ensure depth_val is unique in 32-bit REAL precision across existing_samples - existing_f32_depths = { - float(np.float32(s.depth)) for s in existing_samples - } + existing_f32_depths = {float(np.float32(s.depth)) for s in existing_samples} while float(np.float32(depth_val)) in existing_f32_depths: depth_val = round(depth_val + 0.002, 4) @@ -2813,9 +2963,15 @@ def replace_rca_samples(self, samples: List[Dict[str, Any]]): # If another plug in a different core interval shares the same plug number (e.g. "1"), # disambiguate by depth so distinct plugs are never overwritten - if sname in dedup_by_name and abs(dedup_by_name[sname]["depth"] - depth_val) >= 1e-2: + if ( + sname in dedup_by_name + and abs(dedup_by_name[sname]["depth"] - depth_val) >= 1e-2 + ): sname = f"{sname}@{depth_val:.2f}m" - if sname in dedup_by_name and abs(dedup_by_name[sname]["depth"] - depth_val) >= 1e-2: + if ( + sname in dedup_by_name + and abs(dedup_by_name[sname]["depth"] - depth_val) >= 1e-2 + ): sname = f"{sname}_{idx + 1}" # Avoid uq_well_id_sample_depth collision in 32-bit REAL precision when multiple plugs snap to the same log depth @@ -2904,10 +3060,7 @@ def replace_rca_samples(self, samples: List[Dict[str, Any]]): # Clear all existing RCA measurements for this well, and remove any core sample that has no SCAL data for orm_sample in list(self._orm_well.core_samples): orm_sample.measurements.clear() - if ( - not orm_sample.capillary_pressure_data - and not orm_sample.relperm_data - ): + if not orm_sample.capillary_pressure_data and not orm_sample.relperm_data: self.db_session.delete(orm_sample) self.db_session.flush() @@ -2956,9 +3109,15 @@ def replace_scal_pc_data(self, pc_rows: List[Dict[str, Any]], append: bool = Fal "description": row.get("description"), }, ) - if entry.get("porosity") is None and row.get("porosity", row.get("cpore")) is not None: + if ( + entry.get("porosity") is None + and row.get("porosity", row.get("cpore")) is not None + ): entry["porosity"] = row.get("porosity", row.get("cpore")) - if entry.get("permeability") is None and row.get("permeability", row.get("cperm")) is not None: + if ( + entry.get("permeability") is None + and row.get("permeability", row.get("cperm")) is not None + ): entry["permeability"] = row.get("permeability", row.get("cperm")) if entry.get("raw_depth") is None and row.get("raw_depth") is not None: entry["raw_depth"] = row.get("raw_depth") @@ -2995,9 +3154,7 @@ def replace_scal_pc_data(self, pc_rows: List[Dict[str, Any]], append: bool = Fal for sname, sdata in grouped.items(): pc_points = sdata["pc_data"] orm_existing = self.db_session.scalar( - select(ORMCoreSample).filter_by( - well_id=self.well_id, sample_name=sname - ) + select(ORMCoreSample).filter_by(well_id=self.well_id, sample_name=sname) ) if append and orm_existing and orm_existing.capillary_pressure_data: existing_pts = [ @@ -3025,7 +3182,10 @@ def replace_scal_pc_data(self, pc_rows: List[Dict[str, Any]], append: bool = Fal "value": float(m.value), "unit": m.unit, } - if sdata.get("porosity") is not None and str(sdata.get("porosity")).strip() != "": + if ( + sdata.get("porosity") is not None + and str(sdata.get("porosity")).strip() != "" + ): try: existing_meas_map["CPORE"] = { "property_name": "CPORE", @@ -3034,7 +3194,10 @@ def replace_scal_pc_data(self, pc_rows: List[Dict[str, Any]], append: bool = Fal } except (TypeError, ValueError): pass - if sdata.get("permeability") is not None and str(sdata.get("permeability")).strip() != "": + if ( + sdata.get("permeability") is not None + and str(sdata.get("permeability")).strip() != "" + ): try: existing_meas_map["CPERM"] = { "property_name": "CPERM", @@ -3043,7 +3206,10 @@ def replace_scal_pc_data(self, pc_rows: List[Dict[str, Any]], append: bool = Fal } except (TypeError, ValueError): pass - if sdata.get("raw_depth") is not None and str(sdata.get("raw_depth")).strip() != "": + if ( + sdata.get("raw_depth") is not None + and str(sdata.get("raw_depth")).strip() != "" + ): try: existing_meas_map["RAW_DEPTH"] = { "property_name": "RAW_DEPTH", diff --git a/quick_pp/database/setup_postgresql_db.sql b/quick_pp/database/setup_postgresql_db.sql index f227cbcf..5e92126c 100644 --- a/quick_pp/database/setup_postgresql_db.sql +++ b/quick_pp/database/setup_postgresql_db.sql @@ -123,6 +123,11 @@ CREATE TABLE IF NOT EXISTS "fluid_contacts" ( "well_id" INTEGER NOT NULL REFERENCES "wells"("well_id") ON DELETE CASCADE, "name" VARCHAR(100) NOT NULL, "depth" REAL NOT NULL, + "zone" VARCHAR(255), + "contact_type" VARCHAR(50), + "fault_block" VARCHAR(100), + "source" VARCHAR(100), + "remarks" TEXT, UNIQUE("well_id", "name") ); diff --git a/quick_pp/database/setup_sqlite_db.sql b/quick_pp/database/setup_sqlite_db.sql index d7a8610a..0e179837 100644 --- a/quick_pp/database/setup_sqlite_db.sql +++ b/quick_pp/database/setup_sqlite_db.sql @@ -121,6 +121,11 @@ CREATE TABLE IF NOT EXISTS "fluid_contacts" ( "well_id" INTEGER NOT NULL REFERENCES "wells"("well_id") ON DELETE CASCADE, "name" TEXT NOT NULL, "depth" REAL NOT NULL, + "zone" TEXT, + "contact_type" TEXT, + "fault_block" TEXT, + "source" TEXT, + "remarks" TEXT, UNIQUE("well_id", "name") ); diff --git a/quick_pp/plotter/report_plots.py b/quick_pp/plotter/report_plots.py index 5ac70304..044ea966 100644 --- a/quick_pp/plotter/report_plots.py +++ b/quick_pp/plotter/report_plots.py @@ -7,8 +7,9 @@ from __future__ import annotations +import math from pathlib import Path -from typing import Any, Dict, List, Optional, Sequence, Union +from typing import Any, Dict, List, Optional, Sequence, Tuple, Union import matplotlib import numpy as np @@ -18,6 +19,7 @@ from matplotlib.gridspec import GridSpec from quick_pp import logger +from quick_pp.utils import line_intersection matplotlib.use("Agg", force=False) @@ -125,8 +127,7 @@ def _ensure_df_well_filter( and "WELL_NAME" in df.columns ): sub = df[df["WELL_NAME"].astype(str).isin([str(w) for w in well_names])] - if not sub.empty: - return sub.copy() + return sub.copy() return df.copy() @@ -524,6 +525,12 @@ def generate_nd_crossplot( target_zones: Optional[Sequence[str]] = None, output_dir: Optional[Union[str, Path]] = None, url_prefix: Optional[str] = None, + litho_model: str = "ssc", + dry_sand_point: Tuple[float, float] = (-0.02, 2.65), + dry_silt_point: Optional[Tuple[Optional[float], Optional[float]]] = None, + dry_clay_point: Tuple[float, float] = (0.33, 2.70), + fluid_point: Tuple[float, float] = (1.0, 1.0), + silt_line_angle: float = 119.0, ) -> str: """Generate Figure 4.1: Neutron-Density Crossplot, Core Porosity Calibration & Matrix Grain Density (RHOMA) QC. @@ -534,6 +541,12 @@ def generate_nd_crossplot( target_zones (Optional[Sequence[str]]): Optional target reservoir zone names to focus calibration. output_dir (Optional[Union[str, Path]]): Directory where the rendered PNG will be written. url_prefix (Optional[str]): Optional URL prefix to prepend to the returned filename. + litho_model (str): Active lithology model ('ssc', 'multi_mineral', etc.). Defaults to 'ssc'. + dry_sand_point (Tuple[float, float]): (NPHI, RHOB) coordinates for dry sand. + dry_silt_point (Optional[Tuple]): (NPHI, RHOB) coordinates for dry silt. + dry_clay_point (Tuple[float, float]): (NPHI, RHOB) coordinates for dry clay. + fluid_point (Tuple[float, float]): (NPHI, RHOB) coordinates for formation fluid. + silt_line_angle (float): Angle of silt line in degrees from horizontal. Defaults to 119.0. Returns: str: File path or URL to the generated `fig_4_1_nd_crossplot.png`. @@ -582,46 +595,138 @@ def generate_nd_crossplot( cbar = fig.colorbar(sc, ax=ax1, pad=0.02, fraction=0.046) cbar.set_label(color_by, fontsize=8) - # Plot clean sandstone, limestone, dolomite, and clay endpoints - phi_grid = np.linspace(0.0, 0.40, 50) - ax1.plot( - phi_grid - 0.02 * (1 - phi_grid), - 2.65 * (1 - phi_grid) + 1.0 * phi_grid, - "k-", - lw=1.3, - label="Quartz (2.65 g/cc)", - ) - ax1.plot( - phi_grid, - 2.71 * (1 - phi_grid) + 1.0 * phi_grid, - "b--", - lw=1.2, - label="Calcite (2.71 g/cc)", - ) - ax1.plot( - phi_grid + 0.02 * (1 - phi_grid), - 2.87 * (1 - phi_grid) + 1.0 * phi_grid, - "m:", - lw=1.2, - label="Dolomite (2.87 g/cc)", - ) - ax1.scatter( - [0.33], - [2.70], - color="#dc2626", - marker="^", - s=65, - zorder=5, - label="Dry Clay Endpoint", - ) + if str(litho_model).lower() == "ssc": + # Sand-Silt-Clay (SSC) Ternary Endpoints & Lines + A = dry_sand_point or (-0.02, 2.65) + C = dry_clay_point or (0.33, 2.70) + D = fluid_point or (1.0, 1.0) + + # Compute dry silt point B if not specified + if ( + dry_silt_point + and dry_silt_point[0] is not None + and dry_silt_point[1] is not None + ): + B = (float(dry_silt_point[0]), float(dry_silt_point[1])) + else: + rad = math.radians(float(silt_line_angle)) + p_silt = (D[0] + math.cos(rad), D[1] + math.sin(rad)) + silt_inter = line_intersection((A, C), (D, p_silt)) + if ( + silt_inter is not None + and np.isfinite(silt_inter[0]) + and np.isfinite(silt_inter[1]) + ): + B = (float(silt_inter[0]), float(silt_inter[1])) + else: + drysilt_NPHI = 1.0 - 1.68 * math.tan( + float(silt_line_angle - 90) * math.pi / 180.0 + ) + B = (drysilt_NPHI, 2.68) + + # Plot SSC ternary model lines + ax1.plot( + [D[0], A[0]], + [D[1], A[1]], + color="#1e40af", + lw=1.3, + label=f"Sand Line ({A[1]:.2f} g/cc)", + ) + ax1.plot( + [D[0], B[0]], + [D[1], B[1]], + color="#16a34a", + lw=1.3, + linestyle="-", + label=f"Silt Line ({silt_line_angle:.0f}°)", + ) + ax1.plot( + [D[0], C[0]], + [D[1], C[1]], + color="#9333ea", + lw=1.2, + linestyle="--", + label=f"Clay Line ({C[1]:.2f} g/cc)", + ) + ax1.plot( + [A[0], C[0]], + [A[1], C[1]], + color="#0f172a", + lw=1.4, + linestyle="-", + label="Rock Line", + ) + + # Endpoint markers + ax1.scatter( + [A[0]], + [A[1]], + color="#eab308", + marker="s", + s=50, + zorder=6, + label=f"Dry Sand ({A[0]:.2f}, {A[1]:.2f})", + ) + ax1.scatter( + [B[0]], + [B[1]], + color="#16a34a", + marker="D", + s=45, + zorder=6, + label=f"Dry Silt ({B[0]:.2f}, {B[1]:.2f})", + ) + ax1.scatter( + [C[0]], + [C[1]], + color="#dc2626", + marker="^", + s=60, + zorder=6, + label=f"Dry Clay ({C[0]:.2f}, {C[1]:.2f})", + ) + else: + # Fallback Standard Quartz / Calcite / Dolomite lithology lines + phi_grid = np.linspace(0.0, 0.40, 50) + ax1.plot( + phi_grid - 0.02 * (1 - phi_grid), + 2.65 * (1 - phi_grid) + 1.0 * phi_grid, + "k-", + lw=1.3, + label="Quartz (2.65 g/cc)", + ) + ax1.plot( + phi_grid, + 2.71 * (1 - phi_grid) + 1.0 * phi_grid, + "b--", + lw=1.2, + label="Calcite (2.71 g/cc)", + ) + ax1.plot( + phi_grid + 0.02 * (1 - phi_grid), + 2.87 * (1 - phi_grid) + 1.0 * phi_grid, + "m:", + lw=1.2, + label="Dolomite (2.87 g/cc)", + ) + ax1.scatter( + [0.33], + [2.70], + color="#dc2626", + marker="^", + s=65, + zorder=5, + label="Dry Clay Endpoint", + ) ax1.set_xlim(-0.05, 0.50) ax1.set_ylim(2.95, 1.85) ax1.set_xlabel("Neutron Porosity NPHI (v/v)", fontsize=8.5, fontweight="bold") ax1.set_ylabel("Bulk Density RHOB (g/cc)", fontsize=8.5, fontweight="bold") + model_title = "SSC" if str(litho_model).lower() == "ssc" else "Lithology" ax1.set_title( - "Neutron-Density Lithology Crossplot", fontsize=9.0, fontweight="bold" + f"Neutron-Density {model_title} Crossplot", fontsize=9.0, fontweight="bold" ) - ax1.legend(loc="lower left", fontsize=7.0) + ax1.legend(loc="lower left", fontsize=6.8) ax1.grid(True, linestyle=":", alpha=0.5) else: ax1.text(0.5, 0.5, "NPHI / RHOB curves not available", ha="center", va="center") @@ -672,9 +777,7 @@ def generate_nd_crossplot( fontsize=8.5, fontweight="bold", ) - ax2.set_ylabel( - "Log Total Porosity PHIT (v/v)", fontsize=8.5, fontweight="bold" - ) + ax2.set_ylabel("Log Total Porosity PHIT (v/v)", fontsize=8.5, fontweight="bold") ax2.set_title( f"Core-to-Log Calibration (R²={r2:.2f}, RMSE={rmse:.3f})", fontsize=9.0, @@ -725,7 +828,9 @@ def generate_nd_crossplot( label="Calcite Cement (2.71 g/cc)", ) ax3.set_xlim(2.52, 2.88) - ax3.set_xlabel("Matrix Grain Density ρ_ma (g/cc)", fontsize=8.5, fontweight="bold") + ax3.set_xlabel( + "Matrix Grain Density ρ_ma (g/cc)", fontsize=8.5, fontweight="bold" + ) ax3.set_ylabel("Sample Frequency", fontsize=8.5, fontweight="bold") ax3.set_title("Grain Density (ρ_ma) Matrix QC", fontsize=9.0, fontweight="bold") ax3.legend(loc="upper right", fontsize=7.0) @@ -819,6 +924,8 @@ def generate_rock_typing_perm_plot( df: pd.DataFrame, well_names: Optional[List[str]] = None, rock_types: Optional[List[Dict[str, Any]]] = None, + fzi_cutoffs: Optional[List[float]] = None, + target_zones: Optional[Sequence[str]] = None, output_dir: Optional[Union[str, Path]] = None, url_prefix: Optional[str] = None, ) -> str: @@ -828,6 +935,8 @@ def generate_rock_typing_perm_plot( df (pd.DataFrame): Multi-well log DataFrame containing porosity and permeability curves. well_names (Optional[List[str]]): Optional subset of well names to filter. rock_types (Optional[List[Dict[str, Any]]]): Optional list of rock type definitions. + fzi_cutoffs (Optional[List[float]]): Configured FZI cutoff boundaries. Defaults to [0.5, 1.5, 3.0]. + target_zones (Optional[Sequence[str]]): Optional target reservoir zone names to focus rock typing. output_dir (Optional[Union[str, Path]]): Directory where the rendered PNG will be written. url_prefix (Optional[str]): Optional URL prefix to prepend to the returned filename. @@ -835,7 +944,13 @@ def generate_rock_typing_perm_plot( str: File path or URL to the generated `fig_5_1_perm_crossplot.png`. """ sub = _ensure_df_well_filter(df, well_names) - fig = Figure(figsize=(10.5, 4.5)) + if target_zones and "ZONES" in sub.columns and sub["ZONES"].notna().any(): + tz_set = {str(z).strip().upper() for z in target_zones if z} + tz_df = sub[sub["ZONES"].astype(str).str.strip().str.upper().isin(tz_set)] + if not tz_df.empty: + sub = tz_df + + fig = Figure(figsize=(11.0, 4.5)) gs = GridSpec(1, 2, figure=fig, width_ratios=[1.2, 1.0], wspace=0.28) ax1 = fig.add_subplot(gs[0, 0]) ax2 = fig.add_subplot(gs[0, 1]) @@ -857,7 +972,27 @@ def generate_rock_typing_perm_plot( rqi = 0.0314 * np.sqrt(k_arr / np.clip(p_arr, 1e-4, 0.99)) phiz = p_arr / np.clip(1.0 - p_arr, 1e-4, 1.0) fzi = rqi / np.clip(phiz, 1e-4, 100.0) - rt_bins = np.digitize(fzi, bins=[0.5, 1.5, 3.0]) + 1 + + # Parse and resolve cutoffs + cutoffs: List[float] = [] + if fzi_cutoffs and len(fzi_cutoffs) > 0: + cutoffs = sorted(float(c) for c in fzi_cutoffs if float(c) > 0) + elif rock_types: + import re + + for rt in rock_types: + fzi_r = str(rt.get("fzi_range", "")) + nums = re.findall(r"[-+]?\d*\.\d+|\d+", fzi_r) + for num in nums: + val = float(num) + if val > 0 and val not in cutoffs: + cutoffs.append(val) + cutoffs = sorted(cutoffs) + if not cutoffs: + cutoffs = [0.5, 1.5, 3.0] + + rt_bins = np.digitize(fzi, bins=cutoffs) + 1 + n_rts = len(cutoffs) + 1 if len(p_arr) > 2000: idx = np.linspace(0, len(p_arr) - 1, 2000, dtype=int) @@ -865,29 +1000,40 @@ def generate_rock_typing_perm_plot( else: p_plot, k_plot, rt_plot = p_arr, k_arr, rt_bins - rt_names = { - 1: "RT1 (Nano/Micro-Poro, FZI < 0.5)", - 2: "RT2 (Meso-Poro, 0.5 ≤ FZI < 1.5)", - 3: "RT3 (Macro-Poro, 1.5 ≤ FZI < 3.0)", - 4: "RT4 (Mega-Poro, FZI ≥ 3.0)", - } - phi_line = np.linspace(0.03, 0.36, 50) - for rt_code in (1, 2, 3, 4): + # Plot data points by partitioned rock type + phi_line = np.linspace(0.03, 0.38, 60) + for rt_code in range(1, n_rts + 1): mask = rt_plot == rt_code color = PALETTE[(rt_code - 1) % len(PALETTE)] + if rt_code == 1: + lbl = f"RT1 (FZI < {cutoffs[0]:.2f})" + elif rt_code == n_rts: + lbl = f"RT{rt_code} (FZI ≥ {cutoffs[-1]:.2f})" + else: + lbl = f"RT{rt_code} ({cutoffs[rt_code - 2]:.2f} ≤ FZI < {cutoffs[rt_code - 1]:.2f})" + if np.any(mask): ax1.scatter( p_plot[mask], k_plot[mask], color=color, s=14, - alpha=0.7, - label=rt_names[rt_code], + alpha=0.75, + label=lbl, ) - # Iso-FZI transform curve - fzi_rep = {1: 0.3, 2: 0.9, 3: 2.1, 4: 4.5}[rt_code] - k_iso = 1014.0 * (fzi_rep**2) * (phi_line**3) / ((1.0 - phi_line) ** 2) - ax1.plot(phi_line, k_iso, color=color, linestyle="--", lw=1.2) + + # Plot explicit cutoff boundary curves so points and cutoffs align perfectly + for _i, c_val in enumerate(cutoffs): + k_cutoff = 1014.24 * (c_val**2) * (phi_line**3) / ((1.0 - phi_line) ** 2) + ax1.plot( + phi_line, + k_cutoff, + color="#334155", + linestyle="--", + lw=1.1, + alpha=0.85, + label=f"Cutoff {c_val:.2f} µm", + ) ax1.set_yscale("log") ax1.set_xlim(0.0, 0.40) @@ -895,23 +1041,70 @@ def generate_rock_typing_perm_plot( ax1.set_xlabel("Porosity Φ (v/v)", fontsize=9, fontweight="bold") ax1.set_ylabel("Permeability K (mD)", fontsize=9, fontweight="bold") ax1.set_title( - "Porosity-Permeability Crossplot by FZI Rock Type", - fontsize=9.5, + "Porosity-Permeability by FZI Rock Type", + fontsize=9.0, fontweight="bold", ) - ax1.legend(loc="lower right", fontsize=7.5) + ax1.legend(loc="lower right", fontsize=7.0) ax1.grid(True, which="both", linestyle=":", alpha=0.5) - # Panel 2: Stratigraphic Modified Lorenz Plot (Cumulative Flow vs Storage Capacity) - kh_cum = np.cumsum(k_arr) / max(float(np.sum(k_arr)), 1e-9) - phih_cum = np.cumsum(p_arr) / max(float(np.sum(p_arr)), 1e-9) + # Panel 2: Stratigraphic Modified Lorenz Plot (Ordered by stratigraphic depth on key cored/evaluated well) + well_col = ( + sub["WELL_NAME"] + if "WELL_NAME" in sub.columns + else pd.Series("Project", index=sub.index) + ) + wells_with_pts = well_col[valid].value_counts() + key_well = ( + str(wells_with_pts.index[0]) if not wells_with_pts.empty else "Key Well" + ) + well_mask = valid & (well_col == key_well) + if not well_mask.any(): + well_mask = valid + key_well = "Field" + + w_sub = sub[well_mask].copy() + if "DEPTH" in w_sub.columns and w_sub["DEPTH"].notna().any(): + w_sub = ( + w_sub.dropna(subset=["DEPTH"]) + .sort_values("DEPTH") + .reset_index(drop=True) + ) + w_p = _get_numeric_series(w_sub, "CPORE", "PHIE", "PHIT").to_numpy( + dtype=float + ) + w_p = np.where(w_p > 1.0, w_p / 100.0, w_p) + w_k = _get_numeric_series(w_sub, "CPERM", "PERM").to_numpy(dtype=float) + d_vals = pd.to_numeric(w_sub["DEPTH"], errors="coerce").to_numpy( + dtype=float + ) + dh = np.diff(d_vals, prepend=d_vals[0]) + dh = np.where((dh > 0) & (dh < 5.0), dh, 0.1524) + else: + w_p = p_arr + w_k = k_arr + dh = np.full_like(w_p, 0.1524) + + flow_cap = np.cumsum(np.clip(w_k, 1e-4, 5e4) * dh) + store_cap = np.cumsum(np.clip(w_p, 1e-4, 0.45) * dh) + kh_cum = flow_cap / max(float(flow_cap[-1]), 1e-9) + phih_cum = store_cap / max(float(store_cap[-1]), 1e-9) + + # Calculate Lorenz coefficient L + try: + lorenz_val = max( + 0.0, min(1.0, float(np.trapz(kh_cum, phih_cum) - 0.5) * 2.0) + ) + except Exception: + lorenz_val = 0.50 + ax2.plot([0, 1], [0, 1], "k--", lw=1.1, label="Homogeneous (Lorenz = 0)") ax2.plot( phih_cum, kh_cum, color="#1d4ed8", lw=2.2, - label="Stratigraphic Flow Profile", + label=f"Stratigraphic Flow Profile (L = {abs(lorenz_val):.2f})", ) ax2.fill_between(phih_cum, phih_cum, kh_cum, color="#93c5fd", alpha=0.35) ax2.set_xlim(0, 1) @@ -923,9 +1116,11 @@ def generate_rock_typing_perm_plot( "Cumulative Flow Capacity Σ(K·h) [frac]", fontsize=9, fontweight="bold" ) ax2.set_title( - "Stratigraphic Modified Lorenz Plot (SMLP)", fontsize=9.5, fontweight="bold" + f"Stratigraphic Modified Lorenz Plot (SMLP — {key_well})", + fontsize=9.0, + fontweight="bold", ) - ax2.legend(loc="lower right", fontsize=8) + ax2.legend(loc="lower right", fontsize=7.5) ax2.grid(True, linestyle=":", alpha=0.5) else: ax1.text( @@ -948,6 +1143,8 @@ def generate_pickett_plot( m: float = 2.0, n: float = 2.0, fwl: Optional[float] = None, + fwl_basis: Optional[str] = None, + target_zones: Optional[Sequence[str]] = None, output_dir: Optional[Union[str, Path]] = None, url_prefix: Optional[str] = None, ) -> str: @@ -961,6 +1158,8 @@ def generate_pickett_plot( m (float): Cementation exponent. Defaults to 2.0. n (float): Saturation exponent. Defaults to 2.0. fwl (Optional[float]): Optional Free Water Level depth (`m`) to overlay on the saturation profile. + fwl_basis (Optional[str]): Source / basis of the FWL value (e.g. 'Log Transition Zone', 'Fluid Contacts DB'). + target_zones (Optional[Sequence[str]]): Optional target reservoir zone names to focus Pickett evaluation. output_dir (Optional[Union[str, Path]]): Directory where the rendered PNG will be written. url_prefix (Optional[str]): Optional URL prefix to prepend to the returned filename. @@ -968,8 +1167,14 @@ def generate_pickett_plot( str: File path or URL to the generated `fig_6_1_pickett_shf_plot.png`. """ sub = _ensure_df_well_filter(df, well_names) - fig = Figure(figsize=(10.5, 4.5)) - gs = GridSpec(1, 2, figure=fig, width_ratios=[1.15, 1.0], wspace=0.28) + if target_zones and "ZONES" in sub.columns and sub["ZONES"].notna().any(): + tz_set = {str(z).strip().upper() for z in target_zones if z} + tz_df = sub[sub["ZONES"].astype(str).str.strip().str.upper().isin(tz_set)] + if not tz_df.empty: + sub = tz_df + + fig = Figure(figsize=(11.8, 4.5)) + gs = GridSpec(1, 2, figure=fig, width_ratios=[1.15, 1.0], wspace=0.38) ax1 = fig.add_subplot(gs[0, 0]) ax2 = fig.add_subplot(gs[0, 1]) @@ -992,8 +1197,8 @@ def generate_pickett_plot( rt_v, phi_v, c_v = rt_v[idx], phi_v[idx], c_v[idx] sc = ax1.scatter(rt_v, phi_v, c=c_v, cmap="YlGnBu_r", s=13, alpha=0.65) - cbar = fig.colorbar(sc, ax=ax1, pad=0.02, fraction=0.046) - cbar.set_label("VCLAY / Shaliness", fontsize=8) + cbar = fig.colorbar(sc, ax=ax1, pad=0.03, fraction=0.046) + cbar.set_label("VCLAY (v/v)", fontsize=8.5, fontweight="bold") phi_grid = np.logspace(np.log10(0.02), np.log10(0.45), 60) for sw_iso, col, ls in [ @@ -1018,11 +1223,11 @@ def generate_pickett_plot( ax1.set_xlabel("True Resistivity Rt (Ω·m)", fontsize=9, fontweight="bold") ax1.set_ylabel("Porosity Φ (v/v)", fontsize=9, fontweight="bold") ax1.set_title( - f"Pickett Plot (Rw={rw:.3f} Ω·m, a={a}, m={m}, n={n})", - fontsize=9.5, + f"Pickett Plot (Rw={rw:.3f} Ω·m, a={a:.1f}, m={m:.1f}, n={n:.1f})", + fontsize=9.0, fontweight="bold", ) - ax1.legend(loc="lower left", fontsize=8) + ax1.legend(loc="lower left", fontsize=7.5) ax1.grid(True, which="both", linestyle=":", alpha=0.5) else: ax1.text( @@ -1035,6 +1240,11 @@ def generate_pickett_plot( # Panel 2: Water Saturation vs Depth & Free Water Level (FWL) depth = _get_numeric_series(sub, "TVDSS", "TVD", "DEPTH") + depth_label = ( + "Depth TVDss (m)" + if "TVDSS" in sub.columns + else ("Depth TVD (m)" if "TVD" in sub.columns else "Depth (m)") + ) swt = _get_numeric_series(sub, "SWT", "SW") shf = _get_numeric_series(sub, "SHF") if (depth.notna() & swt.notna()).any(): @@ -1058,23 +1268,24 @@ def generate_pickett_plot( shf_arr, sd_arr, color="#dc2626", s=8, alpha=0.45, label="Capillary SHF" ) if fwl is not None and np.isfinite(fwl): + fwl_basis_label = f" [{fwl_basis}]" if fwl_basis else "" ax2.axhline( float(fwl), color="#0f172a", linestyle="--", lw=1.5, - label=f"FWL ({float(fwl):.1f} m)", + label=f"FWL: {float(fwl):.1f} m{fwl_basis_label}", ) ax2.invert_yaxis() ax2.set_xlim(0.0, 1.05) ax2.set_xlabel("Water Saturation Sw (v/v)", fontsize=9, fontweight="bold") - ax2.set_ylabel("Depth (m)", fontsize=9, fontweight="bold") + ax2.set_ylabel(depth_label, fontsize=9, fontweight="bold") ax2.set_title( "Water Saturation Profile & Contact Reconciliation", - fontsize=9.5, + fontsize=9.0, fontweight="bold", ) - ax2.legend(loc="lower left", fontsize=8) + ax2.legend(loc="lower left", fontsize=7.5) ax2.grid(True, linestyle=":", alpha=0.5) else: ax2.axis("off") @@ -1087,6 +1298,9 @@ def generate_shf_j_function_plot( rock_types: Optional[List[Dict[str, Any]]] = None, fwl: Optional[float] = None, is_analogue_scal: bool = True, + scal_params: Optional[Dict[str, Any]] = None, + shf_params: Optional[Dict[str, Any]] = None, + target_zones: Optional[Sequence[str]] = None, output_dir: Optional[Union[str, Path]] = None, url_prefix: Optional[str] = None, ) -> str: @@ -1098,14 +1312,24 @@ def generate_shf_j_function_plot( fwl (Optional[float]): Optional Free Water Level depth (`m`). is_analogue_scal (bool): Whether the SCAL curves represent analogue defaults vs. measured project SCAL. Defaults to True. + scal_params (Optional[Dict[str, Any]]): Project SCAL parameters (Corey/LET exponents, Swirr, Sor, etc.). + shf_params (Optional[Dict[str, Any]]): Project SHF parameters (IFT, contact angle, fluid densities). + target_zones (Optional[Sequence[str]]): Optional target reservoir zone names. output_dir (Optional[Union[str, Path]]): Directory where the rendered PNG will be written. url_prefix (Optional[str]): Optional URL prefix to prepend to the returned filename. Returns: str: File path or URL to the generated `fig_7_1_shf_scal_plot.png`. """ - fig = Figure(figsize=(10.5, 4.4)) - gs = GridSpec(1, 2, figure=fig, wspace=0.28) + sub = df + if target_zones and "ZONES" in df.columns and df["ZONES"].notna().any(): + tz_set = {str(z).strip().upper() for z in target_zones if z} + tz_df = df[df["ZONES"].astype(str).str.strip().str.upper().isin(tz_set)] + if not tz_df.empty: + sub = tz_df + + fig = Figure(figsize=(11.2, 4.5)) + gs = GridSpec(1, 2, figure=fig, width_ratios=[1.05, 1.0], wspace=0.30) ax1 = fig.add_subplot(gs[0, 0]) ax2 = fig.add_subplot(gs[0, 1]) @@ -1117,14 +1341,78 @@ def generate_shf_j_function_plot( ("RT3 (Macro-Porous)", 0.22, -0.50, PALETTE[2]), ("RT4 (Mega-Porous Sand)", 0.12, -0.60, PALETTE[3]), ] + if rock_types and len(rock_types) >= 2: + new_fits = [] + for i, rt in enumerate(rock_types): + r_name = rt.get("name", f"RT{i + 1}") + # Use specific J-fit coefficients if present + a_p = float(rt.get("j_a", rt_fits[i % len(rt_fits)][1])) + b_p = float(rt.get("j_b", rt_fits[i % len(rt_fits)][2])) + new_fits.append( + (r_name.split("—")[0].strip(), a_p, b_p, PALETTE[i % len(PALETTE)]) + ) + if new_fits: + rt_fits = new_fits + + # Plot actual project data points if Sw, Phi, Perm, and Depth exist + sw_s = _get_numeric_series(sub, "SWT", "SW") + phi_s = _get_numeric_series(sub, "CPORE", "PHIE", "PHIT") + phi_s = pd.Series(np.where(phi_s > 1.0, phi_s / 100.0, phi_s), index=sub.index) + k_s = _get_numeric_series(sub, "CPERM", "PERM") + d_s = _get_numeric_series(sub, "TVDSS", "TVD", "DEPTH") + + shf_dict = shf_params or {} + ift_val = float(shf_dict.get("ift", 30.0)) + theta_val = float(shf_dict.get("theta", 30.0)) + gw_val = float(shf_dict.get("gw", 1.05)) + ghc_val = float(shf_dict.get("ghc", 0.80)) + d_rho = max(gw_val - ghc_val, 0.05) + sigma_cos = max(abs(ift_val * math.cos(math.radians(theta_val))), 1.0) + + if ( + fwl is not None + and (sw_s.notna() & phi_s.notna() & k_s.notna() & d_s.notna()).any() + ): + v_mask = ( + sw_s.notna() + & (sw_s > 0.02) + & (sw_s <= 1.0) + & phi_s.notna() + & (phi_s > 0.02) + & k_s.notna() + & (k_s > 1e-3) + & d_s.notna() + ) + if v_mask.sum() >= 10: + sw_pts = sw_s[v_mask].to_numpy(dtype=float) + phi_pts = phi_s[v_mask].to_numpy(dtype=float) + k_pts = k_s[v_mask].to_numpy(dtype=float) + depth_pts = d_s[v_mask].to_numpy(dtype=float) + h_pts = np.maximum(float(fwl) - depth_pts, 0.2) + pc_psi = 0.433 * d_rho * h_pts * 3.28084 + j_pts = 0.21645 * (pc_psi / sigma_cos) * np.sqrt(k_pts / phi_pts) + valid_j = (j_pts > 0.01) & (j_pts < 12.0) + if valid_j.sum() >= 5: + sub_n = min(valid_j.sum(), 400) + idx_j = np.linspace(0, valid_j.sum() - 1, sub_n, dtype=int) + ax1.scatter( + sw_pts[valid_j][idx_j], + j_pts[valid_j][idx_j], + color="#94a3b8", + s=11, + alpha=0.45, + edgecolors="none", + label=f"Measured Log/Plug J(Sw) (n={sub_n})", + ) + for label, a_param, b_param, col in rt_fits: - sw_j = np.clip(a_param * (j_grid**b_param), 0.08, 1.0) + sw_j = np.clip(a_param * (j_grid**b_param), 0.05, 1.0) ax1.plot( sw_j, j_grid, color=col, lw=2.0, - label=f"{label}: Sw={a_param}·J^({b_param})", + label=f"{label}: Sw={a_param:.2f}·J^({b_param:.2f})", ) ax1.set_xlim(0.0, 1.0) @@ -1135,41 +1423,79 @@ def generate_shf_j_function_plot( ) ax1.set_title( "Capillary Pressure Leverett J-Function by Rock Type", - fontsize=9.5, + fontsize=9.0, fontweight="bold", ) - ax1.legend(loc="upper right", fontsize=7.5) + ax1.legend(loc="upper right", fontsize=7.0) ax1.grid(True, linestyle=":", alpha=0.5) - # Panel 2: Corey / LET Water-Oil Relative Permeability Curves - sw = np.linspace(0.15, 0.80, 80) - se = (sw - 0.15) / (1.0 - 0.15 - 0.20) - krw = 0.45 * (np.clip(se, 0.0, 1.0) ** 2.5) - kro = 0.90 * (np.clip(1.0 - se, 0.0, 1.0) ** 2.5) - - ax2.plot( - sw, krw, color="#2563eb", lw=2.2, label="Water RelPerm Krw (Nw=2.5, Krw0=0.45)" + # Panel 2: Project SCAL Water-Oil Relative Permeability Curves (Corey or LET) + scal = scal_params or {} + model_type = str(scal.get("model", "corey")).lower() + swirr = float(scal.get("swirr", 0.15)) + sor = float(scal.get("sor", 0.20)) + krw0 = float(scal.get("krw_max", 0.45)) + kro0 = float(scal.get("kro_max", 0.90)) + nw = float(scal.get("nw", 2.5)) + no = float(scal.get("no", 2.5)) + + sw = np.linspace(swirr, max(1.0 - sor, swirr + 0.05), 100) + denom_se = max(1.0 - swirr - sor, 0.01) + se = np.clip((sw - swirr) / denom_se, 0.0, 1.0) + + if model_type == "let": + lw = float(scal.get("lw", 2.0)) + ew = float(scal.get("ew", 1.5)) + tw = float(scal.get("tw", 2.0)) + lo = float(scal.get("lo", 2.0)) + eo = float(scal.get("eo", 1.5)) + to = float(scal.get("to", 2.0)) + krw = ( + krw0 + * (lw * (se**ew)) + / np.maximum(lw * (se**ew) + tw * ((1.0 - se) ** ew), 1e-9) + ) + kro = ( + kro0 + * (lo * ((1.0 - se) ** eo)) + / np.maximum(lo * ((1.0 - se) ** eo) + to * (se**eo), 1e-9) + ) + krw_label = f"Krw LET (L={lw:.1f}, E={ew:.1f}, T={tw:.1f})" + kro_label = f"Kro LET (L={lo:.1f}, E={eo:.1f}, T={to:.1f})" + else: + krw = krw0 * (se**nw) + kro = kro0 * ((1.0 - se) ** no) + krw_label = f"Krw Corey (Nw={nw:.1f}, Krw0={krw0:.2f})" + kro_label = f"Kro Corey (No={no:.1f}, Kro0={kro0:.2f})" + + ax2.plot(sw, krw, color="#2563eb", lw=2.2, label=krw_label) + ax2.plot(sw, kro, color="#059669", lw=2.2, label=kro_label) + ax2.axvline( + swirr, color="#64748b", linestyle=":", lw=1.1, label=f"Swirr = {swirr:.2f}" ) - ax2.plot( - sw, kro, color="#059669", lw=2.2, label="Oil RelPerm Kro (No=2.5, Kro0=0.90)" + ax2.axvline( + 1.0 - sor, + color="#dc2626", + linestyle=":", + lw=1.1, + label=f"1 - Sor = {1.0 - sor:.2f}", ) - ax2.axvline(0.15, color="#64748b", linestyle=":", lw=1.1, label="Swc = 0.15") - ax2.axvline(0.80, color="#dc2626", linestyle=":", lw=1.1, label="1 - Sor = 0.80") ax2.set_xlim(0.0, 1.0) ax2.set_ylim(0.0, 1.0) ax2.set_xlabel("Water Saturation Sw (v/v)", fontsize=9, fontweight="bold") ax2.set_ylabel("Relative Permeability Kr (fraction)", fontsize=9, fontweight="bold") - scal_title = ( + + scal_source = scal.get("source_label") or ( "Analogue / Default Corey SCAL RelPerm" if is_analogue_scal - else "Measured Project SCAL Water-Oil Relative Permeability" + else f"Measured Project SCAL {model_type.upper()} RelPerm" ) ax2.set_title( - scal_title, - fontsize=9.0, + scal_source, + fontsize=8.5, fontweight="bold", ) - ax2.legend(loc="center right", fontsize=7.5) + ax2.legend(loc="center right", fontsize=7.2) ax2.grid(True, linestyle=":", alpha=0.5) return _save_fig(fig, "fig_7_1_shf_scal_plot.png", output_dir, url_prefix) @@ -1218,7 +1544,9 @@ def generate_uncertainty_cutoff_plot( phie = _get_numeric_series(sub, "PHIE", "PHIT").to_numpy(dtype=float) vclay = _get_numeric_series(sub, "VCLAY").to_numpy(dtype=float) swt = _get_numeric_series(sub, "SWT", "SW").to_numpy(dtype=float) - n_wells = max(1, int(sub["WELL_NAME"].nunique()) if "WELL_NAME" in sub.columns else 1) + n_wells = max( + 1, int(sub["WELL_NAME"].nunique()) if "WELL_NAME" in sub.columns else 1 + ) valid_m = (phie > 0.0) & np.isfinite(phie) & np.isfinite(vclay) & np.isfinite(swt) if valid_m.any(): phie = phie[valid_m] @@ -1338,7 +1666,9 @@ def generate_uncertainty_cutoff_plot( color="#475569", ) ax2.set_xlabel( - "Mean Well Hydrocarbon Pore Thickness HCPT (m)", fontsize=9, fontweight="bold" + "Mean Well Hydrocarbon Pore Thickness HCPT (m)", + fontsize=9, + fontweight="bold", ) ax2.set_ylabel("Probability Density", fontsize=9, fontweight="bold") ax2.set_title( @@ -1393,7 +1723,9 @@ def generate_upscaling_comparison_plot( ) return _save_fig(fig, "fig_9_1_upscaling_plot.png", output_dir, url_prefix) - ymin, ymax = _resolve_target_depth_window(sub, target_zones=target_zones, max_span=480.0) + ymin, ymax = _resolve_target_depth_window( + sub, target_zones=target_zones, max_span=480.0 + ) d_col = pd.to_numeric(sub["DEPTH"], errors="coerce") sub_win = sub[(d_col >= ymin) & (d_col <= ymax)].copy() if sub_win.empty: @@ -1420,7 +1752,9 @@ def generate_upscaling_comparison_plot( "VCLAY": _get_numeric_series(sub_win, "VCLAY", "VSHALE"), "PHIT": _get_numeric_series(sub_win, "PHIE", "PHIT"), "SWT": _get_numeric_series(sub_win, "SWT", "SW", "SHF"), - "PERM": _get_numeric_series(sub_win, "PERM", "PERM_TRANS").clip(lower=1e-2), + "PERM": _get_numeric_series(sub_win, "PERM", "PERM_TRANS").clip( + lower=1e-2 + ), "ZONES": sub_win["ZONES"].fillna("Target").astype(str) if "ZONES" in sub_win.columns else "Target", @@ -1508,7 +1842,12 @@ def generate_upscaling_comparison_plot( fontsize=7.0, fontweight="bold", color="#0f172a", - bbox={"facecolor": "white", "alpha": 0.8, "edgecolor": "none", "pad": 1.0}, + bbox={ + "facecolor": "white", + "alpha": 0.8, + "edgecolor": "none", + "pad": 1.0, + }, ) ax1.set_ylim(ymax, ymin) @@ -1542,7 +1881,7 @@ def generate_well_cpi_plot( Returns: str: File path or URL to the generated `cpi_.png`. """ - sub_well = _trim_to_logged_interval(_ensure_df_well_filter(df, [well_name])) + sub_well = _ensure_df_well_filter(df, [well_name]) safe_well = "".join( c if c.isalnum() or c in ("-", "_") else "_" for c in str(well_name) ) @@ -1562,12 +1901,32 @@ def generate_well_cpi_plot( axes[0].text(0.5, 0.5, f"No log data for {well_name}", ha="center", va="center") return _save_fig(fig, filename, output_dir, url_prefix) + # Ensure strictly sorted monotonic depth index to prevent any zig-zag line rendering + sub_well = ( + sub_well.dropna(subset=["DEPTH"]).sort_values(by="DEPTH").reset_index(drop=True) + ) + sub_well = _trim_to_logged_interval(sub_well) + # Auto-zoom to target reservoir window (CHK-06: vertical span <= 500 m) - d_min, d_max = _resolve_target_depth_window(sub_well, target_zones=zones, max_span=480.0) + d_min, d_max = _resolve_target_depth_window( + sub_well, target_zones=zones, max_span=480.0 + ) d_col = pd.to_numeric(sub_well["DEPTH"], errors="coerce") sub_full = sub_well[(d_col >= d_min) & (d_col <= d_max)].copy() if sub_full.empty: sub_full = sub_well.copy() + sub_full = ( + sub_full.dropna(subset=["DEPTH"]).sort_values(by="DEPTH").reset_index(drop=True) + ) + # Guard against uninterpreted / empty wells: verify valid curves exist in target window + valid_curves = 0 + for curve in ("GR", "RT", "NPHI", "RHOB", "PHIT", "PHIE", "PERM", "SWT"): + s = _get_numeric_series(sub_full, curve) + if s.notna().sum() >= 5: + valid_curves += 1 + if valid_curves < 2: + return None + d_full = pd.to_numeric(sub_full["DEPTH"], errors="coerce").to_numpy(dtype=float) cut = cutoffs or {"vsh": 0.40, "phie": 0.08, "sw": 0.65} @@ -1667,21 +2026,29 @@ def generate_well_cpi_plot( cp = np.where(cp > 1.0, cp / 100.0, cp) cm = np.isfinite(cp) & (cp > 0) if np.any(cm): - ax.scatter(cp[cm], d_full[cm], color="#dc2626", s=18, zorder=5, label="CPORE") + ax.scatter( + cp[cm], d_full[cm], color="#dc2626", s=18, zorder=5, label="CPORE" + ) ax.set_xlim(0.0, 0.50) ax.set_xlabel("Porosity Φ (v/v)", fontsize=8.5, fontweight="bold", color="#1d4ed8") ax.set_title("Track 4\nPorosity & Core", fontsize=8.5, fontweight="bold") # Track 5: Permeability & Core CPERM ax = axes[4] - perm = _get_numeric_series(sub, "PERM", "PERM_TRANS").clip(lower=1e-2).to_numpy(dtype=float) + perm = ( + _get_numeric_series(sub, "PERM", "PERM_TRANS") + .clip(lower=1e-2) + .to_numpy(dtype=float) + ) if np.isfinite(perm).any(): ax.plot(perm, d, color="#059669", lw=1.1, label="PERM") if "CPERM" in sub_full.columns: ck = _get_numeric_series(sub_full, "CPERM").to_numpy(dtype=float) km = np.isfinite(ck) & (ck > 0) if np.any(km): - ax.scatter(ck[km], d_full[km], color="#b91c1c", s=18, zorder=5, label="CPERM") + ax.scatter( + ck[km], d_full[km], color="#b91c1c", s=18, zorder=5, label="CPERM" + ) ax.set_xscale("log") ax.set_xlim(1e-2, 1e4) ax.set_xlabel("PERM (mD)", fontsize=8.5, fontweight="bold", color="#059669") @@ -1743,7 +2110,12 @@ def generate_well_cpi_plot( for idx in change_idx[:14]: z_depth = float(d[idx]) z_label = str(z_series.iloc[idx]).strip() - if not z_label or z_label.upper() in ("NAN", "MAIN RESERVOIR", "UNKNOWN", "ALL"): + if not z_label or z_label.upper() in ( + "NAN", + "MAIN RESERVOIR", + "UNKNOWN", + "ALL", + ): continue for a_i in axes: a_i.axhline(z_depth, color="k", linestyle="--", lw=0.85, alpha=0.85) diff --git a/quick_pp/saturation.py b/quick_pp/saturation.py index 72799946..e8fdff8e 100644 --- a/quick_pp/saturation.py +++ b/quick_pp/saturation.py @@ -6,6 +6,8 @@ above free water level (SHF) calculations. """ +from typing import Dict, Optional + import matplotlib.pyplot as plt import matplotlib.ticker as ticker import numpy as np @@ -1697,6 +1699,7 @@ def optimize_fwl( non_reservoir_flags=None, loss: str = "rmse", pore_volume_weighted: bool = False, + pay_weighted: bool = False, equal_well_weights: bool = False, mdt_fwl: float = None, pressure_df: pd.DataFrame = None, @@ -1961,24 +1964,40 @@ def optimize_fwl( if not np.any(res_mask): res_mask = np.isfinite(depth_arr) & np.isfinite(sw_log_arr) - # Compute sample weights (pore-volume weighting & equal well weighting) + # Compute sample weights (pore-volume weighting, pay-weighting & equal well weighting) weights = np.ones(len(work_df), dtype=float) if pore_volume_weighted: weights = weights * np.maximum(phit_arr, 1e-4) + if pay_weighted: + # Hydrocarbon pay confidence weighting: phi * (1 - Sw) + # Floor at 0.01 so wet intervals retain gentle regularization without overpowering the oil column + bvh_proxy = np.maximum(phit_arr * (1.0 - sw_log_arr), 0.01) + weights = weights * bvh_proxy if equal_well_weights: for w in np.unique(wells_arr[res_mask]): w_m = res_mask & (wells_arr == w) - n_w = np.sum(w_m) - if n_w > 0: - weights[w_m] = weights[w_m] / float(n_w) + sum_w = np.sum(weights[w_m]) + if sum_w > 0: + weights[w_m] = weights[w_m] / sum_w w_res = weights[res_mask] w_res = w_res / np.maximum(np.sum(w_res), 1e-12) - # 4. Determine FWL search bounds + # 4. Determine FWL search bounds and physical constraints (FWL >= ODT) d_min = float(np.nanmin(depth_arr[res_mask])) d_max = float(np.nanmax(depth_arr[res_mask])) d_span = max(d_max - d_min, 50.0) + # Detect lowest confirmed hydrocarbon depth (Oil Down To / Gas Down To) + hc_sample_mask = ( + res_mask + & (sw_log_arr < 0.75) + & (phit_arr >= max(phit_cutoff, 0.05)) + ) + if is_leverett: + hc_sample_mask = hc_sample_mask & (perm_arr >= max(perm_cutoff, 0.1)) + + odt = float(np.nanmax(depth_arr[hc_sample_mask])) if np.any(hc_sample_mask) else None + if fwl_bounds is not None: fwl_low, fwl_high = float(fwl_bounds[0]), float(fwl_bounds[1]) elif mdt_fwl is not None and mdt_tolerance is not None: @@ -1991,8 +2010,12 @@ def optimize_fwl( fwl_low = min(fwl_low, float(mdt_fwl) - 0.25 * d_span) fwl_high = max(fwl_high, float(mdt_fwl) + 0.25 * d_span) + # Physical constraint: Free Water Level (Pc = 0) must be at or below lowest confirmed hydrocarbon (ODT) + if odt is not None: + fwl_low = max(fwl_low, odt) + if fwl_high <= fwl_low: - fwl_high = fwl_low + 100.0 + fwl_high = fwl_low + max(0.20 * d_span, 50.0) loss_clean = loss.strip().lower() @@ -2160,12 +2183,16 @@ def _joint_obj_sh(x): dtype=float, ) + # Evaluate transition zone on representative pay samples (so tight shaly samples don't inflate TTZ) + tz_phit = phit_arr[hc_sample_mask] if np.any(hc_sample_mask) else phit_arr[res_mask] + tz_perm = perm_arr[hc_sample_mask] if np.any(hc_sample_mask) else perm_arr[res_mask] + tz_info = analyze_transition_zone( fwl=opt_fwl, model=model, shm_params=active_shm_params, - phit=phit_arr[res_mask], - perm=perm_arr[res_mask], + phit=tz_phit, + perm=tz_perm, ift=ift, theta=theta, gw=gw, @@ -2345,8 +2372,21 @@ def _joint_obj_sh(x): z_span = max(z_d_max - z_d_min, 25.0) z_low = z_d_min - 0.15 * z_span z_high = z_d_max + 0.60 * z_span + + # Physical constraint per zone: FWL >= ODT within this zone + z_hc_mask = ( + z_mask + & (sw_log_arr < 0.75) + & (phit_arr >= max(phit_cutoff, 0.05)) + ) + if is_leverett: + z_hc_mask = z_hc_mask & (perm_arr >= max(perm_cutoff, 0.1)) + if np.any(z_hc_mask): + z_odt = float(np.nanmax(depth_arr[z_hc_mask])) + z_low = max(z_low, z_odt) + if z_high <= z_low: - z_high = z_low + 50.0 + z_high = z_low + max(0.25 * z_span, 30.0) z_w = weights[z_mask] z_w = z_w / np.maximum(np.sum(z_w), 1e-12) @@ -2387,12 +2427,14 @@ def _joint_obj_sh(x): z_phi = phit_arr[z_mask] z_perm = perm_arr[z_mask] + z_tz_phi = phit_arr[z_hc_mask] if np.any(z_hc_mask) else z_phi + z_tz_perm = perm_arr[z_hc_mask] if np.any(z_hc_mask) else z_perm z_tz = analyze_transition_zone( fwl=fwl_zone_opt, model=model, shm_params=active_shm_params, - phit=z_phi, - perm=z_perm, + phit=z_tz_phi, + perm=z_tz_perm, ift=ift, theta=theta, gw=gw, @@ -2486,6 +2528,17 @@ def _joint_obj_sh(x): float(opt_fwl - float(mdt_fwl)) if mdt_fwl is not None else None ) + # Extract comprehensive fluid limits and contacts per well and zone + contacts_df = extract_fluid_contacts_and_limits( + df=work_df, + depth_col=depth_col, + sw_col=sw_col, + well_col=well_col, + zone_col=zone_col, + pressure_df=pressure_df, + fwl_dict=fwl_by_zone or {"default": opt_fwl}, + ) + return { "fwl": opt_fwl, "fwl_by_zone": fwl_by_zone, @@ -2504,10 +2557,363 @@ def _joint_obj_sh(x): "well_metrics": well_metrics_df, "zone_metrics": zone_metrics_df, "grid_profile": grid_profile_df, + "fluid_contacts": contacts_df, "df": work_df, } +def extract_fluid_contacts_and_limits( + df: pd.DataFrame, + depth_col: str = "TVD", + sw_col: str = "SWT", + phit_col: str = "PHIT", + vsh_col: str = "VSH", + well_col: str = "WELL_NAME", + zone_col: Optional[str] = "ZONES", + fault_block_col: Optional[str] = "FAULT_BLOCK", + pressure_df: Optional[pd.DataFrame] = None, + fwl_dict: Optional[Dict[str, float]] = None, + sw_pay_cutoff: float = 0.65, + sw_water_cutoff: float = 0.90, + phit_cutoff: float = 0.05, + vsh_cutoff: float = 0.50, + min_sand_thickness: float = 1.0, + min_sand_samples: int = 5, + require_pay: bool = True, +) -> pd.DataFrame: + """Extract comprehensive petrophysical fluid limits and contacts per well and per zone. + + Identifies and classifies fluid contacts and transition limits from log saturation profiles, + formation pressure tests (MDT/RFT/FPT), and zonal intervals: + - Gas limits & contacts: GUT (Gas Up To), GDT (Gas Down To), GOC (Gas-Oil Contact), GWC (Gas-Water Contact) + - Oil limits & contacts: OUT (Oil Up To), ODT (Oil Down To), OWC (Oil-Water Contact) + - Water limits & contacts: WUT (Water Up To), FWL (Free Water Level) + Note: WDT (Water Down To) is explicitly excluded as it carries no petrophysical utility. + + Args: + df (pd.DataFrame): Well log DataFrame containing depth, water saturation, porosity, and clay/shale curves. + depth_col (str, optional): Depth column name (TVDSS, TVD, or DEPTH). Defaults to "TVD". + sw_col (str, optional): Water saturation column name. Defaults to "SWT". + phit_col (str, optional): Total porosity column name. Defaults to "PHIT". + vsh_col (str, optional): Shale/clay volume column name. Defaults to "VSH". + well_col (str, optional): Well identifier column name. Defaults to "WELL_NAME". + zone_col (str, optional): Reservoir zonation column name. Defaults to "ZONES". + fault_block_col (str, optional): Fault block column name. Defaults to "FAULT_BLOCK". + pressure_df (pd.DataFrame, optional): Formation pressure test DataFrame with columns ['DEPTH', 'PRESSURE', 'FLUID_TYPE']. + fwl_dict (dict[str, float], optional): Optional dictionary mapping zone/well names to Free Water Level depths. + sw_pay_cutoff (float, optional): Maximum water saturation threshold for hydrocarbon pay. Defaults to 0.65. + sw_water_cutoff (float, optional): Minimum water saturation threshold for 100% water zone. Defaults to 0.90. + phit_cutoff (float, optional): Minimum porosity threshold for reservoir rock. Defaults to 0.05. + vsh_cutoff (float, optional): Maximum shale volume threshold for reservoir rock. Defaults to 0.50. + min_sand_thickness (float, optional): Minimum vertical net sand thickness required to declare a contact. Defaults to 1.0. + min_sand_samples (int, optional): Minimum consecutive reservoir samples. Defaults to 5. + require_pay (bool, optional): Whether to require active pay intervals when establishing contacts. Defaults to True. + + Returns: + pd.DataFrame: Table of detected fluid contacts and limits with columns: + ['well_name', 'zone', 'fault_block', 'contact_type', 'depth', 'source', 'remarks']. + """ + contact_cols = [ + "well_name", + "zone", + "fault_block", + "contact_type", + "depth", + "source", + "remarks", + ] + if df is None or len(df) == 0: + return pd.DataFrame(columns=contact_cols) + + work_df = df.copy() + + # Depth column resolution (prefer TVDSS) + z_col = None + for cand in ("TVDSS", "DEPT_TVDSS", "DEPTH_TVDSS", "TVD_SS", depth_col, "TVD", "DEPTH"): + if cand in work_df.columns and pd.to_numeric(work_df[cand], errors="coerce").notna().sum() >= 2: + z_col = cand + break + if z_col is None: + return pd.DataFrame(columns=contact_cols) + + # Coerce depth to positive TVDSS + z_vals = pd.to_numeric(work_df[z_col], errors="coerce") + if (z_vals.dropna() < 0).mean() > 0.8: + z_vals = z_vals.abs() + work_df["_DEPTH_CALC"] = z_vals + + # Resolve well, zone, fault block + w_col = next((c for c in (well_col, "WELL_NAME", "WELL", "UWI") if c in work_df.columns), None) + if w_col is None: + work_df["_WELL_NAME"] = "Field" + w_col = "_WELL_NAME" + + z_name_col = next((c for c in (zone_col, "ZONES", "ZONE", "FORMATION") if c in work_df.columns), None) + fb_col = next((c for c in (fault_block_col, "FAULT_BLOCK", "FAULTBLOCK", "BLOCK") if c in work_df.columns), None) + + # Saturation, porosity, vsh + s_col = next((c for c in (sw_col, "SW", "SWT", "SWE", "SW_WS", "SW_ARCHIE") if c in work_df.columns), None) + if s_col is None: + return pd.DataFrame(columns=contact_cols) + work_df["_SW_CALC"] = pd.to_numeric(work_df[s_col], errors="coerce").clip(0.0, 1.0) + + p_col = next((c for c in (phit_col, "PHIT", "POR", "PHIE", "POROSITY") if c in work_df.columns), None) + if p_col is not None: + work_df["_PHIT_CALC"] = pd.to_numeric(work_df[p_col], errors="coerce").clip(0.0, 0.5) + else: + work_df["_PHIT_CALC"] = 0.20 + + v_col = next((c for c in (vsh_col, "VSH", "VCL", "VSHALE", "GR_NORM") if c in work_df.columns), None) + if v_col is not None: + work_df["_VSH_CALC"] = pd.to_numeric(work_df[v_col], errors="coerce").clip(0.0, 1.0) + else: + work_df["_VSH_CALC"] = 0.0 + + # Gas indicator (either explicit GAS flag/RHOB crossover or RHO_HC) + has_gas_curve = "GAS_FLAG" in work_df.columns or "IS_GAS" in work_df.columns + + records = [] + + # Group by well and optionally zone + group_cols = [w_col] + if z_name_col: + group_cols.append(z_name_col) + + for keys, grp in work_df.groupby(group_cols): + if not isinstance(keys, tuple): + keys = (keys,) + well_val = str(keys[0]) + zone_val = str(keys[1]) if len(keys) > 1 and pd.notna(keys[1]) else None + fb_val = str(grp[fb_col].dropna().iloc[0]) if fb_col and fb_col in grp.columns and grp[fb_col].notna().any() else None + + valid = grp[ + grp["_DEPTH_CALC"].notna() + & grp["_SW_CALC"].notna() + & (grp["_PHIT_CALC"] >= phit_cutoff) + & (grp["_VSH_CALC"] <= vsh_cutoff) + ].sort_values(by="_DEPTH_CALC") + + # Petrophysical Sand Validation: + # A zone without minimum reservoir sand cannot contain or produce fluids. + # Eliminates non-reservoir shale/tuff/marl formations (e.g. Sele, Balder Tuff, Montrose shale). + if len(valid) < min_sand_samples: + continue + + depths = valid["_DEPTH_CALC"].to_numpy() + sand_thickness = float(np.max(depths) - np.min(depths)) + if sand_thickness < min_sand_thickness and len(valid) < 10: + continue + + sws = valid["_SW_CALC"].to_numpy() + is_hc = sws < sw_pay_cutoff + is_water = sws >= sw_water_cutoff + + # If require_pay is True, do not register contacts for 100% wet / non-pay zones + # (prevents auto-setting WUT at the top of purely wet zones where no hydrocarbon exists) + if require_pay and not np.any(is_hc): + continue + + # Gas vs Oil separation if gas indicator available + if has_gas_curve: + gas_col = "GAS_FLAG" if "GAS_FLAG" in valid.columns else "IS_GAS" + is_gas = is_hc & (valid[gas_col].to_numpy() > 0.5) + is_oil = is_hc & ~is_gas + else: + is_gas = np.zeros(len(depths), dtype=bool) + is_oil = is_hc + + # 1. Gas Up To (GUT) & Gas Down To (GDT) + if np.any(is_gas): + gut = float(np.min(depths[is_gas])) + gdt = float(np.max(depths[is_gas])) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "GUT", + "depth": gut, + "source": "Log Evaluation (Gas column top)", + "remarks": f"Proved Gas Up To at {gut:.1f} m TVDss in {zone_val or 'reservoir'}", + }) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "GDT", + "depth": gdt, + "source": "Log Evaluation (Lowest proved gas pay)", + "remarks": f"Proved Gas Down To at {gdt:.1f} m TVDss in {zone_val or 'reservoir'}", + }) + + # 2. Oil Up To (OUT) & Oil Down To (ODT) + if np.any(is_oil): + out = float(np.min(depths[is_oil])) + odt = float(np.max(depths[is_oil])) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "OUT", + "depth": out, + "source": "Log Evaluation (Oil column top)", + "remarks": f"Proved Oil Up To at {out:.1f} m TVDss in {zone_val or 'reservoir'}", + }) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "ODT", + "depth": odt, + "source": "Log Evaluation (Lowest proved oil pay, Sw < 0.65)", + "remarks": f"Proved Oil Down To at {odt:.1f} m TVDss in {zone_val or 'reservoir'} ({well_val})", + }) + + # Lowest confirmed hydrocarbon base (ODT or GDT) + hc_base = None + if np.any(is_oil): + hc_base = float(np.max(depths[is_oil])) + elif np.any(is_gas): + hc_base = float(np.max(depths[is_gas])) + + # 3. Water Up To (WUT) - shallowest proved water in the basal water leg + # Note: WDT is explicitly omitted + if hc_base is not None: + # When hydrocarbon is present in the zone, WUT is the shallowest proved 100% water BELOW the HC column + basal_water_mask = is_water & (depths >= hc_base) + if np.any(basal_water_mask): + wut = float(np.min(depths[basal_water_mask])) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "WUT", + "depth": wut, + "source": "Log Evaluation (100% Sw water leg below HC)", + "remarks": f"Proved Water Up To at {wut:.1f} m TVDss in {zone_val or 'reservoir'} (below ODT {hc_base:.1f}m)", + }) + else: + # 100% wet reservoir interval without confirmed hydrocarbon pay (only reached if require_pay=False) + if np.any(is_water): + wut = float(np.min(depths[is_water])) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "WUT", + "depth": wut, + "source": "Log Evaluation (100% Water-bearing Sand)", + "remarks": f"Proved Water-bearing Sand top at {wut:.1f} m TVDss in {zone_val or 'reservoir'} (aquifer interval)", + }) + + # 4. Inferred Discrete Contacts (GOC, GWC, OWC) + if np.any(is_gas) and np.any(is_oil): + gdt_val = float(np.max(depths[is_gas])) + out_val = float(np.min(depths[is_oil])) + if gdt_val <= out_val: + goc = round(float((gdt_val + out_val) / 2.0), 1) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "GOC", + "depth": goc, + "source": "Calculated (GDT-OUT midpoint bracket)", + "remarks": f"Gas-Oil Contact bracketed between GDT ({gdt_val:.1f}m) and OUT ({out_val:.1f}m)", + }) + elif np.any(is_gas) and np.any(is_water): + gdt_val = float(np.max(depths[is_gas])) + wut_val = float(np.min(depths[is_water])) + if gdt_val <= wut_val: + gwc = round(float((gdt_val + wut_val) / 2.0), 1) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "GWC", + "depth": gwc, + "source": "Calculated (GDT-WUT midpoint bracket)", + "remarks": f"Gas-Water Contact bracketed between GDT ({gdt_val:.1f}m) and WUT ({wut_val:.1f}m)", + }) + + if np.any(is_oil) and np.any(is_water): + odt_val = float(np.max(depths[is_oil])) + wut_val = float(np.min(depths[is_water])) + if odt_val <= wut_val: + owc = round(float((odt_val + wut_val) / 2.0), 1) + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "OWC", + "depth": owc, + "source": "Calculated (ODT-WUT midpoint bracket)", + "remarks": f"Oil-Water Contact bracketed between ODT ({odt_val:.1f}m) and WUT ({wut_val:.1f}m)", + }) + + # 5. Zonal or Well FWL if supplied + if fwl_dict: + z_fwl = fwl_dict.get(zone_val) or fwl_dict.get(well_val) or fwl_dict.get("default") + if z_fwl is not None: + records.append({ + "well_name": well_val, + "zone": zone_val, + "fault_block": fb_val, + "contact_type": "FWL", + "depth": round(float(z_fwl), 1), + "source": "FWL Optimizer (Capillary Pc = 0 datum)", + "remarks": f"Optimized Free Water Level (Pc = 0 datum) for {zone_val or 'zone'}", + }) + + # 6. Pressure Test Integration (if pressure_df provided) + if pressure_df is not None and not pressure_df.empty: + p_df = pressure_df.copy() + if "TVD" in p_df.columns or "DEPTH" in p_df.columns: + p_z_col = "TVD" if "TVD" in p_df.columns else "DEPTH" + for _, prow in p_df.iterrows(): + p_fl = str(prow.get("FLUID_TYPE") or prow.get("FLUID") or "").upper() + p_wn = str(prow.get("WELL_NAME") or prow.get("WELL") or "Field") + p_z = str(prow.get("ZONE") or prow.get("ZONES") or "") or None + p_fb = str(prow.get("FAULT_BLOCK") or "") or None + p_d = float(prow[p_z_col]) if pd.notna(prow[p_z_col]) else None + if p_d is None: + continue + if "GAS" in p_fl: + records.append({ + "well_name": p_wn, + "zone": p_z, + "fault_block": p_fb, + "contact_type": "GDT", + "depth": p_d, + "source": "MDT Pre-test (Gas gradient point)", + "remarks": f"Proved gas by pressure testing at {p_d} m TVDss", + }) + elif "OIL" in p_fl: + records.append({ + "well_name": p_wn, + "zone": p_z, + "fault_block": p_fb, + "contact_type": "ODT", + "depth": p_d, + "source": "MDT Pre-test (Oil gradient point)", + "remarks": f"Proved oil by pressure testing at {p_d} m TVDss", + }) + elif "WATER" in p_fl: + records.append({ + "well_name": p_wn, + "zone": p_z, + "fault_block": p_fb, + "contact_type": "WUT", + "depth": p_d, + "source": "MDT Pre-test (Water gradient point)", + "remarks": f"Proved formation water by pressure testing at {p_d} m TVDss", + }) + + out_df = pd.DataFrame(records, columns=contact_cols) + if not out_df.empty: + out_df = out_df.sort_values(by=["well_name", "depth"]).reset_index(drop=True) + return out_df + + def plot_fwl_reconciliation( opt_result: dict, depth_col: str = "DEPTH", diff --git a/scripts/README.md b/scripts/README.md index 56d157c9..ab5f2333 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -12,7 +12,8 @@ Running `python scripts/release.py --bump ` automatically updates: 3. [`CITATION.cff`](../CITATION.cff) (`version: `) 4. [`quick_pp/app/frontend/package.json`](../quick_pp/app/frontend/package.json) (`"version": ""`) 5. [`quick_pp/app/frontend/src/lib/state/auth.svelte.ts`](../quick_pp/app/frontend/src/lib/state/auth.svelte.ts) (`appVersion` fallback) -6. [`uv.lock`](../uv.lock) (via `uv lock`) +6. [`quick_pp/app/frontend/src/lib/components/shell/WorkflowGuideSheet.svelte`](../quick_pp/app/frontend/src/lib/components/shell/WorkflowGuideSheet.svelte) (`quick_pp SSOT Technical Reference v`) +7. [`uv.lock`](../uv.lock) (via `uv lock`) > **Note:** Remember to also add release notes for the new version in [`docs/community/changelog.md`](../docs/community/changelog.md). diff --git a/scripts/release.py b/scripts/release.py index 6eadeeb8..1af62b61 100644 --- a/scripts/release.py +++ b/scripts/release.py @@ -64,6 +64,10 @@ FRONTEND_DIR / "src" / "lib" / "state" / "auth.svelte.ts", r"(appVersion\s*=\s*\$state\(')([^']+)('\))", ), + "quick_pp/app/frontend/src/lib/components/shell/WorkflowGuideSheet.svelte": ( + FRONTEND_DIR / "src" / "lib" / "components" / "shell" / "WorkflowGuideSheet.svelte", + r"(quick_pp SSOT Technical Reference v)([^<]+)()", + ), "uv.lock": ( ROOT / "uv.lock", r'(name\s*=\s*"quick-pp"\r?\nversion\s*=\s*")([^"]+)(")', @@ -381,6 +385,15 @@ def main() -> None: if target.exists(): shutil.rmtree(target) + # Clean ephemeral database and report static files generated by tests or dev runs + for db_pattern in ("*.db*", "*.sqlite*"): + for f in (ROOT / "quick_pp").rglob(db_pattern): + if f.is_file(): + try: + f.unlink() + except Exception: + pass + # Ensure ephemeral report static outputs generated by tests are cleaned before building wheel reports_static_dir = ROOT / "quick_pp" / "app" / "backend" / "static" / "reports" if reports_static_dir.exists(): diff --git a/tests/test_assistant_graceful_handling.py b/tests/test_assistant_graceful_handling.py new file mode 100644 index 00000000..03f686e0 --- /dev/null +++ b/tests/test_assistant_graceful_handling.py @@ -0,0 +1,79 @@ +from quick_pp.app.backend.services.chat import ( + PRESET_QUICK_ACTIONS, + SYSTEM_PROMPT, + AgentChatRequest, + _handle_unrecognized_native_prompt, + _plan_native_tool_calls, +) + + +def test_balmoral_sandstone_does_not_randomly_run_tools(): + """Verify that external regional queries do not trigger random tools or blind fallbacks.""" + req = AgentChatRequest( + message="what is the average porosity in balmoral sandstone", + project_id=1, + well_name="30-7a-8", + provider="native_mcp", + ) + planned = _plan_native_tool_calls(req) + # Must NOT plan any tools (no random inspect, no conformal bounds, no irrelevant SSOT chapters) + assert planned == [] + + +def test_unrecognized_native_prompt_highlights_low_confidence_and_quick_actions(): + """Verify that unhandled prompts return low confidence warning and preset quick actions.""" + req = AgentChatRequest( + message="what is the average porosity in balmoral sandstone", + well_name="30-7a-8", + provider="native_mcp", + ) + reply, actions = _handle_unrecognized_native_prompt(req) + + # Must highlight answer confidence + assert "Answer Confidence: Low" in reply + assert "Why the agent is not confident in this answer" in reply + assert "Balmoral" in reply or "balmoral" in reply + + # Must return all 7 preset quick actions + assert len(actions) == 7 + assert len(actions) == len(PRESET_QUICK_ACTIONS) + assert any(act["label"] == "Inspect Well" for act in actions) + assert any(act["label"] == "Run QAQC" for act in actions) + assert any(act["label"] == "Multi-Mineral" for act in actions) + assert any(act["label"] == "Conformal Bounds" for act in actions) + assert "Preset Quick Actions" in reply + + +def test_valid_petrophysical_prompts_still_route_correctly(): + """Verify that legitimate petrophysical commands still trigger the correct tools.""" + req_inspect = AgentChatRequest( + message="Inspect this well and check curve inventory", + project_id=1, + well_name="30-7a-8", + provider="native_mcp", + ) + planned_inspect = _plan_native_tool_calls(req_inspect) + assert len(planned_inspect) == 1 + assert planned_inspect[0]["name"] == "inspect_well" + + req_qaqc = AgentChatRequest( + message="Run QAQC badhole detection on this well", + project_id=1, + well_name="30-7a-8", + provider="native_mcp", + ) + planned_qaqc = _plan_native_tool_calls(req_qaqc) + assert len(planned_qaqc) == 1 + assert planned_qaqc[0]["name"] == "run_well_qaqc" + + +def test_system_prompt_mandates_answer_confidence_assessment(): + """Verify that the LLM system prompt conditions the agent to assess confidence and highlight gaps in answers.""" + assert "SELF-ASSESSED ANSWER CONFIDENCE & EPISTEMIC HUMILITY" in SYSTEM_PROMPT + assert "High Confidence" in SYSTEM_PROMPT + assert "Medium Confidence" in SYSTEM_PROMPT + assert "Low Confidence" in SYSTEM_PROMPT + assert "Answer Confidence: [Low / Medium]" in SYSTEM_PROMPT + assert "Why I am not confident in this answer" in SYSTEM_PROMPT + assert "Balmoral" in SYSTEM_PROMPT + diff --git a/tests/test_fluid_contacts_and_limits.py b/tests/test_fluid_contacts_and_limits.py new file mode 100644 index 00000000..6b4e0d43 --- /dev/null +++ b/tests/test_fluid_contacts_and_limits.py @@ -0,0 +1,427 @@ +import numpy as np +import pandas as pd +import pytest + +from quick_pp.database import objects as db_objects +from quick_pp.database.db_connector import DBConnector +from quick_pp.saturation import extract_fluid_contacts_and_limits + + +def test_extract_fluid_contacts_oil_water_column(): + """Verify that ODT, OUT, OWC, WUT, and FWL are correctly extracted and WDT is excluded.""" + depths = np.linspace(2900, 3050, 31) # 5m steps: 2900, 2905, ..., 2990, 2995, 3000, 3005, 3010, ... + phit = np.full_like(depths, 0.22) + vsh = np.full_like(depths, 0.05) + sw = np.ones_like(depths) + for i, d in enumerate(depths): + if d <= 2990: + sw[i] = 0.20 # clean oil pay + elif d < 3020: + sw[i] = 0.20 + (d - 2990) * 0.025 # transition zone: 2995->0.325, 3000->0.45, 3005->0.575 (<0.65), 3010->0.70 (>0.65) + else: + sw[i] = 1.00 # 100% water leg (d >= 3020) + + df = pd.DataFrame({ + "WELL_NAME": ["Well-30/7a-8"] * len(depths), + "ZONES": ["Balmoral Sandstone"] * len(depths), + "FAULT_BLOCK": ["Main"] * len(depths), + "TVDSS": depths, + "SWT": sw, + "PHIT": phit, + "VSH": vsh, + }) + + contacts = extract_fluid_contacts_and_limits( + df=df, + fwl_dict={"Balmoral Sandstone": 3025.0}, + sw_pay_cutoff=0.65, + sw_water_cutoff=0.90, + ) + + assert not contacts.empty + types = contacts["contact_type"].tolist() + + # Verify WDT is strictly NOT present + assert "WDT" not in types + + # Verify oil limits + assert "OUT" in types + assert "ODT" in types + out_row = contacts[contacts["contact_type"] == "OUT"].iloc[0] + odt_row = contacts[contacts["contact_type"] == "ODT"].iloc[0] + assert out_row["depth"] == 2900.0 + # Lowest pay point with Sw < 0.65 is at 3005m + assert odt_row["depth"] == 3005.0 + assert "Oil column top" in out_row["source"] + assert "Lowest proved oil pay" in odt_row["source"] + assert "Well-30/7a-8" in odt_row["remarks"] + + # Verify water limit: first 100% water (Sw >= 0.90) is at 3020m + assert "WUT" in types + wut_row = contacts[contacts["contact_type"] == "WUT"].iloc[0] + assert wut_row["depth"] == 3020.0 + assert "100% Sw water leg" in wut_row["source"] + + # Verify OWC bracket: midpoint between ODT (3005) and WUT (3020) = 3012.5m + assert "OWC" in types + owc_row = contacts[contacts["contact_type"] == "OWC"].iloc[0] + assert owc_row["depth"] == pytest.approx(3012.5, abs=0.5) + assert "ODT-WUT midpoint" in owc_row["source"] + + # Verify FWL + assert "FWL" in types + fwl_row = contacts[contacts["contact_type"] == "FWL"].iloc[0] + assert fwl_row["depth"] == 3025.0 + assert "FWL Optimizer" in fwl_row["source"] + + # Verify fault block and zone are captured + assert all(contacts["fault_block"] == "Main") + assert all(contacts["zone"] == "Balmoral Sandstone") + + +def test_extract_fluid_contacts_gas_cap(): + """Verify GUT, GDT, GOC, and OUT are extracted when a gas cap exists above oil.""" + depths = np.linspace(2500, 2600, 21) + df = pd.DataFrame({ + "WELL_NAME": ["Well-GasCap"] * len(depths), + "ZONES": ["GasZone"] * len(depths), + "TVDSS": depths, + "SWT": [0.15] * 21, + "PHIT": [0.25] * 21, + "VSH": [0.02] * 21, + "GAS_FLAG": [1 if d <= 2540 else 0 for d in depths], + "OIL_FLAG": [1 if d > 2540 else 0 for d in depths], + }) + + contacts = extract_fluid_contacts_and_limits(df=df) + types = contacts["contact_type"].tolist() + + assert "GUT" in types + assert "GDT" in types + assert "OUT" in types + assert "ODT" in types + assert "GOC" in types + assert "WDT" not in types + + gdt_row = contacts[contacts["contact_type"] == "GDT"].iloc[0] + out_row = contacts[contacts["contact_type"] == "OUT"].iloc[0] + goc_row = contacts[contacts["contact_type"] == "GOC"].iloc[0] + + assert gdt_row["depth"] == 2540.0 + assert out_row["depth"] == 2545.0 + assert goc_row["depth"] == pytest.approx(2542.5, abs=0.5) + assert "GDT-OUT midpoint" in goc_row["source"] + + +def test_database_contacts_persistence(): + """Test saving and retrieving fluid contacts with zone, fault_block, source, and remarks.""" + db_conn = DBConnector(db_url="sqlite:///:memory:") + db_conn.setup_db() + with db_conn.get_session() as session: + proj = db_objects.Project(session, name="TestContactsProj") + proj.save() + well = db_objects.Well(session, project_id=proj.project_id, name="TestWell", uwi="TW-01") + well.save() + + test_contacts = [ + { + "contact_type": "ODT", + "depth": 2995.0, + "zone": "Balmoral Sandstone", + "fault_block": "FB-West", + "source": "Log Interpretation (Lowest Sw pay)", + "remarks": "Deepest proved oil pay at 2995.0 m TVDSS", + }, + { + "contact_type": "WUT", + "depth": 3030.0, + "zone": "Balmoral Sandstone", + "fault_block": "FB-West", + "source": "Log Interpretation (100% Sw)", + "remarks": "Shallowest 100% water at 3030.0 m TVDSS", + }, + { + "contact_type": "FWL", + "depth": 3025.0, + "zone": "Balmoral Sandstone", + "fault_block": "FB-West", + "source": "Multi-Well SHF FWL Optimizer", + "remarks": "Zero capillary pressure level (Pc=0)", + }, + ] + + well.add_fluid_contacts(test_contacts, replace=True) + retrieved_df = well.get_fluid_contacts() + + assert len(retrieved_df) == 3 + assert set(retrieved_df["contact_type"]) == {"ODT", "WUT", "FWL"} + assert set(retrieved_df["zone"]) == {"Balmoral Sandstone"} + assert set(retrieved_df["fault_block"]) == {"FB-West"} + assert all(retrieved_df["source"].notna()) + assert all(retrieved_df["remarks"].notna()) + + +def test_fdp_report_saturation_contacts_rendering(): + """Verify that 06_saturation_contacts.md.j2 renders contacts table and proved column narrative without error.""" + import jinja2 + template_dir = r"quick_pp/app/backend/template/reports" + env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir)) + tmpl = env.get_template("sections/06_saturation_contacts.md.j2") + + sample_context = { + "report_type": "FIELD_FDP", + "sw_params": { + "model_name": "Waxman-Smits Shaly-Sand Model", + "fwl": 3025.0, + "salinity": 35000, + "rw": 0.08, + "temp": 75.0, + "a": 1.0, + "m": 2.0, + "n": 2.0, + }, + "fluid_contacts_rows": [ + { + "well_name": "30/7a-8", + "zone": "Balmoral Sandstone", + "fault_block": "Main", + "contact_type": "OUT", + "depth": 2950.0, + "source": "Log Interpretation (Oil column top)", + "remarks": "Shallowest proved oil pay", + }, + { + "well_name": "30/7a-8", + "zone": "Balmoral Sandstone", + "fault_block": "Main", + "contact_type": "ODT", + "depth": 2995.0, + "source": "Log Interpretation (Lowest proved oil pay)", + "remarks": "Deepest proved oil pay at 2995.0 m TVDSS", + }, + { + "well_name": "30/7a-8", + "zone": "Balmoral Sandstone", + "fault_block": "Main", + "contact_type": "OWC", + "depth": 3005.0, + "source": "Log Bracket (ODT-WUT midpoint)", + "remarks": "Interpreted OWC bracketed between proved ODT and WUT", + }, + { + "well_name": "30/7a-8", + "zone": "Balmoral Sandstone", + "fault_block": "Main", + "contact_type": "WUT", + "depth": 3020.0, + "source": "Log Interpretation (100% Sw water leg)", + "remarks": "Shallowest 100% water-bearing sand", + }, + { + "well_name": "30/7a-8", + "zone": "Balmoral Sandstone", + "fault_block": "Main", + "contact_type": "FWL", + "depth": 3025.0, + "source": "Multi-Well SHF FWL Optimizer", + "remarks": "Zero capillary pressure level (Pc=0)", + }, + ], + "fluid_contacts_summary": [ + { + "zone": "Balmoral Sandstone", + "fault_block": "Main", + "gut": None, + "gdt": None, + "out": 2950.0, + "odt": 2995.0, + "wut": 3020.0, + "goc": None, + "gwc": None, + "owc": 3005.0, + "fwl": 3025.0, + } + ], + "assets": {}, + "include_ai_commentary": False, + } + + rendered = tmpl.render(**sample_context) + assert "Multi-Well & Zonal Fluid Contacts" in rendered + assert "30/7a-8" in rendered + assert "Balmoral Sandstone" in rendered + assert "ODT" in rendered + assert "Proved Oil Column:" in rendered + assert "Free Water Level (FWL):" in rendered + assert "WDT" not in rendered + + +def test_extract_fluid_contacts_wut_never_shallower_than_out(): + """Verify that when a wet interval exists at the top of a zone (shallower than OUT), + WUT is picked strictly from the basal water leg beneath the hydrocarbon accumulation.""" + depths = np.linspace(2800, 2900, 21) # 2800, 2805, ..., 2900 + sw = np.ones_like(depths) + # 2800 - 2820: wet sand (Sw = 1.0) + # 2825 - 2860: clean oil pay (Sw = 0.20) -> OUT = 2825, ODT = 2860 + # 2865 - 2900: basal water leg (Sw = 1.0) -> WUT = 2865 + for i, d in enumerate(depths): + if d < 2825: + sw[i] = 1.00 + elif d <= 2860: + sw[i] = 0.20 + else: + sw[i] = 1.00 + + df = pd.DataFrame({ + "WELL_NAME": ["Well-TopWet"] * len(depths), + "ZONES": ["Balmoral Sandstone"] * len(depths), + "TVDSS": depths, + "SWT": sw, + "PHIT": [0.22] * len(depths), + "VSH": [0.05] * len(depths), + }) + + contacts = extract_fluid_contacts_and_limits(df=df) + types = contacts["contact_type"].tolist() + + assert "OUT" in types + assert "ODT" in types + assert "WUT" in types + out_row = contacts[contacts["contact_type"] == "OUT"].iloc[0] + odt_row = contacts[contacts["contact_type"] == "ODT"].iloc[0] + wut_row = contacts[contacts["contact_type"] == "WUT"].iloc[0] + + assert out_row["depth"] == 2825.0 + assert odt_row["depth"] == 2860.0 + # WUT MUST be at 2865.0 (below ODT), NOT at 2800.0 or 2820.0 (above OUT)! + assert wut_row["depth"] == 2865.0 + assert wut_row["depth"] >= odt_row["depth"] + assert wut_row["depth"] > out_row["depth"] + + +def test_extract_fluid_contacts_no_sand_and_no_pay_skips_zone(): + """Verify that: + 1. A shale zone with NO reservoir sand (e.g. Sele / Balder Tuff) registers ZERO contacts. + 2. A clean sand zone with NO hydrocarbon pay (100% wet sand) registers ZERO contacts by default, + avoiding phantom WUT lines at the top of the formation. + 3. A pay zone in the same well registers valid contacts.""" + # Build 3 zones in one well: + # Zone 1: 'Sele' (Pure Shale: VSH = 0.85, PHIT = 0.02) + # Zone 2: 'Andrew Sandstone' (Clean Sand, 100% Wet: VSH = 0.08, PHIT = 0.22, SWT = 1.00) + # Zone 3: 'Balmoral Sandstone' (Oil Reservoir: VSH = 0.05, PHIT = 0.25, SWT = 0.20 in pay, 1.00 in water leg) + z1_depths = np.linspace(2700, 2750, 11) # Sele shale + z2_depths = np.linspace(2760, 2800, 9) # Andrew wet sand + z3_depths = np.linspace(2810, 2880, 15) # Balmoral oil reservoir + + d_all = np.concatenate([z1_depths, z2_depths, z3_depths]) + zones_all = ( + ["Sele"] * len(z1_depths) + + ["Andrew Sandstone"] * len(z2_depths) + + ["Balmoral Sandstone"] * len(z3_depths) + ) + + vsh_all = ( + [0.85] * len(z1_depths) + + [0.08] * len(z2_depths) + + [0.05] * len(z3_depths) + ) + phit_all = ( + [0.02] * len(z1_depths) + + [0.22] * len(z2_depths) + + [0.25] * len(z3_depths) + ) + sw_all = ( + [1.0] * len(z1_depths) + + [1.0] * len(z2_depths) + + [0.20 if d <= 2840 else 1.0 for d in z3_depths] + ) + + df = pd.DataFrame({ + "WELL_NAME": ["Well-MultiZone"] * len(d_all), + "ZONES": zones_all, + "TVDSS": d_all, + "SWT": sw_all, + "PHIT": phit_all, + "VSH": vsh_all, + }) + + contacts = extract_fluid_contacts_and_limits(df=df, require_pay=True) + + # 1. Sele (no sand) must have ZERO contacts + sele_contacts = contacts[contacts["zone"] == "Sele"] + assert len(sele_contacts) == 0, f"Expected 0 contacts in shale zone Sele, found: {sele_contacts}" + + # 2. Andrew Sandstone (clean sand, but 100% wet / no pay) must have ZERO contacts + andrew_contacts = contacts[contacts["zone"] == "Andrew Sandstone"] + assert len(andrew_contacts) == 0, f"Expected 0 contacts in 100% wet sand Andrew, found: {andrew_contacts}" + + # 3. Balmoral Sandstone (oil reservoir) must have OUT, ODT, WUT, OWC + balm_contacts = contacts[contacts["zone"] == "Balmoral Sandstone"] + assert len(balm_contacts) > 0 + balm_types = balm_contacts["contact_type"].tolist() + assert "OUT" in balm_types + assert "ODT" in balm_types + assert "WUT" in balm_types + assert "OWC" in balm_types + assert "WDT" not in balm_types + + +def test_clean_slate_for_zone_under_purview(): + """Verify that identifying and saving new contacts starts a clean slate for the zones + under purview, clearing old obsolete/stale contacts for that zone without altering other zones.""" + db_conn = DBConnector(db_url="sqlite:///:memory:") + db_conn.setup_db() + with db_conn.get_session() as session: + proj = db_objects.Project(session, name="CleanSlateProj") + proj.save() + well = db_objects.Well(session, project_id=proj.project_id, name="Well-CS", uwi="CS-01") + well.save() + + # Step 1: Initial state has contacts across 3 zones: + # - 'Balmoral Sandstone': OUT, ODT, and an old obsolete WUT + # - 'Forties': OUT, ODT (unrelated zone) + # - 'Sele': a bogus WUT + initial_contacts = [ + {"zone": "Balmoral Sandstone", "contact_type": "OUT", "depth": 2830.0}, + {"zone": "Balmoral Sandstone", "contact_type": "ODT", "depth": 2840.0}, + {"zone": "Balmoral Sandstone", "contact_type": "WUT", "depth": 2820.0}, # Old shallow WUT + {"zone": "Forties", "contact_type": "OUT", "depth": 2500.0}, + {"zone": "Forties", "contact_type": "ODT", "depth": 2550.0}, + {"zone": "Sele", "contact_type": "WUT", "depth": 2740.0}, # Bogus shale contact + ] + well.add_fluid_contacts(initial_contacts, replace=True) + assert len(well.get_fluid_contacts()) == 6 + + # Step 2: Re-evaluating Balmoral Sandstone with fresh consistent contacts: + # [OUT at 2833.0, ODT at 2839.0, FWL at 2895.0] (notice: NO WUT, and new OUT/ODT depths) + fresh_balm_contacts = [ + {"zone": "Balmoral Sandstone", "contact_type": "OUT", "depth": 2833.0}, + {"zone": "Balmoral Sandstone", "contact_type": "ODT", "depth": 2839.0}, + {"zone": "Balmoral Sandstone", "contact_type": "FWL", "depth": 2895.0}, + ] + well.add_fluid_contacts(fresh_balm_contacts) + + df_after_balm = well.get_fluid_contacts() + balm_df = df_after_balm[df_after_balm["zone"] == "Balmoral Sandstone"] + + # Balmoral Sandstone MUST have only the 3 new contacts. + # The old obsolete WUT (2820.0) and old OUT/ODT must be completely gone! + assert len(balm_df) == 3 + assert set(balm_df["contact_type"]) == {"OUT", "ODT", "FWL"} + assert set(balm_df["depth"]) == {2833.0, 2839.0, 2895.0} + + # Forties and Sele must still be intact at this point + assert len(df_after_balm[df_after_balm["zone"] == "Forties"]) == 2 + assert len(df_after_balm[df_after_balm["zone"] == "Sele"]) == 1 + + # Step 3: Cleaning the slate for Sele (which is under purview and determined to have no sand/pay) + deleted = well.delete_fluid_contacts(zones=["Sele"]) + assert deleted == 1 + + df_final = well.get_fluid_contacts() + assert len(df_final[df_final["zone"] == "Sele"]) == 0 + assert len(df_final[df_final["zone"] == "Forties"]) == 2 + assert len(df_final[df_final["zone"] == "Balmoral Sandstone"]) == 3 + + + diff --git a/tests/test_params_and_contacts_reset.py b/tests/test_params_and_contacts_reset.py new file mode 100644 index 00000000..6434d358 --- /dev/null +++ b/tests/test_params_and_contacts_reset.py @@ -0,0 +1,163 @@ +from contextlib import contextmanager + +import pytest +from sqlalchemy import create_engine +from sqlalchemy.orm import sessionmaker +from sqlalchemy.pool import StaticPool + +from quick_pp.app.backend.services.ancillary import delete_fluid_contacts_project +from quick_pp.app.backend.services.workspace import ( + WorkspaceModuleParamsEntry, + WorkspaceModuleParamsRequest, + workspace_delete_project_module_params, + workspace_delete_well_module_params, + workspace_save_module_params, + workspace_save_project_module_params, +) +from quick_pp.database.models import Base +from quick_pp.database.objects import Project, Well + + +@pytest.fixture +def test_db(monkeypatch): + engine = create_engine( + "sqlite:///:memory:", + connect_args={"check_same_thread": False}, + poolclass=StaticPool, + ) + Base.metadata.create_all(engine) + Session = sessionmaker(bind=engine) + + @contextmanager + def _get_test_db(): + session = Session() + try: + yield session + session.commit() + except Exception: + session.rollback() + raise + finally: + session.close() + + monkeypatch.setattr("quick_pp.app.backend.services.ancillary.get_db", _get_test_db) + monkeypatch.setattr("quick_pp.app.backend.services.workspace.get_db", _get_test_db) + monkeypatch.setattr("quick_pp.app.backend.services.workspace.is_demo_mode", lambda: False) + + session = Session() + proj = Project(db_session=session, name="test_reset_proj") + proj.save() + + w1 = Well(db_session=session, project_id=proj.project_id, name="W-1", uwi="U-1") + w1.save() + + # Add 1 manual contact and 2 auto contacts + w1.add_fluid_contacts( + [ + { + "name": "OWC", + "depth": 2400.0, + "source": "Log Evaluation (Midpoint)", + "contact_type": "OWC", + }, + { + "name": "ODT", + "depth": 2390.0, + "source": "Log Evaluation (Lowest proved oil)", + "contact_type": "ODT", + }, + { + "name": "Manual_FWL", + "depth": 2410.0, + "source": "Handover Import", + "contact_type": "FWL", + }, + ] + ) + + yield {"session": session, "project_id": proj.project_id, "well_name": w1.name} + + +def test_delete_auto_fluid_contacts(test_db): + pid = test_db["project_id"] + + # Delete auto-only contacts + res = delete_fluid_contacts_project(project_id=pid, auto_only=True) + assert res["deleted_count"] == 2 + assert "W-1" in res["wells_affected"] + + # Check remaining contacts in well + session = test_db["session"] + proj = Project.load(session, project_id=pid) + w = proj.get_well("W-1") + remaining = w.get_fluid_contacts().to_dict(orient="records") + assert len(remaining) == 1 + assert remaining[0]["name"] == "Manual_FWL" + + +def test_project_module_params_delete(test_db): + pid = test_db["project_id"] + + # Save 2 entries + req = WorkspaceModuleParamsRequest( + entries=[ + WorkspaceModuleParamsEntry( + module="rock-typing", + key="all", + selection={"zones": None}, + label="All zones", + params={"num_clusters": 3}, + ), + WorkspaceModuleParamsEntry( + module="swirr-corr", + key="zone:A", + selection={"zones": ["A"]}, + label="Zone A", + params={"referencePcPsi": 100.0}, + ), + ] + ) + saved = workspace_save_project_module_params(pid, req) + assert "rock-typing" in saved["module_params"] + assert "swirr-corr" in saved["module_params"] + + # Delete specific module + del_res = workspace_delete_project_module_params(pid, module="rock-typing") + assert "rock-typing" not in del_res["module_params"] + assert "swirr-corr" in del_res["module_params"] + + # Delete all remaining + del_all = workspace_delete_project_module_params(pid) + assert del_all["module_params"] == {} + + +def test_well_module_params_delete(test_db): + pid = test_db["project_id"] + wn = test_db["well_name"] + + req = WorkspaceModuleParamsRequest( + entries=[ + WorkspaceModuleParamsEntry( + module="litho-poro", + key="well", + selection={"zones": None}, + label="Whole well", + params={"model": "ssc"}, + ), + WorkspaceModuleParamsEntry( + module="saturation", + key="zone:A", + selection={"zones": ["A"]}, + label="Zone A", + params={"m": 2.0, "n": 2.0}, + ), + ] + ) + saved = workspace_save_module_params(pid, wn, req) + assert "litho-poro" in saved["module_params"] + assert "saturation" in saved["module_params"] + + # Delete saturation + del_res = workspace_delete_well_module_params(pid, wn, module="saturation") + assert "saturation" not in del_res["module_params"] + assert "litho-poro" in del_res["module_params"] diff --git a/uv.lock b/uv.lock index d7274a4a..259e75b2 100644 --- a/uv.lock +++ b/uv.lock @@ -5272,7 +5272,7 @@ wheels = [ [[package]] name = "quick-pp" -version = "0.3.3" +version = "0.3.4" source = { editable = "." } dependencies = [ { name = "click" },