现有组件功能与Amesim组件比较对齐,补全功能;仿真服务启动环境检测,旧版json工程文件适配读取

This commit is contained in:
ljz committed 2026-09-13 20:01:56 +08:00
1 parent b6c22a54f8
commit 6fc9afe41d
119 files changed
+260114 -351

No files matched your search

+71 -17
View File
@@ -5,6 +5,7 @@ from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass, field
from hashlib import sha256
import json
import logging
import os
from pathlib import Path
import re
@@ -17,8 +18,20 @@ from typing import Any, Callable
from .compiler import NativeProgram
from .modules import component_modules
from .processes import command_environment, is_driver_launch_failure, is_transient_process_error, LAUNCH_RETRY_DELAYS
from .cache_storage import acquire_cache_lease, touch_cache_entry, prune_cache
LOGGER = logging.getLogger(__name__)
WINDOWS = os.name == 'nt'
class NativeCommandError(RuntimeError):
def __init__(self, message, command, *, exit_code=None, stdout=b'', stderr='', error_type='CalledProcessError', attempts=1):
super().__init__(message)
self.details = {'command': list(command), 'cwd': str(Path.cwd()), 'exitCode': exit_code,
'stdout': stdout.decode('utf-8',errors='replace')[-8000:], 'stderr': stderr[-8000:],
'errorType': error_type, 'attempts': attempts}
ROOT = Path(__file__).resolve().parents[3]
NATIVE = ROOT / "native"
CACHE = ROOT / "app/data/native-builds"
@@ -135,12 +148,38 @@ def _publish(stage: Path, target: Path, key_name: str, key: str, version: int, r
return manifest
def _command(command: list[str], *, log: list[str], timeout: float = 120) -> bytes:
result = subprocess.run(command, capture_output=True, timeout=timeout)
log.append(" ".join(command) + "\n" + result.stderr.decode("utf-8", errors="replace"))
if result.returncode:
raise RuntimeError("Native compilation failed: " + result.stderr.decode("utf-8", errors="replace")[-3000:])
return result.stdout
def _command(command: list[str], *, log: list[str], timeout: float = 120,
require_stdout: bool = False) -> bytes:
environment = command_environment(command[0])
for attempt in range(len(LAUNCH_RETRY_DELAYS) + 1):
try:
result = subprocess.run(command, capture_output=True, timeout=timeout,
stdin=subprocess.DEVNULL, close_fds=True, env=environment,
creationflags=subprocess.CREATE_NO_WINDOW if WINDOWS else 0)
except OSError as exc:
if not is_transient_process_error(exc) or attempt == len(LAUNCH_RETRY_DELAYS):
raise
reason = f'{type(exc).__name__}: {exc}'
else:
stderr = result.stderr.decode('utf-8', errors='replace')
log.append(' '.join(command) + '\n' + stderr)
empty = result.returncode == 0 and require_stdout and not result.stdout.strip()
transient = is_driver_launch_failure(stderr) or (empty and WINDOWS)
if not result.returncode and not empty:
return result.stdout
reason = 'Compiler returned no identification output.' if empty else stderr[-3000:]
if not transient or attempt == len(LAUNCH_RETRY_DELAYS):
prefix = ('GCC 已启动,但编译子进程启动失败(已完成短间隔重试);'
if is_driver_launch_failure(stderr) else 'Native compilation failed: ')
raise NativeCommandError(prefix + reason, command, exit_code=result.returncode,
stdout=result.stdout, stderr=stderr, error_type='EmptyCompilerOutput' if empty else 'CalledProcessError',
attempts=attempt+1)
delay = LAUNCH_RETRY_DELAYS[attempt]
log.append(f'Compiler launch retry {attempt+1}/{len(LAUNCH_RETRY_DELAYS)} after {delay:g}s: {reason}')
LOGGER.warning('Compiler process launch failed; retry %d/%d in %.2fs: %s',
attempt+1, len(LAUNCH_RETRY_DELAYS), delay, reason.strip())
time.sleep(delay)
raise AssertionError('Unreachable compiler retry state')
def toolchain() -> tuple[str, Path, str]:
@@ -156,16 +195,14 @@ def toolchain() -> tuple[str, Path, str]:
base = next((path for path in candidates if (path / "include/cvode/cvode.h").is_file()), candidates[0])
if not (base / "include/cvode/cvode.h").is_file():
raise RuntimeError("SUNDIALS C development files not found; set SUNDIALS_ROOT.")
version = subprocess.run([compiler, "--version"], capture_output=True, text=True, check=True, timeout=15).stdout.splitlines()[0]
if os.name == 'nt':
compiler = str(Path(shutil.which(compiler) or compiler).resolve())
version = _command([compiler, '--version'], log=[], timeout=15, require_stdout=True).decode('utf-8', errors='replace').strip().splitlines()[0]
return compiler, base, version
def build_native(program: NativeProgram, *, cache_dir: Path | None = None,
progress_callback: Callable[[str], None] | None = None) -> NativeBuild:
started = time.perf_counter()
if progress_callback:
progress_callback("native-cache-check")
compiler, sundials, compiler_version = toolchain()
def platform_build_inputs(sundials: Path) -> tuple[list[str], list[Path], list[Path], str]:
"""Share production flags and dependencies with the startup smoke check."""
flags = list(COMPILER_FLAGS)
executable_name = "model.exe" if os.name == "nt" else "model"
if os.name == "nt":
@@ -182,6 +219,23 @@ def build_native(program: NativeProgram, *, cache_dir: Path | None = None,
raise RuntimeError("SUNDIALS static libraries not found; set SUNDIALS_ROOT.")
libraries = [directory / f"libsundials_{name}.a" for name in LIBRARIES]
dlls = []
return flags, libraries, dlls, executable_name
def link_library_arguments(libraries: list[Path]) -> list[str]:
arguments = list(map(str, libraries))
if sys.platform.startswith("linux"):
arguments = ["-Wl,--start-group", *arguments, "-Wl,--end-group"]
return arguments
def build_native(program: NativeProgram, *, cache_dir: Path | None = None,
progress_callback: Callable[[str], None] | None = None) -> NativeBuild:
started = time.perf_counter()
if progress_callback:
progress_callback("native-cache-check")
compiler, sundials, compiler_version = toolchain()
flags, libraries, dlls, executable_name = platform_build_inputs(sundials)
cache = (cache_dir or CACHE).resolve()
cache.mkdir(parents=True, exist_ok=True)
for kind in ("models", "objects"):
@@ -205,7 +259,7 @@ def build_native(program: NativeProgram, *, cache_dir: Path | None = None,
(stage / "model.h").write_text(program.header, encoding="utf-8", newline="\n")
compiler_path = Path(shutil.which(compiler) or compiler).resolve()
compiler_identity = {"version": compiler_version, "binarySha256": _hash(compiler_path),
"target": _command([compiler, "-dumpmachine"], log=logs).decode().strip(),
"target": _command([compiler, "-dumpmachine"], log=logs, require_stdout=True).decode().strip(),
"flags": flags, "platform": sys.platform}
runtime = _runtime_sources(program)
sources = [stage / "model.c", *runtime]
@@ -252,6 +306,7 @@ def build_native(program: NativeProgram, *, cache_dir: Path | None = None,
"unitCount": len(units), "objectCacheHits": 0, "objectCompilations": 0,
"preprocessSeconds": preprocessing_seconds, "compileSeconds": 0.0,
"compileWallSeconds": 0.0, "linkSeconds": 0.0, "modelCacheHit": manifest is not None}
details['compilerLaunchRetries'] = sum(line.startswith('Compiler launch retry ') for line in logs)
if manifest is not None:
if progress_callback:
progress_callback("native-cache-hit")
@@ -302,14 +357,13 @@ def build_native(program: NativeProgram, *, cache_dir: Path | None = None,
details["objectCompilations"] = len(objects) - details["objectCacheHits"]
for item in objects:
logs.extend(item["log"])
link_libraries = list(map(str, libraries))
if sys.platform.startswith("linux"):
link_libraries = ["-Wl,--start-group", *link_libraries, "-Wl,--end-group"]
link_libraries = link_library_arguments(libraries)
if progress_callback:
progress_callback("native-linking")
before_link = time.perf_counter()
_command([compiler, *flags, *[str(item["path"]) for item in objects], *link_libraries,
"-lm", "-o", str(stage / executable_name)], log=logs)
details['compilerLaunchRetries'] = sum(line.startswith('Compiler launch retry ') for line in logs)
details["linkSeconds"] = time.perf_counter() - before_link
if {p.name: _hash(p) for p in libraries + dlls} != dependencies:
raise RuntimeError("Native dependencies changed during linking; retry with a stable toolchain.")
+10 -3
View File
@@ -74,11 +74,16 @@ def compile_native_program(network: SimulationNetwork) -> NativeProgram:
network.validate_port_supplies()
from .extended import catalog_contracts
contracts = catalog_contracts()
from app.simulation.components.amesim.semantics import validate_numerical_semantics
for component in network.components.values():
if type(component) is not contracts.get(component.model_type):
raise NativeCapabilityError(f'{component.name}: no native contract for {component.model_type}')
if component.MODEL_VERSION != SUPPORTED_VERSIONS.get(component.model_type):
raise NativeCapabilityError(f'{component.name}: native kernel version does not match the component contract')
try:
validate_numerical_semantics(component)
except ValueError as exc:
raise NativeCapabilityError(str(exc)) from exc
# Preserve the compact, verified schedule for its supported topology.
# Both paths emit C; this never falls back to Python numerical execution.
try:
@@ -258,9 +263,11 @@ def _compile_storage_anchored_program(network: SimulationNetwork) -> NativeProgr
put(c, "dvol", f"{get(c, 'vol')} <= {_number(c.cvol0 / 100)} ? 0.0 : ({rate})")
volume, rate = get(c, "vol"), get(c, "dvol")
volume_expressions[c.name] = (volume, rate)
# Match the Python constructor: m/U use configured storage volume;
# connected moving volumes subsequently change recovered p/T.
initial_volume = max(base, c.cvol0 / 100) if c.model_type == "amesim_pnch012" else base
# p0/T0 refer to the complete connected volume at the initial positions.
initial_volume = base
if c.model_type == "amesim_pnch012":
initial_volume += sum(p.effective_area * (p.x0 + mass_at(p, "port_5").x0 - mass_at(p, "port_4").x0) for p in connected)
initial_volume = max(initial_volume, c.cvol0 / 100)
init.append(f"if (!native_gas_init({_number(c.p0)}, {_number(c.T0)}, {_number(initial_volume)}, &y[{si(c, 'm')}])) return 0;")
lines.append(f"NativeGas gas_{gi};")
lines.append(f"if (!native_gas_context(properties, y[{si(c, 'm')}], y[{si(c, 'U')}], {volume}, &gas_{gi})) return 0;")
+112 -22
View File
@@ -6,6 +6,7 @@ All thermodynamics, flow/stream closure and derivatives execute in the EXE.
from __future__ import annotations
import json
import math
import re
from importlib import import_module
@@ -14,6 +15,7 @@ from .contracts import SUPPORTED_VERSIONS
from .schedule import Computation, EvaluationSchedule, references
from .tolerances import state_absolute_tolerance
from .jacobian import StateDependencies, expression_inputs
from app.simulation.components.amesim.semantics import contact_stiffness, validate_numerical_semantics
GAS_TYPES = {'amesim_pnch023', 'amesim_pnch012', 'amesim_pnl0001',
@@ -23,6 +25,10 @@ RESISTORS = {'amesim_pnor001', 'amesim_pnvo001_fixed', 'amesim_pnvo001',
'amesim_pnl00r', 'pipe', 'orifice'}
def is_polytropic(c):
return c.model_type in {'amesim_pnl0001', 'amesim_pnl0002', 'amesim_pnl0003'} and int(c.mode) == 1
def catalog_contracts():
from app.simulation.components.amesim.library import LIBRARY as a
from app.simulation.components.experimental.library import LIBRARY as e
@@ -94,6 +100,10 @@ def compile_extended_program(network):
raise NativeCapabilityError(f'{c.name}: no native contract for {c.model_type} / {type(c).__name__}')
if c.MODEL_VERSION != SUPPORTED_VERSIONS.get(c.model_type):
raise NativeCapabilityError(f'{c.name}: native kernel version does not match the component contract')
try:
validate_numerical_semantics(c)
except ValueError as exc:
raise NativeCapabilityError(str(exc)) from exc
variables = tuple(v for c in components for v in c.result_variable_metadata())
if not variables:
raise NativeCapabilityError('Simulation requires a runtime component; medium definitions alone have no outputs')
@@ -131,7 +141,8 @@ def compile_extended_program(network):
mass_groups = {}
for c in components:
if c.model_type in GAS_TYPES:
fields = ('m1', 'U1', 'm2', 'U2') if c.model_type == 'amesim_pnl0003' else ('m', 'U')
thermal = 'T' if is_polytropic(c) else 'U'
fields = ('m1', thermal+'1', 'm2', thermal+'2') if c.model_type == 'amesim_pnl0003' else ('m', thermal)
add_states(c, fields, [0.0]*len(fields))
elif c.model_type == 'amesim_mecmas21':
root = groups.find((c.name, 'port_1'))
@@ -144,6 +155,17 @@ def compile_extended_program(network):
raise NativeCapabilityError('Rigidly connected masses require consistent initial x/v')
for field in ('v', 'x'):
states[c.name, field] = states[ref.name, field]
friction_groups = []
for group in mass_groups.values():
dry = [c for c in group if c.use_friction and int(c.stoptype) != 3 and c.fstick > 0]
if dry:
ref = group[0]
threshold = min(c.dvel if int(c.frictionType) == 2 else 0 for c in dry)
mode = (1 if ref.v0 > 0 else -1) if abs(ref.v0) > threshold else 0
if mode == 0:
initial[states[ref.name, 'v']] = 0.0
add_states(ref, ('_friction_mode',), (mode,))
friction_groups.append((group, dry, threshold))
if len(state_keys)>1024 or len(variables)>16384:
raise NativeCapabilityError('Native model exceeds the 1024-state / 16384-output resource limit')
# Algebraic models use an internal constant state; the public state map stays empty.
@@ -270,13 +292,25 @@ def compile_extended_program(network):
initial_volume=(max(c.cvol0+sum(c.external_volumes.values()),c.cvol0/100)
if kind=='amesim_pnch012' else
c.cvol if kind=='amesim_pnch023' else c.V if kind in ('cylinder','tank') else c.volume)
gas_initializers.append(f'if(!native_medium_init({medium(c)},{num(p0)},{num(T0)},{num(initial_volume)},{int(kind in ("cylinder","tank"))},&y[{states[c.name,"m"+suffix]}])) return 0;')
lines.append(f'if(!native_medium_gas_context(gas_properties,{medium(c)}, {y(c,"m"+suffix)}, {y(c,"U"+suffix)}, {V}, &{gas})) return 0;')
gas_inputs = expression_inputs(f'{y(c,"m"+suffix)}+{y(c,"U"+suffix)}+({V})')
if kind == 'amesim_pnch012':
def initial_x(d, port):
return mass_groups[groups.find(ep(d, port))][0].x0
initial_volume = max(c.cvol0 + sum(c.external_volumes.values()) + sum(
d.effective_area * (d.x0 + initial_x(d, 'port_5') - initial_x(d, 'port_4'))
for d in attached), c.cvol0 / 100)
thermal = ('T' if is_polytropic(c) else 'U') + suffix
if is_polytropic(c):
gas_initializers += [f'{y(c,"m"+suffix)}={num(p0*initial_volume/(c.medium.R_gas*T0))};', f'{y(c,thermal)}={num(T0)};']
gas_function = 'native_polytropic_gas_context'
else:
gas_initializers.append(f'if(!native_medium_init({medium(c)},{num(p0)},{num(T0)},{num(initial_volume)},{int(kind in ("cylinder","tank"))},&y[{states[c.name,"m"+suffix]}])) return 0;')
gas_function = 'native_medium_gas_context'
lines.append(f'if(!{gas_function}(gas_properties,{medium(c)}, {y(c,"m"+suffix)}, {y(c,thermal)}, {V}, &{gas})) return 0;')
gas_inputs = expression_inputs(f'{y(c,"m"+suffix)}+{y(c,thermal)}+({V})')
for field in ('p','T','rho','u','h'):
dependencies.assign(gas+'.'+field, gas_inputs)
for field in ('m', 'U'):
put(c, field+suffix, y(c, field+suffix))
put(c, 'm'+suffix, y(c, 'm'+suffix))
put(c, 'U'+suffix, f'{y(c,"m"+suffix)}*{gas}.u' if is_polytropic(c) else y(c,'U'+suffix))
for field in ('p','T','rho','u','h'):
put(c, field+suffix, gas+'.'+field)
anchored = pnames(c)
@@ -296,6 +330,10 @@ def compile_extended_program(network):
if len(partitions)<2: continue
if any(volume is None for _,volume in partitions.values()) or len({id(c.medium) for c,_ in partitions.values()})!=1:
raise NativeCapabilityError('Direct gas-storage coupling requires compatible fixed pipe compliances; insert a resistance between independent chambers')
laws = {(is_polytropic(c), c.k if is_polytropic(c) else None) for c,_ in partitions.values()}
if len(laws) != 1:
raise NativeCapabilityError('Directly coupled pipe compliances require the same thermodynamic mode and polytropic exponent')
poly = next(iter(laws))[0]
offsets=[];volumes=[]
for (name,half),(c,volume) in partitions.items():
offsets.append(states[name,'m'+(str(half) if half else '')]);volumes.append(volume)
@@ -305,11 +343,12 @@ def compile_extended_program(network):
if max(values)-min(values)>1e-9*max(1,*map(abs,values)):
raise NativeCapabilityError('Ideally coupled pipe compliances require consistent initial pressure and temperature')
total=sum(volumes)
project.append('{ double mass='+ '+'.join(f'y[{i}]' for i in offsets)+',energy='+ '+'.join(f'y[{i+1}]' for i in offsets)+';')
thermal_sum = '+'.join(f'y[{i+1}]*{num(v/total)}' if poly else f'y[{i+1}]' for i,v in zip(offsets,volumes))
project.append('{ double mass='+ '+'.join(f'y[{i}]' for i in offsets)+',thermal='+thermal_sum+';')
for i,volume in zip(offsets,volumes):
project += [f'projected[{i}]=mass*{num(volume/total)};projected[{i+1}]=energy*{num(volume/total)};']
project += [f'projected[{i}]=mass*{num(volume/total)};projected[{i+1}]=thermal*{num(1 if poly else volume/total)};']
project.append('}')
coupled.append((root,offsets,volumes))
coupled.append((root,offsets,volumes,poly))
dependencies.project_states(offsets)
dependencies.project_states([i+1 for i in offsets])
for root, gas in anchor.items():
@@ -379,7 +418,7 @@ def compile_extended_program(network):
for terms,rhs in flow_eq:
known=''.join(f'-({num(v)})*{k}' for k,v in terms.items() if k in flow_known)
reduced.append(({k:v for k,v in terms.items() if k not in flow_known},rhs+known))
free_flows={f'q[{pi[e]}]' for root,_,_ in coupled for e in pneu if groups.find(e)==root}
free_flows={f'q[{pi[e]}]' for root,_,_,_ in coupled for e in pneu if groups.find(e)==root}
flow_bindings=linear_assignments(reduced,unknownq,free_flows)
flow_temporary_count=max((int(key[1:])+1 for key,_ in flow_bindings if key.startswith('b')),default=0)
for key,expr in flow_bindings:
@@ -474,7 +513,7 @@ def compile_extended_program(network):
elif kind=='amesim_lstp00a':
put(c,'gap',f'{num(c.gap0)}+{w(c,"port_2.x")}-{w(c,"port_1.x")}')
put(c,'penetration',f'fmax(-{w(c,"gap")},0)')
put(c,'force',f'native_contact({w(c,"penetration")},{w(c,"port_1.v")}-{w(c,"port_2.v")},{num(c.kcont)},{num(c.rcont)},{num(c.Pdis)},{int(c.discContactOption)})')
put(c,'force',f'native_contact({w(c,"penetration")},{w(c,"port_1.v")}-{w(c,"port_2.v")},{num(contact_stiffness(c))},{num(c.rcont)},{num(c.Pdis)},{int(c.discContactOption)})')
feq += [({w(c,'port_1.f'):1},w(c,'force')),({w(c,'port_2.f'):1},'-'+w(c,'force'))]
elif kind=='amesim_pnrp17':
put(c,'pressure_force',f'({w(c,"port_1.p")}-101300)*{num(c.effective_area)}')
@@ -484,19 +523,56 @@ def compile_extended_program(network):
elif kind=='amesim_mecmas21':
v,x=w(c,'v'),w(c,'x')
put(c,'Fvisc',f'-{num(c.rvisc)}*{v}' if c.use_friction else '0.0')
put(c,'Ffric',f'{v}>0?-{num(c.fcoul)}:({v}<0?{num(c.fcoul)}:0)' if c.use_friction else '0.0')
put(c,'Ffric','0.0')
for field,penetration,velocity,suffix in [('Fmin',num(c.xmin)+'-'+x,'-'+v,'min'),('Fmax',x+'-'+num(c.xmax),v,'max')]:
expr=f'native_limit_force({penetration},{velocity},{num(getattr(c,"Kb"+suffix))},{num(getattr(c,"Db"+suffix))},{num(getattr(c,"Pd"+suffix))},{int(c.discContactOption)})' if int(c.stoptype)==2 else '0.0'
put(c,field,expr)
ref=mass_groups[groups.find(ep(c,'port_1'))][0]
extra=f'{w(c,"Fvisc")}+{w(c,"Ffric")}+{w(c,"Fmin")}-{w(c,"Fmax")}'
if c.use_friction: extra+=f'-{num(c.wind)}*{v}*fabs({v})'
extra += f'+({num(c.mass * 9.80665 * math.sin(math.radians(c.theta)))})'
feq.append(({w(c,'port_1.f'):1,w(c,'port_2.f'):1,w(ref,'a'):-c.mass},f'-({extra})'))
unknownf=[w(c,name+'.f') for c in components for name in mnames(c)]+[w(group[0],'a') for group in mass_groups.values()]
# Keep the same elimination/order while registering its structured bindings.
for target, expression in linear_assignments(feq, unknownf):
mechanical_assignments = linear_assignments(feq, unknownf)
for target, expression in mechanical_assignments:
lines.append(f'{"double " if target.startswith("b") else ""}{target} = {expression};')
dependencies.expression(target, expression)
for index, (group, dry, threshold) in enumerate(friction_groups):
ref = group[0]
drive = f'friction_drive_{index}'
expression = f'{num(sum(c.mass for c in group))}*{w(ref,"a")}'
lines.append(f'double {drive}={expression};')
dependencies.expression(drive, expression)
# Amesim ideal stops take priority over dry friction. A blocked mass
# carries its load through the stop, not through an extra friction force.
blocked = f'friction_blocked_{index}'
ideal = [c for c in group if int(c.stoptype) == 1]
conditions = []
for c in ideal:
x, v = y(ref, 'x'), y(ref, 'v')
conditions += [f'({x}<={num(c.xmin+1e-12*max(abs(c.xmin),1))} && {v}<=1e-12 && {drive}<=0)',
f'({x}>={num(c.xmax-1e-12*max(abs(c.xmax),1))} && {v}>=-1e-12 && {drive}>=0)']
blocked_expression = ' || '.join(conditions) or '0'
lines.append(f'double {blocked}=({blocked_expression});')
dependencies.expression(blocked, blocked_expression)
lines.append(f'{drive}={blocked}?0:{drive};')
dependencies.expression(drive, f'{blocked}?0:{drive}')
lines.append(f'if(friction_drives) friction_drives[{index}]={drive};')
capacity = sum(c.fstick for c in dry)
for c in dry:
put(c, 'Ffric', f'{blocked}?0:native_dry_friction({y(ref,"v")},{drive}*{num(c.fstick/capacity)},'
f'{num(c.fcoul)},{num(c.fstick)},{num(c.astrib)},'
f'{int(int(c.frictionType)==2 and int(c.strib)==2)},{y(ref,"_friction_mode")})')
lines.append(f'dy[{states[ref.name,"_friction_mode"]}]=0;')
dependencies.expression(f'dy[{states[ref.name,"_friction_mode"]}]', '0')
if friction_groups:
# The first linear solve supplies the drive without dry friction. The
# second redistributes actual friction forces through the same network.
# No pressure/flow iteration or mutable RHS mode is introduced.
for target, expression in mechanical_assignments:
lines.append(f'{target}={expression};')
dependencies.expression(target, expression)
for c in components:
for name in mnames(c): assigned.add(c.name+'.'+name+'.f')
if c.model_type=='amesim_lmechn1':
@@ -545,11 +621,13 @@ def compile_extended_program(network):
if kind.startswith('amesim_pnch'):
heat=f'{num(c.kth*c.sth)}*({num(c.extemp)}-{gas}.T)-{gas}.p*({volume_rate[c.name]})'
elif kind.startswith('amesim_pnl') and int(c.mode)!=1:
temp=f'.5*({gases[c.name,1]}.T+{gases[c.name,2]}.T)' if half else gas+'.T'
temp=gas+'.T'
heat=f'{num(c.kth*c.exchange_area/(2 if half else 1))}*({num(c.extemp)}-({temp}))'
lines += [f'dy[{states[c.name,"m"+suffix]}]={mass};',f'dy[{states[c.name,"U"+suffix]}]={energy}+({heat});']
thermal = ('T' if is_polytropic(c) else 'U') + suffix
thermal_rhs = f'{num(c.k-1)}*{gas}.T/({y(c,"m"+suffix)})*({mass})' if is_polytropic(c) else f'{energy}+({heat})'
lines += [f'dy[{states[c.name,"m"+suffix]}]={mass};',f'dy[{states[c.name,thermal]}]={thermal_rhs};']
dependencies.expression(f'dy[{states[c.name,"m"+suffix]}]', mass)
dependencies.expression(f'dy[{states[c.name,"U"+suffix]}]', f'{energy}+({heat})')
dependencies.expression(f'dy[{states[c.name,thermal]}]', thermal_rhs)
if kind.startswith('amesim_pnl'):
diag=[]
if kind=='amesim_pnl0002':
@@ -578,10 +656,11 @@ def compile_extended_program(network):
expr=f'acc[{i}]/{len(diag)}'
put(c,field,f'fmin({expr},64000000)' if field=='ff' else expr)
lines.append('}')
for _,offsets,volumes in coupled:
lines.append('{ double mass='+ '+'.join(f'dy[{i}]' for i in offsets)+',energy='+ '+'.join(f'dy[{i+1}]' for i in offsets)+';')
for _,offsets,volumes,poly in coupled:
thermal_sum = '+'.join(f'dy[{i+1}]*{num(v/sum(volumes))}' if poly else f'dy[{i+1}]' for i,v in zip(offsets,volumes))
lines.append('{ double mass='+ '+'.join(f'dy[{i}]' for i in offsets)+',thermal='+thermal_sum+';')
for i,volume in zip(offsets,volumes):
lines.append(f'dy[{i}]=mass*{num(volume/sum(volumes))};dy[{i+1}]=energy*{num(volume/sum(volumes))};')
lines.append(f'dy[{i}]=mass*{num(volume/sum(volumes))};dy[{i+1}]=thermal*{num(1 if poly else volume/sum(volumes))};')
dependencies.assign(f'dy[{i}]', (f'dy[{j}]' for j in offsets))
dependencies.assign(f'dy[{i+1}]', (f'dy[{j+1}]' for j in offsets))
lines.append('}')
@@ -590,14 +669,21 @@ def compile_extended_program(network):
if not state_keys: lines.append('dy[0]=0;')
jacobian = dependencies.build()
np,ng,nq=max(1,len(pgroups)),max(1,gas_count),max(1,len(pneu))
# Advanced-friction reference runs at 5 N and 7 N confirm Amesim 2404's
# 0.1% breakaway hysteresis. Simple friction uses the exact static threshold.
source='\n'.join(['#include "model.h"','#include <math.h>',*declarations,
f'const NativeFriction model_frictions[{max(1,len(friction_groups))}] = {{'+(','.join(
'{'+str(states[group[0].name,'v'])+','+str(states[group[0].name,'_friction_mode'])+','+num(threshold)+','+
num(sum(c.fstick*(1.001 if int(c.frictionType)==2 else 1) for c in dry))+'}'
for group,dry,threshold in friction_groups) or '{0,0,0,0}')+'};',
f'const NativeStop model_stops[{max(1,len(stops))}] = {{'+(','.join('{'+str(s[0])+','+','.join(num(v) for v in s[1:])+'}' for s in stops) or '{0,0,0,0,0,0,0}')+'};',
'const double model_atol[NSTATES] = {'+','.join(map(state_absolute_tolerance, state_keys or ['dummy']))+'};',
'const char *const model_output_keys[NOUTPUTS] = {'+(','.join(json.dumps(v.key,ensure_ascii=True) for v in variables) or '""')+'};',
*jacobian.source_lines(),
*schedule_helpers,
'int model_init(double *y) {',*[f'y[{i}]={num(v)};' for i,v in enumerate(initial)],*gas_initializers,'return 1;}',
'static int model_eval_internal(double t,const double *y,double *dy,double *w,int canonical) {',
'static int model_eval_internal(double t,const double *y,double *dy,double *w,int canonical,double *friction_drives) {',
'(void)friction_drives;',
f'NativePropertyState property_states[{min(256,max(16,4*gas_count+2*len(components)))}];',
'NativePropertyCache property_cache, *properties=&property_cache;',
'native_properties_init(properties,property_states,sizeof(property_states)/sizeof(property_states[0]));',
@@ -609,8 +695,9 @@ def compile_extended_program(network):
'(void)t;(void)y;(void)w;(void)p;(void)h;(void)q;(void)g;',*lines,
'for(int i=0;i<NSTATES;i++) if(!isfinite(dy[i])) return 0;',
'for(int i=0;i<NOUTPUTS;i++) if(!isfinite(w[i])) return 0;','return 1;}',
'int model_eval(double t,const double *y,double *dy,double *w) {return model_eval_internal(t,y,dy,w,0);}',
'int model_eval_jacobian(double t,const double *y,double *dy,double *w) {return model_eval_internal(t,y,dy,w,1);}',
'int model_eval(double t,const double *y,double *dy,double *w) {return model_eval_internal(t,y,dy,w,0,NULL);}',
'int model_eval_jacobian(double t,const double *y,double *dy,double *w) {return model_eval_internal(t,y,dy,w,1,NULL);}',
'int model_friction_drives(double t,const double *y,double *drives) {double dy[NSTATES],w[NOUTPUTS];return model_eval_internal(t,y,dy,w,0,drives);}',
'double model_next_break(double t,double end) { double result=end;(void)t;',*breaks,'return result;}',''])
header=f'''#ifndef GENERATED_NATIVE_MODEL_H
#define GENERATED_NATIVE_MODEL_H
@@ -618,6 +705,9 @@ def compile_extended_program(network):
#define NSTATES {nstates}
#define NOUTPUTS {max(1,len(variables))}
#define NSTOPS {len(stops)}
#define NFRICTIONS {len(friction_groups)}
extern const NativeFriction model_frictions[{max(1,len(friction_groups))}];
int model_friction_drives(double t,const double *y,double *drives);
extern const NativeStop model_stops[{max(1,len(stops))}];
extern const double model_atol[NSTATES];
extern const char *const model_output_keys[NOUTPUTS];
+1 -1
View File
@@ -19,7 +19,7 @@ _IDENTIFIER = re.compile(r'\b[A-Za-z_]\w*\b')
_NUMBER = re.compile(r'(?<![\w.])(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?')
_FUNCTIONS = frozenset({
'fmax', 'fmin', 'fabs', 'sqrt', 'copysign', 'pow',
'native_signal', 'native_contact', 'native_limit_force',
'native_signal', 'native_contact', 'native_limit_force', 'native_dry_friction',
'native_pipe_flow_context', 'native_pipe_flow_cached_context',
'native_temperature_ph_context', 'native_density_context',
})
+2 -2
View File
@@ -8,13 +8,13 @@ EXPORTS = {
"native_properties_init", "native_temperature_ph_context", "native_density_context",
"native_gas_init", "native_gas", "native_gas_context", "native_medium_init",
"native_density", "native_temperature_ph", "native_viscosity",
"native_medium_gas", "native_medium_gas_context",
"native_medium_gas", "native_medium_gas_context", "native_polytropic_gas_context",
}),
"orifice": frozenset({"native_orifice", "native_orifice_context", "native_medium_orifice", "native_medium_orifice_context"}),
"pipe": frozenset({"native_pipe_resistance", "native_pipe_flow", "native_pipe_flow_context",
"native_pipe_flow_cached", "native_pipe_flow_cached_context",
"native_pipe_diagnostics", "native_pipe_diagnostics_context"}),
"mechanics": frozenset({"native_contact", "native_stop_motion", "native_limit_force"}),
"mechanics": frozenset({"native_contact", "native_stop_motion", "native_limit_force", "native_dry_friction"}),
"signal": frozenset({"native_signal", "native_signal_break"}),
}
DEPENDENCIES = {"orifice": ("properties",), "pipe": ("properties",)}
@@ -0,0 +1,35 @@
"""Portable process setup and classification for the native compiler driver.
MinGW maps different CreateProcess failures to ENOENT. That text does not prove
the compiler/subprogram is missing; never treat a C diagnostic as a launch retry.
"""
from __future__ import annotations
import os
from pathlib import Path
import re
import shutil
IS_WINDOWS = os.name == 'nt'
LAUNCH_RETRY_DELAYS = (0.05, 0.1, 0.2, 0.35, 0.5)
def command_environment(executable: str) -> dict[str, str] | None:
if not IS_WINDOWS:
return None # Preserve the Linux toolchain and environment unchanged.
env = os.environ.copy()
path_key = next((key for key in env if key.upper() == 'PATH'), 'PATH')
directory = str(Path(shutil.which(executable) or executable).resolve().parent)
env[path_key] = directory + os.pathsep + env.get(path_key, '')
return env
def is_driver_launch_failure(stderr: str) -> bool:
# GCC 8 emits "gcc.exe: error: CreateProcess: ..."; other releases add
# "cannot execute 'cc1':". Anchor the driver diagnostic, not source text.
return IS_WINDOWS and bool(re.search(
r'(?im)^(?:[^\r\n]*[/\\])?[\w.+-]*gcc(?:\.exe)?: (?:fatal )?error: (?:cannot execute [^\r\n]*: )?CreateProcess:', stderr))
def is_transient_process_error(exc: OSError) -> bool:
return IS_WINDOWS and getattr(exc, 'winerror', None) in (5, 32, 1450, 1455)
+208
View File
@@ -0,0 +1,208 @@
"""Small, uncached compile/link/run check using the production native toolchain."""
from __future__ import annotations
import os
from pathlib import Path
import shutil
import signal
import subprocess
import tempfile
from threading import Event
from time import monotonic
from typing import Callable
from .build import CACHE, NativeCommandError, link_library_arguments, platform_build_inputs, toolchain
from .processes import command_environment, is_driver_launch_failure
STAGE_MESSAGES = {
"toolchain": "正在查找编译器与 SUNDIALS",
"preparing": "正在准备最小自检程序",
"preprocessing": "正在预处理最小自检程序",
"compiling": "正在编译最小自检程序",
"linking": "正在链接最小自检程序",
"running": "正在运行最小自检程序",
"schema": "正在检查模型 XML 解析环境",
}
OUTPUT_MARKER = "native-runtime-check-ok"
# One smooth scalar equation exercises every library used by production BDF.
# Its known answer is independent of the user's model and cannot hit model cache.
SOURCE = r'''
#include <cvode/cvode.h>
#include <cvode/cvode_ls.h>
#include <nvector/nvector_serial.h>
#include <sunmatrix/sunmatrix_dense.h>
#include <sunlinsol/sunlinsol_dense.h>
#include <math.h>
#include <stdio.h>
static int rhs(sunrealtype t, N_Vector y, N_Vector dy, void *data) {
(void)t; (void)data;
NV_Ith_S(dy,0)=-NV_Ith_S(y,0);
return 0;
}
int main(void) {
SUNContext context=NULL;
N_Vector y=NULL;
SUNMatrix matrix=NULL;
SUNLinearSolver linear=NULL;
void *solver=NULL;
int result=1;
sunrealtype time=0;
if (SUNContext_Create(SUN_COMM_NULL,&context)) goto done;
y=N_VNew_Serial(1,context);
if (!y) goto done;
NV_Ith_S(y,0)=1;
matrix=SUNDenseMatrix(1,1,context);
if (!matrix) goto done;
linear=SUNLinSol_Dense(y,matrix,context);
solver=CVodeCreate(CV_BDF,context);
if (!linear || !solver) goto done;
if (CVodeInit(solver,rhs,0,y)<0 ||
CVodeSStolerances(solver,1e-8,1e-10)<0 ||
CVodeSetLinearSolver(solver,linear,matrix)<0 ||
CVode(solver,0.1,y,&time,CV_NORMAL)<0) goto done;
if (!isfinite(NV_Ith_S(y,0)) || fabs(NV_Ith_S(y,0)-exp(-0.1))>1e-6) goto done;
puts("native-runtime-check-ok");
result=0;
done:
if (result) fputs("Minimal BDF self-check failed.\n",stderr);
if (solver) CVodeFree(&solver);
if (linear) SUNLinSolFree(linear);
if (matrix) SUNMatDestroy(matrix);
if (y) N_VDestroy(y);
if (context) SUNContext_Free(&context);
return result;
}
'''
class CheckCancelled(Exception):
pass
class CheckFailure(RuntimeError):
def __init__(self, message: str, *, command=(), cwd=None, exit_code=None,
stdout="", stderr="", error_type="RuntimeError"):
super().__init__(message)
self.details = {"command": list(command), "cwd": str(cwd) if cwd else None,
"exitCode": exit_code, "stdout": stdout[-8000:],
"stderr": stderr[-8000:], "errorType": error_type}
def _stop_process_tree(process: subprocess.Popen) -> None:
# GCC starts cc1/as/ld. Killing only its driver can leave inherited pipes open.
if os.name == "nt":
try:
subprocess.run(["taskkill", "/PID", str(process.pid), "/T", "/F"],
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, timeout=5,
creationflags=subprocess.CREATE_NO_WINDOW)
except (OSError, subprocess.SubprocessError):
pass
if process.poll() is None:
process.kill()
else:
try:
os.killpg(process.pid, signal.SIGKILL)
except ProcessLookupError:
pass
def _command(command: list[str], directory: Path, stop: Event, timeout: float = 30) -> str:
if stop.is_set():
raise CheckCancelled()
try:
with subprocess.Popen(command, cwd=directory, stdin=subprocess.DEVNULL,
stdout=subprocess.PIPE, stderr=subprocess.PIPE,
env=command_environment(command[0]), close_fds=True,
start_new_session=os.name != "nt",
creationflags=subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0) as process:
deadline = monotonic() + timeout
while True:
try:
stdout, stderr = process.communicate(timeout=0.2)
break
except subprocess.TimeoutExpired:
if stop.is_set() or monotonic() >= deadline:
_stop_process_tree(process)
stdout, stderr = process.communicate()
if stop.is_set():
raise CheckCancelled()
raise CheckFailure(f"命令执行超过 {timeout:g} 秒", command=command,
cwd=directory, exit_code=process.returncode,
stdout=stdout.decode("utf-8", errors="replace"),
stderr=stderr.decode("utf-8", errors="replace"),
error_type="TimeoutExpired")
except OSError as exc:
raise CheckFailure(str(exc), command=command, cwd=directory,
error_type=type(exc).__name__) from exc
output = stdout.decode("utf-8", errors="replace")
if process.returncode:
stderr_text = stderr.decode('utf-8', errors='replace')
message = ('GCC 已启动,但无法启动编译子进程;该错误不能直接判定为编译器不存在'
if is_driver_launch_failure(stderr_text) else '命令执行失败')
# Whole-check retries remain owned by warmup (initial check + 5 rounds).
raise CheckFailure(message, command=command, cwd=directory,
exit_code=process.returncode, stdout=output,
stderr=stderr_text,
error_type="CalledProcessError")
return output
def check_native_runtime(progress: Callable[..., None], stop: Event) -> None:
def stage(name: str, **details):
if stop.is_set():
raise CheckCancelled()
progress(stage=name, message=STAGE_MESSAGES[name], **details)
candidate = os.environ.get("SIMULATION_NATIVE_CC") or shutil.which("gcc")
stage("toolchain", compiler=candidate)
try:
compiler, sundials, version = toolchain()
except NativeCommandError as exc:
details = exc.details
failure = CheckFailure(str(exc), command=details['command'], cwd=details['cwd'],
exit_code=details['exitCode'], stdout=details['stdout'], stderr=details['stderr'],
error_type=details['errorType'])
failure.details['attempts'] = details['attempts']
raise failure from exc
except subprocess.SubprocessError as exc:
def decoded(value):
return value.decode("utf-8", errors="replace") if isinstance(value, bytes) else (value or "")
raise CheckFailure(str(exc), command=getattr(exc, "cmd", ()), cwd=Path.cwd(),
exit_code=getattr(exc, "returncode", None),
stdout=decoded(getattr(exc, "stdout", "")),
stderr=decoded(getattr(exc, "stderr", "")),
error_type=type(exc).__name__) from exc
except OSError as exc:
raise CheckFailure(str(exc), command=[candidate, "--version"] if candidate else [],
cwd=Path.cwd(), error_type=type(exc).__name__) from exc
compiler = str(Path(shutil.which(compiler) or compiler).resolve())
stage("preparing", compiler=compiler, compilerVersion=version, sundialsRoot=str(sundials))
flags, libraries, dlls, executable_name = platform_build_inputs(sundials)
# Keep the probe on the build filesystem: Linux /tmp may be mounted noexec.
# Disposable probes never populate or evict the model/object caches.
directory_root = CACHE.parent / "native-runtime-checks"
directory_root.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory(prefix="check-", dir=directory_root) as temporary:
directory = Path(temporary).resolve()
source, preprocessed, obj = directory / "check.c", directory / "check.i", directory / "check.o"
executable = directory / executable_name
source.write_text(SOURCE, encoding="utf-8", newline="\n")
stage("preprocessing")
_command([compiler, *flags, "-E", "-P", f"-fmacro-prefix-map={directory}=/generated",
"-I", str(sundials / "include"), str(source), "-o", str(preprocessed)], directory, stop)
stage("compiling")
_command([compiler, *flags, "-x", "cpp-output", "-c", str(preprocessed), "-o", str(obj)], directory, stop)
stage("linking")
_command([compiler, *flags, str(obj), *link_library_arguments(libraries),
"-lm", "-o", str(executable)], directory, stop)
stage("running")
for dll in dlls:
shutil.copyfile(dll, directory / dll.name)
output = _command([str(executable)], directory, stop, timeout=10)
if output.strip() != OUTPUT_MARKER:
raise CheckFailure("最小程序未返回预期的自检成功标记", command=[str(executable)],
cwd=directory, exit_code=0, stdout=output, error_type="UnexpectedOutput")
stage("schema")
from app.system_xml import validate_system_xml_document
validate_system_xml_document(b"<System/>")