Skip to content

Commit

Permalink
MATLAB code updated
Browse files Browse the repository at this point in the history
  • Loading branch information
allaffa committed Sep 28, 2015
1 parent a2cd06e commit 89be3e1
Show file tree
Hide file tree
Showing 71 changed files with 3,742 additions and 1,035 deletions.
33 changes: 18 additions & 15 deletions MAX_PROJECT/current_code/core/MCSA_adjoint1.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function [sol, rel_res, VAR, RES, DX, NWALKS, tally, count]=MCSA_adjoint1(fp, dist, P, cdf, numer, stat)
function [sol, rel_res, VAR, REL_RES, DX, NWALKS, tally, count]=MCSA_adjoint1(fp, dist, P, cdf, numer, stat)

VAR=[];
rich_it=numer.rich_it;
RES=cell(rich_it,1);
DX=[];
NWALKS=[];
tally=[];
REL_RES=[];
n_walks=stat.nwalks;
max_step=stat.max_step;
eps=numer.eps;
Expand All @@ -32,7 +32,7 @@

r=rhs-B*sol;
R=norm(r,2)/norm(rhs,2);

REL_RES=[REL_RES R];
var_ref=stat.varcut;

if stat.adapt_walks==1 || stat.adapt_cutoff==1
Expand All @@ -42,8 +42,8 @@
r=rhs-B*sol;
[Pb, cdfb]=prob_adjoint_rhs(r, dist);

if stat.adapt_walks==1 && count>1 && R(end)>R(end-1)
stat.varcut=stat.varcut/ceil(R(end)/min(R));
if stat.adapt_walks==1 && count>1 && REL_RES(end)>REL_RES(end-1)
stat.varcut=stat.varcut/ceil(REL_RES(end)/min(REL_RES));
end

[dx, dvar, tot_walks, aux, resid]=MC_adjoint_adapt(H, r, P, cdf, Pb, cdfb, stat);
Expand All @@ -54,9 +54,8 @@
r=rhs-B*sol;
display(strcat('residual norm: ', num2str(norm(r)/norm(rhs))));
rel_residual=norm(r,2)/norm(rhs,2);
R=[R rel_residual];
REL_RES=[REL_RES rel_residual];
VAR=[VAR dvar];
RES{count}=resid;
DX=[DX dx];
count=count+1;
end
Expand All @@ -73,6 +72,7 @@
r=rhs-B*sol;
display(strcat('residual norm: ', num2str(norm(r)/norm(rhs))));
rel_residual=norm(r,2)/norm(rhs,2);
REL_RES=[REL_RES rel_residual];
VAR=[VAR dvar];
DX=[DX dx];
count=count+1;
Expand Down Expand Up @@ -102,10 +102,11 @@

%matrix to be used for the computation of the redisual at each Richardson
%iteration
B1=(eye(size(H1))-H1);
B2=(eye(size(H2))-H2);
B1=sparse(speye(size(H1))-H1);
B2=sparse(speye(size(H2))-H2);

rel_residual=norm(rhs1-B1*sol,2)/norm(rhs1,2);
REL_RES=[REL_RES rel_residual];
count=1;

VAR1=[];
Expand All @@ -118,10 +119,10 @@
sol=sol+r;
r=rhs1-B1*sol;
[Pb, cdfb]=prob_adjoint_rhs(r, dist);
if count>1 && R(end)>R(end-1)
stat.varcut=stat.varcut/ceil(R(end)/min(R));
end

if stat.adapt_walks==1 && count>1 && REL_RES(end)>REL_RES(end-1)
stat.varcut=stat.varcut/ceil(REL_RES(end)/min(REL_RES));
end

[dx, dvar, tot_walks, aux, resid]=MC_adjoint_adapt(H1, r, P.P1, cdf.cdf1, Pb, cdfb, stat);
NWALKS=[NWALKS tot_walks];
Expand All @@ -130,9 +131,9 @@
r=rhs1-B1*sol;
VAR1=[VAR1 dvar];
VAR=[VAR dvar];
RES{count}=resid;
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs1,2);
REL_RES=[REL_RES rel_residual];
else
sol=sol+r;
r=rhs2-B2*sol;
Expand All @@ -149,9 +150,9 @@
r=rhs2-B2*sol;
VAR2=[VAR2 dvar];
VAR=[VAR dvar];
RES{count}=resid;
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs2,2);
REL_RES=[REL_RES rel_residual];
end
count=count+1;
end
Expand All @@ -172,6 +173,7 @@
VAR=[VAR dvar];
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs1,2);
REL_RES=[REL_RES rel_residual];
else
sol=sol+r;
r=rhs2-B2*sol;
Expand All @@ -185,6 +187,7 @@
VAR=[VAR dvar];
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs2,2);
REL_RES=[REL_RES rel_residual];
end
count=count+1;
end
Expand Down
31 changes: 18 additions & 13 deletions MAX_PROJECT/current_code/core/MCSA_adjoint2.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function [sol, rel_res, VAR, RES, DX, NWALKS, tally, count, reject]=MCSA_adjoint2(fp, dist, P, cdf, numer, stat)
function [sol, rel_res, VAR, REL_RES, DX, NWALKS, tally, count, reject]=MCSA_adjoint2(fp, dist, P, cdf, numer, stat)

DX=[];
NWALKS=[];
Expand All @@ -9,7 +9,7 @@
eps=numer.eps;
rich_it=numer.rich_it;
VAR=cell(rich_it,1);
RES=cell(rich_it,1);
REL_RES=[];

if ~ strcmp(fp.precond, 'alternating')

Expand All @@ -29,6 +29,7 @@

r=rhs-B*sol;
rel_residual=norm(r,2)/norm(rhs,2);
REL_RES=[REL_RES rel residual];
count=1;

if stat.adapt_walks==1 || stat.adapt_cutoff==1
Expand All @@ -38,8 +39,8 @@
r=rhs-B*sol;
[Pb, cdfb]=prob_adjoint_rhs(r, dist);

if stat.adapt_walks==1 && count>1 && R(end)>R(end-1)
stat.varcut=stat.varcut/ceil(R(end)/min(R));
if stat.adapt_walks==1 && count>1 && REL_RES(end)>REL_RES(end-1)
stat.varcut=stat.varcut/ceil(REL_RES(end)/min(REL_RES));
end

[dx, dvar, tot_walks, aux, resid, rej]=MC_adjoint_adapt2(H, r, P, cdf, Pb, cdfb, stat);
Expand All @@ -50,8 +51,8 @@
r=rhs-B*sol;
display(strcat('residual norm: ', num2str(norm(r)/norm(rhs))));
rel_residual=norm(r,2)/norm(rhs,2);
REL_RES=[REL_RES rel_residual];
VAR{count}=dvar;
RES{count}=resid;
DX=[DX dx];
count=count+1;
end
Expand All @@ -69,6 +70,7 @@
r=rhs-B*sol;
display(strcat('residual norm: ', num2str(norm(r)/norm(rhs))));
rel_residual=norm(r,2)/norm(rhs,2);
REL_RES=[REL_RES rel_residual];
VAR=[VAR dvar];
DX=[DX dx];
count=count+1;
Expand Down Expand Up @@ -99,10 +101,11 @@

%matrix to be used for the computation of the redisual at each Richardson
%iteration
B1=(eye(size(H1))-H1);
B2=(eye(size(H2))-H2);
B1=sparse(speye(size(H1))-H1);
B2=sparse(speye(size(H2))-H2);

rel_residual=norm(rhs1-B1*sol,2)/norm(rhs1,2);
REL_RES=[REL_RES rel_residual];
count=1;

VAR1=[];
Expand All @@ -116,8 +119,8 @@
r=rhs1-B1*sol;
[Pb, cdfb]=prob_adjoint_rhs(r, dist);

if stat.adapt_walks==1 && count>1 && R(end)>R(end-1)
stat.varcut=stat.varcut/ceil(R(end)/min(R));
if stat.adapt_walks==1 && count>1 && REL_RES(end)>REL_RES(end-1)
stat.varcut=stat.varcut/ceil(REL_RES(end)/min(REL_RES));
end

[dx, dvar, tot_walks, aux, resid, rej]=MC_adjoint_adapt2(H1, r, P.P1, cdf.cdf1, Pb, cdfb, stat);
Expand All @@ -127,16 +130,16 @@
sol=sol+dx;
r=rhs1-B1*sol;
VAR{count}=dvar;
RES{count}=resid;
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs1,2);
REL_RES=[REL_RES rel_residual];
else
sol=sol+r;
r=rhs2-B2*sol;
[Pb, cdfb]=prob_adjoint_rhs(r, dist);

if stat.adapt_walks==1 && count>1 && R(end)>R(end-1)
stat.varcut=stat.varcut/ceil(R(end)/min(R));
if stat.adapt_walks==1 && count>1 && REL_RES(end)>REL_RES(end-1)
stat.varcut=stat.varcut/ceil(REL_RES(end)/min(REL_RES));
end

[dx, dvar, tot_walks, aux, resid, rej]=MC_adjoint_adapt2(H2, r, P.P2, cdf.cdf2, Pb, cdfb, stat);
Expand All @@ -146,9 +149,9 @@
sol=sol+dx;
r=rhs2-B2*sol;
VAR{count}=dvar;
RES{count}=resid;
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs2,2);
REL_RES=[REL_RES rel_residual];
end
count=count+1;
end
Expand All @@ -168,6 +171,7 @@
VAR=[VAR dvar];
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs1,2);
REL_RES=[REL_RES rel residual];
else
sol=sol+r;
r=rhs2-B2*sol;
Expand All @@ -180,6 +184,7 @@
VAR=[VAR dvar];
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs2,2);
REL_RES=[REL_RES rel_residual];
end
count=count+1;
end
Expand Down
6 changes: 3 additions & 3 deletions MAX_PROJECT/current_code/core/MCSA_forward.m
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
function [sol, rel_res, VAR, DX, NWALKS, tally, count, reject]=MCSA_forward(fp, P, cdf, numer, stat)
function [sol, rel_res, VAR, REL_RES, DX, NWALKS, tally, count, reject]=MCSA_forward(fp, P, cdf, numer, stat)

reject=0;

if (stat.nchecks == 1)
[sol, rel_res, VAR, DX, NWALKS, tally, count]=MCSA_forward1(fp, P, cdf, numer, stat);
[sol, rel_res, VAR, REL_RES, DX, NWALKS, tally, count]=MCSA_forward1(fp, P, cdf, numer, stat);

elseif (stat.nchecks == 2)
[sol, rel_res, VAR, DX, NWALKS, tally, count, reject]=MCSA_forward2(fp, P, cdf, numer, stat);
[sol, rel_res, VAR, REL_RES, DX, NWALKS, tally, count, reject]=MCSA_forward2(fp, P, cdf, numer, stat);

end

Expand Down
28 changes: 26 additions & 2 deletions MAX_PROJECT/current_code/core/MCSA_forward1.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
function [sol, rel_res, VAR, DX, NWALKS, tally, count]=MCSA_forward1(fp, P, cdf, numer, stat)
function [sol, rel_res, VAR, REL_RES, DX, NWALKS, tally, count]=MCSA_forward1(fp, P, cdf, numer, stat)

VAR=[];
NWALKS=[];
DX=[];
REL_RES=[];
n_walks=stat.nwalks;
max_step=stat.max_step;
eps=numer.eps;
Expand All @@ -27,19 +28,26 @@

r=rhs-B*sol;
rel_residual=norm(r,2)/norm(rhs,2);
REL_RES=[REL_RES rel_residual];
count=1;

if stat.adapt_walks==1 || stat.adapt_cutoff==1
while(rel_residual>eps && count<=rich_it)
display(strcat('iteration number', {' '}, num2str(count)))
sol=sol+r;
r=rhs-B*sol;

if stat.adapt_walks==1 && count>1 && REL_RES(end)>REL_RES(end-1)
stat.varcut=stat.varcut/ceil(REL_RES(end)/min(REL_RES));
end

[dx, dvar, nwalks]=MC_forward_adapt(H, r, P, cdf, stat);
NWALKS=[NWALKS nwalks];
sol=sol+dx;
r=rhs-B*sol;
display(strcat('residual norm: ', num2str(norm(r)/norm(rhs))));
rel_residual=norm(r,2)/norm(rhs,2);
REL_RES=[REL_RES rel_residual];
VAR=[VAR dvar];
DX=[DX dx];
count=count+1;
Expand All @@ -55,6 +63,7 @@
r=rhs-B*sol;
display(strcat('residual norm: ', num2str(norm(r)/norm(rhs))));
rel_residual=norm(r,2)/norm(rhs,2);
REL_RES=[REL_RES rel_residual];
VAR=[VAR dvar];
DX=[DX dx];
count=count+1;
Expand Down Expand Up @@ -93,6 +102,7 @@

r=rhs1-B1*sol;
rel_residual=norm(r,2)/norm(rhs1,2);
REL_RES=[REL_RES rel_residual];
count=1;

if stat.adapt_walks==1 || stat.adapt_cutoff==1
Expand All @@ -104,21 +114,33 @@
NWALKS=[NWALKS nwalks];
sol=sol+dx;
r=rhs1-B1*sol;

if stat.adapt_walks==1 && count>1 && REL_RES(end)>REL_RES(end-1)
stat.varcut=stat.varcut/ceil(REL_RES(end)/min(REL_RES));
end

VAR1=[VAR1 dvar];
VAR=[VAR dvar];
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs1,2);
REL_RES=[REL_RES rel_residual];
else
sol=sol+r;
r=rhs2-B2*sol;

if stat.adapt_walks==1 && count>1 && REL_RES(end)>REL_RES(end-1)
stat.varcut=stat.varcut/ceil(REL_RES(end)/min(REL_RES));
end

[dx, dvar, nwalks]=MC_forward_adapt(H2, r, P.P2, cdf.cdf2, stat);
NWALKS=[NWALKS nwalks];
sol=sol+dx;
r=rhs2-B2*sol;
r=rhs2-B2*sol;
VAR2=[VAR2 dvar];
VAR=[VAR dvar];
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs2,2);
REL_RES=[REL_RES rel_residual];
end

count=count+1;
Expand All @@ -137,6 +159,7 @@
VAR=[VAR dvar];
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs1,2);
REL_RES=[REL_RES rel_residual];
else
sol=sol+r;
r=rhs2-B2*sol;
Expand All @@ -148,6 +171,7 @@
VAR=[VAR dvar];
DX=[DX dx];
rel_residual=norm(r,2)/norm(rhs2,2);
REL_RES=[REL_RES rel_residual];
end
count=count+1;
end
Expand Down
Loading

0 comments on commit 89be3e1

Please sign in to comment.