function [Err] = ObjFxn_HM_PROXY(x,S_struct)

global S_struct

inj = S_struct.inj;
qp = S_struct.qp;
T = S_struct.T;
cinj = S_struct.cinj;
cwf = S_struct.cwf;
% NDiv = S_struct.NDiv;
Lwell = S_struct.Lwell;

ni = size(inj,2);
np = size(qp,2);
TC = ni*np;

Np = size(x,1);
Err = zeros(Np,1);

parfor i=1:Np
    xx  = x(i,:);
    xx = xx';
    count = 0;
    cwf_prod = zeros(length(T),np);
    for ii = 1:ni
        len1 = np*(ii-1)+1;
        len2 = np*ii;
        fij = xx(len1:len2); %fij for corresponding injector with all producer
        for pp = 1:np
            sum1 = 0;
            f = xx(pp:np:TC); %fij for corresponding producer with all injector
            for i1 = 1:ni
                sum1 = sum1 + (f(i1)*inj(:,i1));
            end
            qp1 = (qp(:,pp)*f(ii).*inj(:,ii))./sum1;
            inj1 = inj(:,ii)*fij(pp);
            len3 = TC + count+1;
            len4 = 2*TC + count+1;
            len5 = 3*TC + count+1;
            len6 = 4*TC + count+1;
            Vp = xx(len3);
            D = xx(len4);
            qf = xx(len5);
            NDiv = round(xx(len6));
            cwf_prod(:,pp) = cwf_prod(:,pp) + ADE_model(qp1,inj1,T,cinj,NDiv,Lwell(count+1),Vp,D,qf);
            count = count + 1;
        end
    end
    Err1 = cwf_prod(:,1)-cwf(:,1);
    Err2 = cwf_prod(:,2)-cwf(:,2);
    Err(i) = (sqrt(Err1'*Err1)/length(Err1)) + (sqrt(Err2'*Err2)/length(Err2));
    if isnan(Err(i))
        Err(i) = 1e5;
    end
end


