Forum Replies Created

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • Tcollings
    Participant

    Hi Silvia,

    Happy to help. I used this paper a lot as the basis of my code – http://www2.egr.uh.edu/~aleon3/papers_PDF/Journal/HEC_RAS_Controller.pdf

    Below is the loop I wrote complete the task. Feel free to get in touch if it needs more explaining.

    Cheers,

    Tom

    % outputs
    reach_in= [1,1,2,3]; % 1 = Upper, 2 =Saltash, 3 = Devonport
    Ri_St_in=[42,50,4,4];% 42 = 3905, 50 = 5018, 4 = 2526, 4 = 1251

    for ii = 1:5 % Tides
    disp(ii)
    for jj=1:7 % Surges
    for kk = 1:7 % Lag
    Tide_in=Tide(Lag(:,kk),ii)+(Surge(Lag(:,kk),jj).*Surge_cont(Lag(:,kk))’); % create stage boundary condition to rewrite HEC-RAS input file
    for pp = 1:192
    if Tide_in(pp) <0 %formatting the numbers to the correct format to paste into the input file
    Tide_in_str{pp}=num2str(Tide_in(pp),'%.5f');
    else
    Tide_in_str{pp}=num2str(Tide_in(pp),'%8f');
    end
    end
    Tide_in_str=string(Tide_in_str);
    % the code above here formulates the unsteady flow data to be
    % input in the hec-ras unsteady flow file

    filenameinput = ('C:\Users\Tom\Documents\HECRAS\TamarTavyEstuary_TEMP.u15');
    filenameoutput = ('C:\Users\Tom\Documents\HECRAS\TamarTavyEstuary.u15');
    copyfile(filenameoutput,filenameinput);
    fid = fopen(filenameinput, 'rt');
    fout = fopen (filenameoutput , 'wt');

    while ~feof(fid)
    strTextLine = fgetl(fid); %To read one additional line

    if strfind(strTextLine,'Stage Hydrograph= 192 ') % find line to enter new stage data below
    fprintf(fout,'%s\n',strTextLine);
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(1:10));
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(11:20));
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(21:30)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(31:40)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(41:50)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(51:60)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(61:70)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(71:80)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(81:90)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(91:100)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(101:110));
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(111:120));
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(121:130)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(131:140)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(141:150)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(151:160)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(161:170)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(171:180)) ;
    fprintf(fout,'%s%s%s%s%s%s%s%s%s%s\n',Tide_in_str(181:190)) ;
    fprintf(fout,'%s%s',Tide_in_str(191:192)); %print in the new data line by line
    fseek(fid,1574,'cof'); % skip forward in the reading file to the next line below the old input data
    else
    fprintf(fout,'%s\n',strTextLine); % copy line into the new input file
    end
    end
    fclose(fid);
    fclose(fout);
    h=actxserver('RAS507.HECRASCONTROLLER'); %start hecras in the background
    h.Project_Open('C:\Users\Tom\Documents\HECRAS\TamarTavyEstuary.prj'); %open project file
    h.Compute_HideComputationWindow;
    h.Plan_SetCurrent('Plan 24');
    h.Compute_CurrentPlan(0,0);
    pause(12) % pause necessary to allow Hec-ras time to run the model before matlab reads the output and closes it
    run_check(ii,jj,kk)=h.Compute_Complete() % checks if HEC-RAS has run - if 0, increases pause time

    for ll = 1:4
    for mm = 2:193
    z=h.Output_NodeOutput(2,reach_in(ll),Ri_St_in(ll),0,mm,2); %read output (WS elevation) for the 4 river stations
    WS_elv(mm-1,ll)=z; %save into mega matrix for processing and analysis
    end
    end
    h.Project_Save;
    h.Project_Close()
    h.QuitRas

    %WS_elv=run_hec();

    WS_El(:,:,ii,jj,kk)=WS_elv; %save into mega matrix for processing and analysis

    clear Tide_in
    clear z
    clear Tide_in_str

    end
    end
    end

    Tcollings
    Participant

    I have managed to solve it.

    Matlab wasn’t waiting for HEC-RAS to run and so was closing the program almost immediately after opening it. By simply adding a pause(10) and making the loop pause for 10 seconds, matlab now gives hecras enough time to run before reading out the outputs and closing the program.

    Cheers,

    Tom

Viewing 2 posts - 1 through 2 (of 2 total)