(* ::Package:: *)

(*update.m*)

(*In this subroutine, Jacobi relaxation will be executed to update the potential fields until 
convergence requirement (determined by tol) is achieved. The output would in the List of Table 
named "tV02" containing the data of the format {x[i],y[j],V[i,j]}*)

While[sd>tol,
tV={};tt={};

For [j=jini+1,j<=jlast-1,j++,(*end for j*)
For [i=iini+1,i<=ilast-1,i++,(*end for i*)
Vold[i,j]=V[i,j];
V[i,j]=(1/4)*(V[i-1,j]+V[i+1,j]+V[i,j+1]+V[i,j-1]);
DeltaV[i,j]=Abs[V[i,j]-Vold[i,j]];
](*end for i*)
](*end for j*)


(*Creating a list the form of {x[i],y[j],V[i,j]} for contourplot *)

For[i=iini,i<=ilast,i++,
	For[j=jini,j<=jlast,j++,
		tt={x[i],y[j],V[i,j]};
		tV=Append[tV,tt]
		];
	];

(*tV2=Table[{x[i],y[j],V[i,j]},{i,iini,ilast},{j,jini,jlast}];*)

Print["Print from update.m"];
Print["nt, sd = ",nt, " ",sd];
Print[ListContourPlot[tV,Mesh->None,ContourLabels->All]];
Print[" "];

(*Test convergence*)
sd=Sqrt[Sum[DeltaV[i,j]^2,{i,iini+1,ilast-1},{j,jini+1,jlast-1}]]/((ilast-iini-1)*(jlast-jini-1));
Print["sd =",sd];

If[sd<tol,
Print["Convergence achieved, sd= ",sd];
(*
For[j=jini,j<=jlast,j++,
Print[Table[V[i,j],{i,iini,ilast}]]
];(*end for j*)
*)
(*Print[tV02];*)
(*Break[]*)
];(*end if test tol*)
(*End of test convergence*)
nt=nt+1;
];(*end while*)


