Skip to content
Snippets Groups Projects
SCDclass_expcode.m 34.6 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
classdef SCDclass_expcode
    %A SCD expcode object
    %   This object is a virgin SCD expode
    %   its constructor builds an expode 
    %   with all templates algorithms
    
    properties (Access = private)     
        activenodes       % numerical list of configured active nodes
        activecpus        % numerical list of configured CPUs
        %nodeddlist       % list of data dictionaries at node level
        wrapperlist       % list of configured wrappers
        algoobjlist       % list of loaded algorithm objects
        algonamelist      % List of loaded algorithm names 
        algoddlist        % list of data dictionaries at algorithm level
        mdscontainer      % container class for MDS+ interface objects
        taskcontainer     % container class for generic init and term task objects
        exportedtps       % list of tunable parameters variable to be exported
        stdinits          % list of standard inits scripts
        
    end
    
    properties
        % Main properties
        name              % Expcode main name
        maincode          % Expcode numerical code
        status            % Expcode development status
        loadverbose       % Verbosity level of the loading (currently 0 or 1)        
       
        % Per node properties structs
        node01
        node02
        node03
        node04
        node05
        node06
        node07
        node08
       

    end
    
    methods
        
        function obj=SCDclass_expcode()
           % All templates constructor
       
            obj.name = 'template';
            obj.maincode = 1;
            obj.status = 'debug';
            obj.loadverbose = 1;
            
            obj.activenodes=[1 2 3 6 7 8];
            obj.activecpus=[ {1}; ...
                             {1:4}; ...
                             {1:4}; ...
                             {1:4}; ...
                             {1:4}; ...
                             {1}]; 
            
            
%             obj.nodeddlist=cell(6,1);
%             obj.nodeddlist{1} = 'SCD_rtc_01.sldd';
%             obj.nodeddlist{2} = 'SCD_rtc_02.sldd';
%             obj.nodeddlist{3} = 'SCD_rtc_03.sldd';
%             obj.nodeddlist{4} = 'SCD_rtc_06.sldd';
%             obj.nodeddlist{5} = 'SCD_rtc_07.sldd';
%             obj.nodeddlist{6} = 'SCD_rtc_08.sldd';
            
            obj.algonamelist={};
            obj.algoddlist={};
            obj.wrapperlist={};
            obj.exportedtps = [];
            
            obj.mdscontainer = SCDclass_mdsobjcontainer;
            obj.taskcontainer = SCDclass_taskcontainer;

            
            
            obj.node01.active = 0;
            obj.node01.type = 'std1cpu2015a';
            obj.node01.timing.t_start = -4.5;
            obj.node01.timing.t_stop = 3;
            obj.node01.timing.dt = 1e-4;
            obj.node01.varalgo = 1;
            obj.node01.buildcfg.conffile{1} = 'standard';
            obj.node01.buildcfg.initscdbeforecomp = 0;
            obj.node01.usewavegen = 0;
            obj.node01.datadict='SCD_rtc_01.sldd';
            obj.node01.wrapdatadicts{1} = 'SCDwrap_template0101.sldd';
            
            obj.node02.active = 0;
            obj.node02.type = 'std1cpu2015a';
            obj.node02.timing.t_start = -4.5;
            obj.node02.timing.t_stop = 3;
            obj.node02.timing.dt = 1e-4;
            obj.node02.varalgo = [1 1 1 1];
            obj.node02.thperiod = [1.0000e-03 1.0000e-03 1.0000e-03 1.0000e-03];
            obj.node02.buildcfg.conffile{1} = 'standard';
            obj.node02.buildcfg.conffile{2} = 'standard';
            obj.node02.buildcfg.conffile{3} = 'standard';
            obj.node02.buildcfg.conffile{4} = 'standard';
            obj.node02.buildcfg.conffile=obj.node02.buildcfg.conffile';
            obj.node02.buildcfg.initscdbeforecomp = 0;
            obj.node02.usewavegen = 0;
            obj.node02.datadict='SCD_rtc_02.sldd';
            obj.node02.wrapdatadicts{1} = 'SCDwrap_template0201.sldd';
            obj.node02.wrapdatadicts{2} = 'SCDwrap_template0202.sldd';
            obj.node02.wrapdatadicts{3} = 'SCDwrap_template0203.sldd';
            obj.node02.wrapdatadicts{4} = 'SCDwrap_template0204.sldd';
            obj.node02.wrapdatadicts=obj.node02.wrapdatadicts';
            
            obj.node03.active = 0;
            obj.node03.type = '4cpus2015a';
            obj.node03.timing.t_start = -2.0;
            obj.node03.timing.t_stop = 2.75;
            obj.node03.timing.dt = 1e-3;
            obj.node03.thdwsample = [0 0 0 0];
            obj.node03.thperiod = [1.0000e-03 1.0000e-03 1.0000e-03 1.0000e-03];
            obj.node03.varalgo = [1 1 1 1];
            obj.node03.buildcfg.conffile{1} = 'standard';
            obj.node03.buildcfg.conffile{2} = 'standard';
            obj.node03.buildcfg.conffile{3} = 'standard';
            obj.node03.buildcfg.conffile{4} = 'standard';
            obj.node03.buildcfg.conffile=obj.node03.buildcfg.conffile';
            obj.node03.buildcfg.initscdbeforecomp = [0 0 0 0];
            obj.node03.usewavegen = 0;
            obj.node03.useethcat1 = 0;
            obj.node03.datadict='SCD_rtc_03.sldd';            
            obj.node03.wrapdatadicts{1} = 'SCDwrap_template0301.sldd';
            obj.node03.wrapdatadicts{2} = 'SCDwrap_template0302.sldd';
            obj.node03.wrapdatadicts{3} = 'SCDwrap_template0303.sldd';
            obj.node03.wrapdatadicts{4} = 'SCDwrap_template0304.sldd';
            obj.node03.wrapdatadicts=obj.node03.wrapdatadicts';
            
            obj.node04.active = 0;
            obj.node04.type = 'std1cpu2015a';
            obj.node04.timing.t_start = -4.5;
            obj.node04.timing.t_stop = 3;
            obj.node04.timing.dt = 1e-4;
            obj.node04.varalgo = 1;
            obj.node04.buildcfg.conffile{1} = 'standard';
            obj.node04.buildcfg.initscdbeforecomp = 0;
            obj.node04.usewavegen = 0;

            obj.node05.active = 0;
            obj.node05.type = 'std1cpu2015a';
            obj.node05.timing.t_start = -4.5;
            obj.node05.timing.t_stop = 3;
            obj.node05.timing.dt = 1e-4;
            obj.node05.varalgo = 1;
            obj.node05.buildcfg.conffile{1} = 'standard';
            obj.node05.buildcfg.initscdbeforecomp = 0;
            obj.node05.usewavegen = 0;

            obj.node06.active = 0;
            obj.node06.type = '4cpus2015a';
            obj.node06.timing.t_start = 0.0;
            obj.node06.timing.t_stop = 2.75;
            obj.node06.timing.dt = 1e-3;
            obj.node06.thdwsample = [0 0 0 0];
            obj.node06.thperiod = [1.0000e-03 1.0000e-03 1.0000e-03 1.0000e-03];
            obj.node06.varalgo = [1 1 1 1];
            obj.node06.buildcfg.conffile{1} = 'standard';
            obj.node06.buildcfg.conffile{2} = 'standard';
            obj.node06.buildcfg.conffile{3} = 'standard';
            obj.node06.buildcfg.conffile{4} = 'standard';
            obj.node06.buildcfg.conffile=obj.node06.buildcfg.conffile';
            obj.node06.buildcfg.initscdbeforecomp = [0 0 0 0];
            obj.node06.usewavegen = 0;
            obj.node06.datadict='SCD_rtc_06.sldd';                        
            obj.node06.wrapdatadicts{1} = 'SCDwrap_template0601.sldd';
            obj.node06.wrapdatadicts{2} = 'SCDwrap_template0602.sldd';
            obj.node06.wrapdatadicts{3} = 'SCDwrap_template0603.sldd';
            obj.node06.wrapdatadicts{4} = 'SCDwrap_template0604.sldd';
            obj.node06.wrapdatadicts=obj.node06.wrapdatadicts';
            
            obj.node07.active = 0;
            obj.node07.type = '4cpus2015a';
            obj.node07.timing.t_start = -0.5;
            obj.node07.timing.t_stop = 2.5;
            obj.node07.timing.dt = 1e-3;
            obj.node07.thdwsample = [0 0 0 0];
            obj.node07.thperiod = [1.0000e-03 1.0000e-03 1.0000e-03 1.0000e-03];
            obj.node07.varalgo = [1 1 1 1];
            obj.node07.buildcfg.conffile{1} = 'standard';
            obj.node07.buildcfg.conffile{2} = 'standard';
            obj.node07.buildcfg.conffile{3} = 'standard';
            obj.node07.buildcfg.conffile{4} = 'standard';
            obj.node07.buildcfg.conffile=obj.node07.buildcfg.conffile';
            obj.node07.buildcfg.initscdbeforecomp = [0 0 0 0];
            obj.node07.usewavegen = 0;
            obj.node07.datadict='SCD_rtc_07.sldd';
            obj.node07.wrapdatadicts{1} = 'SCDwrap_template0701.sldd';
            obj.node07.wrapdatadicts{2} = 'SCDwrap_template0702.sldd';
            obj.node07.wrapdatadicts{3} = 'SCDwrap_template0703.sldd';
            obj.node07.wrapdatadicts{4} = 'SCDwrap_template0704.sldd';
            obj.node07.wrapdatadicts=obj.node07.wrapdatadicts';

            obj.node08.active = 0;
            obj.node08.type = '4cpus2015a';
            obj.node08.timing.t_start = -0.5;
            obj.node08.timing.t_stop = 2.5;
            obj.node08.timing.dt = 1e-3;
            obj.node08.thdwsample = [0 0 0 0];
            obj.node08.thperiod = [1.0000e-03 1.0000e-03 1.0000e-03 1.0000e-03];
            obj.node08.varalgo = [1 1 1 1];
            obj.node08.buildcfg.conffile{1} = 'standard';
            obj.node08.buildcfg.conffile{2} = 'standard';
            obj.node08.buildcfg.conffile{3} = 'standard';
            obj.node08.buildcfg.conffile{4} = 'standard';
            obj.node08.buildcfg.conffile=obj.node08.buildcfg.conffile';
            obj.node08.buildcfg.initscdbeforecomp = [0 0 0 0];
            obj.node08.usewavegen = 0;
            obj.node08.datadict='SCD_rtc_08.sldd';            
            obj.node08.wrapdatadicts{1} = 'SCDwrap_template0801.sldd';
       
            
            
        
        end
        
        function printinfo(obj)
           fprintf('*****************************************************\n');
           fprintf('* SCD expcode: ''%s'', main code: %d\n',obj.name,obj.maincode); 
           fprintf('*****************************************************\n');           
           fprintf('* Configured wrappers:\n')
           if(numel(obj.wrapperlist)>0)
               for ii=1:numel(obj.wrapperlist)
                   fprintf('  Node: %d, CPU: %d, varalgo: %d, wrapper: ''%s'', datadict: ''%s''\n', ...
                        obj.wrapperlist{ii}{1}, ...
                        obj.wrapperlist{ii}{2}, ...
                        obj.wrapperlist{ii}{3}, ...
                        obj.wrapperlist{ii}{4}, ...
                        obj.wrapperlist{ii}{5} ...
                    );
               end
           end
           fprintf('* Configured algorithms:\n');
           if(numel(obj.algonamelist)>0)
               for ii=1:numel(obj.algonamelist), fprintf('  ''%s''\n', char(obj.algonamelist{ii})); end 
           end
           fprintf('* Configured algorithm data dictionaries:\n')
           if(numel(obj.algoddlist)>0)
               for ii=1:numel(obj.algoddlist), fprintf('  ''%s''\n', char(obj.algoddlist{ii})); end 
           end
           fprintf('* Configured exported tunparams structures:\n')
           if(numel(obj.exportedtps)>0)
               for ii=1:numel(obj.exportedtps), fprintf('  ''%s''\n', char(obj.exportedtps{ii})); end
           end
           fprintf('* Configured MDS tunparams objects: %d (use printparameters method for details)\n', obj.mdscontainer.numparams);
           fprintf('* Configured MDS wavegens objects: %d (use printwavegens method for details)\n', obj.mdscontainer.numwavegens);
           fprintf('* Configured general purpose tasks: %d (use printtasks method for details)\n', obj.taskcontainer.numtasks);
           fprintf('* Configured init scripts: %d (use printinits method for details)\n', numel(obj.stdinits));
        end
        
        function obj = addwrapper(obj, node, cpu, varalgo, modelname, varargin) 
            
            p=inputParser;
            checknodes = @(x) ismember(x,obj.activenodes );
            checkcpu = @(x) ismember(x,obj.activecpus{node});
          
            addRequired(p, 'node', checknodes); 
            addRequired(p, 'cpu', checkcpu); 
            addRequired(p, 'varalgo');
            addRequired(p, 'modelname', @(x) ischar(x));
            addOptional(p, 'datadictionary', '', @(x) ischar(x));
            
            parse(p, node, cpu, varalgo, modelname, varargin{:});
            
            % In data dcitionary is not specified it is assumed having
            % the same name than the model
            if(isempty(p.Results.datadictionary))
                datadictionary=[p.Results.modelname '.sldd'];
            else
                datadictionary=[p.Results.datadictionary '.sldd'];
            end
            
            % Frist checking for duplicates
            if(numel(obj.wrapperlist)>0)
                wrappernamelist={};
                wrapperddnamelist={};
                for ii=1:numel(obj.wrapperlist)
                    wrappernamelist{end+1}=obj.wrapperlist{ii}{4};
                    wrapperddnamelist{end+1}=obj.wrapperlist{ii}{5};
                end
           
                if(ismember(p.Results.modelname,wrappernamelist) || ismember(p.Results.datadictionary,wrapperddnamelist))
                    warning('SCDclass_expcode:addwrapper','wrapper ''%s'' already present, ignoring.',modelname);
                end
                
                return
            end
                
            tempwrapper=cell(10,1);
            tempwrapper{1}=p.Results.node;
            tempwrapper{2}=p.Results.cpu;
            tempwrapper{3}=p.Results.varalgo;
            tempwrapper{4}=p.Results.modelname;
            tempwrapper{5}=datadictionary;
            obj.wrapperlist{end+1}=tempwrapper;
            
        end     
            
        function obj = addalgorithm(obj, in_node, in_cpu, in_algo)
            
            % TODO: import the entrire object into a container class ??
            
            p=inputParser;
            checknodes = @(x) ismember(x,obj.activenodes );
            checkcpu = @(x) ismember(x,obj.activecpus{in_node});
          
            addRequired(p, 'node', checknodes); 
            addRequired(p, 'cpu', checkcpu); 
            addRequired(p, 'algo', @(x) isobject(x));
            
            parse(p, in_node, in_cpu, in_algo);

            node = p.Results.node;
            cpu = p.Results.cpu;
            algo = p.Results.algo;
            
            % Checking and importing algorithm name
            if(~ismember(algo.getname,obj.algonamelist))
                obj.algonamelist{end+1}=algo.getname;
            else
                warning('SCDclass_expcode:addalgorithm','algorithm ''%s'' already present in the expcode, skipping!',algo.getname);
                return;
            end
                
            % Importing algorithm MDS objects into the main mdscontainer,
            % filling base structure name
            algomdscontainer=algo.getmdscontainer;
            basewgstruct=sprintf('SCDsimdata.SCDnode%02d%02d_simdata.wavegen', node, cpu);
            algomdscontainer=algomdscontainer.setwavegenbasestruct(basewgstruct);
            obj.mdscontainer=obj.mdscontainer.importmdsobjects(algomdscontainer);
             
            % Importing exported tps
            algoexptps=algo.getexportedtps;
            if(numel(algoexptps)>0)
                for(ii=1:numel(algoexptps))
                    if(~ismember(algoexptps{ii},obj.exportedtps))
                        obj.exportedtps{end+1}=algoexptps{ii};
                    else
                        warning('SCDclass_expcode:addalgorithm','exported tunparams sctruct ''%s'' already present, ignoring',algoexptps{ii})
                    end
                end
            end
            
            % Importing algorithms data dictionary, only those with proper
            % name
            algodd=algo.getdatadictionary;
            if(strcmp(algodd(1:7),'SCDalgo'))
                if(~ismember(algodd,obj.algoddlist))
                    obj.algoddlist{end+1}=algodd;
                else
                    warning('SCDclass_expcode:addalgorithm','algorithm data dictionary ''%s'' already present, ignoring', algodd);
                end
            else
                % warning here ?
            end
           
            % Importing tasks 
            algotaskcontainer=algo.gettaskcontainer;
            obj.taskcontainer=obj.taskcontainer.importtaskobjects(algotaskcontainer);
            
            % Importing inits
            algoinits=algo.getinits;
            if(numel(algoinits)>0)
                for ii=1:numel(algoinits)
                    toadd=1;
                    if(~isempty(obj.stdinits))
                        for jj=1:numel(obj.stdinits)
                             if(strcmp(char(obj.stdinits{jj}{2}),algoinits{ii}{2}))
                                 warning('SCDclass_expcode:addalgorithm','An init driving the structure %s has already been added, ignoring algorithm object.\d',algoinits{ii}{2})
                                 toadd=0;
                             end
                        end
                    end
                    if(toadd)
                        temp=cell(10,1);
                        temp{1}=algoinits{ii}{1};
                        temp{2}=algoinits{ii}{2};
                        obj.stdinits{end+1}=temp;
                        obj.algoobjlist{end+1}=algo; %% Add the full algorithm object here, to see if it is fine
                    end
                end
            end            
        end     
            
        function out = getexportedtps(obj)
            out = obj.exportedtps;
        end
        
        function obj = importexpcode(obj, expcode)
           % This function imports settings of an existing expcode into another
           
           warning('Not yet supported!');
           return;
           
           % tunparamstruct import
           exportedtpstoadd = expcode.getexportedtps;
           for ii=1:numel(exportedtpstoadd)
               if ~sum(ismember(exportedtpstoadd{ii},obj.exportedtps))
                   obj.addtunparamstruct(exportedtpstoadd{ii});
               end
           end
            
           % parameters import
           obj.mdscontainer=obj.mdscontainer.importmdsobjects(expcode.mdscontainer);
        end
        
        
        function setup(obj)
           % This function sets up the TCV Simulink  model
           % to simulate this experimental code.
           % NOTE THAT it doesn't actualize tunable parameters
           % and wavegens, call actualizedata for that
           fprintf('Setting up expcode %d, ''%s'', configuring wrappers ...\n',obj.maincode,obj.name);
           obj=obj.setupwrappers;
           obj.setupvaralgo;
           fprintf('Setting up expcode %d, ''%s'', configuring data dictionaries ...\n',obj.maincode,obj.name);
           obj.setupwrapdd;
           obj.setupalgodd;
           fprintf('Setting up expcode %d, ''%s'', configuring global data ...\n',obj.maincode,obj.name);
           obj.setupmain;
           fprintf('Setting up expcode %d, ''%s'', configuring main workspace variables ...\n',obj.maincode,obj.name);           
           obj.buildworkspacesimstruct;
           obj.buildworkspacetpstruct;
        end
        
        function actualize(obj, shot)
            obj.actualizedata(shot);
             fprintf('Actualizing expcode %d, ''%s'', performing tasks ...\n',obj.maincode,obj.name);
            obj.taskcontainer.exectasksoninit(shot);
        end
        
        function actualizedata(obj, shot)
           % This function actualizes configured
           % tunable parameters and wavegens timeseries
           % according to the given shot
           
           currentexpcode=Simulink.data.evalinGlobal('tcv','scd.expcode');
           if(currentexpcode~=obj.maincode)
               error('SCDclass_expcode:wrongexpcodeloaded','Cannot perform data actualization, the loaded expcode is not matching, try to setup the expcode first');
           end
           fprintf('Actualizing expcode %d, ''%s'', configuring tunable parameters ...\n',obj.maincode,obj.name);
           obj.actualizeparameters(shot)
           fprintf('Actualizing expcode %d, ''%s'', configuring wavegens ...\n',obj.maincode,obj.name);
           obj.actualizewavegens(shot);
        end

        
        function printparameters(obj)
            obj.mdscontainer.printparameters;
        end

        function printwavegens(obj)
            obj.mdscontainer.printwavegens;
        end
        
        function printtasks(obj)
            obj.taskcontainer.printtasks;
        end
        
        function actualizeparameters(obj,shot)
            obj.mdscontainer.actualizeparameters(shot);
        end
        
        function actualizewavegens(obj,shot)
            obj.mdscontainer.actualizewavegens(shot);
        end
        
        
        function printinits(obj)
           if(~isempty(obj.stdinits))
               for ii=1:numel(obj.stdinits)
                   fprintf('%s -> %s\n',char(obj.stdinits{ii}{1}),char(obj.stdinits{ii}{2}));
               end
           end
        end
       
       
        function callinits(obj)
          if(~isempty(obj.algoobjlist))
              for(ii=1:numel(obj.algoobjlist))
                 
                  obj.algoobjlist{ii}.callinits;
                     
              end
          end
        end
        
        function printMARTe2config(obj, shot)
            fprintf("+MDSParameters = {\n");
            fprintf("   Class = MDSParameters\n");
            fprintf("   Shot = %d\n",shot);
            obj.mdscontainer.printMARTe2parconfig(shot);
            fprintf("}\n");
            
            fprintf("+MDSWavegens = {\n");
            fprintf("   Class = MDSWavegens\n");
            fprintf("   Shot = %d\n",shot);
            obj.mdscontainer.printMARTe2wgconfig(shot);
            fprintf("}\n");
            
        end
        
    end
   
    methods (Access = private)
        
        function obj=printlog(obj,str)
            if obj.loadverbose==1
               fprintf('Expcode %d, %s\n', obj.maincode, str); 
            end
        end
                 
        function obj = buildworkspacetpstruct(obj)
            % this funtion builds a workspace structures containing
            % replicas of all tunable parameters structurea in the data
            % dictionaries, this structure is the one actually used 
            % for loading simulation wavegen data
            % It is better not to use directly data dictionaries structures
            % to avoid flooding dds with big sim data sets (and
            % conseguently the SCD SVN itself
           
            dd=SCDconf_getdatadict('tcv.sldd');
            
            for ii=1:numel(obj.exportedtps)
                simstructnamedd=sprintf('%s_tmpl',char(obj.exportedtps(ii)));
                simstructnamews=char(obj.exportedtps(ii));
                simstruct=dd.getEntry(simstructnamedd).getValue;
                assignstr=sprintf('%s=temp;',simstructnamews);
                assignin('base','temp',simstruct);
                evalin('base',assignstr);
            end
            
            evalin('base','clear temp;');            
        end
        
           
        function obj = setupwrapdd(obj)
            % Sets up the data dictionaries of the wrappers to be loaded
            % on the nodes inside the main tcv.sldd
            
            % For every node, given the current status finds its dictionary
            % and the requred data sources, find actions to do and setup
            % wrappers data sources accordingly
            
            % It is assumed that there is only 1 wrapper and 1 wrapper data
            % dictionary active at a given time for every CPU model
            
            % Looping through every active node
            for idx_nodedds=1:numel(obj.activenodes)
                
                
               % Getting data dictionary and required data sources 
               datadictname=eval(sprintf('obj.node%02d.datadict',obj.activenodes(idx_nodedds)));
               nodedd=Simulink.data.dictionary.open(datadictname);
               nodeddsources=nodedd.DataSources;
               reqsources=eval(sprintf('obj.node%02d.wrapdatadicts', obj.activenodes(idx_nodedds)));
               
               % Filtering out not pertinent data sources by name prefix
               nodeddsourcesok={};
               nodeddsourcesokitems=0;
               for ii=1:numel(nodeddsources)
                  if(strcmp(nodeddsources{ii}(1:7),'SCDwrap'))
                      temp={nodeddsources{ii}, 0};
                      nodeddsourcesok=[nodeddsourcesok; temp];
                      nodeddsourcesokitems=nodeddsourcesokitems+1;
                  end
               end
               
               reqsourcesok={};
               reqsourcesokitems=0;
               for ii=1:numel(reqsources)
                  if(strcmp(reqsources{ii}(1:7),'SCDwrap'))           
                      temp={reqsources{ii}, 1};
                      reqsourcesok=[reqsourcesok; temp];
                      reqsourcesokitems=reqsourcesokitems+1;
                  else
                      str=sprintf('Proposed wrapper data dictionary %s does not begin with SCDwrap, ignoring it.', reqsources{ii});
                      warning(str);
                      %obj.printlog(str);
                  end
                  
               end
               
               % Finding which data sources to eliminate and which to add
               if(nodeddsourcesokitems>0 && reqsourcesokitems>0)
                  for ii=1:nodeddsourcesokitems
                     todel=1;
                     for jj=1:reqsourcesokitems
                         if(strcmp(char(nodeddsourcesok{ii,1}),char(reqsourcesok{jj,1})))
                             todel=0;
                             break
                         end
                     end
                     nodeddsourcesok{ii,2}=todel;
                  end
                  
                  for ii=1:reqsourcesokitems
                     toadd=1;
                     for jj=1:nodeddsourcesokitems
                         if(strcmp(char(reqsourcesok{ii,1}),char(nodeddsourcesok{jj,1})))
                             toadd=0;
                             break
                         end
                     end
                     reqsourcesok{ii,2}=toadd;
                  end  
               end
               
               % Removing data sources
               if(nodeddsourcesokitems>0)
                  for ii=1:nodeddsourcesokitems
                    if(nodeddsourcesok{ii,2}==1)
                       str=sprintf('Removing wrapper data source %s from %s', char(nodeddsourcesok{ii,1}), datadictname);
                       obj.printlog(str);
                       removeDataSource(nodedd, char(nodeddsourcesok{ii,1}));
                    end
                  end
               end
               
               % Adding data sources
               if(reqsourcesokitems>0)
                  for ii=1:reqsourcesokitems
                      if(reqsourcesok{ii,2}==1)
                         str=sprintf('Adding wrapper data source %s to %s', char(reqsourcesok{ii,1}), datadictname); 
                         obj.printlog(str);
                         addDataSource(nodedd, char(reqsourcesok{ii,1}));
                      end
                  end
               end
                   
            end  
        end
        
        function obj = setupalgodd(obj)
            
               % Getting data dictionary and required data sources 
               tcvdd=Simulink.data.dictionary.open('tcv.sldd');
               tcvddsources=tcvdd.DataSources;
               reqsources=obj.algoddlist;
               
               % Filtering out not pertinent data sources by name prefix
               tcvddsourcesok={};
               tcvddsourcesokitems=0;
               for ii=1:numel(tcvddsources)
                  if(strcmp(tcvddsources{ii}(1:7),'SCDalgo'))
                      temp={tcvddsources{ii}, 1};
                      tcvddsourcesok=[tcvddsourcesok; temp];
                      tcvddsourcesokitems=tcvddsourcesokitems+1;
                  end
               end
               
               reqsourcesok={};
               reqsourcesokitems=0;
               for ii=1:numel(reqsources)
                  if(strcmp(reqsources{ii}(1:7),'SCDalgo'))           
                      temp={reqsources{ii}, 1};
                      reqsourcesok=[reqsourcesok; temp];
                      reqsourcesokitems=reqsourcesokitems+1;
                  else
                      warning('SCDexpcode:setupalgodd','Proposed algo data dictionary %s does not begin with SCDalgo, ignoring it.', reqsources{ii});
                  end
               end
               
               % Finding which data sources to eliminate and which to add
               if(tcvddsourcesokitems>0 && reqsourcesokitems>0)
                  for ii=1:tcvddsourcesokitems
                     todel=1;
                     for jj=1:reqsourcesokitems
                         if(strcmp(char(tcvddsourcesok{ii,1}),char(reqsourcesok{jj,1})))
                             todel=0;
                             break
                         end
                     end
                     tcvddsourcesok{ii,2}=todel;
                  end

                  for ii=1:reqsourcesokitems
                     toadd=1;
                     for jj=1:tcvddsourcesokitems
                         if(strcmp(char(reqsourcesok{ii,1}),char(tcvddsourcesok{jj,1})))
                             toadd=0;
                             break
                         end
                     end
                     reqsourcesok{ii,2}=toadd;
                  end  
               end
               
               % Removing data sources
               if(tcvddsourcesokitems>0)
                  for ii=1:tcvddsourcesokitems
                    if(tcvddsourcesok{ii,2}==1)
                       str=sprintf('Removing algorithm data source %s', char(tcvddsourcesok{ii,1}));
                       obj.printlog(str);
                       removeDataSource(tcvdd, char(tcvddsourcesok{ii,1}));
                    end
                  end
               end
               
               % Adding data sources
               if(reqsourcesokitems>0)
                  for ii=1:reqsourcesokitems
                      if(reqsourcesok{ii,2}==1)
                         str=sprintf('Adding algorithm data source %s', char(reqsourcesok{ii,1})); 
                         obj.printlog(str);
                         addDataSource(tcvdd, char(reqsourcesok{ii,1}));
                      end
                  end
               end
        end
        
        function buildworkspacesimstruct(obj)
            obj.mdscontainer.buildworkspacesimstruct;
        end    


        function obj = setupmain(obj)
            % sets up global configs for the expcode
            assigncmd=sprintf('scd.expcode=%d;',obj.maincode);
            Simulink.data.evalinGlobal('tcv',assigncmd);
        end
        
        function obj = setupwrappers(obj)
            
            for ii=1:numel(obj.wrapperlist)
                switch obj.wrapperlist{ii}{1}
                    case 1
                        if(obj.wrapperlist{ii}{2}==1)
                            obj.node01.varalgo=obj.wrapperlist{ii}{3};
                            obj.node01.wrapdatadicts{1}=obj.wrapperlist{ii}{5};
                        else
                            warning('SCDclass_expcode:setupwrappers','not supported node01 cpu');
                        end
                        
                    case 2
                        if(obj.wrapperlist{ii}{2}>=1 &&  obj.wrapperlist{ii}{2}<=4)
                            obj.node02.varalgo(obj.wrapperlist{ii}{2})=obj.wrapperlist{ii}{3};
                            obj.node02.wrapdatadicts{obj.wrapperlist{ii}{2}}=obj.wrapperlist{ii}{5};
                        else
                            warning('SCDclass_expcode:setupwrappers','not supported node02 cpu');
                        end
                        
                    case 3
                        if(obj.wrapperlist{ii}{2}>=1 &&  obj.wrapperlist{ii}{2}<=4)
                            obj.node03.varalgo(obj.wrapperlist{ii}{2})=obj.wrapperlist{ii}{3};
                            obj.node03.wrapdatadicts{obj.wrapperlist{ii}{2}}=obj.wrapperlist{ii}{5};
                        else
                            warning('SCDclass_expcode:setupwrappers','not supported node03 cpu');
                        end
                                             
                    case 6
                        if(obj.wrapperlist{ii}{2}>=1 &&  obj.wrapperlist{ii}{2}<=4)
                            obj.node06.varalgo(obj.wrapperlist{ii}{2})=obj.wrapperlist{ii}{3};
                            obj.node06.wrapdatadicts{obj.wrapperlist{ii}{2}}=obj.wrapperlist{ii}{5};
                        else
                            warning('SCDclass_expcode:setupwrappers','not supported node06 cpu');
                        end
                        
                    case 7
                        if(obj.wrapperlist{ii}{2}>=1 &&  obj.wrapperlist{ii}{2}<=4)
                            obj.node07.varalgo(obj.wrapperlist{ii}{2})=obj.wrapperlist{ii}{3};
                            obj.node07.wrapdatadicts{obj.wrapperlist{ii}{2}}=obj.wrapperlist{ii}{5};
                        else
                            warning('SCDclass_expcode:setupwrappers','not supported node07 cpu');
                        end
                        
                    case 8
                        if(obj.wrapperlist{ii}{2}==1)
                            obj.node08.varalgo=obj.wrapperlist{ii}{3};
                            obj.node08.wrapdatadicts{1}=obj.wrapperlist{ii}{5};
                        else
                            warning('SCDclass_expcode:setupwrappers','not supported node08 cpu');
                        end
                        
                    otherwise
                        warning('SCDclass_expcode:setupwrappers','not supported node');
                  
                end
                
            end
            
        end
        
        function obj = setupvaralgo(obj)
            % Sets up varalgo structure in main tcv data dictionary
            % according to varalgo info of the nodes

            % Getting data section of main tcv data dictionary
            d=Simulink.data.dictionary.open('tcv.sldd');
            dd=getSection(d, 'Design Data');
            varalgodd=dd.getEntry('SCDvaralgo');
            
            s=struct();
            
            % Looping through every active node
            for idx_nodedds=1:numel(obj.activenodes)
               varalgo=eval(sprintf('obj.node%02d.varalgo',obj.activenodes(idx_nodedds)));
               
               for ii=1:numel(varalgo)
                   str=sprintf('s.algo%02d%02d=%d;',obj.activenodes(idx_nodedds),ii,varalgo(ii));
                   eval(str);
               end
            end
            
            varalgodd.setValue(s);
            
        end

        
    end 
    
    
end