# Unit Testing

### Question

• i want to unit test the code below can someone help me get the stub of this code done ??????

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package Core;

/**
*
* @author M Shakeer Anver
*/
public class CalCore
{
int ip,ir,nxe,ikp,iks,iwp1,idof,nodof,no,nincr;
double rlpile,ell,ei,val,dia;

private int NELEM;
int NELEP1;
private int NDOF;
private int NCOEF;
private final double PI = 3.14159265358979;
private int niter;
private double tol;

plimit[],fmax[],excess[],dsoilm[],fsoil[],dfsoil[],ftemp[],disp[],oldisp[];
private double kp[][],eld[],fvec[],xy[];
private int nf[][],g[];
private int inf;

/**
* Calculation Core performs the required calculations on the received data
* and produces the results
*/
public CalCore(){

kp = new double[5][5];
eld = new double[5];
fvec = new double[5];
xy = new double[3];

g = new int[5];
niter = 200;
tol = 0.001;
ikp = 4;
idof = 4;
iwp1 = 4;
nodof = 2;

}

/**
* Calculates the result based on the received data
* @param data
* @return
*  The calculation results
*/
public Result calculate(Data data){
NELEM = data.getNumOfElements();
NELEP1 = NELEM+2;
NDOF = 2 * NELEP1;
NCOEF = 4 * NDOF;
//Prepare arrays
kglb = new double[NCOEF];
bendm = new double[NELEP1];
shear = new double[NELEP1];
ksvec = new double[NELEP1];
kslatd = new double[NELEP1];
zcoord = new double[NELEP1];
soilm = new double[NELEP1];
soilpr = new double[NELEP1];
plimit = new double[NELEP1];
fmax = new double[NELEP1];
excess = new double[NELEP1];
dsoilm = new double[NELEP1];
fsoil = new double[NELEP1];
dfsoil = new double[NELEP1];
ftemp = new double[NELEP1];
disp = new double[NDOF];
oldisp = new double[NDOF];
nf = new int[NELEP1][3];
inf = NELEP1;

Result result = new Result();
nxe = data.getNumOfElements();
rlpile = data.getPileLength();
ei = data.getEI();
dia = data.getPileDiameter();
nincr = data.getNumOfPiles();
int nn=nxe+1;

for(int i=0;i<nn;i++){
}
result.setNumOfNodes(nn);
for(int i=1;i<=nn;i++){
zcoord[i] = data.getPileNodeCoordinate(i-1);
soilm[i] = data.roundLateralSoilMovement(i-1,4);
plimit[i] = data.roundLimitSoilPressure(i-1,2);
}
int n = 2*nn;
ir = n*iwp1;
nulvec(kglb,ir);
nulvec(fsoil,nn);
nulvec(disp,n);

//Convert Soil limit pressure to limit force
for(int i=1;i<=nn;i++){
if(i==1)
fmax[i] = plimit[i]*dia*0.5D*(zcoord[i+1]-zcoord[i]);
else if(i==nn)
fmax[i] = plimit[i]*dia*0.5*(zcoord[i]-zcoord[i-1]);
else
fmax[i] = plimit[i]*dia*0.5*(zcoord[i+1]-zcoord[i-1]);
}

//Assembly of global stifness matrix for pile elements
formng(nf,inf,nn,nodof);
for(ip = 1;ip<=nxe;ip++){
formg(ip,g,nf,inf);
ell = zcoord[ip+1] - zcoord[ip];
kplat(kp,ei,ell);
formkv(kglb,kp,ikp,g,n,idof);
}

//Form diagonal lumped soil stiffness
for(int i=1;i<=nn;i++){
if(i==1)
kslatd[i]=ksvec[i]*0.5D*(zcoord[i+1]-zcoord[i]);
else if (i==nn)
kslatd[i]=ksvec[i]*0.5D*(zcoord[i]-zcoord[i-1]);
else
kslatd[i] = ksvec[i]*0.5D*(zcoord[i+1]-zcoord[i-1]);

}

//Assembly of pile-soil system global stiffness matrix
for(int i=1;i<=nn;i++){
kglb[2*i-1] = kglb[2*i-1]+kslatd[i];
}

//Reduce global stiffness equation
banred(kglb,n,iwp1);

//Incremental soil movements
for(int i=1;i<=nn;i++){
dsoilm[i]=(double)(soilm[i]/nincr);

}

//apply lateral soil movements in increments
for(int incr = 1;incr<=nincr;incr++){
nulvec(excess,nn);
nulvec(oldisp,n);

//iterate for convergence
int iter = 0;
Double error = new Double(0.0);
do{
iter++;
//induced incremental lateral loads due incremental lateral soil
//movements
for(int i=1;i<=nn;i++){
}
nulvec(excess,nn);

//solution for stiffness equations

//recover soil spring forces
for(int i=1;i<=nn;i++){
ftemp[i] = fsoil[i]+dfsoil[i];

if(Double.compare(Math.abs(ftemp[i]), fmax[i])>0){
excess[i]=Math.abs(ftemp[i])-fmax[i];
excess[i]= DSIGN(excess[i], ftemp[i]);
ftemp[i] = DSIGN(fmax[i], ftemp[i]);
}
}
error = 0.0;
for(int i=1;i<=n;i+=2){
}

}while(error.compareTo(tol)>0&&iter<=niter);

result.setNumOfItrForConv(niter);

for(int i=1;i<=nn;i++){
fsoil[i] = ftemp[i];
}

for(int i=1;i<=n;i++){
}

for(ip=1;ip<=nxe;ip++){
formg(ip, g, nf, inf);
ell = zcoord[ip+1]-zcoord[ip];
kplat(kp, ei, ell);
for(int m=1;m<=idof;m++){
eld[m] = disp[g[m]];
}
//Shear force and bending moment in pile
mvmult(kp,ikp,eld,idof,idof,fvec);
double factor = 0.0;
if(ip==1)
factor = 1.0;
else
factor = (zcoord[ip]-zcoord[ip-1])/(zcoord[ip+1]-zcoord[ip-1]);
shear[ip] = fvec[1]+factor*fsoil[ip];
bendm[ip] = fvec[2];
if(ip==nxe){
shear[ip+1] = -fvec[3]-fsoil[ip+1];
bendm[ip+1] = -fvec[4];
}
}
//Soil pressure acting on pile at nodes
for(int i=1;i<=nn;i++){
if(i==1)
soilpr[i] = fsoil[i]/(dia*0.5*(zcoord[i+1]-zcoord[i]));
else if(i==nn)
soilpr[i] = fsoil[i]/(dia*0.5*(zcoord[i]-zcoord[i-1]));
else
soilpr[i] = fsoil[i]/(dia*0.5*(zcoord[i+1]-zcoord[i-1]));

}

for(int i=1;i<=nxe+1;i++){
result.roundSoilMovement(i-1, 4);
result.roundSoilPressure(i-1, 4);
result.roundLimitPressure(i-1, 4);
}

for(int i =1;i<=nxe+1;i++){
int j=0;
for(j=1;j<=nodof;j++){
xy[j] = disp[nf[i][j]];
if(j%2!=0){
result.roundDisp(i-1, 6);
}

else{
result.roundRot(i-1, 6);
}

}

result.roundBendingMoment(i-1, 4);
result.roundShearForce(i-1, 4);
}

}

return result;
}

private double DSIGN(double num1,double num2){
double result=0.0;
if(num2>=0)
result =  num1;
if(num2<0)
result = -num1;
return result;
}
private void nulvec(double[] vec, int n) {
for(int i=1;i<=n;i++){
vec[i] = .0;
}
}

private void formng(int[][] nf, int inf, int nn, int nodof) {
int i=0,j=0,k=0;
for(i=1;i<=nn;i++){
for(j=1;j<=nodof;j++){
k=k+1;
nf[i][j] = k;
}
}

}

private void formg(int ip, int[] g, int[][] nf, int inf) {
int i1,i2;
i1=ip;
i2=ip+1;
g[1] = nf[i1][1];
g[2] = nf[i1][2];
g[3] = nf[i2][1];
g[4] = nf[i2][2];
}

private void kplat(double[][] kp, double ei, double ell) {
kp[1][1] = 12D*ei/(ell*ell*ell);
kp[1][2] = 6.00*ei/(ell*ell);
kp[1][3] = -kp[1][1];
kp[1][4] = kp[1][2];
kp[2][1] = kp[1][2];
kp[2][2] = 4.00*ei/ell;
kp[2][3] = -kp[2][1];
kp[2][4] = 2.00*ei/ell;
kp[3][1] = kp[1][3];
kp[3][2] = kp[2][3];
kp[3][3] = kp[1][1];
kp[3][4] = kp[3][2];
kp[4][1] = kp[1][4];
kp[4][2] = kp[2][4];
kp[4][3] = kp[3][4];
kp[4][4] = kp[2][2];

}

private void formkv(double[] bk, double[][] km, int ikm, int[] g, int n, int dof) {
int cd,value;

for(int i=1;i<=dof;i++){
if(g[i]==0) continue;
for(int j=1;j<=dof;j++){
if(g[j]==0) continue;
cd = g[j]-g[i]+1;
if(cd-1<0)continue;
value=n*(cd-1)+g[i];
bk[value] = bk[value] + km[i][j];
}
}

}

private void banred(double[] bk, int l, int kb) {
double sum = 0.0;
int il1,kbl,ij,nkb,ni,nj;
for(int i=2;i<=l;i++){
il1 = i-1;
kbl = il1+kb;
if(kb-l>0)
kbl=l;
for(int j=i;j<=kbl;j++){
ij = (j-i)*l+i;
sum = bk[ij];
nkb = j-kb+1;
if(nkb<=0)
nkb=1;
if(nkb-il1<=0){
for(int n=nkb;n<=il1;n++){
ni = (i-n)*l+n;
nj = (j-n)*l+n;
sum=sum-bk[ni]*bk[nj]/bk[n];
}

}
bk[ij] = sum;
}
}
}

private void back1(double[] bk, double[] r, int l, int kb) {
double sum=0.0;
int i1,nkb,jn;
r[1] = r[1]/bk[1];
for(int i=2;i<=l;i++){
sum = r[i];
i1 = i-1;
nkb = i-kb+1;
if(nkb<=0)
nkb=1;
for(int k=nkb;k<=i1;k++){
jn = (i-k)*l+k;
sum = sum-bk[jn]*r[k];
}
r[i] = sum/bk[i];
}

}

private void back2(double[] bk, double[] r, int l, int kb) {
double sum = 0.0;
int i,i1,nkb,jn;
for(int jj=2;jj<=l;jj++){
i = l-jj+1;
sum = 0.0;
i1 = i+1;
nkb=i-1+kb;
if(nkb-l>0)
nkb = l;
for(int k = i1;k<=nkb;k++){
jn = (k-i)*l+i;
sum = sum+bk[jn]*r[k];
}
r[i] = r[i] - sum/bk[i];
}
}

private void copyv(double[] loads, double[] oldisp, int n) {
for(int i=1;i<=n;i++){
}
}

private void mvmult(double[][] m, int im, double[] v, int k, int l, double[] y) {

double x=0.0;
for(int i=1;i<=k;i++){
x=0.00;
for(int j=1;j<=l;j++){
x=x+m[i][j]*v[j];
}
y[i] = x;
}
}

/**
* Rounds the decimal number
* @param input Number to be rounded
* @param roundFactor Decimal Places
* @return rounded value
*/
public double round(double input, int roundFactor) {
double p1 = (double) Math.pow(10, roundFactor);
input = input * p1;
double temp = Math.round(input);
return temp / p1;
}

}

Saturday, November 05, 2011 8:01 AM