function summary=run_range() % New zero-wind returnable-range task; reuse the frozen full-mission model. assert(strcmp(version('-release'),'2025a'),'Use MATLAB R2025a.'); here=fileparts(mfilename('fullpath'));modelRoot=fileparts(fileparts(here)); reportsRoot=fileparts(fileparts(modelRoot));projectRoot=fileparts(reportsRoot); addpath(fullfile(modelRoot,'matlab')); climbHelper=fullfile(modelRoot,'supplements','mission-climb','prepare_climb_runtime.m'); assert(isfile(climbHelper),'The supervisor must provide the isolated climb-fix helper before execution.'); addpath(fileparts(climbHelper));climbRuntime=prepare_climb_runtime(projectRoot); modelVersion='R23-HE2.1.0'; assert(strcmp(climbRuntime.model_version,modelVersion)&&climbRuntime.original_sources_unchanged); climbRuntime=portable_paths(climbRuntime,projectRoot); runRoot=fullfile(reportsRoot,'05_simulation','R23-001','runs','SIM-R3-001-he-v2'); output=fullfile(reportsRoot,'05_simulation','R23-001','range-supplement'); assert(~isfile(fullfile(output,'run-receipt.json')),'Preserve the previous range run; do not overwrite a receipt.'); if ~isfolder(output),mkdir(output);end existing=dir(output);existingNames={existing(~[existing.isdir]).name}; launcherFiles={'native-launch.log','native-stderr.log','native-launch-receipt.json'}; assert(isempty(setdiff(existingNames,launcherFiles)),'Preserve prior partial range outputs before retrying.'); diary(fullfile(output,'matlab-range.log'));logGuard=onCleanup(@()diary('off')); started=utc_now();clockStart=tic; originalReceipt=fullfile(runRoot,'run-receipt-all-completed.json'); original=jsondecode(fileread(originalReceipt));originalFiles=[original.source_files;original.input_files]; assert(numel(originalFiles)==73,'Unexpected original scientific manifest.'); verify_files(originalFiles,projectRoot); inputFile=fullfile(runRoot,'full-analysis-results.mat'); sourceNames={'run_range.m'};sources=file_manifest(fullfile(here,sourceNames),projectRoot); packageNames={'parameters.m','mission_settings.m','simulate_mission.m','propulsion_power.m','power.m','write_json.m','save_figure.m','sha256_file.m'}; sources=[sources;file_manifest(fullfile(modelRoot,'matlab','+r23',packageNames),projectRoot)]; sources=[sources;file_manifest({climbHelper},projectRoot)]; inputs=file_manifest({inputFile,originalReceipt,fullfile(here,'README.md'), ... fullfile(reportsRoot,'08_collaboration','contracts','SIM-R3-003-range.md'), ... fullfile(reportsRoot,'docs','memory','R23-REQUIREMENTS-CARD.md')},projectRoot); loaded=load(inputFile,'out');frozen=loaded.out;clear loaded; assert(strcmp(frozen.model_version,'R23-HE2.0.0'),'Unexpected frozen hardware/input source.'); p=r23.parameters(); cfg=struct('version','RANGE-1.0.0','wind_m_s',0,'payload_kg',1, ... 'speed_min_m_s',5,'speed_max_m_s',20,'coarse_speed_step_m_s',1, ... 'fine_speed_step_m_s',0.5,'fine_neighborhood_m_s',1, ... 'coarse_leg_step_s',60,'fine_leg_step_s',30,'initial_leg_upper_s',1800, ... 'maximum_leg_s',8*3600,'simulation_dt_s',2,'work_s',0, ... 'distance_definition','horizontal round trip=2*airspeed*single_leg_time; zero wind; radius=airspeed*single_leg_time', ... 'search_scope','two screened fixed-hardware candidates; speed grid plus local refinement; no global optimality certificate'); ids={'IE24_A9_P80G30_NOCHG','IE24_A12_P80G30_NOCHG'}; r23.write_json(fullfile(output,'settings.json'),cfg); parameterRows={'all','wind',0,'m/s','new round-trip task'; ... 'all','payload',1,'kg','unchanged frozen task'; ... 'all','speed_min',5,'m/s','new speed search';'all','speed_max',20,'m/s','original declared speed domain'; ... 'all','coarse_speed_step',1,'m/s','engineering grid';'all','fine_speed_step',0.5,'m/s','local engineering refinement'; ... 'all','fine_neighborhood',1,'m/s','around each branch coarse winner'; ... 'all','coarse_leg_step',60,'s','engineering time grid';'all','fine_leg_step',30,'s','engineering time grid'; ... 'all','initial_leg_upper',1800,'s','bracket seed';'all','maximum_leg',cfg.maximum_leg_s,'s','numerical ceiling; flag if reached'; ... 'all','simulation_dt',cfg.simulation_dt_s,'s','same model integration; shared by search and final'; ... 'all','work',0,'s','new closed round-trip task'}; branchResults=struct([]);scans=cell(1,numel(ids));bestRanges=-inf(1,numel(ids)); for bi=1:numel(ids) index=find(strcmp({frozen.integration.branches.design_id},ids{bi})); assert(isscalar(index),'Required hardware branch is missing/duplicated.'); b=frozen.integration.branches(index);c=r23.mission_settings(b,p); assert(abs(b.payload_kg-cfg.payload_kg)<1e-9 && c.charge_current_A==0,'Keep1kg/no-charge hardware.'); assert(~isfield(c,'storage'),'This supplement is ambient compressed hydrogen only.'); base=struct('branch_id',b.design_id,'original_power_screen_pass',logical(b.power_screen_pass), ... 'payload_kg',b.payload_kg,'M0_kg',b.M0_kg,'H0_kg',b.loaded_H2_kg, ... 'empty_tank_kg',b.empty_tank_kg,'cylinder_water_volume_L',b.cylinder_water_volume_L, ... 'propulsion_id',b.propulsion_id,'propeller_diameter_m',b.propeller_diameter_m, ... 'resolved_BOM_path',relative_path(fullfile(runRoot,b.row_file),projectRoot), ... 'FC_hover_margin_W',c.FC_cap_W-b.bus_hover_W,'motor_hover_margin_W',b.motor_hover_margin_W, ... 'original_model_version',frozen.model_version,'design_version',b.design_version, ... 'status','','scan_file','','best',[]); inputs=[inputs;file_manifest({fullfile(runRoot,b.row_file)},projectRoot)]; %#ok parameterRows=[parameterRows;{b.design_id,'M0',b.M0_kg,'kg','frozen complete BOM'; ... b.design_id,'H0',b.loaded_H2_kg,'kg','frozen cylinder capacity'; ... b.design_id,'empty_tank',b.empty_tank_kg,'kg','frozen actual cylinder'; ... b.design_id,'water_volume',b.cylinder_water_volume_L,'L','frozen actual cylinder'; ... b.design_id,'FC_cap',c.FC_cap_W,'W','frozen net bus cap'; ... b.design_id,'motor_continuous_each',c.continuous_motor_each_W,'W','frozen engineering budget'; ... b.design_id,'motor_peak_each',c.peak_motor_each_W,'W','frozen10s budget'; ... b.design_id,'SOCmin',c.SOCmin,'1','frozen task';b.design_id,'hydrogen_reserve',c.hydrogen_reserve_fraction,'1','frozen task'}]; %#ok r23.write_json(fullfile(output,[b.design_id '-inherited-settings.json']),c); if ~b.power_screen_pass base.status='excluded_by_original_hardware_screen';base.best=[];base.scan_file=''; branchResults=append_struct(branchResults,base);continue; end c.design_id=[b.design_id '__range_roundtrip']; rows=struct([]);best=[];bestRow=[]; coarse=cfg.speed_min_m_s:cfg.coarse_speed_step_m_s:cfg.speed_max_m_s; for speed=coarse [row,result]=search_leg(c,b.prop,p,speed,cfg.coarse_leg_step_s,cfg,'coarse'); rows=append_struct(rows,row); if row.feasible && (isempty(bestRow)||row.range_m>bestRow.range_m),bestRow=row;best=result;end end if ~isempty(bestRow) localLo=max(cfg.speed_min_m_s,bestRow.speed_m_s-cfg.fine_neighborhood_m_s); localHi=min(cfg.speed_max_m_s,bestRow.speed_m_s+cfg.fine_neighborhood_m_s); fine=localLo:cfg.fine_speed_step_m_s:localHi; for speed=fine [row,result]=search_leg(c,b.prop,p,speed,cfg.fine_leg_step_s,cfg,'fine'); rows=append_struct(rows,row); if row.feasible && (row.range_m>bestRow.range_m || ... (row.range_m==bestRow.range_m && row.leg_step_s end grid(ax,'on');xlabel(ax,'Cruise airspeed (m/s)');ylabel(ax,'Horizontal round-trip range (km)'); title(ax,'Fixed-hardware range with complete mission and reserves');legend(ax,strrep(labels,'_',' '),'Location','best'); xtickformat(ax,'%.2f');ytickformat(ax,'%.2f'); r23.save_figure(fig,struct('figures',output),'range-vs-speed');clear figGuard; verify_files(originalFiles,projectRoot); verify_files(sources,projectRoot);verify_files(inputs,projectRoot); diary('off');clear logGuard; files=dir(output);outputNames={files(~[files.isdir]).name}; outputNames=setdiff(outputNames,[{'run-receipt.json'},launcherFiles]); outputs=file_manifest(fullfile(output,outputNames),projectRoot); receipt=struct('task_id','SIM-R3-003','supplement_version',cfg.version, ... 'model_version',modelVersion,'frozen_hardware_source_model_version',frozen.model_version, ... 'climb_runtime_provenance',climbRuntime, ... 'status','matlab_computation_completed_native_exit_pending','matlab_release',version('-release'), ... 'matlab_version',version,'started_utc',started,'completed_utc',utc_now(),'elapsed_s',toc(clockStart), ... 'script_entry',relative_path(fullfile(here,'run_range.m'),projectRoot),'randomness','none; deterministic grids and time bracketing', ... 'original73_source_and_input_hashes_unchanged',true,'old_all_fit_mission_EOS_rerun',false, ... 'matlab_function_completed',true,'native_process_exit_code',[], ... 'native_exit_evidence_required','R3/supervisor must append actual commandExecution completion; this running function cannot observe its parent-process exit.', ... 'source_files',sources,'input_files',inputs,'output_files',outputs); r23.write_json(fullfile(output,'run-receipt.json'),receipt); fprintf('RANGE_SUPPLEMENT_COMPUTATION_COMPLETED; external native exit acceptance pending.\n'); end function [row,best]=search_leg(c,prop,p,speed,quantum,cfg,phase) c.transit_speed_m_s=speed;c.transit_each_s=0; best=r23.simulate_mission(c,prop,p,0,cfg.simulation_dt_s); lo=0;hi=0;cap=floor(cfg.maximum_leg_s/quantum);next=best;censored=false; if best.feasible hi=min(cap,max(1,ceil(cfg.initial_leg_upper_s/quantum))); while true c.transit_each_s=hi*quantum;next=r23.simulate_mission(c,prop,p,0,cfg.simulation_dt_s); if ~next.feasible,break;end lo=hi;best=next; if hi==cap,censored=true;break;end hi=min(cap,2*hi); end while ~censored && hi-lo>1 mid=floor((lo+hi)/2);c.transit_each_s=mid*quantum; trial=r23.simulate_mission(c,prop,p,0,cfg.simulation_dt_s); if trial.feasible,lo=mid;best=trial;else,hi=mid;next=trial;end end end row=struct('phase',phase,'speed_m_s',speed,'leg_step_s',quantum,'simulation_dt_s',cfg.simulation_dt_s, ... 'leg_s',lo*quantum,'range_m',NaN,'radius_m',NaN,'feasible',logical(best.feasible), ... 'flight_s',best.flight_s,'SOC_final',best.SOC_final,'SOC_min',best.SOC_min,'H2_final_kg',best.H2_final_kg, ... 'final_mass_kg',best.final_mass_kg,'FC_peak_W',best.maximum_FC_W,'motor_each_peak_W',best.maximum_motor_each_W, ... 'battery_bus_peak_W',best.maximum_battery_discharge_W,'FC_peak_margin_W',c.FC_cap_W-best.maximum_FC_W, ... 'motor_peak_margin_each_W',c.peak_motor_each_W-best.maximum_motor_each_W, ... 'battery_peak_margin_W',c.battery_current_check_V*c.battery_peak_current_A-best.maximum_battery_discharge_W, ... 'transit_FC_margin_W',NaN,'transit_motor_margin_each_W',NaN, ... 'motor_max_over_cont_s',max(best.series.motor_over_cont_s),'battery_max_over_cont_s',max(best.series.battery_over_cont_s), ... 'power_constraints_pass',logical(best.power_constraints_pass),'SOC_constraints_pass',logical(best.SOC_constraints_pass), ... 'H2_reserve_pass',logical(best.H2_reserve_pass),'reason',best.reason,'time_ceiling_reached',censored, ... 'next_leg_s',hi*quantum,'next_leg_feasible',logical(next.feasible),'next_leg_reason',next.reason, ... 'energy_balance_residual_J',best.energy_balance_residual_J,'H2_balance_residual_kg',best.H2_balance_residual_kg); if best.feasible row.range_m=2*speed*lo*quantum;row.radius_m=speed*lo*quantum; transit=ismember(best.series.segment,[5 7]); if any(transit) row.transit_FC_margin_W=c.FC_cap_W-max(best.series.bus_load_W(transit)); row.transit_motor_margin_each_W=c.continuous_motor_each_W-max(best.series.propulsion_DC_W(transit))/4; end end end function values=append_struct(values,value) if isempty(values),values=value;else,values(end+1)=value;end end function s=utc_now() s=char(datetime('now','TimeZone','UTC','Format','yyyy-MM-dd HH:mm:ss')); end function p=relative_path(p,root) prefix=[root filesep];assert(startsWith(p,prefix));p=strrep(p(numel(prefix)+1:end),'\','/'); end function value=portable_paths(value,root) % Keep every provenance field; repository-relative paths survive publication. if isstruct(value) names=fieldnames(value); for i=1:numel(value),for k=1:numel(names),value(i).(names{k})=portable_paths(value(i).(names{k}),root);end,end elseif iscell(value) for i=1:numel(value),value{i}=portable_paths(value{i},root);end elseif ischar(value)&&startsWith(value,[root filesep]) value=relative_path(value,root); end end function items=file_manifest(files,root) items=repmat(struct('path','','sha256',''),numel(files),1); for i=1:numel(files),items(i).path=relative_path(files{i},root);items(i).sha256=r23.sha256_file(files{i});end end function verify_files(items,root) for i=1:numel(items) assert(strcmp(r23.sha256_file(fullfile(root,items(i).path)),items(i).sha256),'A frozen scientific source/input changed.'); end end function write_table_exact(T,path) fid=fopen(path,'w','n','UTF-8');assert(fid>0,'Cannot create CSV.');guard=onCleanup(@()fclose(fid)); fprintf(fid,'%s\n',strjoin(T.Properties.VariableNames,',')); if all(varfun(@(v)isnumeric(v)||islogical(v),T,'OutputFormat','uniform')) matrix=table2array(T);fmt=[repmat('%.17g,',1,width(T)-1) '%.17g\n'];fprintf(fid,fmt,matrix.'); else for i=1:height(T) row=cell(1,width(T)); for j=1:width(T) value=T{i,j};if iscell(value),value=value{1};end if isnumeric(value)||islogical(value),row{j}=sprintf('%.17g',value); else,row{j}=['"' strrep(char(string(value)),'"','""') '"'];end end fprintf(fid,'%s\n',strjoin(row,',')); end end end