Skip to content
Snippets Groups Projects
complete_IDS_CORSICA.m 8.59 KiB
Newer Older
function IDS_out = complete_IDS_CORSICA(IDS_in,varargin)

  % This script will complete the IDS of CORSICA with the missing fields recomputing the missing quantities from available information

  data_path_in = '/NoTivoli/carpanes/LIU_RAP_ITER/CORSICA_ids/CORSICA_130510.mat';
  %path_IDS = '/NoTivoli/carpanes/LIU_RAP_ITER/CORSICA_ids/CORSICA_130506.mat';
  data_path_out = '/NoTivoli/carpanes/LIU_RAP_ITER/CORSICA_ids/CORSICA_130510_extended_COCOS17_trial.mat';
  % Load the IDS_in file
  IDS_out = load(data_path_in);
else
  IDS_out = IDS_in;
%% Correct COCOS convention on original data

% Put homogenous = 0 on magnetic but 1 on the coils.
% Sign of the angle of the magnetic probes.
G = G_ITER_generator;
% Get the structure from generating files

Pliuqe = liupiter(1000,'uuu',ones(G.na,1),'vvv',ones(G.nm,1), 'www', ones(G.nf,1)); % Some default setting that could be removed probably to use the liupiter directly
G = liug(G,Pliuqe);

L = liuc(Pliuqe,G);

% Extract information from CORSICA IDS_out
[LC,LXC,LYC] = LLXLY_IDS_corsica(L, IDS_out);
%%  -------------------- ADD the missing information on the existing IDS_out CORSICA -----------------------

%% Consider each circuit to be composed by only one coil
tmp = data_coils();
for ii = 1:numel(tmp.names)
        % Remove strange character in coil names
        IDS_out.pf_active.coil{jj}.name = regexprep(IDS_out.pf_active.coil{jj}.name,'[\n\r]+','');
        if strcmp(tmp.names{ii},IDS_out.pf_active.coil{jj}.name)
            IDS_out.pf_active.coil{jj}.element{1}.geometry.geometry_type = 2; % Rectangle description
            IDS_out.pf_active.coil{jj}.element{1}.geometry.rectangle.r = tmp.R(ii);
            IDS_out.pf_active.coil{jj}.element{1}.geometry.rectangle.z = tmp.Z(ii);
            IDS_out.pf_active.coil{jj}.element{1}.geometry.rectangle.width = tmp.dR(ii);
            IDS_out.pf_active.coil{jj}.element{1}.geometry.rectangle.height = tmp.dZ(ii);
            IDS_out.pf_active.coil{jj}.element{1}.turns_with_sign = tmp.N(ii);
        end
    end
end

%% Add the data to the circuit
tmp = data_circuits();
Ncircuits = size(tmp,1);
Ncoils = numel(IDS_out.pf_active.coil);
% time will be set in circuit.current.time so should not put it at top and set homogeneous to 0?
IDS_out.pf_active.time = [];
IDS_out.pf_active.ids_properties.homogeneous_time = 0;
for ii=1:Ncircuits
    IDS_out.pf_active.circuit{ii}.name = tmp{ii,1}{1};
    IDS_out.pf_active.supply{ii}.name = tmp{ii,1}{1};
    % Find index of the coils belonging to ii circuit
    index_coil = [];
    for jj =1:Ncoils
      if any(strcmp(tmp{ii,2}, IDS_out.pf_active.coil{jj}.name))
           index_coil = [index_coil jj];
      end
    end
    % Add the current data to the circuit from the data on the coils.
    % The coils must share the same current in the circuit so just take the first one.
    IDS_out.pf_active.circuit{ii}.current.data = IDS_out.pf_active.coil{index_coil(1)}.current.data/IDS_out.pf_active.coil{index_coil(1)}.element{1}.turns_with_sign;
    IDS_out.pf_active.circuit{ii}.current.time = IDS_out.pf_active.coil{index_coil(1)}.current.time;
    
    IDS_out.pf_active.circuit{ii}.connections = zeros(2*numel(index_coil) -1 , 2*Ncircuits + 2*Ncoils);
    for jj = 1:numel(index_coil)
    IDS_out.pf_active.circuit{ii}.connections( jj , 2*Ncircuits + 2*(index_coil(jj)-1) +1) = 1;
    IDS_out.pf_active.circuit{ii}.connections( jj +1 , 2*Ncircuits + 2*(index_coil(jj)-1) +2) = 1;
    end
    
    % Add the connection to the power supply
    IDS_out.pf_active.circuit{ii}.connections(1,  2*ii -1) =   1; 
    IDS_out.pf_active.circuit{ii}.connections(end,  2*ii ) =1; 
end

%% Limiter description
tmp = data_limiter();
IDS_out.wall.description_2d{1}.limiter.unit{1}.outline.r = tmp.r;
IDS_out.wall.description_2d{1}.limiter.unit{1}.outline.z = tmp.z;

%% Vessel description
% Understand what I need to do for the double layer vessel

%%  -------------- Synthetic diagnostics------------ Need to be recomputed from CORSICA flux map

IDS_out.magnetics.method{1}.ip.time = LXC.t;
IDS_out.magnetics.method{1}.ip.data = LXC.Ip;

% Correct IDS magnetics
aa = gdat([],'ids','source','magnetics');
magnetics_default = aa.magnetics;
mag_fluxloop_def_fields = fieldnames(magnetics_default.flux_loop{1});
if isfield(IDS_out.magnetics,'flux_loop') && length(IDS_out.magnetics.flux_loop) > 0
  mag_fluxloop_fields = fieldnames(IDS_out.magnetics.flux_loop{1});
else
  mag_fluxloop_fields = {};
end
fields_to_add = setdiff(mag_fluxloop_def_fields,mag_fluxloop_fields);

%% Ff
tmp = data_Ff();
  if ~isempty(fields_to_add)
    for idef=1:length(fields_to_add)
      % add first defaults and fill in after
      IDS_out.magnetics.flux_loop{ii}.(fields_to_add{idef}) = magnetics_default.flux_loop{1}.(fields_to_add{idef});
    end
    if ii==1; warning(['following fields added to magnetics.flux_loop: ' sprintf('%s ',fields_to_add{:})]); end
  end
  IDS_out.magnetics.flux_loop{ii}.position{1}.r = tmp.r(ii);
  IDS_out.magnetics.flux_loop{ii}.position{1}.z = tmp.z(ii);
  IDS_out.magnetics.flux_loop{ii}.name = tmp.name{ii};
end

for ii=1:numel(tmp.r)
    IDS_out.magnetics.flux_loop{ii}.flux.data = -LXC.Ff(ii,:)';
    IDS_out.magnetics.flux_loop{ii}.flux.time = LXC.t;
% Correct IDS magnetics
mag_bpol_probe_def_fields = fieldnames(magnetics_default.bpol_probe{1});
if isfield(IDS_out.magnetics,'bpol_probe') && length(IDS_out.magnetics.bpol_probe) > 0
  mag_bpol_probe_fields = fieldnames(IDS_out.magnetics.bpol_probe{1});
else
  mag_bpol_probe_fields = {};
end
fields_to_add = setdiff(mag_bpol_probe_def_fields,mag_bpol_probe_fields);
mag_bpol_probe_def_fields = fieldnames(magnetics_default.bpol_probe{1}.position);
if length(IDS_out.magnetics.bpol_probe) > 0 && isfield(IDS_out.magnetics.bpol_probe{1},'position')
  mag_bpol_probe_fields = fieldnames(IDS_out.magnetics.bpol_probe{1}.position);
else
  mag_bpol_probe_fields = {};
end
fields_to_add_position = setdiff(mag_bpol_probe_def_fields,mag_bpol_probe_fields);

tmp = data_Bm();
for ii=1:numel(tmp.name)
  if ~isempty(fields_to_add)
    for idef=1:length(fields_to_add)
      % add first defaults and fill in after
      IDS_out.magnetics.bpol_probe{ii}.(fields_to_add{idef}) = magnetics_default.bpol_probe{1}.(fields_to_add{idef});
    end
    if ii==1; warning(['following fields added to magnetics.bpol_probe: ' sprintf('%s ',fields_to_add{:})]); end
  end
  if ~isempty(fields_to_add_position)
    for idef=1:length(fields_to_add_position)
      % add_position first defaults and fill in after
      IDS_out.magnetics.bpol_probe{ii}.position.(fields_to_add_position{idef}) = magnetics_default.bpol_probe{1}.position.(fields_to_add_position{idef});
    end
    if ii==1; warning(['following fields add_positioned to magnetics.bpol_probe.position: ' sprintf('%s ',fields_to_add_position{:})]); end
  end
  IDS_out.magnetics.bpol_probe{ii}.position.r = tmp.r(ii);
  IDS_out.magnetics.bpol_probe{ii}.position.z = tmp.z(ii);
  IDS_out.magnetics.bpol_probe{ii}.poloidal_angle = -tmp.am(ii); % Correct the sign to be consistent with COCOS 11
  IDS_out.magnetics.bpol_probe{ii}.name = tmp.name{ii};
end

for ii=1:numel(tmp.name)
    IDS_out.magnetics.bpol_probe{ii}.field.data = LXC.Bm(ii,:)';
    IDS_out.magnetics.bpol_probe{ii}.field.time = LXC.t;
mag_method_def_fields = fieldnames(magnetics_default.method{1});
if isfield(IDS_out.magnetics,'method') && length(IDS_out.magnetics.method) > 0
  mag_method_fields = fieldnames(IDS_out.magnetics.method{1});
else
  mag_method_fields = {};
end
fields_to_add = setdiff(mag_method_def_fields,mag_method_fields);
if ~isempty(fields_to_add)
  for idef=1:length(fields_to_add)
    % add first defaults and fill in after
    IDS_out.magnetics.method{1}.(fields_to_add{idef}) = magnetics_default.method{1}.(fields_to_add{idef});
  end
  warning(['following fields added to magnetics.method: ' sprintf('%s ',fields_to_add{:})]);
end
IDS_out.magnetics.method{1}.diamagnetic_flux.data = -LXC.Ft;
IDS_out.magnetics.method{1}.diamagnetic_flux.time = LXC.t;
IDS_out.tf.b_field_tor_vacuum_r.time  = LXC.t;
IDS_out.tf.b_field_tor_vacuum_r.data  = LXC.rBt;

%% Convert from cocos_in to cocos_out
cocos_in = 17;
cocos_out = 11;
to_transform = {'pf_active','pf_passive','magnetics','tf','wall','equilibrium'};
for i=1:length(to_transform)
  IDS_out.(to_transform{i}) = ids_generic_cocos_nodes_transformation_symbolic(IDS_out.(to_transform{i}), to_transform{i}, cocos_in, cocos_out,[],[],[],[],[],3);
end
%% Store the resulting data
  save(data_path_out, '-struct', 'IDS_out')