function out=integration_analysis(p,paths) d=jsondecode(fileread(fullfile(paths.inputs,'r2-integration.json'))); candidate=jsondecode(fileread(fullfile(paths.inputs,'r2-candidates-current.json'))); T=readtable(fullfile(paths.inputs,'r2-bom.csv'),'TextType','string','VariableNamingRule','preserve','Encoding','UTF-8'); numericFields={'quantity','unit_mass_kg','budget_unit_mass_kg','budget_mass_lower_kg','budget_mass_upper_kg','budget_expression_scale_low','budget_expression_scale_high','power_W_budget'}; for k=1:numel(numericFields) n=numericFields{k};if ~isnumeric(T.(n)),T.(n)=str2double(T.(n));end end branches=d.design_branches; if ~iscell(branches),branches=num2cell(branches);end props=candidate.propulsion_candidates;if ~iscell(props),props=num2cell(props);end designSHA=r23.sha256_file(fullfile(paths.inputs,'r2-integration.json'));candidateSHA=r23.sha256_file(fullfile(paths.inputs,'r2-candidates-current.json'));bomSHA=r23.sha256_file(fullfile(paths.inputs,'r2-bom.csv')); assert(strcmp(d.bom_sha256,bomSHA),'Frozen design/BOM hash mismatch'); out=struct('design_version',d.version,'candidate_version',candidate.dataset_version,'design_input_sha256',designSHA,'candidate_input_sha256',candidateSHA,'bom_sha256',bomSHA,'branches',struct([])); for bi=1:numel(branches) b=branches{bi};active=ismember(T.bom_id,string(b.active_BOM_rows));parent=ismember(T.bom_id,string(b.parent_function_rows_no_extra_mass)); mass=nan(height(T),1);lower=mass;upper=mass;budgetFixedPower=0;rule=repmat("inactive",height(T),1); for i=find(active)' id=char(T.bom_id(i));qty=T.quantity(i); if parent(i),mass(i)=0;lower(i)=0;upper(i)=0;rule(i)="incremental_zero_parent_function_not_unknown";continue;end if isfield(b.core_budget_overrides,id) ov=b.core_budget_overrides.(id);m=ov.mass_each_kg; if isfield(ov,'quantity'),qty=ov.quantity;end lo=m;hi=m; if isfield(ov,'lower_kg'),lo=ov.lower_kg;hi=ov.upper_kg;end if isfield(ov,'mass_lower_each_kg'),lo=ov.mass_lower_each_kg;hi=ov.mass_upper_each_kg;end rule(i)="branch_override"; elseif strlength(T.budget_mass_expression_si(i))>0 && ~ismissing(T.budget_mass_expression_si(i)) m=arithmetic(T.budget_mass_expression_si(i));lo=m;hi=m; if isfinite(T.budget_expression_scale_low(i)),lo=m*T.budget_expression_scale_low(i);hi=m*T.budget_expression_scale_high(i);end rule(i)="declared_geometry_times_source_material_mass"; elseif isfinite(T.budget_unit_mass_kg(i)) m=T.budget_unit_mass_kg(i);lo=T.budget_mass_lower_kg(i);hi=T.budget_mass_upper_kg(i); if ~isfinite(lo),lo=m;end;if ~isfinite(hi),hi=m;end rule(i)="source_or_explicit_engineering_budget"; else,error('Active mass unknown: %s',id); end mass(i)=qty*m;lower(i)=qty*lo;upper(i)=qty*hi; if isfinite(T.power_W_budget(i))&&~strcmp(id,'B30'),budgetFixedPower=budgetFixedPower+qty*T.power_W_budget(i);end end I=find(active);row=T(I,:);row.mass_used_kg=mass(I);row.mass_low_kg=lower(I);row.mass_high_kg=upper(I);row.accounting_rule=rule(I); writetable(row,fullfile(paths.run,[b.design_branch_id '-mass-closure.csv']),'Encoding','UTF-8'); M=sum(mass(active));ml=sum(lower(active));mu=sum(upper(active)); prop=props{1}; if isfield(b,'propulsion_component_id') ix=find(cellfun(@(z)strcmp(z.component_id,b.propulsion_component_id),props),1); assert(~isempty(ix));prop=props{ix}; end if ~isfield(prop,'bench_points')||isempty(prop.bench_points),error('Missing selected static map');end if isfield(prop,'engineering_continuous_power_budget_each_W')&&~isempty(prop.engineering_continuous_power_budget_each_W) continuous=prop.engineering_continuous_power_budget_each_W;continuousIdentity='engineering_thermal_budget_not_manufacturer_verified'; else continuous=prop.manufacturer_continuous_power_each_W;continuousIdentity='manufacturer_label_caveat'; end q=p;q.aux_power=0; [Pprop,map]=r23.propulsion_power(M,0,0,1,prop,q); hydrogen=mass(T.bom_id=="B18");payload=mass(T.bom_id=="B47"); coreIds=["B10","B11","B12","B13","B17","B18","B29","B47"]; coreMass=sum(mass(active&ismember(T.bom_id,coreIds)));addMass=M-coreMass; auxBus=budgetFixedPower/0.90;wiring=(Pprop/50)^2*0.013; Pbus=Pprop+auxBus+wiring; s=struct('design_id',b.design_branch_id,'design_version',d.version,'M0_kg',M,'m0_no_extra_payload_kg',M-payload, ... 'mass_low_kg',ml,'mass_high_kg',mu,'payload_kg',payload,'loaded_H2_kg',hydrogen, ... 'FC_module_kg',mass(T.bom_id=="B13"),'empty_tank_kg',mass(T.bom_id=="B17"),'buffer_mass_kg',mass(T.bom_id=="B29"), ... 'cylinder_water_volume_L',b.core_budget_overrides.B17.water_volume_L, ... 'core_mass_kg',coreMass,'integration_additions_kg',addMass, ... 'fixed_load_budget_W',budgetFixedPower,'bus_fixed_load_W',auxBus,'conduction_loss_W',wiring, ... 'battery_self_load_W',T.power_W_budget(T.bom_id=="B30"), ... 'propulsion_hover_W',Pprop,'bus_hover_W',Pbus,'motor_each_hover_W',Pprop/4, ... 'motor_continuous_each_budget_W',continuous,'continuous_limit_identity',continuousIdentity, ... 'motor_hover_margin_W',4*continuous-Pprop,'FC_nominal_W',2400,'FC_bus_scenario_cap_W',2280, ... 'FC_hover_margin_W',2280-Pbus,'static_map_in_domain',map.in_domain, ... 'power_screen_pass',map.in_domain&&Pprop<=4*continuous&&Pbus<=2280, ... 'nominal_constant_mass_endurance_h',hydrogen*0.95*p.LHV*0.50/Pbus/3600, ... 'propeller_diameter_m',prop.propeller_diameter_m,'propulsion_id',prop.component_id,'charging_mode',b.charging_mode, ... 'map',map,'row_file',[b.design_branch_id '-mass-closure.csv']); s.prop=prop;s.electrical=d.electrical;s.design_input_sha256=designSHA;s.candidate_input_sha256=candidateSHA;s.bom_input_sha256=bomSHA; if isempty(out.branches),out.branches=s;else,out.branches(end+1)=s;end end summary=rmfield(out.branches,{'prop','electrical','map'}); r23.write_json(fullfile(paths.report,'integration-summary.json'),struct('design_version',d.version,'branches',summary, ... 'status','R2_proposals_evaluated_not_airworthiness_or_final_hardware_freeze', ... 'mass_bounds','engineering budgets; not95%statistical bands', ... 'power_boundary','static electrical map plus fixed-load/conduction budgets; no repeated motor/ESC efficiency')); writetable(struct2table(summary),fullfile(paths.run,'integration-comparison.csv'),'Encoding','UTF-8'); save(fullfile(paths.run,'integration-results.mat'),'out'); fprintf('INTEGRATION_%s_COMPLETE\n',d.version); for b=out.branches,fprintf('%s M=%.5fkg Phover=%.5fW motorMargin=%.5fW screen=%d\n',b.design_id,b.M0_kg,b.bus_hover_W,b.motor_hover_margin_W,b.power_screen_pass);end end function v=arithmetic(s) s=char(s);assert(~isempty(regexp(s,'^[0-9eE+*/().\s-]+$','once')),'Unsafe/non-numeric expression'); v=str2num(s);assert(isscalar(v)&&isfinite(v)); %#ok end