void mm(double * C, double * A, double *B, int n){ int split, i, ctr = 0, nchunk, chunk; for(split= n/2+1; (split <= n ) && (n % split != 0); split++){ } chunk = n/split; for(i=0; i<split; i++) { if(ctr == 0) { cilk_spawn multMatrices(A, B, C, n, chunk, ctr); cilk_sync; ctr += chunk; } else if(i == split - 1) { nchunk = n - chunk * i; ctr = chunk * i; cilk_spawn multMatrices(A, B, C, n, nchunk, ctr); cilk_sync; } else { cilk_spawn multMatrices(A, B, C, n, chunk, ctr); cilk_sync; ctr += chunk; } } }
void multMatrices( double* A, double* B, double* C, int size, int length, int ctr) { int j, h, k ; if(length == 1) { for(j=0; j<size; j++) { k = ctr; C[ctr*size+j] = 0; for(h = 0; h < size; h++) { C[ctr*size+j] += A[k*size+h] * B[h*size+j]; } } } else { cilk_spawn multMatrices(A, B, C, size, length/2, ctr); cilk_spawn multMatrices(A, B, C, size,length/2, ctr + length/2); if(length % 2 != 0) { cilk_spawn multMatrices(A, B, C, size, 1, ctr + length - 1); } cilk_sync; } }
void myrotate(float a[16],float x,float y,float z) { float cosa,sina; if (x !=0.0) { cosa = cosf(x*TO_RADS); sina = sinf(x*TO_RADS); rotmatx[5]=cosa; rotmatx[6]=-sina; rotmatx[9]=sina; rotmatx[10]=cosa; multMatrices(a,rotmatx); } if (y != 0.0) { cosa = cosf(y*TO_RADS); sina = sinf(y*TO_RADS); rotmaty[0] = cosa; rotmaty[2] = sina; rotmaty[8] = -sina; rotmaty[10] = cosa; multMatrices(a,rotmaty); } if (z !=0.0) { cosa=cosf(z*TO_RADS); sina=sinf(z*TO_RADS); rotmatz[0] = cosa; rotmatz[1] = -sina; rotmatz[4] = sina; rotmatz[5] = cosa; multMatrices(a,rotmatz); } }
/* ** This is a screwball function. What it does is the following: ** Given screen x and y coordinates, compute the corresponding object space ** x and y coordinates given that the object space z is 0.9 + OFFSETZ. ** Since the tops of (most) pieces are at z = 0.9 + OFFSETZ, we use that ** number. */ int computeCoords(int piece, int mousex, int mousey, GLfloat * selx, GLfloat * sely) { GLfloat modelMatrix[16]; GLfloat projMatrix[16]; GLfloat finalMatrix[16]; GLfloat in[4]; GLfloat a, b, c, d; GLfloat top, bot; GLfloat z; GLfloat w; GLfloat height; if (piece == 0) return 0; height = zsize[piece] - 0.1 + OFFSETZ; glGetFloatv(GL_PROJECTION_MATRIX, projMatrix); glGetFloatv(GL_MODELVIEW_MATRIX, modelMatrix); multMatrices(modelMatrix, projMatrix, finalMatrix); if (!invertMatrix(finalMatrix, finalMatrix)) return 0; in[0] = (2.0 * (mousex - viewport[0]) / viewport[2]) - 1; in[1] = (2.0 * ((H - mousey) - viewport[1]) / viewport[3]) - 1; a = in[0] * finalMatrix[0 * 4 + 2] + in[1] * finalMatrix[1 * 4 + 2] + finalMatrix[3 * 4 + 2]; b = finalMatrix[2 * 4 + 2]; c = in[0] * finalMatrix[0 * 4 + 3] + in[1] * finalMatrix[1 * 4 + 3] + finalMatrix[3 * 4 + 3]; d = finalMatrix[2 * 4 + 3]; /* ** Ok, now we need to solve for z: ** (a + b z) / (c + d z) = height. ** ("height" is the height in object space we want to solve z for) ** ** ==> a + b z = height c + height d z ** bz - height d z = height c - a ** z = (height c - a) / (b - height d) */ top = height * c - a; bot = b - height * d; if (bot == 0.0) return 0; z = top / bot; /* ** Ok, no problem. ** Now we solve for x and y. We know that w = c + d z, so we compute it. */ w = c + d * z; /* ** Now for x and y: */ *selx = (in[0] * finalMatrix[0 * 4 + 0] + in[1] * finalMatrix[1 * 4 + 0] + z * finalMatrix[2 * 4 + 0] + finalMatrix[3 * 4 + 0]) / w - OFFSETX; *sely = (in[0] * finalMatrix[0 * 4 + 1] + in[1] * finalMatrix[1 * 4 + 1] + z * finalMatrix[2 * 4 + 1] + finalMatrix[3 * 4 + 1]) / w - OFFSETY; return 1; }
int main(void) { double h; // lattice spacing double tau; // time step printf("empece\n"); double tMax=1.0; char filename[80]; sprintf(filename,"UpwindGodunov"); double U[N][3]; initialize(U); double gamma=1.4; double t = 0.0; int step = 0; int plot = 0; int i; FILE *out; char filename_tmp[1024]; int j; double rho_avg = 0.0, u_avg = 0.0, e_avg = 0.0, P_avg = 0.0; double rho, u, e, P; h = 1.0 * L / (N - 1); tau = CFL * h / cMax(U); sprintf(filename_tmp, "%s_step_%d.dat", filename, plot); if(!(out = fopen(filename_tmp, "w"))){ fprintf(stderr, "problem opening file %s\n", filename); exit(1); } // write solution in plot files and print for (j = 0; j < N; j++) { rho = U[j][0]; u = U[j][1] / U[j][0]; e = U[j][2]; P = (U[j][2] - U[j][1] * U[j][1] / U[j][0] / 2)* (gama - 1.0); rho_avg += rho; u_avg += u; e_avg += e; P_avg += P; fprintf(out, "%d\t%f\t%f\t%f\t%f\n", j, rho, u, e, P); } fclose(out); double usol[N][3]; obtenerUsol(usol,U); plot=1; while (t < tMax+0.1) { tau = CFL * h / cMax(U); double htot[N]; for(i=0;i<N;i++){ htot[i]=(gamma/(gamma-1))*(usol[i][2]/usol[i][0])+0.5*pow(usol[i][1],2); } double Phi[N-1][3]; for(j=0;j<N-1;j++){ double r=sqrt(usol[j+1][0]/usol[j][0]); double rm=r*usol[j][0]; double um=(r*usol[j+1][1]+usol[j][1])/(r+1); double hm=(r*htot[j+1]+htot[j])/(r+1); double am=sqrt((gamma-1)*(hm-0.5*um*um)); double alfa1=(gamma-1)*um*um/(2*am*am); double alfa2=(gamma-1)/(am*am); double Udif[3]; for(i=0;i<3;i++){ Udif[i]=U[j+1][i]-U[j][i]; } double Pinv[3][3]; Pinv[0][0]=0.5*(alfa1+um/am); Pinv[0][1]=-0.5*(alfa2*um+1/am); Pinv[0][2]=alfa2/2; Pinv[1][0]=1-alfa1; Pinv[1][1]=alfa2*um; Pinv[1][2]=-alfa2; Pinv[2][0]=0.5*(alfa1-um/am); Pinv[2][1]=-0.5*(alfa2*um-1/am); Pinv[2][2]=alfa2/2; double P[3][3]; P[0][0]=1.0; P[0][1]=1.0; P[0][2]=1.0; P[1][0]=um-am; P[1][1]=um; P[1][2]=um+am; P[2][0]=hm-am*um; P[2][1]=0.5*um*am; P[2][2]=hm+am*um; double L[3][3]; L[0][0]=fabs(um-am); L[0][1]=0; L[0][2]=0; L[1][0]=0; L[1][1]=fabs(um); L[1][2]=0; L[2][0]=0; L[2][1]=0; L[2][2]=fabs(um+am); double R[3][3]; multMatrices(P,L,R); double R1[3][3]; multMatrices(R,Pinv,R1); double Phi1[3]; matrizVector(R1,Udif,Phi1); for(i=0;i<3;i++){ Phi[j][i]=Phi1[i]; } } double F[N][3]; for(j=0;j<N;j++){ F[j][0]=U[j][1]; double uloc1=U[j][1]/U[j][0]; double rhou2=U[j][1]*uloc1; double press1=(gamma-1)*(U[j][2]-0.5*rhou2); F[j][1]=rhou2+press1; F[j][2]=(U[j][2]+press1)*uloc1; } for(j=0;j<N-1;j++){ for(i=0;i<3;i++){ Phi[j][i]*=-0.5; Phi[j][i]+=0.5*(F[j][i]+F[j+1][i]); } } for(j=1;j<N-1;j++){ for(i=0;i<3;i++){ U[j][i]=U[j][i]-tau/h*(Phi[j][i]-Phi[j-1][i]); //printf("%f ",w[i][j]); } } if(t>=plot*0.2){ sprintf(filename_tmp, "%s_step_%d.dat", filename, plot); out = fopen(filename_tmp, "w"); plot++; for(i=0;i<N;i++){ usol[i][0]=U[i][0]; //printf("%f ",usol[0][i]); usol[i][1]=U[i][1]/U[i][0]; //printf("%f ",usol[1][i]); usol[i][2]=(gamma-1)*(U[i][2]-0.5*U[i][1]*usol[i][1]); //printf("%f \n",usol[2][i]); fprintf(out, "%d\t%f\t%f\t%f\t%f\n", i, usol[i][0], usol[i][1], U[i][2], usol[i][2]); } } t += tau; obtenerUsol(usol,U); } }