can't get sequential .exe execution to occur in matlab

조회 수: 1 (최근 30일)
Wesser
Wesser 2022년 7월 1일
댓글: Wesser 2022년 7월 8일
Hi,
I'm trying to copy a reference file LEVEL_01.dir to a number of number of sub directories and then change the content of LEVEL_01.dir to hold the new dr
clear all;
clc;
%Solute transport parameters in row 47 of SELECTOR.IN file
SolTrans=[0.035 0 1 0.0479 0 0 0 0 273895 1 0 0 0 0]; %[Kd Nu Beta Henry SnkL1 SnkS1 SnkG1 SnkL1p SnkS1p SnkG1p SnkL10 SnkS10 SnkG10 Alfa] !! UPDATE NUMBERS IN BRACKETS !!
%~~~~~~
%ADD NOISE TO SOLUTE TRANSPORT PARAMETERS
num_sim=100; %100 Monte Carlo Simulations
Kd=SolTrans(1)+.02*rand(1,num_sim); %a vector with 100 values of Kd parameters. We assume that the error is normally distributed with a standard deviation of ##.
Nu=SolTrans(2)+.02*rand(1,num_sim);
Beta=SolTrans(3)+.02*rand(1,num_sim); %!!! CHANGE .02 FOR ALL THESE TO APPROPRIATE STANDARD DEVIATIONSN !!!
Henry=SolTrans(4)+.02*rand(1,num_sim);
SnkL1=SolTrans(5);
SnkS1=SolTrans(6);
SnkG1=SolTrans(7);
SnkL1p=SolTrans(8);
SnkS1p=SolTrans(9)+.02*rand(1,num_sim); %!!! FOR AWI MODIFIED MODEL - KL - LANGMUIR EXPONENT FOR AWI FOR LANGMUIR SORPTION or 0 FOR FREUNDLICH SORPTION
SnkG1p=SolTrans(10);
SnkL10=SolTrans(11);
SnkS10=SolTrans(12);
SnkG10=SolTrans(13);
Alfa=SolTrans(14);
%~~~~~~
%INITIALIZE FILES AND DIRECTORIES
hydrus_exec='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\H1D_CALC.exe'; %'C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe';
hydrus_ref='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS';
profileDAT='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\PROFILE.DAT';
options='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\OPTIONS.IN';
atmosph='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\ATMOSPH.IN';
selectorIN='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\SELECTOR.IN';
level='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\LEVEL_01.dir';
work_dir='C:\Users\jessi\Desktop\Simulations\';
mkdir(work_dir);
%~~~~~~
%CREATE DIRECTORIES AND INPUT FILES - This block is doing the work. Creates the input files for all realizations. Copies the mandatory file Profile.dat to
% the corresponding folder and creates the corresponding Selector
% .in. The Selector.in file is different for each realization since the solute transport parameters is
% variable. The code reads the reference Selector.in, copies the first 46 lines to the new file, write the van Genuchten parameters to the 47th line,
% and finally copies the last 10 lines from the reference Selector.in to the new file.
path=cell(1,num_sim);
for i=1:num_sim
path{i}=strcat(work_dir,'run_',num2str(i)); %create folder for each run
mkdir(path{i});
copyfile(profileDAT,path{i}); %copy profileDAt from reference directory to the simulation directory
copyfile(options,path{i}); %copy options from reference directory to the simulation directory
copyfile(atmosph,path{i}); %copy atmosph from reference directory to the simulation director
copyfile(level,path{i}); %copy options from reference directory to the simulation directory
copyfile(hydrus_exec,path{i}); %copy options from reference directory to the simulation directory
fileID_out=fopen(strcat(path{i},'\selector.in'),'wt'); %manipulate Selector.in for each run
% "wt"= permision for file axis type
% w=Open or create new file for writing. Discard existing contents, if any.
% To open files in text mode, attach the letter 't' to the permission argument,
fileID_in=fopen(selectorIN);
skip_lines=46; %!!! THIS IS THE LINE THAT CHANGES IN SELECTOR.IN FILE , i.e. solute transport parameters!!!
for k=1:(skip_lines)
x=fgetl(fileID_in); %x = fgetl(fileID)= returns the next line of the specified file, removing the newline characters.
fprintf(fileID_out,'%s\n',x); % %s in the formatSpec input indicates that the values of the variables url and sitename, should be printed as text.
% '\n' as a newline indicator.
% "x" = prints the values from variable x
end
out_Sol=SolTrans; % Renaming...
%Whatever the solute transport parameter is for that monte carlo run (i)
out_Sol(1)=Kd(i); % Solid phase sorption Kd
out_Sol(2)=Nu(i); % Van Genuchten parameter
out_Sol(3)=Beta(i); % Van Genuchten parameter
out_Sol(4)=Henry(i); % Kh = KL*Rmax
out_Sol(9)=SnkS1p(i); % KL - LANGMUIR EXPONENT FOR AWI FOR LANGMUIR SORPTION or 0 FOR FREUNDLICH SORPTION
fprintf(fileID_out,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f\n',out_Sol');
% %f = Fixed-point notation (Use a precision operator to specify the number of digits after the decimal point.)
% '\n' as a newline indicator.
fgetl(fileID_in); % returns the next line of the specified file, removing the newline characters.
skip_lines_end=5; % CORRECT NUMBER??? DOUBLE CHECK
for k=1:(skip_lines_end)
x=fgetl(fileID_in);
fprintf(fileID_out,'%s\n',x);
end
fclose('all');
end
path=cell(1,num_sim);
%For this section I want hydrus.exe to run in each path{1}...when I run the
%code as is, only the first of the 100 simulations is run. In other words,
%hydrus.exe is sucsessufully completed in path{1}, but then for path{2} and
%all the rest, the files are copied into these new directories, but the
%.exe doesn't run in any of the other sub files. I what think this has something
%to do with how the below segment is scripted but I don't know what to try
%next...
for i=1:num_sim
exec_path=['"' hydrus_exec '" "' path{i} '"'];
[x, y]= dos(exec_path);
if x % unsuccessful
error('exe failed'); % or take some other action besides throwing an error
end
end

채택된 답변

Voss
Voss 2022년 7월 1일
Try removing this line (between the for loops):
path=cell(1,num_sim);
That line has the effect of resetting path to a cell array of empty arrays, after path was already set up in the previous for loop but before path is used in exec_path in the next for loop.
  댓글 수: 3
Voss
Voss 2022년 7월 1일
편집: Voss 2022년 7월 1일
In that case, don't copy LEVEL_01.dir to the different simulation directories. Instead, in each directory write a new LEVEL_01.dir that contains the correct path:
path=cell(1,num_sim);
for i=1:num_sim
path{i}=strcat(work_dir,'run_',num2str(i)); %create folder for each run
mkdir(path{i});
copyfile(profileDAT,path{i}); %copy profileDAt from reference directory to the simulation directory
copyfile(options,path{i}); %copy options from reference directory to the simulation directory
copyfile(atmosph,path{i}); %copy atmosph from reference directory to the simulation director
% don't copy LEVEL_01.dir:
% copyfile(level,path{i}); %copy options from reference directory to the simulation directory
% instead, write it:
fid_level = fopen(fullfile(path{i},'LEVEL_01.dir'),'w');
fprintf(fid_level,'%s',path{i});
fclose(fid_level);
copyfile(hydrus_exec,path{i}); %copy options from reference directory to the simulation directory
% ...
% rest of code
% ...
end
And actually, it doesn't seem like you need to copy the exe itself to each directory, since you're always running the original exe located at C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\H1D_CALC.exe but with a different path{i} argument each time.
An alternative might be to copy the exe (like you are doing) and run the copies (which you are not doing), which might simplify the necessary setup, e.g., if the exe defaults to looking in its current working directory for the files it needs. Hard to say for sure without knowing what the exe expects.
Wesser
Wesser 2022년 7월 8일
"An alternative might be to copy the exe (like you are doing) and run the copies (which you are not doing), which might simplify the necessary setup, e.g., if the exe defaults to looking in its current working directory for the files it needs. "
This is exaclty what the .exe does...it does not work well when trying to call files outside its working directory. How would I script this so that I am running the copies of the .exe in each path{i} ?
Also thank you SOOO much for your assistance with this!!!
Here is the current version of the code:
clear all;
clc;
%Solute transport parameters in row 47 of SELECTOR.IN file
SolTrans=[0.035 0 1 0.0479 0 0 0 0 273895 1 0 0 0 0]; %[Kd Nu Beta Henry SnkL1 SnkS1 SnkG1 SnkL1p SnkS1p SnkG1p SnkL10 SnkS10 SnkG10 Alfa] !! UPDATE NUMBERS IN BRACKETS !!
%~~~~~~
%ADD NOISE TO SOLUTE TRANSPORT PARAMETERS
num_sim=100; %100 Monte Carlo Simulations
Kd=SolTrans(1)+.02*rand(1,num_sim); %a vector with 100 values of Kd parameters. We assume that the error is normally distributed with a standard deviation of ##.
Nu=SolTrans(2)+.02*rand(1,num_sim);
Beta=SolTrans(3)+.02*rand(1,num_sim); %!!! CHANGE .02 FOR ALL THESE TO APPROPRIATE STANDARD DEVIATIONSN !!!
Henry=SolTrans(4)+.02*rand(1,num_sim);
SnkL1=SolTrans(5);
SnkS1=SolTrans(6);
SnkG1=SolTrans(7);
SnkL1p=SolTrans(8);
SnkS1p=SolTrans(9)+.02*rand(1,num_sim); %!!! FOR AWI MODIFIED MODEL - KL - LANGMUIR EXPONENT FOR AWI FOR LANGMUIR SORPTION or 0 FOR FREUNDLICH SORPTION
SnkG1p=SolTrans(10);
SnkL10=SolTrans(11);
SnkS10=SolTrans(12);
SnkG10=SolTrans(13);
Alfa=SolTrans(14);
%~~~~~~
%INITIALIZE FILES AND DIRECTORIES
hydrus_exec='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\H1D_CALC.exe'; %'C:\Program Files (x86)\PC-Progress\Hydrus-1D 4\H1D_calc.exe';
hydrus_ref='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS';
profileDAT='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\PROFILE.DAT';
options='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\OPTIONS.IN';
atmosph='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\ATMOSPH.IN';
selectorIN='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\SELECTOR.IN';
level='C:\Users\jessi\Desktop\Hydrus\Projects\ATSDR\AFFF1PFOS\LEVEL_01.dir';
work_dir='C:\Users\jessi\Desktop\Simulations\';
mkdir(work_dir);
%~~~~~~
%CREATE DIRECTORIES AND INPUT FILES - This block is doing the work. Creates the input files for all realizations. Copies the mandatory file Profile.dat to
% the corresponding folder and creates the corresponding Selector
% .in. The Selector.in file is different for each realization since the solute transport parameters is
% variable. The code reads the reference Selector.in, copies the first 46 lines to the new file, write the van Genuchten parameters to the 47th line,
% and finally copies the last 10 lines from the reference Selector.in to the new file.
path=cell(1,num_sim);
for i=1:num_sim
path{i}=strcat(work_dir,'run_',num2str(i)); %create folder for each run
mkdir(path{i});
copyfile(profileDAT,path{i}); %copy profileDAt from reference directory to the simulation directory
copyfile(options,path{i}); %copy options from reference directory to the simulation directory
copyfile(atmosph,path{i}); %copy atmosph from reference directory to the simulation director
copyfile(hydrus_exec,path{i}); %copy options from reference directory to the simulation directory
fid_level = fopen(fullfile(path{i},'LEVEL_01.dir'),'w');
fprintf(fid_level,'%s',path{i});
fclose(fid_level);
fileID_out=fopen(strcat(path{i},'\selector.in'),'wt'); %manipulate Selector.in for each run
% "wt"= permision for file axis type
% w=Open or create new file for writing. Discard existing contents, if any.
% To open files in text mode, attach the letter 't' to the permission argument,
fileID_in=fopen(selectorIN);
skip_lines=46; %!!! THIS IS THE LINE THAT CHANGES IN SELECTOR.IN FILE , i.e. solute transport parameters!!!
for k=1:(skip_lines)
x=fgetl(fileID_in); %x = fgetl(fileID)= returns the next line of the specified file, removing the newline characters.
fprintf(fileID_out,'%s\n',x); % %s in the formatSpec input indicates that the values of the variables url and sitename, should be printed as text.
% '\n' as a newline indicator.
% "x" = prints the values from variable x
end
out_Sol=SolTrans; % Renaming...
%Whatever the solute transport parameter is for that monte carlo run (i)
out_Sol(1)=Kd(i); % Solid phase sorption Kd
out_Sol(2)=Nu(i); % Van Genuchten parameter
out_Sol(3)=Beta(i); % Van Genuchten parameter
out_Sol(4)=Henry(i); % Kh = KL*Rmax
out_Sol(9)=SnkS1p(i); % KL - LANGMUIR EXPONENT FOR AWI FOR LANGMUIR SORPTION or 0 FOR FREUNDLICH SORPTION
fprintf(fileID_out,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f\n',out_Sol');
% %f = Fixed-point notation (Use a precision operator to specify the number of digits after the decimal point.)
% '\n' as a newline indicator.
fgetl(fileID_in); % returns the next line of the specified file, removing the newline characters.
skip_lines_end=5; % CORRECT NUMBER??? DOUBLE CHECK
for k=1:(skip_lines_end)
x=fgetl(fileID_in);
fprintf(fileID_out,'%s\n',x);
end
fclose('all');
end
for i=1:num_sim
exec_path=['"' hydrus_exec '" "' path{i} '"'];
[x, y]= dos(exec_path);
if x % unsuccessful
error('exe failed'); % or take some other action besides throwing an error
end
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Time Series Events에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by