Files
Atomizer/optimization_engine/extractors/extract_mass_from_bdf.py
Anto01 2b3573ec42 feat: Add AtomizerField training data export and intelligent model discovery
Major additions:
- Training data export system for AtomizerField neural network training
- Bracket stiffness optimization study with 50+ training samples
- Intelligent NX model discovery (auto-detect solutions, expressions, mesh)
- Result extractors module for displacement, stress, frequency, mass
- User-generated NX journals for advanced workflows
- Archive structure for legacy scripts and test outputs
- Protocol documentation and dashboard launcher

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-26 12:01:50 -05:00

63 lines
1.8 KiB
Python

"""
Extract mass from Nastran BDF/DAT file as fallback when OP2 doesn't have GRDPNT
"""
from pathlib import Path
from typing import Dict, Any
import re
def extract_mass_from_bdf(bdf_file: Path) -> Dict[str, Any]:
"""
Extract mass from Nastran BDF file by parsing material and element definitions.
This is a fallback when OP2 doesn't have PARAM,GRDPNT output.
Args:
bdf_file: Path to .dat or .bdf file
Returns:
dict: {
'mass_kg': total mass in kg,
'mass_g': total mass in grams,
'method': 'bdf_calculation'
}
"""
bdf_file = Path(bdf_file)
if not bdf_file.exists():
raise FileNotFoundError(f"BDF file not found: {bdf_file}")
# Parse using pyNastran BDF reader
from pyNastran.bdf.bdf import read_bdf
model = read_bdf(str(bdf_file), validate=False, xref=True, punch=False,
encoding='utf-8', log=None, debug=False, mode='msc')
# Calculate total mass by summing element masses
# model.mass_properties() returns (mass, cg, inertia)
mass_properties = model.mass_properties()
mass_ton = mass_properties[0] # Mass in tons (ton-mm-sec)
# NX Nastran typically uses ton-mm-sec units
mass_kg = mass_ton * 1000.0 # Convert tons to kg
mass_g = mass_kg * 1000.0 # Convert kg to grams
return {
'mass_kg': mass_kg,
'mass_g': mass_g,
'mass_ton': mass_ton,
'method': 'bdf_calculation',
'units': 'ton-mm-sec (converted to kg/g)'
}
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
bdf_file = Path(sys.argv[1])
result = extract_mass_from_bdf(bdf_file)
print(f"Mass from BDF: {result['mass_kg']:.6f} kg ({result['mass_g']:.3f} g)")
else:
print(f"Usage: python {sys.argv[0]} <bdf_file>")