feat: Add MLP surrogate with Turbo Mode for 100x faster optimization

Neural Acceleration (MLP Surrogate):
- Add run_nn_optimization.py with hybrid FEA/NN workflow
- MLP architecture: 4-layer (64->128->128->64) with BatchNorm/Dropout
- Three workflow modes:
  - --all: Sequential export->train->optimize->validate
  - --hybrid-loop: Iterative Train->NN->Validate->Retrain cycle
  - --turbo: Aggressive single-best validation (RECOMMENDED)
- Turbo mode: 5000 NN trials + 50 FEA validations in ~12 minutes
- Separate nn_study.db to avoid overloading dashboard

Performance Results (bracket_pareto_3obj study):
- NN prediction errors: mass 1-5%, stress 1-4%, stiffness 5-15%
- Found minimum mass designs at boundary (angle~30deg, thick~30mm)
- 100x speedup vs pure FEA exploration

Protocol Operating System:
- Add .claude/skills/ with Bootstrap, Cheatsheet, Context Loader
- Add docs/protocols/ with operations (OP_01-06) and system (SYS_10-14)
- Update SYS_14_NEURAL_ACCELERATION.md with MLP Turbo Mode docs

NX Automation:
- Add optimization_engine/hooks/ for NX CAD/CAE automation
- Add study_wizard.py for guided study creation
- Fix FEM mesh update: load idealized part before UpdateFemodel()

New Study:
- bracket_pareto_3obj: 3-objective Pareto (mass, stress, stiffness)
- 167 FEA trials + 5000 NN trials completed
- Demonstrates full hybrid workflow

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Antoine
2025-12-06 20:01:59 -05:00
parent 0cb2808c44
commit 602560c46a
70 changed files with 31018 additions and 289 deletions

View File

@@ -1,7 +1,7 @@
# Analyze Model Skill
**Last Updated**: November 25, 2025
**Version**: 1.0 - Model Analysis and Feature Extraction
**Last Updated**: December 6, 2025
**Version**: 2.0 - Added Comprehensive Model Introspection
You are helping the user understand their NX model's structure and identify optimization opportunities.
@@ -11,7 +11,8 @@ Extract and present information about an NX model to help the user:
1. Identify available parametric expressions (potential design variables)
2. Understand the simulation setup (analysis types, boundary conditions)
3. Discover material properties
4. Recommend optimization strategies based on model characteristics
4. Identify extractable results from OP2 files
5. Recommend optimization strategies based on model characteristics
## Triggers
@@ -20,28 +21,107 @@ Extract and present information about an NX model to help the user:
- "show me the expressions"
- "look at my NX model"
- "what parameters are available"
- "introspect my model"
- "what results are available"
## Prerequisites
- User must provide path to NX model files (.prt, .sim, .fem)
- NX must be available on the system (configured in config.py)
- Model files must be valid NX format
- User must provide path to NX model files (.prt, .sim, .fem) or study directory
- NX must be available on the system for part/sim introspection
- OP2 introspection works without NX (pure Python)
## Information Gathering
Ask these questions if not already provided:
1. **Model Location**:
- "Where is your NX model? (path to .prt file)"
- "Where is your NX model? (path to .prt file or study directory)"
- Default: Look in `studies/*/1_setup/model/`
2. **Analysis Interest**:
- "What type of optimization are you considering?" (optional)
- This helps focus the analysis on relevant aspects
---
## MANDATORY: Model Introspection
**ALWAYS use the introspection module for comprehensive model analysis:**
```python
from optimization_engine.hooks.nx_cad.model_introspection import (
introspect_part,
introspect_simulation,
introspect_op2,
introspect_study
)
# Option 1: Introspect entire study directory (recommended)
study_info = introspect_study("studies/my_study/")
# Option 2: Introspect individual files
part_info = introspect_part("path/to/model.prt")
sim_info = introspect_simulation("path/to/model.sim")
op2_info = introspect_op2("path/to/results.op2")
```
### What Introspection Extracts
| Source | Information Extracted |
|--------|----------------------|
| `.prt` | Expressions (count, values, types), bodies, mass, material, features |
| `.sim` | Solutions, boundary conditions, loads, materials, mesh info, output requests |
| `.op2` | Available results (displacement, stress, strain, SPC forces, etc.), subcases |
### Introspection Report Generation
**MANDATORY**: Generate `MODEL_INTROSPECTION.md` for every study:
```python
# Generate and save introspection report
study_info = introspect_study(study_dir)
# Create markdown report
report = generate_introspection_report(study_info)
with open(study_dir / "MODEL_INTROSPECTION.md", "w") as f:
f.write(report)
```
---
## Execution Steps
### Step 1: Validate Model Files
### Step 1: Run Comprehensive Introspection
**Use the introspection module (MANDATORY)**:
```python
from optimization_engine.hooks.nx_cad.model_introspection import introspect_study
# Introspect the entire study
result = introspect_study("studies/my_study/")
if result["success"]:
# Part information
for part in result["data"]["parts"]:
print(f"Part: {part['file']}")
print(f" Expressions: {part['data'].get('expression_count', 0)}")
print(f" Bodies: {part['data'].get('body_count', 0)}")
# Simulation information
for sim in result["data"]["simulations"]:
print(f"Simulation: {sim['file']}")
print(f" Solutions: {sim['data'].get('solution_count', 0)}")
# OP2 results
for op2 in result["data"]["results"]:
print(f"OP2: {op2['file']}")
available = op2['data'].get('available_results', {})
print(f" Displacement: {available.get('displacement', False)}")
print(f" Stress: {available.get('stress', False)}")
```
### Step 2: Validate Model Files
Check that required files exist:
@@ -95,26 +175,21 @@ def validate_model_files(model_path: Path) -> dict:
return result
```
### Step 2: Extract Expressions
### Step 3: Extract Expressions (via Introspection)
Use NX Python API to extract all parametric expressions:
The introspection module extracts expressions automatically:
```python
# This requires running a journal inside NX
# Use the expression extractor from optimization_engine
from optimization_engine.hooks.nx_cad.model_introspection import introspect_part
from optimization_engine.extractors.expression_extractor import extract_all_expressions
expressions = extract_all_expressions(prt_file)
# Returns: [{'name': 'thickness', 'value': 2.0, 'unit': 'mm', 'formula': None}, ...]
result = introspect_part("path/to/model.prt")
if result["success"]:
expressions = result["data"].get("expressions", [])
for expr in expressions:
print(f" {expr['name']}: {expr['value']} {expr.get('unit', '')}")
```
**Manual Extraction Method** (if NX API not available):
1. Read the .prt file header for expression metadata
2. Look for common parameter naming patterns
3. Ask user to provide expression names from NX
### Step 3: Classify Expressions
### Step 4: Classify Expressions
Categorize expressions by likely purpose:
@@ -154,53 +229,121 @@ Based on analysis, recommend:
## Output Format
Present analysis in structured format:
Present analysis using the **MODEL_INTROSPECTION.md** format:
```markdown
# Model Introspection Report
**Study**: {study_name}
**Generated**: {date}
**Introspection Version**: 1.0
---
## 1. Files Discovered
| Type | File | Status |
|------|------|--------|
| Part (.prt) | {prt_file} | ✓ Found |
| Simulation (.sim) | {sim_file} | ✓ Found |
| FEM (.fem) | {fem_file} | ✓ Found |
| Results (.op2) | {op2_file} | ✓ Found |
---
## 2. Part Information
### Expressions (Potential Design Variables)
| Name | Value | Unit | Type | Optimization Candidate |
|------|-------|------|------|------------------------|
| thickness | 2.0 | mm | User | ✓ High |
| hole_diameter | 10.0 | mm | User | ✓ High |
| p173_mass | 0.125 | kg | Reference | Read-only |
### Mass Properties
| Property | Value | Unit |
|----------|-------|------|
| Mass | 0.125 | kg |
| Material | Aluminum 6061-T6 | - |
---
## 3. Simulation Information
### Solutions
| Solution | Type | Nastran SOL | Status |
|----------|------|-------------|--------|
| Solution 1 | Static | SOL 101 | ✓ Active |
| Solution 2 | Modal | SOL 103 | ✓ Active |
### Boundary Conditions
| Name | Type | Applied To |
|------|------|------------|
| Fixed_Root | SPC | Face_1 |
### Loads
| Name | Type | Magnitude | Direction |
|------|------|-----------|-----------|
| Tip_Force | FORCE | 500 N | -Z |
---
## 4. Available Results (from OP2)
| Result Type | Available | Subcases |
|-------------|-----------|----------|
| Displacement | ✓ | 1 |
| SPC Forces | ✓ | 1 |
| Stress (CHEXA) | ✓ | 1 |
| Stress (CPENTA) | ✓ | 1 |
| Strain Energy | ✗ | - |
| Frequencies | ✓ | 2 |
---
## 5. Optimization Recommendations
### Suggested Objectives
| Objective | Extractor | Source |
|-----------|-----------|--------|
| Minimize mass | E4: `extract_mass_from_bdf` | .dat |
| Maximize stiffness | E1: `extract_displacement` → k=F/δ | .op2 |
### Suggested Constraints
| Constraint | Type | Threshold | Extractor |
|------------|------|-----------|-----------|
| Max stress | less_than | 250 MPa | E3: `extract_solid_stress` |
### Recommended Protocol
- **Protocol 11 (Multi-Objective NSGA-II)** - Multiple competing objectives
- Multi-Solution: **Yes** (static + modal)
---
*Ready to create optimization study? Say "create study" to proceed.*
```
MODEL ANALYSIS REPORT
=====================
Model: {model_name}
Location: {model_path}
### Saving the Report
FILES FOUND
-----------
✓ Part file: {prt_file}
✓ Simulation: {sim_file}
✓ FEM mesh: {fem_file}
**MANDATORY**: Save the introspection report to the study directory:
PARAMETRIC EXPRESSIONS
----------------------
| Name | Current Value | Unit | Category | Optimization Candidate |
|------|---------------|------|----------|----------------------|
| thickness | 2.0 | mm | Structural | ✓ High |
| hole_diameter | 10.0 | mm | Geometric | ✓ High |
| fillet_radius | 3.0 | mm | Geometric | ✓ Medium |
| length | 100.0 | mm | Dimensional | ? Check constraints |
```python
from pathlib import Path
SIMULATION SETUP
----------------
Analysis Types: Static (SOL 101), Modal (SOL 103)
Material: Aluminum 6061-T6 (E=68.9 GPa, ρ=2700 kg/m³)
Loads:
- Force: 500 N at tip
- Constraint: Fixed at root
RECOMMENDATIONS
---------------
Suggested Objectives:
- Minimize mass (extract from p173 expression or FEM)
- Maximize first natural frequency
Suggested Constraints:
- Max von Mises stress < 276 MPa (Al 6061 yield)
- Max displacement < {user to specify}
Recommended Protocol: Protocol 11 (Multi-Objective NSGA-II)
- Reason: Multiple competing objectives (mass vs frequency)
Ready to create optimization study? Say "create study" to proceed.
```
def save_introspection_report(study_dir: Path, report_content: str):
"""Save MODEL_INTROSPECTION.md to study directory."""
report_path = study_dir / "MODEL_INTROSPECTION.md"
with open(report_path, 'w') as f:
f.write(report_content)
print(f"Saved introspection report: {report_path}")
## Error Handling