🔬 Cavity Detection Pipeline — Full Version

From CIF file to 3D visualization · SDF + Fill & Spread + Marching Cubes + Crystal Channels + Property Mapping + QSPR

Main Step
Optimization
Parallel Processing
Critical Step
Channel Detection
Property Mapping
QSPR / Analysis
SQE Charges
PHASE 1: Initialization Load CIF, symmetry expansion, atom preparation, VdW scaling, SQE charges 📄 Load CIF File Parse structure file Unit cell: a, b, c, α, β, γ Space group, symmetry ops Cell volume: 100–10,000 ų 🔁 Symmetry Expansion Generate all equivalents Apply space group operations Max 48 symmetry ops Unique atoms + duplicates ⚛️ Atoms + Neighbors Full atom list for scanning VdW radii with multiplier (0.8–2.0) HydrogenMultiplier separate Neighbor margin: 2.0 Å ⚡ SQE Charges Calculate atomic charges SQE method: 800+ parameters Clamp: -2.0 to +3.0 ⚠️ Check: O = negative, H = positive ⚡ Fast Atom Cache Optimized atom storage FastAtom[] struct array Sorted by X coordinate Bounding boxes per atom 🔴 SQE — źródło ładunków dla atomów ⚡ 30-45% speedup PHASE 2: VdW Scaling Loop — Multi-Scale Analysis Run detection for multiple VdW scales (1.35 → 1.00) 📊 Scale Iteration Default: 1.35, 1.30, 1.25, 1.20, 1.15, 1.10, 1.05, 1.00 Update VdW multiplier Update resolution (auto) Progress: 0–100% 🧮 Cavity Detection DetectVoidsAsync() SDF + Fill & Spread Marching Cubes mesh Crystal channel analysis (for VdW=1.0) 📈 Data Collection Volume, Surface Area Sphericity, V/S ratio Channel detection Electrostatic data (charge, hydrophobicity) 🔁 Cavity Tracking Track cavities across scales CavitySnapshots per scale Disappearance scale Critical events detection 🔴 Critical — main computational load PHASE 3: SDF — Signed Distance Function Compute distance from each voxel to nearest atom surface 📊 Voxel Grid Creation Crystal-aligned grid Resolution: 108–154 per axis Voxel size: 0.10–0.30 Å Only voxels within 2Å of cell 🧮 SDF Calculation minDist² to all atoms SDF = distance to surface Negative = inside atom Voxels >2Å from cell → skipped ⚡ Fast Rejection 3D Binary search + 3D rejection Only 5% atoms reach full dist² Culling, local variables Bounding box per atom 🔧 Fill Gaps (disabled) 3 iterative passes Fill SDF holes Currently disabled — direct density Uses fractional coordinates 🔴 Critical for quality ⚡ 85-95% speedup (only 5% atoms reach full dist²) 🔷 2Å cell margin — reduces voxel count PHASE 4: Buffer Split — Two Separate Grids One for rendering (with walls), one for detection (periodic) 🎨 Graphic Density (for Rendering) WITH artificial walls for Marching Cubes Wall thickness: 4 voxels Closed mesh generation Copied DIRECTLY from periodic cell Buffer.BlockCopy optimized 🔄 Periodic Density (for Detection) WITHOUT walls — periodic boundary conditions Copy center + borders from supercell Open boundaries for tunnel detection Buffer.BlockCopy optimized Source for graphic density copy copy 🔷 Graphic buffer copied DIRECTLY from periodic grid (no separate computation) PHASE 5: Fill & Spread — Cavity Detection Identify connected void regions on periodic grid 🌊 Flood Fill Spread through empty voxels 6 directions (±X, ±Y, ±Z) Periodic boundary wrap Queue-based BFS 📦 Cavity Regions Voxel list, volume, center Bounding box calculation Min volume filtering Min voxel count: 100 🧱 Walls & Tunnels Touches X-/X+ Y-/Y+ Z-/Z+ Channel detection: XX, YY, ZZ BFS connection check (no wrap) Topology = wall connections 🔗 Periodic Groups Merge duplicate cavities PeriodicGroupId IsPeriodicDuplicate flag Color grouping 🔮 Channel: XX, YY, ZZ, XXYY, XXZZ, YYZZ, XXYYZZ ⚡ 40-60% speedup (parallel per cavity) PHASE 6: Gradient Computation — For Channel Pathfinder Compute SDF gradient on supercell for A* pathfinding 📊 Gradient on Supercell ∇SDF = (∂S/∂x, ∂S/∂y, ∂S/∂z) Central differences: (f(x+1)-f(x-1))/(2·dx) Parallel For per voxel 📦 Copy to Periodic Cell GradientX, GradientY, GradientZ Stored in Cavity object Used by CavityPathFinder ⚡ Optimized Storage float[,,] for each component Parallel copy with Buffer.BlockCopy UseGradient flag in PathFinder ⚡ Enables gradient-guided A* pathfinding PHASE 7: Marching Cubes — Mesh Generation Convert density grid to triangle mesh (parallel per cavity) 🎭 Apply Mask Use VoxelMask from detection Only cavity voxels = empty Outside = -10.0f (wall) 🔺 Triangle Generation Marching Cubes lookup EdgeTable + TriTable Parallel per cavity 🧭 Raw vs Smoothed Mesh RAW mesh → Surface Area SMOOTHED mesh → Display Gaussian blur + smoothing ✂️ Clip & Merge Clip to cell (Vector4.Transform) SnapSharedVertices Calculate normals from SDF 🔴 Most expensive step ⚡ 35-60% speedup (stackalloc, long keys) 🔷 Vector4.Transform optimized PHASE 8: Crystal Channels — Semi-Periodic Path Analysis A* pathfinding through cavities with gradient guidance 🗺️ A* Pathfinding Find path between walls Heuristic: distance to wall Gradient-guided cost PriorityQueue implementation 📊 Channel Analysis XX, YY, ZZ directions Total length, avg/min/max width Helicity calculation Effective diameter 🌀 Helicity & Smoothing Path smoothing (iterations) Helicity = twist angle Smoothing error % Cell crossings count 🔗 Storage & Render CrystalChannelResults WorldPath + FracPath MinPath + MaxPath XNA conversion for rendering 🔮 Semi-periodic boundary conditions PHASE 9: Property Mapping — Physical Properties on Cavity Surface Map atomic properties (charge, hydrophobicity, etc.) onto mesh vertices 🔴 SQE — Atom Charges Source: SqeChargeCalculator 800+ parameters, chi/eta/beta Clamp: -2.0 to +3.0 Normalization: max 0.2 correction ⚠️ OXYGEN = NEGATIVE (ujemny) ⚠️ HYDROGEN = POSITIVE (dodatni) 🔵 Niebieski = ujemny → tlen 🔴 Czerwony = dodatni → wodór 🔵 PropertyMapper — Cavity Charges Source: Scanner atoms + SQE Builds property map from atoms 3×3×3 neighbor lookup Weighted average or nearest Contrast: 2.8× for charge Contrast: 1.5× for hydrophobicity Search radius: 3.0 Å VertexColors per cavity 📊 Color Manager Value → Color mapping Palette: Charge, Hydrophobicity Dynamic range from molecule SetDynamicRange() for correct scale 🔵 Niebieski = min (ujemny) 🔴 Czerwony = max (dodatni) Contrast: 1.0–2.8x ⚠️ Sprawdź: skala zgadza się z atomami! ⚡ Apply to All Apply property to cavities Async Task.Run Parallel.For per cavity Reset to default colors Update renderer colors Force refresh 🔴 SQE — ładunki dla atomów 🔵 PropertyMapper — ładunki dla kawitacji ⚠️ Oba źródła muszą być spójne! PHASE 10: QSPR & Host-Guest Analysis Calculate descriptors and assess guest compatibility 📊 QSPR Descriptors Fragmentation Index (I_frag) Collapse Scale (f_collapse) Electrostatic Frustration (Ω_Q) Hydrophobic Frustration (Ψ_H) 🧪 Host-Guest Report Advanced compatibility scoring Volume, channel width, helicity Charge & hydrophobicity Guest molecule compatibility 📄 Report Generation Full text report HTML report with screenshots Channel summary tables Network analysis (1D/2D/3D) 💾 Export CSV export Text file export HTML report export Screenshots included ✅ OUTPUT: Cavity data ready for analysis Voxels · Surface Points · Marching Cubes Mesh · Volume · Surface Area · Channels · Sphericity · QSPR · Host-Guest · Property Colors 10 phases · 45+ steps · Parallel processing · Optimized for speed and accuracy · 2026 ⚠️ UWAGA: DWA ŹRÓDŁA ŁADUNKÓW — MUSZĄ BYĆ SPÓJNE! SQE (atomy) → PropertyMapper (kawitacje) → oba używają tych samych wartości charge