function out=run_thermal_conditioning() % Bounded postprocessing only. No modification/rerun of the original model. here=fileparts(mfilename('fullpath')); root=fileparts(fileparts(fileparts(fileparts(fileparts(here))))); model=fullfile(root,'reports','04_models','R23-001'); pub=fullfile(root,'reports','06_reports','03_endurance_simulation','SIM-R3-001'); base=fullfile(root,'reports','05_simulation','R23-001','runs','SIM-R3-001-he-v2'); dest=fullfile(root,'reports','05_simulation','R23-001','thermal-conditioning-supplement'); assert(strcmp(version('-release'),'2025a'),'Use MATLAB R2025a.'); if ~isfolder(dest),mkdir(dest);end addpath(fullfile(model,'matlab')); diary(fullfile(dest,'matlab-postprocessing.log')); started=char(datetime('now','TimeZone','UTC')); frozen=jsondecode(fileread(fullfile(base,'run-receipt-all-completed.json'))); assert(strcmp(frozen.status,'completed')); verify_frozen(frozen); reportFile=fullfile(pub,'report-data.json'); assert(strcmp(r23.sha256_file(reportFile),'6e9c51d39dd24bb7a9a9ff71aee42ab2011a858dbde905e8851e95c2648406fd')); d=jsondecode(fileread(reportFile)); assert(strcmp(d.Storage.cryo_budget_revision,'CRYO-BUDGET-1.2.0')); c=d.Mission.scenarios(d.Mission.selected_index).input_settings; designFile=fullfile(model,'inputs','r2-integration.json'); design=jsondecode(fileread(designFile)); registryFile=fullfile(model,'all-engineering-parameters.csv'); reg=readtable(registryFile,'TextType','string','Encoding','UTF-8'); fluidFile=fullfile(base,'full-analysis-results.mat'); loaded=load(fluidFile,'out');f=loaded.out.fluid.normal;clear loaded; assert(strcmp(f.name,'Hydrogen'),'All UAV comparisons require normal-H2.'); Ttarget=c.ambient_T_K; Pgauge=design.fuel_supply.IE_inlet_pressure_gauge_bar.nominal; barToPa=100000;Ptarget=c.base_pressure_Pa+Pgauge*barToPa; rootTol=1e-12;pressureTol=0.1;integralRelTol=1e-10; guess=Ptarget/(f.R*Ttarget); rhoTarget=fzero(@target_residual,[.5*guess,2*guess],optimset('TolX',rootTol,'Display','off')); target=r23.eos(Ttarget,rhoTarget,f); QtankCap=registry_value('CRYO.recovery_cap'); QtankFraction=registry_value('CRYO.recovery_fraction'); parameters={ 'target_T',Ttarget,'K','research target: inherited 25degC ambient, NOT an OEM measured inlet requirement','report-data.Mission.selected.input_settings.ambient_T_K'; 'inlet_gauge_pressure',Pgauge,'bar(g)','IE manual nominal, inherited frozen R2 input; pressure-drop qualification pending','r2-integration.json fuel_supply.IE_inlet_pressure_gauge_bar.nominal'; 'ambient_pressure',c.base_pressure_Pa,'Pa','inherited reference atmosphere','report-data selected input_settings.base_pressure_Pa'; 'bar_to_Pa',barToPa,'Pa/bar','exact unit conversion','SI unit definition'; 'target_absolute_pressure',Ptarget,'Pa','derived ambient plus nominal gauge pressure','Pambient+Pgauge*bar_to_Pa'; 'target_density',rhoTarget,'kg/m3','native normal-H2 EOS result, not an independent measurement','fzero(pEOS(Ttarget,rho)-Ptarget)'; 'target_enthalpy',target.h_J_kg,'J/kg','native normal-H2 EOS result, same energy reference as tank','r23.eos; Leachman2009'; 'FC_net_efficiency',c.eta_fc_bus,'1','inherited net electrical efficiency scenario','report-data selected input_settings.eta_fc_bus'; 'H2_LHV',c.LHV_J_kg,'J/kg','inherited lower heating value','report-data selected input_settings.LHV_J_kg'; 'tank_recovery_cap',QtankCap,'W','original tank-pressure support branch only, not external HX capacity','all-engineering-parameters.csv CRYO.recovery_cap'; 'tank_recovery_fraction_cap',QtankFraction,'1','original cap relative to chemical-minus-net-power upper bound','all-engineering-parameters.csv CRYO.recovery_fraction'; 'C05_HX_budget',d.Storage.cases(1).vessel.warming_HX_budget_kg,'kg','hardware mass budget only; no UA/thermal-capacity evidence','frozen CRYO-BUDGET-1.2.0 vessel.warming_HX_budget_kg'; 'startup_purge_H2',c.startup_vent_H2_kg,'kg','conditional 1g heating illustration; purge need not all pass through stack','report-data selected input_settings.startup_vent_H2_kg'; 'startup_duration',c.startup_s,'s','original startup interval, no startup thermal-inertia model','report-data selected input_settings.startup_s'; 'seconds_per_hour',3600,'s/h','exact unit conversion','unit definition'; 'target_density_root_TolX',rootTol,'kg/m3','numerical root setting, not physical accuracy','supplement code'; 'prestate_pressure_check_tolerance',pressureTol,'Pa','CSV round-trip/state-alignment check only','supplement code'; 'integral_closure_relative_tolerance',integralRelTol,'1','postprocessing integration check only','supplement code'}; R=cell(size(parameters)); for k=1:size(parameters,1) R(k,:)=parameters(k,:);R{k,2}=num2str(parameters{k,2},17); end PT=cell2table(R,'VariableNames',{'parameter_id','value','unit','identity_or_assumption','source_or_definition'}); writetable(PT,fullfile(dest,'parameters.csv'),'Encoding','UTF-8'); inputFiles={reportFile,designFile,registryFile,fluidFile, ... fullfile(model,'inputs','eos-Hydrogen.json'), ... fullfile(root,'reports','09_reviews','R23-001-manuscript-audit','thermal-conditioning-boundary.md'), ... fullfile(root,'reports','08_collaboration','amendments','R23-001-thermal-conditioning.md')}; for k=1:numel(d.Storage.cases) id=d.Storage.cases(k).design_id; inputFiles=[inputFiles,{fullfile(base,[id '-mission.csv']),fullfile(base,[id '-summary.json'])}]; %#ok end inputHashes=hash_files(inputFiles); dependencies={mfilename('fullpath'),fullfile(model,'matlab','+r23','eos.m'),fullfile(model,'matlab','+r23','thermo_state.m'), ... fullfile(model,'matlab','+r23','write_json.m'),fullfile(model,'matlab','+r23','sha256_file.m')}; dependencies{1}=[dependencies{1} '.m']; codeHashes=hash_files(dependencies); out=struct('task_id','SIM-R3-001-THERMAL-CONDITIONING','supplement_version','TC-1.0.0', ... 'original_model_version',d.model_version,'original_cryo_budget','CRYO-BUDGET-1.2.0', ... 'original_report_data_sha256',r23.sha256_file(reportFile), ... 'target',struct('temperature_K',Ttarget,'pressure_Pa',Ptarget,'enthalpy_J_kg',target.h_J_kg, ... 'density_kg_m3',rhoTarget,'temperature_identity','25degC ambient research target, not an OEM measured inlet temperature'), ... 'method','existing left-step FC power/flow; tank pre-step h_out; two-phase vapor enthalpy; ideal isenthalpic valve; no added latent heat', ... 'off_state_policy','Qwarm and required recovery ratio are NA at FC=0; startup 1g is a separate conditional heat illustration, not validated zero demand', ... 'waste_heat_boundary','chemical input minus net electrical output is only an upper bound, not all accessible/recoverable heat', ... 'endurance_condition','Published cryogenic endurance requires ground preconditioning plus sufficient additional external-HX waste-heat supply; new electric heating requires SOC/endurance recomputation', ... 'physical_limits','No HX UA, pinch/heat-grade, pressure drop, thermal inertia, purge routing, cold start or experimental heat-recovery qualification', ... 'cases',struct([])); allChecks=struct([]); for k=1:numel(d.Storage.cases) id=d.Storage.cases(k).design_id; s=jsondecode(fileread(fullfile(base,[id '-summary.json']))); S=readtable(fullfile(base,[id '-mission.csv']),'VariableNamingRule','preserve'); dt=[diff(S.time_s);s.total_profile_s-S.time_s(end)]; assert(all(dt>0)); active=S.FC_bus_W>0; mfc=S.FC_bus_W/(c.eta_fc_bus*c.LHV_J_kg); chem=mfc*c.LHV_J_kg; upper=chem-S.FC_bus_W; knownWaste=upper;knownWaste(~active)=NaN; thermal=s.feasible&&all(isfinite(S.tank_T_K))&&all(isfinite(S.tank_P_Pa)); n=height(S);hOut=nan(n,1);pOut=hOut;TOut=hOut;qWarm=hOut;ratio=hOut;combo=hOut; phase=repmat("unavailable_base_infeasible_no_tank_history",n,1); initial=r23.thermo_state(s.source_state.T0_K,s.source_state.rho0_kg_m3,f); conditionalStartup=c.startup_vent_H2_kg*max(target.h_J_kg-initial.h_out,0); maxPerror=NaN; if thermal TOut=[s.source_state.T0_K;S.tank_T_K(1:end-1)]; recordedP=[initial.p_Pa;S.tank_P_Pa(1:end-1)]; for j=1:n state=r23.thermo_state(TOut(j),S.H2_kg(j)/s.vessel.V_inner_m3,f); hOut(j)=state.h_out;pOut(j)=state.p_Pa;phase(j)=string(state.phase); end maxPerror=max(abs(pOut-recordedP));assert(maxPerror=Ptarget-pressureTol),'Valve cannot supply the chosen pressure by reduction alone.'); qWarm(active)=mfc(active).*max(target.h_J_kg-hOut(active),0); combo(active)=qWarm(active)+S.heat_recovery_W(active); ratio(active)=combo(active)./upper(active); phase(~active)=phase(~active)+";FC_off_conditioning_NA"; end trace=table(S.time_s,dt,S.segment,S.FC_bus_W,mfc,chem,knownWaste, ... TOut,pOut,hOut,repmat(target.h_J_kg,n,1),qWarm,S.heat_recovery_W,combo,ratio,active,repmat(thermal,n,1),phase, ... 'VariableNames',{'time_s','step_s','original_segment','FC_net_W','FC_H2_flow_kg_s','FC_chemical_W','waste_upper_W', ... 'tank_out_pre_T_K','tank_out_pre_P_Pa','tank_out_h_J_kg','target_h_J_kg','Qwarm_external_W', ... 'recorded_tank_recovered_W','combined_recovery_target_W','minimum_total_recovery_fraction', ... 'FC_operating','thermal_history_available','state_note'}); writetable(trace,fullfile(dest,[id '-trace.csv']),'Encoding','UTF-8'); summary=struct('design_id',id,'route',s.route,'constraint_case',s.constraint_case, ... 'original_feasible',logical(s.feasible),'thermal_history_available',thermal, ... 'valid_full_mission_thermal_target',thermal,'original_flight_s_if_feasible',NaN, ... 'recorded_duration_s',s.total_profile_s,'FC_off_duration_s',sum(dt(~active)), ... 'FC_active_duration_s',NaN,'Qwarm_peak_W',NaN,'Qwarm_mean_FC_active_W',NaN,'Qwarm_integral_J',NaN,'Qwarm_integral_Wh',NaN, ... 'existing_tank_recovered_integral_Wh',NaN,'combined_recovery_peak_W',NaN, ... 'minimum_total_recovery_fraction_peak',NaN,'minimum_total_recovery_fraction_time_mean',NaN, ... 'minimum_total_recovery_fraction_integral',NaN,'waste_upper_integral_Wh',NaN, ... 'conditional_startup_1g_heat_J',conditionalStartup,'conditional_startup_1g_heat_Wh',conditionalStartup/3600, ... 'conditional_startup_identity','only if all 1g purge is warmed at initial tank outlet enthalpy; purge need not enter stack; HX/pipe inertia and ground preconditioning extra', ... 'thermal_target_identity','necessary external-HX/combined recovery target, not qualified hardware or available heat guarantee'); if thermal time=sum(dt(active));warmJ=sum(qWarm(active).*dt(active));combinedJ=sum(combo(active).*dt(active));wasteJ=sum(upper(active).*dt(active)); summary.original_flight_s_if_feasible=s.flight_s; summary.FC_active_duration_s=time; summary.Qwarm_peak_W=max(qWarm(active));summary.Qwarm_mean_FC_active_W=warmJ/time; summary.Qwarm_integral_J=warmJ;summary.Qwarm_integral_Wh=warmJ/3600; summary.existing_tank_recovered_integral_Wh=sum(S.heat_recovery_W.*dt)/3600; summary.combined_recovery_peak_W=max(combo(active)); summary.minimum_total_recovery_fraction_peak=max(ratio(active)); summary.minimum_total_recovery_fraction_time_mean=sum(ratio(active).*dt(active))/time; summary.minimum_total_recovery_fraction_integral=combinedJ/wasteJ; summary.waste_upper_integral_Wh=wasteJ/3600; end check=struct('design_id',id,'FC_H2_integral_error_kg',sum(mfc.*dt)-s.H2_FC_used_kg, ... 'FC_electric_integral_error_J',sum(S.FC_bus_W.*dt)-s.FC_energy_Wh*3600, ... 'prestate_max_pressure_difference_Pa',maxPerror,'tank_recovered_integral_error_Wh',NaN, ... 'off_state_Qwarm_and_ratio_are_NA',all(isnan(qWarm(~active)))&&all(isnan(ratio(~active))), ... 'failed_route_effective_endurance_not_reported',s.feasible||isnan(summary.original_flight_s_if_feasible)); assert(abs(check.FC_H2_integral_error_kg)<=integralRelTol*max(1,s.H2_FC_used_kg)); assert(abs(check.FC_electric_integral_error_J)<=integralRelTol*max(1,s.FC_energy_Wh*3600)); if thermal check.tank_recovered_integral_error_Wh=summary.existing_tank_recovered_integral_Wh-s.heat_recovery_Wh; assert(abs(check.tank_recovered_integral_error_Wh)<=integralRelTol*max(1,s.heat_recovery_Wh)); end assert(check.off_state_Qwarm_and_ratio_are_NA&&check.failed_route_effective_endurance_not_reported); if isempty(out.cases),out.cases=summary;allChecks=check;else,out.cases(end+1)=summary;allChecks(end+1)=check;end fprintf('%s thermal=%d QwarmPeak=%.9gW QwarmMean=%.9gW Qwarm=%.9gWh recoveryPeak=%.9g\n', ... id,thermal,summary.Qwarm_peak_W,summary.Qwarm_mean_FC_active_W,summary.Qwarm_integral_Wh,summary.minimum_total_recovery_fraction_peak); end out.checks=allChecks; out.aggregation_scope='All heat means/integrals and ratios use recorded FC-operating steps only; startup/off periods remain separate NA conditions. Infeasible route has no effective-flight or warming target claim.'; r23.write_json(fullfile(dest,'summary.json'),out); writetable(struct2table(out.cases),fullfile(dest,'summary.csv'),'Encoding','UTF-8'); r23.write_json(fullfile(dest,'checks.json'),allChecks); save(fullfile(dest,'thermal-conditioning-results.mat'),'out','target','PT'); verify_frozen(frozen); for j=1:numel(inputHashes),assert(strcmp(r23.sha256_file(fullfile(root,inputHashes(j).path)),inputHashes(j).sha256));end fprintf('THERMAL_CONDITIONING_POSTPROCESS_COMPLETED\n'); diary('off'); files=dir(fullfile(dest,'*'));outputFiles={}; for j=1:numel(files) if ~files(j).isdir&&~ismember(files(j).name,{'run-receipt.json','native-launch.log'}),outputFiles{end+1}=fullfile(dest,files(j).name);end %#ok end receipt=struct('task_id',out.task_id,'supplement_version',out.supplement_version,'status','completed', ... 'matlab_release',version('-release'),'matlab_version',version,'computer',computer, ... 'started_utc',started,'completed_utc',char(datetime('now','TimeZone','UTC')), ... 'script_entry','reports/04_models/R23-001/supplements/thermal-conditioning/run_thermal_conditioning.m', ... 'randomness','none; deterministic postprocessing','all_original_scientific_source_and_input_hashes_unchanged',true, ... 'original_all_rerun',false,'startup_temp','{PROJECT_CHECKOUT}/.tmp/r23', ... 'source_files',codeHashes,'input_files',inputHashes,'output_files',hash_files(outputFiles)); r23.write_json(fullfile(dest,'run-receipt.json'),receipt); function err=target_residual(rho) state=r23.eos(Ttarget,rho,f);err=state.p_Pa-Ptarget; end function x=registry_value(id) ix=reg.parameter_id==string(id);assert(sum(ix)==1); value=reg.value_or_expression(ix);if isnumeric(value),x=value;else,x=str2double(value);end assert(isscalar(x)&&isfinite(x)); end function list=hash_files(names) list=struct([]); for ii=1:numel(names) z=struct('path',strrep(erase(names{ii},[root filesep]),filesep,'/'),'sha256',r23.sha256_file(names{ii})); if isempty(list),list=z;else,list(end+1)=z;end end end function verify_frozen(record) items=[record.source_files(:);record.input_files(:)]; for ii=1:numel(items),assert(strcmp(r23.sha256_file(fullfile(root,items(ii).path)),items(ii).sha256),'Original science changed');end end end