Which parts should I change?


I’m new to both MATLAB and EPANET , Please help me understand this, if you can.
thanks in advance for your help!

What parts of the below code should I change if I want the following scenarios ?

  • injection flow rate of 125 L/h
  • contaminant concentration of 230,000 mg/L
  • injection duration of 2 h

%%  Case Study for CCWI2016

close all; clear; rng(1)

%% Load EPANET Network and MSX
G = epanet('BWSN_Network_1.inp'); % Load EPANET Input file
G.loadMSXFile('Arsenite.msx'); % Load MSX file

% Sensor locations
sensor_id = {'JUNCTION-17', 'JUNCTION-83', 'JUNCTION-122', 'JUNCTION-31', 'JUNCTION-45'};
sensor_index = G.getNodeIndex(sensor_id);

%% Simulation Setup
t_d = 5; % days
G.setTimeSimulationDuration(t_d*24*60*60); % Set simulation duration of 5 days

%% Get Network data
demand_pattern = G.getPattern;
roughness_coeff = G.getLinkRoughnessCoeff;
node_id = G.getNodeNameID;


%% Scenarios
Ns = 100; % Number of scenarios to simulate
u_p = 0.20; % pattern uncertainty
u_r = 0.20; % roughness coefficient uncertainty
max_inj_conc = 2.0;
inj_start_time = 2*48; % after day 2 (Dt = 30min)
inj_duration = 24; % maximum duration of 12 hours
inj_sc=[randi(G.NodeCount,Ns,1), max_inj_conc*rand(Ns,1), randi(48,Ns,1)+inj_start_time, randi(inj_duration,Ns,1)]; % Injection location, magnitude, start time, duration 

%% Run epochs
for i = 1:Ns
    disp(['Iteration ', int2str(i)])
    % Randomize hydraulics
    r_p = -u_p + 2*u_p.*rand(size(demand_pattern,1),size(demand_pattern,2));
    new_demand_pattern = demand_pattern + demand_pattern.*r_p;
    G.setPatternMatrix(new_demand_pattern); % Set new patterns
    r_r = -u_r + 2*u_r.*rand(size(roughness_coeff,1),size(roughness_coeff,2));
    new_roughness_coeff = roughness_coeff + roughness_coeff.*r_r;
    G.setLinkRoughnessCoeff(new_roughness_coeff); % Set new roughness coefficients
    % Randomize injection
    G.setMSXSources(node_id(inj_sc(i,1)), 'AsIII', 'Setpoint', inj_sc(i,2), 'AS3PAT'); % Specify Arsenite injection source
    as3_pat = zeros(1, t_d*48);
    as3_pat(inj_sc(i,3):(inj_sc(i,3)+inj_sc(i,4))) = 1; % 
    G.setMSXPattern('AS3PAT',as3_pat); % Set pattern for injection
    Q{i} = G.getMSXComputedQualityNode(sensor_index); % Solve hydraulics and MSX quality dynamics
    G.setMSXSources(node_id(inj_sc(i,1)), 'AsIII', 'Setpoint', 0, 'AS3PAT'); % Reset injection source

%% Plot results
for i = 1:Ns
    for j = 1:length(sensor_index)
       plot(Q{i}.Time/24/60/60, Q{i}.Quality{j}(:,1),'-','Color',[0,0.7,0.9]); hold on; grid on
for i = 1:length(sensor_index)
   ylabel('Cl_2 (mg/L)')
   xlabel('Time (days)')

Best regards,


The ''inj_sc(i,2)’’ matrix in sample code saves the concentration of specie in different scenarios.


Hi @alireza2032, Thanks! but i didn’t understand what does " inj_sc(i,2)" means. Maybe the problem is that I’m not very familiar with MATLAB.
Anyway, thank you very much


Dear @david, @alireza2032 is right.

I advise you to study a bit more on the MATLAB data structures. Just think of it as a matrix A(i,j), where A is the name of the data structure, i is the row and j is the column.

In any case, if you set as Ns=1, the code will be equivalent to the scalar case.

I suggest you running the code step by step (debug mode) by pressing F10, and check the variables which are created.