fix: Apply expression updates directly in NX journal
Critical fix - the expressions were not being applied during optimization! The journal now receives expression values and applies them using EditExpressionWithUnits() BEFORE rebuilding geometry and regenerating FEM. ## Key Changes ### Expression Application in Journal (solve_simulation.py) - Journal now accepts expression values as arguments (tip_thickness, support_angle) - Applies expressions using EditExpressionWithUnits() on active Bracket part - Calls MakeUpToDate() on each modified expression - Then calls UpdateManager.DoUpdate() to rebuild geometry with new values - Follows the exact pattern from the user's working journal ### NX Solver Updates (nx_solver.py) - Added expression_updates parameter to run_simulation() and run_nx_simulation() - Passes expression values to journal via sys.argv - For bracket: passes tip_thickness and support_angle as separate args ### Test Script Updates (test_journal_optimization.py) - Removed nx_updater step (no longer needed - expressions applied in journal) - model_updater now just stores design vars in global variable - simulation_runner passes expression_updates to nx_solver - Sequential workflow: update vars -> run journal (apply expressions) -> extract results ## Results - OPTIMIZATION NOW WORKS! Before (all trials same stress): - Trial 0: tip=23.48, angle=37.21 → stress=197.89 MPa - Trial 1: tip=20.08, angle=20.32 → stress=197.89 MPa (SAME!) - Trial 2: tip=18.19, angle=35.23 → stress=197.89 MPa (SAME!) After (varying stress values): - Trial 0: tip=21.62, angle=30.15 → stress=192.71 MPa ✅ - Trial 1: tip=17.17, angle=33.52 → stress=167.96 MPa ✅ BEST! - Trial 2: tip=15.06, angle=21.81 → stress=242.50 MPa ✅ Mesh also changes: 1027 → 951 CTETRA elements with different parameters. The optimization loop is now fully functional with expressions being properly applied and the FEM regenerating with correct geometry! 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -27,13 +27,23 @@ from optimization_engine.result_extractors.extractors import (
|
||||
)
|
||||
|
||||
|
||||
# Global variable to store current design variables for the simulation runner
|
||||
_current_design_vars = {}
|
||||
|
||||
|
||||
def bracket_model_updater(design_vars: dict):
|
||||
"""Update bracket model parameters."""
|
||||
prt_file = project_root / "examples/bracket/Bracket.prt"
|
||||
print(f"\n[MODEL UPDATE] {prt_file.name}")
|
||||
"""
|
||||
Store design variables for the simulation runner.
|
||||
|
||||
Note: We no longer directly update the .prt file here.
|
||||
Instead, design variables are passed to the journal which applies them in NX.
|
||||
"""
|
||||
global _current_design_vars
|
||||
_current_design_vars = design_vars.copy()
|
||||
|
||||
print(f"\n[MODEL UPDATE] Design variables prepared")
|
||||
for name, value in design_vars.items():
|
||||
print(f" {name} = {value:.4f}")
|
||||
update_nx_model(prt_file, design_vars, backup=False)
|
||||
|
||||
|
||||
def bracket_simulation_runner() -> Path:
|
||||
@@ -42,23 +52,29 @@ def bracket_simulation_runner() -> Path:
|
||||
|
||||
This connects to the running Simcenter3D GUI and:
|
||||
1. Opens the .sim file
|
||||
2. Updates the FEM component
|
||||
3. Solves the simulation
|
||||
4. Returns path to .op2 file
|
||||
2. Applies expression updates in the journal
|
||||
3. Updates geometry and FEM
|
||||
4. Solves the simulation
|
||||
5. Returns path to .op2 file
|
||||
"""
|
||||
global _current_design_vars
|
||||
sim_file = project_root / "examples/bracket/Bracket_sim1.sim"
|
||||
|
||||
print("\n[SIMULATION] Running via journal on NX GUI...")
|
||||
print(f" SIM file: {sim_file.name}")
|
||||
if _current_design_vars:
|
||||
print(f" Expression updates: {_current_design_vars}")
|
||||
|
||||
try:
|
||||
# Run solver via journal (connects to running NX GUI)
|
||||
# Pass expression updates directly to the journal
|
||||
op2_file = run_nx_simulation(
|
||||
sim_file=sim_file,
|
||||
nastran_version="2412",
|
||||
timeout=300, # 5 minute timeout
|
||||
cleanup=True, # Clean up temp files
|
||||
use_journal=True # Use journal mode (requires NX GUI open)
|
||||
use_journal=True, # Use journal mode (requires NX GUI open)
|
||||
expression_updates=_current_design_vars # Pass design vars to journal
|
||||
)
|
||||
|
||||
print(f"[SIMULATION] Complete! Results: {op2_file.name}")
|
||||
|
||||
Reference in New Issue
Block a user