void linterpmatrix(Matrix R,Matrix A,Matrix B,float t) /* * Linearly interpolate matrices. The rotation submatrix is interpolated * by finding the axis of rotation between the two matrix and interpolating * the angle of rotation. The translation part is just interpolated. * * t=0 gives A; t=1 gives B. * * R = (1-t) * A + t * B */ { Matrix Ainv,T; float angle,axis[3]; Quaternion q; inverthomomatrix(Ainv,A); matmult(T,B,Ainv); matrixtoq(q,T); qtoaxis(&angle,axis,q); angle *= t; axistoq(q,angle,axis); qtomatrix(T,q); matmult(R,T,A); vecinterp(R[3],A[3],B[3],t); }
static void recompute_mask(tdflippo_instance_t* inst) { float xpos=(float)inst->width*inst->center[0]; float ypos=(float)inst->height*inst->center[1]; float **mat=mat_translate(xpos,ypos,0.0); if(inst->flip[0]!=0.5) mat=matmult(mat,mat_rotate(AXIS_X,(inst->flip[0]-0.5)*TWO_PI)); if(inst->flip[1]!=0.5) mat=matmult(mat,mat_rotate(AXIS_Y,(inst->flip[1]-0.5)*TWO_PI)); if(inst->flip[2]!=0.5) mat=matmult(mat,mat_rotate(AXIS_Z,(inst->flip[2]-0.5)*TWO_PI)); mat=matmult(mat,mat_translate(-xpos,-ypos,0.0)); #if 0 fprintf(stderr,"Resarra %.2f %.2f %.2f %.2f | %.2f %.2f %.2f %.2f | %.2f %.2f %.2f %.2f | %.2f %.2f %.2f %.2f\n", mat[0][0],mat[0][1],mat[0][2],mat[0][3], mat[1][0],mat[1][1],mat[1][2],mat[1][3], mat[2][0],mat[2][1],mat[2][2],mat[2][3], mat[3][0],mat[3][1],mat[3][2],mat[3][3]); #endif int x,y,nx,ny,pos; float xf,yf,zf; if(!inst->dontblank) memset(inst->mask,0xff,sizeof(int)*inst->fsize); for(y=0,pos=0;y<inst->height;y++) for(x=0;x<inst->width;x++,pos++) { xf=x; yf=y; zf=0.0; vetmat(mat,&xf,&yf,&zf); nx=(int)(xf+0.5); ny=(int)(yf+0.5); if(nx>=0 && nx<inst->width && ny>=0 && ny<inst->height) { if(!inst->invertrot) inst->mask[ny*inst->width+nx]=pos; else inst->mask[pos]=ny*inst->width+nx; } } matfree(mat); }
int main (void) { int i, j, res = 0; for (i = 0; i < N; i++) for (j = 0; j < N; j++) { B[i][j] = j; C[i][j] = i; } matmult (); for (i = 0; i < N; i++) res += A[i][i]; #if DEBUG fprintf (stderr, "res = %d \n", res); #endif if (res != 529340000) abort (); return 0; }
void addRotation(float rad)//adds given rotation angle to currunt {float tarr[3][3]={0}; tarr[0][0]=cos(rad);tarr[0][1]=sin(rad); tarr[1][0]=-tarr[0][1];tarr[1][1]=tarr[0][0]; tarr[2][2]=1; matmult(matrix,tarr); }
static void apply(obj *p, double *M, double d) /* apply matrix M (determinant d) to object p */ { float bnd[4]; if (p->o_type == PLACE) { matmult (cur_xform, M); if (d != 1) checkscale(sqrt(d)); } else if (p->o_type <= TEXT) { matmult (p->o_xform, M); get_bounds(p, bnd, 0); track_bounds(bnd[0], bnd[1], bnd[2], bnd[3]); redo_gbox = 1; } }
void drawBillboard(Texture *tex, float *mtx, float sizeX, float sizeY) { float tmpMtx[16]; bbobj.mtl->texture[0]=tex; // ### Changing material properties bbUniforms.scaleX = sizeX; bbUniforms.scaleY = sizeY; matmult(tmpMtx, invcammat, mtx); bbUniforms.worldView.x = tmpMtx[mat_xpos]; bbUniforms.worldView.y = tmpMtx[mat_ypos]; bbUniforms.worldView.z = tmpMtx[mat_zpos]; drawObj3D(&bbobj, mtx, &bbUniforms); }
// P0 = A * (F - H) // P1 = (A + B) * H // P2 = (C + D) * E // P3 = D * (G - E) // P4 = (A + D) * (E + H) // P5 = (B - D) * (G + H) // P6 = (A - C) * (E + F) // _ _ // Z = | (P3 + P4) + (P5 - P1) P0 + P1 | // | P2 + P3 (P0 + P4) - (P2 + P6)| // - - // void matmult_fast(int n, int Xpitch, const double X[], int Ypitch, const double Y[], int Zpitch, double Z[], int min_mat_recurse) { /* TODO: min_mat_recurse'e kucuk esik bir matris geldiyse klasik algoritmayi * kullan */ if (n <= min_mat_recurse) { matmult(n, Xpitch, X, Ypitch, Y, Zpitch, Z); return; } /* Bu cagridaki alt-matrislerin boylari n/2 olacak */ const int new_n = n / 2; const int sz = new_n * new_n * sizeof(double); double *P[7]; /* TODO: 7 adet Px hesabi icin heap'ten yer ayirin */ /* TODO: Toplama ve cikarmalar icin gecici T ve U matrisleri icin yer ayirin */ /* TODO: A-B-C-D matrislerinin baslangic adreslerini ayarlayin * (Hepsi X matrisinin icerisinde gomulu) */ /* TODO: E-F-G-H matrislerinin baslangic adreslerini ayarlayin * (Hepsi Y matrisinin icerisinde gomulu) */ /* TODO: P0 = A*(F - H) */ /* TODO: P1 = (A + B)*H */ /* TODO: P2 = (C + D)*E */ /* TODO: P3 = D*(G - E) */ /* TODO: P4 = (A + D)*(E + H) */ /* TODO: P5 = (B - D)*(G + H) */ /* TODO: P6 = (A - C)*(E + F) */ /* Sonucun hesaplanmasi */ /* TODO: Z sol ust = (P3 + P4) + (P5 - P1) */ /* TODO: Z sol alt = (P2 + P3) */ /* TODO: Z sag ust = (P0 + P1) */ /* TODO: Z sag alt = (P0 + P4) - (P2 + P6) */ /* TODO: Gecici pointerlar U ve T'yi free() edin */ /* TODO: P[] dizisindeki heap alanlarini free() edin */ }
void Bfield::Update() { for (int i = 0; i < 3; i++) B[i][0] = Bgui[i]->Value() + B0gui[i]->Value(); I = matmult(B2I, B); for (int i = 0; i < 3; i++) Igui[i]->SetValue(I[i][0]); for_each(Igui.begin(), Igui.end(), mem_fun(&AnalogOutParameter::UpdateOutput)); }
int main(int argc, char **argv) { int counter = 0; int dim, nth, nbi, nbj, iter; if (3 != argc) usage(argv); nbi = strtol(argv[1], NULL, 0); nbj = strtol(argv[2], NULL, 0); if (nbi <= 0 || nbj <= 0) usage(argv); int niter = 10; for (dim = MINDIM; dim <= MAXDIM; dim *= 2) { nth = nbi * nbj; /* Once to warm up. */ genmatrix(counter++); matmult(nbi, nbj, dim); printf("matrix size: %dx%d = %d (%d bytes) ", dim, dim, dim*dim, dim*dim*(int)sizeof(elt)); printf("blksize %dx%d thr %d itr %d:\n", dim/nbi, dim/nbj, nth, niter); for (iter = 0; iter < niter; iter++) { genmatrix(counter++); uint64_t ts = bench_time(); matmult(nbi, nbj, dim); ts = bench_time() - ts; printf("%lld.%09lld\n", (long long)ts / 1000000000, (long long)ts % 1000000000); } } return 0; }
void ForceConst::Svd(){ int n=Rms.dim1(); int StartLoop=static_cast<int> (Percent*n); Array2D<double> U, V, S; Array1D<double> s; /* for(int i=0;i<n;i++) for(int j=0;j<n;j++) { Rms[i][j]=Rms[i][j]*100.0; if(Dist[i][j] > 0.6) { cout << i << " " << j << " " << Dist[i][j] << endl; Rms[i][j]=0.0; } } */ SVD<double> G(Rms); G.getU(U); G.getV(V); G.getS(S); G.getSingularValues(s); Array2D<double> Sm1(n,n), Ks(n,n), Rms_b(n,n), V1(n,n), U1(n,n), Id(n,n); for(int i=StartLoop;i<n;i++) S[i][i]=0.0; V1=transpose(V); U1=transpose(U); Rms_b=matmult(U,matmult(S,V1)); Sm1=S; for(int i=0;i<n;i++) Sm1[i][i]=(Sm1[i][i] == 0.0)?0.0:1.0/Sm1[i][i]; Ks=matmult(V,matmult(Sm1,U1)); Id=matmult(transpose(Ks),Rms_b); for(int i=0;i<n;i++) printf(" %5d %12.5e %12.5e \n",i,Rms[i][i],Rms_b[i][i]); // for(int i=0;i<n;i++) // for(int j=i;j<n;j++) // printf(" %5d %5d %12.4f %12.5e %12.5e \n",i,j,Dist[i][j],Ks[i][j], Rms[i][j]); }
int main(int argc, char **argv) { int i; for (i = 0; i < MAXDIM*MAXDIM; i++) a[i] = b[i] = i; int dim, nth, nbi, nbj, iter; for (dim = MINDIM; dim <= MAXDIM; dim *= 2) { printf("matrix size: %dx%d = %d (%d bytes)\n", dim, dim, dim*dim, dim*dim*(int)sizeof(elt)); for (nth = nbi = nbj = 1; nth <= MAXTHREADS; ) { assert(nth == nbi * nbj); int niter = MAXDIM/dim; niter = niter * niter; // * niter; // MM = O(n^3) matmult(nbi, nbj, dim); // once to warm up... uint64_t ts = bench_time(); for (iter = 0; iter < niter; iter++) matmult(nbi, nbj, dim); uint64_t td = (bench_time() - ts) / niter; printf("blksize %dx%d thr %d itr %d: %lld.%09lld\n", dim/nbi, dim/nbj, nth, niter, (long long)td / 1000000000, (long long)td % 1000000000); if (nbi == nbj) nbi *= 2; else nbj *= 2; nth *= 2; } } return 0; }
TNT::Array2D<double> LeastSquares::singularValueDecomposition(TNT::Array2D<double>& A, int m, int n) { JAMA::SVD<double> SVD(A); TNT::Array2D<double> U = TNT::Array2D<double>(m, n); TNT::Array2D<double> D = TNT::Array2D<double>(n, n); TNT::Array2D<double> V = TNT::Array2D<double>(n, n); SVD.getU(U); SVD.getV(V); SVD.getS(D); // Pseudoinverse von D berechnen for (int i = 0; i < n; i++) { if (D[i][i] != 0.0) D[i][i] = 1.0 / D[i][i]; } TNT::Array2D<double> UT = TNT::Array2D<double>(n, m); // U transponieren for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { UT[j][i] = U[i][j]; } } // Pseudoinverse von P berechnen TNT::Array2D<double> P_plus = matmult(D, UT); P_plus = matmult(V, P_plus); return P_plus; }
main () { int i, j, scale, gcd, C[N][N], S[N][N], Madj[N][N], Tadj[N][N], Mdet, Tdet; Tdet = adjoint (T, Tadj); /* inverse without division by */ Mdet = adjoint (M, Madj); /* determinant of T and M */ matmult (Madj, Tadj, C); matmult (C, M, S); /* Madj*Tadj*M -> S */ scale = gcd = Mdet * Tdet; /* scale factors of both determinants */ for (i = 0; i < N; i++) /* find the greatest common */ { /* denominator of S and determinants */ for (j = 0; j < N; j++) gcd = Gcd (gcd, S[i][j]); } scale /= gcd; /* divide everything by gcd to get */ for (i = 0; i < N; i++) /* matrix and scale factor in lowest */ { /* integer terms possible */ for (j = 0; j < N; j++) S[i][j] /= gcd; } printf ("scale factor = 1/%d ", scale); print_mat ("M=", M, N); /* display the results */ print_mat ("T=", T, N); print_mat ("S=", S, N); /* subdivision matrix */ exit (0); }
int main() { T2ii matrices[] = { //{1,2},{2,20},{20,2},{2,4},{4,2},{2,1},{1,7},{7,3} {20,165},{165,91},{91,231},{231,121},{121,186},{186,253},{253,234},{234,130},{130,101},{101,154},{154,207},{207,44},{44,84},{84,169},{169,212},{212,191},{191,56},{56,46},{46,215},{215,12},{12,76},{76,119},{119,178},{178,184},{184,141},{141,66},{66,242},{242,233},{233,110},{110,196},{196,223},{223,208},{208,205},{205,72},{72,215},{215,38},{38,186},{186,23},{23,210},{210,180},{180,83},{83,116},{116,145},{145,212},{212,183},{183,127},{127,44},{44,166},{166,43},{43,178},{178,231},{231,154},{154,113},{113,17},{17,51},{51,193},{193,108},{108,18},{18,116},{116,187},{187,103},{103,159},{159,195},{195,117},{117,18},{18,138},{138,4},{4,97},{97,227},{227,26},{26,197},{197,173},{173,143},{143,157},{157,91},{91,40},{40,169},{169,20},{20,236},{236,250},{250,159},{159,248},{248,200},{200,248},{248,223},{223,151},{151,17},{17,34},{34,39},{39,248},{248,40},{40,160},{160,171},{171,117},{117,26},{26,36},{36,238},{238,241},{241,213},{213,150},{150,161},{161,4},{4,162},{162,48},{48,62},{62,205},{205,83},{83,196},{196,72},{72,164},{164,204},{204,248},{248,12},{12,180},{180,9},{9,239},{239,12},{12,208},{208,49},{49,113},{113,66},{66,45},{45,66},{66,177},{177,60},{60,248},{248,231},{231,134},{134,231},{231,194},{194,7},{7,211},{211,41},{41,153},{153,139},{139,106},{106,93},{93,20},{20,12},{12,249},{249,130},{130,61},{61,137},{137,57},{57,45},{45,82},{82,195},{195,202},{202,170},{170,100},{100,93},{93,43},{43,8},{8,186},{186,246},{246,177},{177,164},{164,219},{219,143},{143,176},{176,88},{88,46},{46,149},{149,164},{164,195},{195,148},{148,34},{34,152},{152,20},{20,212},{212,149},{149,229},{229,105},{105,149},{149,124},{124,223},{223,127},{127,39},{39,100},{100,29},{29,152},{152,161},{161,163},{163,138},{138,150},{150,148},{148,158},{158,85},{85,62},{62,240},{240,80},{80,210},{210,154},{154,179},{179,190},{190,217},{217,253},{253,16},{16,97},{97,73},{73,204},{204,195},{195,93},{93,68},{68,248},{248,56},{56,42},{42,237},{237,15},{15,27},{27,9},{9,230},{230,216},{216,47},{47,50},{50,31},{31,223},{223,41},{41,208},{208,48},{48,196},{196,86},{86,145},{145,166},{166,123},{123,230},{230,79},{79,84},{84,13},{13,34},{34,59},{59,35},{35,234},{234,39},{39,68},{68,188},{188,147},{147,237},{237,50},{50,61},{61,201},{201,41},{41,80},{80,97},{97,2},{2,132},{132,81},{81,206},{206,28},{28,242},{242,1},{1,108},{108,86},{86,155},{155,19},{19,46},{46,173},{173,7},{7,78},{78,238},{238,191},{191,73},{73,241},{241,149},{149,238},{238,141},{141,158},{158,160},{160,62},{62,181},{181,197},{197,143},{143,85},{85,147},{147,55},{55,139},{139,174},{174,130},{130,175},{175,15},{15,38},{38,29},{29,85},{85,78},{78,53},{53,73},{73,105},{105,155},{155,43},{43,41},{41,146},{146,179},{179,12},{12,54},{54,183},{183,113},{113,15},{15,88},{88,90},{90,48},{48,83},{83,61},{61,143},{143,123},{123,73},{73,154},{154,250},{250,139},{139,81},{81,105},{105,94},{94,118},{118,251},{251,239},{239,77},{77,106},{106,115},{115,204},{204,145},{145,71},{71,17},{17,252},{252,220},{220,49},{49,88},{88,174},{174,170},{170,194},{194,98},{98,203},{203,71},{71,128},{128,164},{164,218},{218,51},{51,35},{35,11},{11,62},{62,176},{176,37},{37,172},{172,151},{151,229},{229,52},{52,133},{133,77},{77,50},{50,225},{225,211},{211,148},{148,95},{95,192},{192,120},{120,95},{95,99},{99,228},{228,35},{35,86},{86,133},{133,105},{105,157},{157,107},{107,33},{33,98},{98,97},{97,121},{121,175},{175,121},{121,229},{229,62},{62,173},{173,135},{135,139},{139,61},{61,17},{17,234},{234,160},{160,252},{252,204},{204,184},{184,60},{60,229},{229,47},{47,60},{60,147},{147,181},{181,29},{29,156},{156,245},{245,241},{241,159},{159,197},{197,136},{136,41},{41,109},{109,178},{178,221},{221,240},{240,83},{83,130},{130,246},{246,52},{52,64},{64,137},{137,208},{208,124},{124,168},{168,187},{187,151},{151,186},{186,232},{232,6},{6,127},{127,203},{203,249},{249,119},{119,56},{56,113},{113,87},{87,180},{180,32},{32,211},{211,217},{217,219},{219,111},{111,73},{73,153},{153,240},{240,196},{196,108},{108,76},{76,89},{89,149},{149,69},{69,126},{126,123},{123,33},{33,221},{221,124},{124,46},{46,160},{160,65},{65,229},{229,83},{83,168},{168,220},{220,132},{132,224},{224,66},{66,15},{15,109},{109,177},{177,105},{105,151},{151,108},{108,84},{84,172},{172,248},{248,243},{243,216},{216,171},{171,139},{139,134},{134,243},{243,222},{222,181},{181,82},{82,110},{110,175},{175,207},{207,20},{20,82},{82,75},{75,181},{181,17},{17,23},{23,200},{200,126},{126,17},{17,2},{2,111},{111,58},{58,123},{123,86},{86,67},{67,178},{178,211},{211,125},{125,3},{3,187},{187,17},{17,59},{59,37},{37,144},{144,204},{204,109},{109,80},{80,93},{93,196},{196,249},{249,187},{187,178},{178,104},{104,148},{148,111},{111,89},{89,184},{184,26},{26,217},{217,7},{7,158},{158,4} }; struct timeval ts, te; gettimeofday(&ts, NULL); T3iii res = matmult(matrices, 512); gettimeofday(&te, NULL); double last=(te.tv_sec-ts.tv_sec)*1000.0+(te.tv_usec-ts.tv_usec)/1000.0; printf("Runnin time: %7.2fms [%7.2f,%7.2f], %d runs \n",last); printf("(%d, %d, %d)\n", res.t1, res.t2, res.t3 ); }
int main(int argc, char** argv) { /* * Matrix a element R^mn where A_jk = a_j+k*m of Matrix A * element R^mxn */ double a[DIM_M*DIM_N]; double b[DIM_N*DIM_O]; double c[DIM_M*DIM_O]; printf("Input matrix A[%d][%d]:\n", DIM_M, DIM_N); scanvector(a, DIM_M*DIM_N); printf("Input matrix B[%d][%d]:\n", DIM_N, DIM_O); scanvector(b, DIM_N*DIM_O); matmult(a, b, c, DIM_M, DIM_N, DIM_O); printf("Solved matrix C[%d][%d]:\n", DIM_M, DIM_O); printvector(c, DIM_M*DIM_O); return EXIT_SUCCESS; }
void LeastSquares::compute_leastSquaresAproxForCurve(int n_) { data->lsq_points.clear(); n = n_; if (n > m) { std::cout << "Grad größer als Punkteanzahl" << std::endl; return; } TNT::Array2D<double> A(m, n+1); for (int i = 0; i < m; i++) { for (int j = 0; j <= n; j++) { A[i][j] = std::pow(ti[i], j); } } TNT::Array2D<double> P_plus = singularValueDecomposition(A, m, n+1); c_x = TNT::Array2D<double>(m, 1); c_y = TNT::Array2D<double>(m, 1); c_z = TNT::Array2D<double>(m, 1); for (int i = 0; i < m; i++) { c_x[i][0] = x_sorted[i]; c_y[i][0] = y_sorted[i]; c_z[i][0] = z_sorted[i]; } c_x = matmult(P_plus, c_x); c_y = matmult(P_plus, c_y); c_z = matmult(P_plus, c_z); result_x = matmult(A, c_x); result_y = matmult(A, c_y); result_z = matmult(A, c_z); for (int i = 0; i < m; i++) { data->lsq_points.push_back(Vector3D(result_x[i][0], result_y[i][0], result_z[i][0])); } }
int main1(int argc, char **argv) { int nbi = 4; int nbj = 4; int nth = nbi * nbj; int dim = 1024; /* Block size = 256x256 */ int i; int niter = 10; uint64_t tt = 0; for (i = 0; i < niter; ++i) { genmatrix(i); uint64_t ts = bench_time(); matmult(nbi, nbj, dim); tt += bench_time() - ts; } tt /= niter; printf("blksize %dx%d thr %d itr %d: %lld.%09lld\n", dim/nbi, dim/nbj, nth, niter, (long long)tt / 1000000000, (long long)tt % 1000000000); return 0; }
int main(int argc, char **argv) { int nth, nbi, nbj, iter; int counter = 0; if (3 != argc) usage(argv); nbi = strtol(argv[1], NULL, 0); nbj = strtol(argv[2], NULL, 0); if (nbi <= 0 || nbj <= 0) usage(argv); nth = nbi * nbj; int niter = 10; for (n = MINDIM; n <= MAXDIM; n *= 2) { printf("matrix size: %dx%d = %d (%d bytes) ", n, n, n*n, n*n*(int)sizeof(mtype)); printf("blksize %dx%d thr %4d itr %d:\n", n/nbi, n/nbj, nth, niter); for (iter = 0; iter < niter; iter++) { genmatrix(counter++); uint64_t ts = bench_time(); plu(nbi, nbj); ts = bench_time() - ts; printf("%lld.%09lld\n", (long long)ts / 1000000000, (long long)ts % 1000000000); #if CHECK_CORRECTNESS matmult(); check(Orig, R); #endif } } return 0; }
void altRss2(double *tmppheno, double *pheno, int nphe, int n_ind, int n_gen1, int n_gen2, int *Draws1, int *Draws2, double **Addcov, int n_addcov, double **Intcov, int n_intcov, double *lrss, double *dwork_add, double *dwork_full, int multivar, double *weights, int n_col2drop, int *allcol2drop) { int i, j, k, s, nrss, lwork, rank, info; int n_col_a, n_col_f, n_gen_sq, ind_idx; double *x, *x_bk, *singular, *yfit, *work, *rss, *rss_det=0, *coef; double alpha=1.0, beta=0.0, tol=TOL, dtmp; if( (nphe==1) || (multivar==1) ) nrss = 1; else nrss = nphe; /* allocate memory */ rss = (double *)R_alloc(nrss, sizeof(double)); /* constants */ /* number of columns for Q1*Q2 */ n_gen_sq = n_gen1*n_gen2; /* number of columns of X for additive model */ n_col_a = (n_gen1+n_gen2-1) + n_addcov + n_intcov*(n_gen1+n_gen2-2); /* number of columns of X for full model */ n_col_f = n_gen_sq + n_addcov + n_intcov*(n_gen_sq-1); /************************************** * Now work on the additive model **************************************/ /* split the memory block */ lwork = 3*n_col_a + MAX(n_ind, nphe); singular = dwork_add; work = singular + n_col_a; x = work + lwork; x_bk = x + n_ind*n_col_a; yfit = x_bk + n_ind*n_col_a; coef = yfit + n_ind*nphe; if(multivar == 1) rss_det = yfit + n_col_a*nphe; /* zero out X matrix */ for(i=0; i<n_ind*n_col_a; i++) x[i] = 0.0; rank = n_col_a; /* fill up X matrix */ for(i=0; i<n_ind; i++) { x[i+(Draws1[i]-1)*n_ind] = weights[i]; /* QTL 1 */ s = n_gen1; if(Draws2[i] < n_gen2) /* QTL 2 */ x[i+(Draws2[i]-1+s)*n_ind] = weights[i]; s += (n_gen2-1); for(k=0; k<n_addcov; k++) /* add cov */ x[i+(k+s)*n_ind] = Addcov[k][i]; s += n_addcov; for(k=0; k<n_intcov; k++) { if(Draws1[i] < n_gen1) /* QTL1 x int cov */ x[i+(Draws1[i]-1+s)*n_ind] = Intcov[k][i]; s += (n_gen1-1); if(Draws2[i] < n_gen2) /* QTL 2 x int cov*/ x[i+(Draws2[i]-1+s)*n_ind] = Intcov[k][i]; s += (n_gen2-1); } } /* end loop over individuals */ /* drop cols */ if(n_col2drop) dropcol_x(&n_col_a, n_ind, allcol2drop, x); rank = n_col_a; /* make a copy of x matrix, we may need it */ memcpy(x_bk, x, n_ind*n_col_a*sizeof(double)); /* copy pheno to tmppheno, because dgelss will destroy the input pheno array */ memcpy(tmppheno, pheno, n_ind*nphe*sizeof(double)); /* Call LAPACK engine DGELSS to do linear regression. Note that DGELSS doesn't have the assumption that X is full rank. */ /* Pass all arguments to Fortran by reference */ mydgelss (&n_ind, &n_col_a, &nphe, x, x_bk, pheno, tmppheno, singular, &tol, &rank, work, &lwork, &info); /* calculate residual sum of squares */ if(nphe == 1) { /*only one phenotype */ /* if the design matrix is full rank */ if(rank == n_col_a) for (i=rank, rss[0]=0.0; i<n_ind; i++) rss[0] += tmppheno[i]*tmppheno[i]; else { /* the desigm matrix is not full rank, this is trouble */ /* calculate the fitted value */ matmult(yfit, x_bk, n_ind, n_col_a, tmppheno, 1); /* calculate rss */ for (i=0, rss[0]=0.0; i<n_ind; i++) rss[0] += (pheno[i]-yfit[i]) * (pheno[i]-yfit[i]); } } else { /* multiple phenotypes */ if(multivar == 1) { /* note that the result tmppheno has dimension n_ind x nphe, the first ncolx rows contains the estimates. */ for (i=0; i<nphe; i++) memcpy(coef+i*n_col_a, tmppheno+i*n_ind, n_col_a*sizeof(double)); /* calculate yfit */ matmult(yfit, x_bk, n_ind, n_col_a, coef, nphe); /* calculate residual, put the result in tmppheno */ for (i=0; i<n_ind*nphe; i++) tmppheno[i] = pheno[i] - yfit[i]; /* calcualte rss_det = tmppheno'*tmppheno. */ /* clear rss_det */ for (i=0; i<nphe*nphe; i++) rss_det[i] = 0.0; /* Call BLAS routine dgemm. Note that the result rss_det is a symemetric positive definite matrix */ /* the dimension of tmppheno is n_ind x nphe */ mydgemm(&nphe, &n_ind, &alpha, tmppheno, &beta, rss_det); /* calculate the determinant of rss */ /* do Cholesky factorization on rss_det */ mydpotrf(&nphe, rss_det, &info); for(i=0, rss[0]=1.0;i<nphe; i++) rss[0] *= rss_det[i*nphe+i]*rss_det[i*nphe+i]; } else { /* return rss as a vector */ if(rank == n_col_a) { /* design matrix is of full rank, this is easier */ for(i=0; i<nrss; i++) { ind_idx = i*n_ind; for(j=rank, rss[i]=0.0; j<n_ind; j++) { dtmp = tmppheno[ind_idx+j]; rss[i] += dtmp * dtmp; } } } else { /* design matrix is singular, this is troubler */ /* note that the result tmppheno has dimension n_ind x nphe, the first ncolx rows contains the estimates. */ for (i=0; i<nphe; i++) memcpy(coef+i*n_col_a, tmppheno+i*n_ind, n_col_a*sizeof(double)); /* calculate yfit */ matmult(yfit, x_bk, n_ind, n_col_a, coef, nphe); /* calculate residual, put the result in tmppheno */ for (i=0; i<n_ind*nphe; i++) tmppheno[i] = pheno[i] - yfit[i]; for(i=0; i<nrss; i++) { ind_idx = i*n_ind; for(j=0, rss[i]=0.0; j<n_ind; j++) { dtmp = tmppheno[ind_idx+j]; rss[i] += dtmp * dtmp; } } } } } /* take log10 */ for(i=0; i<nrss; i++) lrss[i] = log10(rss[i]); /************************************** * Finish additive model **************************************/ /******************* * INTERACTIVE MODEL *******************/ /* split the memory block */ lwork = 3*n_col_f + MAX(n_ind, nphe); singular = dwork_full; work = singular + n_col_f; x = work + lwork; x_bk = x + n_ind*n_col_f; yfit = x_bk + n_ind*n_col_f; coef = yfit + n_ind*nphe; if(multivar == 1) rss_det = coef + n_col_f*nphe; /* zero out X matrix */ for(i=0; i<n_ind*n_col_f; i++) x[i] = 0.0; rank = n_col_f; /* fill up X matrix */ for(i=0; i<n_ind; i++) { x[i+(Draws1[i]-1)*n_ind] = weights[i]; /* QTL 1 */ s = n_gen1; if(Draws2[i] < n_gen2) /* QTL 2 */ x[i+(Draws2[i]-1+s)*n_ind] = weights[i]; s += (n_gen2-1); for(k=0; k<n_addcov; k++) /* add cov */ x[i+(k+s)*n_ind] = Addcov[k][i]; s += n_addcov; for(k=0; k<n_intcov; k++) { if(Draws1[i] < n_gen1) /* QTL1 x int cov */ x[i+(Draws1[i]-1+s)*n_ind] = Intcov[k][i]; s += (n_gen1-1); if(Draws2[i] < n_gen2) /* QTL 2 x int cov */ x[i+(Draws2[i]-1+s)*n_ind] = Intcov[k][i]; s += (n_gen2-1); } if(Draws1[i] < n_gen1 && Draws2[i] < n_gen2) /* QTL x QTL */ x[i+((Draws1[i]-1)*(n_gen2-1)+Draws2[i]-1+s)*n_ind] = weights[i]; s += ((n_gen1-1)*(n_gen2-1)); for(k=0; k<n_intcov; k++) { /* QTL x QTL x int cov */ if(Draws1[i] < n_gen1 && Draws2[i] < n_gen2) x[i+((Draws1[i]-1)*(n_gen2-1)+Draws2[i]-1+s)*n_ind] = Intcov[k][i]; s += ((n_gen1-1)*(n_gen2-1)); } } /* end loop over individuals */ /* drop x's */ if(n_col2drop) dropcol_x(&n_col_f, n_ind, allcol2drop, x); rank = n_col_f; /* make a copy of x matrix, we may need it */ memcpy(x_bk, x, n_ind*n_col_f*sizeof(double)); /* copy pheno to tmppheno, because dgelss will destroy the input pheno array */ memcpy(tmppheno, pheno, n_ind*nphe*sizeof(double)); /* Call LAPACK engine DGELSS to do linear regression. Note that DGELSS doesn't have the assumption that X is full rank. */ /* Pass all arguments to Fortran by reference */ mydgelss (&n_ind, &n_col_f, &nphe, x, x_bk, pheno, tmppheno, singular, &tol, &rank, work, &lwork, &info); /* calculate residual sum of squares */ if(nphe == 1) { /* one phenotype */ /* if the design matrix is full rank */ if(rank == n_col_f) for (i=rank, rss[0]=0.0; i<n_ind; i++) rss[0] += tmppheno[i]*tmppheno[i]; else { /* the desigm matrix is not full rank, this is trouble */ /* calculate the fitted value */ matmult(yfit, x_bk, n_ind, n_col_f, tmppheno, 1); /* calculate rss */ for (i=0, rss[0]=0.0; i<n_ind; i++) rss[0] += (pheno[i]-yfit[i]) * (pheno[i]-yfit[i]); } } else { /* mutlple phenotypes */ if(multivar == 1) { /* multivariate model, rss=det(rss) */ /* note that the result tmppheno has dimension n_ind x nphe, the first ncolx rows contains the estimates. */ for (i=0; i<nphe; i++) memcpy(coef+i*n_col_f, tmppheno+i*n_ind, n_col_f*sizeof(double)); /* calculate yfit */ matmult(yfit, x_bk, n_ind, n_col_f, coef, nphe); /* calculate residual, put the result in tmppheno */ for (i=0; i<n_ind*nphe; i++) tmppheno[i] = pheno[i] - yfit[i]; /* calcualte rss_det = tmppheno'*tmppheno. */ /* clear rss_det */ for (i=0; i<nphe*nphe; i++) rss_det[i] = 0.0; /* Call BLAS routine dgemm. Note that the result rss_det is a symemetric positive definite matrix */ /* the dimension of tmppheno is n_ind x nphe */ mydgemm(&nphe, &n_ind, &alpha, tmppheno, &beta, rss_det); /* calculate the determinant of rss */ /* do Cholesky factorization on rss_det */ mydpotrf(&nphe, rss_det, &info); for(i=0, rss[0]=1.0;i<nphe; i++) rss[0] *= rss_det[i*nphe+i]*rss_det[i*nphe+i]; } else { /* return rss as a vector */ if(rank == n_col_f) { /*design matrix is of full rank, this is easier */ for(i=0; i<nrss; i++) { ind_idx = i * n_ind; for(j=rank, rss[i]=0.0; j<n_ind; j++) { dtmp = tmppheno[ind_idx+j]; rss[i] += dtmp * dtmp; } } } else { /* sigular design matrix */ /* note that the result tmppheno has dimension n_ind x nphe, the first ncolx rows contains the estimates. */ for (i=0; i<nphe; i++) memcpy(coef+i*n_col_f, tmppheno+i*n_ind, n_col_f*sizeof(double)); /* calculate yfit */ matmult(yfit, x_bk, n_ind, n_col_f, coef, nphe); /* calculate residual, put the result in tmppheno */ for (i=0; i<n_ind*nphe; i++) tmppheno[i] = pheno[i] - yfit[i]; for(i=0; i<nrss; i++) { ind_idx = i * n_ind; for(j=0, rss[i]=0.0; j<n_ind; j++) { dtmp = tmppheno[ind_idx+j]; rss[i] += dtmp * dtmp; } } } } } /* take log10 */ for(i=0; i<nrss; i++) lrss[i+nrss] = log10(rss[i]); }
/* Function: mush * * Purpose: calculates the path from root to leaf * * Args: p,q: * wtree: * * Returns: void */ void mush(tree wtree, int spp){ float res; int i,j,k,a; int count = 1; float **u,**ut; float **c,**d,**e; brancharray = (node ****)malloc(spp*sizeof(node ***)); arraysize = (int **)calloc(spp,sizeof(int *)); blgth = (float ***)calloc(spp,sizeof(float **)); matl1 = (float **)calloc(spp,sizeof(float *)); spparray = (int **)calloc(spp,sizeof(int *)); a = (spp*(spp-1)/2); matl2 = (float **)calloc(a+1,sizeof(float *)); u = (float **)calloc(1+1,sizeof(float*)); ut = (float **)calloc(a+1,sizeof(float*)); ut[0] = (float *)calloc(1+1,sizeof(float)); ut[1] = (float *)calloc(1+1,sizeof(float)); for (i = 0 ; i <= a ; ++i){ u[i] = (float *)calloc(a+1,sizeof(float)); u[i][1] = 1; ut[1][i] = 1; matl2[i] = (float *)calloc(a+1,sizeof(float )); } for (i = 0; i < spp; i++){ brancharray[i] = (node ***)malloc(spp*sizeof(node **)); arraysize[i] = (int *)calloc(spp,sizeof(int)); blgth[i] = (float **)calloc(spp,sizeof(float*)); matl1[i] = (float *)calloc(spp,sizeof(float )); spparray[i] = (int*)calloc(spp,sizeof(int)); } for (i = 0; i < spp; i++){ for (j=i+1; j < spp; j++){ spparray[i][j]=count; spparray[j][i]=spparray[i][j]; res = leaf2leaf(wtree.nodep[i],wtree,wtree.nodep[j],i,j); matl1[i][j]=res; printf("path %d->%d: %f\n",i,j,res); count++; } } /* a very bad solution */ for (i = 0; i < spp; i++){ for (j = i+1; j < spp; j++){ int l,o; for (o=i; o < spp; ++o){ for (l=(j > o ? j + 1 : o + 1); l < spp; l++){ float lgth=0; for (k= 0; k < arraysize[i][j]; ++k) { int n; for (n=0; n < arraysize[o][l]; ++n){ if(brancharray[i][j][k]==brancharray[o][l][n]){ lgth += blgth[i][j][k]; /*printf("%p %p",brancharray[i][j][k],brancharray[i][l][n]);*/ n=100000; } } } printf("p %d-%d q %d-%d,length %f\n",i,j,o,l,lgth); matl2[spparray[i][j]][spparray[o][l]] = pow(lgth,2)/((matl1[i][j]==0 ? 1 : matl1[i][j]) *( matl1[o][l]==0 ? 1 :matl1[o][l])); printf("p %d-%d q %d-%d,mat %f\n",i,j,o,l,matl2[spparray[i][j]][spparray[o][l]]); } } } } for (i = 0; i <= a; i++){ matl2[i][i]=1; for (j = i+1; j <= a; j++){ matl2[j][i]=matl2[i][j]; } } for (i = 0; i <= a; i++){ for (j =0; j <= a; j++){ printf("%f ",matl2[i][j]); } printf("\n"); } matl2 = matinverse(matl2,a); printf("\n"); for (i = 0; i <= a; i++){ for (j =0; j <= a; j++){ printf("%f ",matl2[i][j]); } printf("\n"); } printf("\n"); c = matmult(ut,matl2,c,1,a,a); for (i = 0; i <= 1; i++){ for (j =0; j <= a; j++){ printf("%8.3f ",c[i][j]); } printf("\n"); } printf("\n"); d = matmult(matl2,u,d,a,a,1); for (i = 0; i <= a; i++){ for (j =0; j <= 1; j++){ printf("%8.3f ",d[i][j]); } printf("\n"); } e = matmult(c,u,e,1,a,1); printf("\n"); for (i = 0; i <= 1; i++){ for (j =0; j <= 1; j++){ printf("%8.3f ",e[i][j]); } printf("\n"); } }
void DiffEq::RHS(int modenum, Grid& thegrid, TwoDVectorGridFunction<complex<double>>& uh, TwoDVectorGridFunction<complex<double>>& RHStdvgf, double t, vector<Array2D<complex<double>>>& du , bool output) { //We can loop over RHS in an external function independent of mode //because in general the right hand side of the differential equation //does not mix spherical harmonic modes. Neither does the numerical flux. int NumElem = thegrid.numberElements(); int vmaxAB = ArightBoundaries[0].getAdim()-1; int vminA = vmaxAB - ArightBoundaries[0].getDdim() +1; Array2D<double> D; D = thegrid.refelem.getD(); Array2D<double> Lift; Lift = thegrid.refelem.getLift(); //pragma omp parallel if(NumElem>lmmodes.ntotal) for #pragma omp parallel for shared(uh,modenum,thegrid,SOURCE_VECNUM,RHStdvgf,params) if(uh.TDVGFdim()<=thegrid.numberElements()) for(int elemnum = 0; elemnum < NumElem; elemnum++){ //Maximum index for both A and B matrix // int vmaxAB = ArightBoundaries[elemnum].getAdim() - 1; //Minimum index for use with trimmed A matrix. //Minimum index for B matrix is zero // int vminA = vmaxAB - ArightBoundaries[elemnum].getDdim() + 1; //The B matrix component of the RHS. Array2D<complex<double>> RHSB(uh.GFarrDim(), ArightBoundaries[elemnum].getAdim()); for(int nodenum = 0; nodenum < uh.GFarrDim(); nodenum++){ Array1D<complex<double>> RHSBpernode; //FIX THIS. THIS CAN BE SPED UP BY NOT COPYING IN GETVECTORASARRAY1D //Multiply the B matrix times a "vector" of u for each node RHSBpernode = matmult(Bmatrices.get(modenum, elemnum, nodenum), uh.getVectorAsArray1D(modenum,elemnum, nodenum, 0, vmaxAB, 0)); //Insert that result into the rows of a larger matrix insert_1D_into_2D(RHSB, RHSBpernode, nodenum, false); }//This can be sped up by skipping the insert step and reading directly //from per node //NOT CHECKED /* if((output)&&(modenum==1)){ cout << RHSB[nodenum][0] << " " << RHSB[nodenum][1] << " " << RHSB[nodenum][2] << endl; }*/ //A contribution: Array2D<complex<double>> RHSA1(uh.GFarrDim(), ArightBoundaries[elemnum].getDdim()); //The A contribution needs to be multiplied one node at a time by the //trimmed A matrix in a similar manner to the B contribution. But first, //we take the spatial derivative across all thegrid.gridNodeLocations(). //FIX THIS. THIS CAN BE SPED UP BY NOT COPYING IN GETVECTORNODEARRAY // Array2D<complex<double>> RHSA1preA = move(thegrid.jacobian(elemnum) // * move((matmult(thegrid.refelem.getD(), // uh.getVectorNodeArray2D(modenum, elemnum, vminA, vmaxAB, 0))))); Array2D<complex<double>> RHSA1preA = thegrid.jacobian(elemnum) * matmult(D, uh.getVectorNodeArray2D(modenum, elemnum, vminA, vmaxAB, 0)); //RHSA1preA is du2drho and du2dpi. /* if(modenum==1){ for(int nodenum=0; nodenum < uh.GFarrDim(); nodenum++){ cout << setprecision(15); cout << t << " " << thegrid.gridNodeLocations().get(elemnum, nodenum) << " " << RHSA1preA[nodenum][0].real() << " " << RHSA1preA[nodenum][1].real() << endl; } }*/ //Multiply each row of RHSA1preA by a different a Atrimmed matrix for(int nodenum=0; nodenum < uh.GFarrDim(); nodenum++) { int M = trimmedAmatrices.get(elemnum,nodenum).dim1(); int N = trimmedAmatrices.get(elemnum,nodenum).dim2(); int K = RHSA1preA.dim1(); int L = RHSA1preA.dim2(); // Array2D<double> tA(M,N); // tA= for (int i=0; i<M; i++){ complex<double> sum = 0; for (int k=0; k<N; k++) sum += -trimmedAmatrices.get(elemnum, nodenum)[i][k] * RHSA1preA[nodenum][k]; RHSA1[nodenum][i] = sum; } //Copied and pasted from TmatmultT in TNT2 a //with modification of variable first matrix //TmatmultT was copied and pasted from matmult in tnt itself } //Negative sign is because of definition of tA //A is definied to appear on the left hand side of the differential //equation, but this routine calculates the right hand side //Needs a multiplication by an A matrix before D //but A is position dependent. //get rid of moves?? PARALLEL PROBLEM? //This is the contribution due to du, or the numerical flux // Array2D<complex<double>> RHSA2 = move(thegrid.jacobian(elemnum) // *move(matmult(thegrid.refelem.getLift(), du[elemnum]))); Array2D<complex<double>> RHSA2 = thegrid.jacobian(elemnum) *matmult(Lift, du[elemnum]); //RHSA and RHSB will have different sizes due to the different //number of diffeq variables stored in each. sum them using a //for loop while assigning values to the RHStdvgf vector grid function // Array2D<complex<double>> RHSA = RHSA1 + RHSA2; //Sum the contributions from B, derivative, and flux, //accounting for different matrix dimensions for(int vecnum = 0; vecnum < RHStdvgf.VGFdim(); vecnum++){ for(int nodenum = 0; nodenum < RHStdvgf.GFarrDim(); nodenum++){ complex<double> tot; if(vecnum<vminA){ tot = -RHSB[nodenum][vecnum]; } else { /*if((output)&&(modenum==1)&&(vecnum==2)){ cout << setprecision(15); cout << nodenum << " " << RHSA[nodenum][vecnum-vminA].real() << endl; }*/ tot=-RHSB[nodenum][vecnum]+RHSA1[nodenum][vecnum-vminA] + RHSA2[nodenum][vecnum-vminA]; }//-sign in B because it is on the left hand side of the if((params.opts.useSource)&&(vecnum==SOURCE_VECNUM)){ tot += source.get(modenum, elemnum, nodenum); //equation in the definition supplied in DiffEq.cpp } RHStdvgf.set(modenum, vecnum, elemnum, nodenum, tot); } } } }
vector<TNT::Array2D<complex<double>>> DiffEq::characteristicflux(double t, int modenum, Grid& thegrid, TwoDVectorGridFunction<complex<double>>& uh, bool output) { //We can loop over characteristicFlux in an external function because //in general, neither the RHS of the differential equation nor du mixes //spherical harmonic modes. int NumElem = thegrid.numberElements(); vector<Array2D<complex<double>>> du; du.resize(NumElem); #pragma omp parallel for shared(du,NumElem,thegrid,output,modenum,uh) if(uh.TDVGFdim()<=thegrid.numberElements()) for(int elemnum=0; elemnum<NumElem; elemnum++){ int indL = 0; //index of leftmost node of that element int indR = uh.GFarrDim()-1; //index of rightmost node of that element double nL = -1.0; //normal to the leftmost node double nR = 1.0; //normal to the rightmost node //Dimension of the components of the differential equation with //spatial derivatives (dimension of the trimmed A matrices) //two for schwarszchild int DdimL = AleftBoundaries[elemnum].getDdim(); int DdimR = ArightBoundaries[elemnum].getDdim(); //vmin and vmax are min and max indices in vector dimension (psi, rho, pi) //one and two respectively for schwarzschild int vmaxL = AleftBoundaries[elemnum].getAdim() - 1; int vminL = vmaxL - DdimL + 1; //neglect zero rows at top of A matrix int vmaxR = ArightBoundaries[elemnum].getAdim() - 1; int vminR = vmaxR - DdimR + 1; //neglect zero rows at top of A matrix Array1D<complex<double>> uintL(DdimL); //internal u at left boundary Array1D<complex<double>> uintR(DdimR); //internal u at right boundary Array1D<complex<double>> uextL(DdimL); //external u at left boundary Array1D<complex<double>> uextR(DdimR); //external u at right boundary uintL = uh.getVectorAsArray1D(modenum,elemnum, indL, vminL, vmaxL, 0); uintR = uh.getVectorAsArray1D(modenum,elemnum, indR, vminR, vmaxR, 0); if(elemnum > 0) { uextL = uh.getVectorAsArray1D(modenum, elemnum - 1, indR, vminL, vmaxL, 0); //external u, left boundary }else{ uextL = uh.getVectorAsArray1D(modenum, NumElem - 1, indR, vminL, vmaxL, 0); //periodic boundary conditions } if(elemnum < NumElem - 1) { uextR = uh.getVectorAsArray1D(modenum, elemnum + 1, indL, vminR, vmaxR, 0); //external u, right boundary }else{ uextR = uh.getVectorAsArray1D(modenum, 0, indL, vminR, vmaxR, 0); //periodic boundary conditions } //Initialize plus and minus components of lambda matrix to zero at both //boundaries Array2D<double> lambdaminusL(DdimL, DdimL, 0.0); Array2D<double> lambdaminusR(DdimR, DdimR, 0.0); Array2D<double> lambdaplusL(DdimL, DdimL, 0.0); Array2D<double> lambdaplusR(DdimR, DdimR, 0.0); Array2D<double> lambdaL= AleftBoundaries[elemnum].getLambda(); Array2D<double> lambdaR= ArightBoundaries[elemnum].getLambda(); //lambda minus contains outward moving wave components //lambda plus contains inward moving wave components //Might be an incorrect summary. Trust the math, not the words //See pg 35 of Hesthaven and Warburten for(int j = 0; j < DdimL; j++) { if(nL * lambdaL[j][j] <= 0) { lambdaminusL[j][j] = nL * lambdaL[j][j]; } else { lambdaplusL[j][j] = nL * lambdaL[j][j]; } if(nR * lambdaR[j][j] <= 0) { lambdaminusR[j][j] = nR * lambdaR[j][j]; } else { lambdaplusR[j][j] = nR * lambdaR[j][j]; } } //S and S inverse matrices at both boundaries Array2D<double> sinvL = AleftBoundaries[elemnum].getSinv(); Array2D<double> sinvR = ArightBoundaries[elemnum].getSinv(); Array2D<double> SL = AleftBoundaries[elemnum].getS(); Array2D<double> SR = ArightBoundaries[elemnum].getS(); //Numerical fluxes at both boundaries //See Hesthaven and Warburten pg 35 (n*F) Array1D<complex<double>> nfluxL1 = matmult(lambdaplusL, matmult(sinvL, uintL)); Array1D<complex<double>> nfluxL2 = matmult(lambdaminusL, matmult(sinvL, uextL)); Array1D<complex<double>> nfluxL = matmult(SL, nfluxL1+nfluxL2); Array1D<complex<double>> nfluxR1 = matmult(lambdaplusR, matmult(sinvR, uintR)); Array1D<complex<double>> nfluxR2 = matmult(lambdaminusR, matmult(sinvR, uextR)); Array1D<complex<double>> nfluxR = matmult(SR, nfluxR1 + nfluxR2); Array2D<double> AtrimmedL= AleftBoundaries[elemnum].getAtrimmed(); Array2D<double> AtrimmedR= ArightBoundaries[elemnum].getAtrimmed(); Array2D<complex<double>> duelem(AtrimmedR.dim1(), 2, {0.0,0.0}); //This gets multiplied by lift matrix to calculate flux Array1D<complex<double>> temp10 = nL* matmult(AtrimmedL,uintL); Array1D<complex<double>> duL = nL * matmult(AtrimmedL, uintL) - nfluxL; Array1D<complex<double>> duR = nR * matmult(AtrimmedR, uintR) - nfluxR; //DU SHOULD BE ZERO AFTER FIRST CALL TO RHS // cout << duL[0].real() << " " <<duL[1].real() << " " << duR[0].real() << " " << duR[1].real() << endl; // Array1D<complex<double>> fL = matmult(AtrimmedL,uintL); // Array1D<complex<double>> fR = matmult(AtrimmedR,uintR); /* if((output)&&(modenum==1)){ ofstream fs3, fs5, fs6; fs3.open("du.txt",ios::app); fs3 << setprecision(16); fs3 << modenum << " " << thegrid.gridNodeLocations().get(elemnum, indL)<< " " << duL[0].real() << " " << duL[1].real() << " " << thegrid.gridNodeLocations().get(elemnum, indR)<<" " << duR[0].real() << " " << duR[1].real() << endl; fs3.close(); fs6.open("uL.txt", ios::app); fs6 << setprecision(16); fs6 << t << " " << modenum << " " << thegrid.gridNodeLocations().get(elemnum, indL) << " " << uintL[0].real() << " " << uextL[0].real() << " " << uintL[1].real() << " " << uextL[1].real()<<endl; fs6.close(); fs5.open("uR.txt", ios::app); fs5 << setprecision(16); fs5 << uintR[0].real() << " " << uextR[0].real() << " " << uintR[1].real() << " " << uextR[1].real()<<endl; fs5.close(); } */ //create a 2D array with one column corresponding to the left //boundary and one column corresponding to the right boundary //of du for the the various components of u. insert_1D_into_2D(duelem, duL, 0, false); insert_1D_into_2D(duelem, duR, 1, false); du[elemnum] = duelem; } return du; }
static int calccoef(struct Control_Points_3D *cp, double OR[], int ndims) { double **src_mat = NULL; double **src_mat_T = NULL; double **dest_mat = NULL; double **dest_mat_T = NULL; double **src_dest_mat = NULL; double *S_vec = NULL; double **R_mat = NULL; double **R_mat_T = NULL; double **mat_mn1 = NULL; double **mat_mn2 = NULL; double **mat_nm1 = NULL; double **mat_nm2 = NULL; double **mat_nn1 = NULL; double **E_mat = NULL; double **P_mat = NULL; double **Q_mat = NULL; double *D_vec = NULL; double *one_vec = NULL; double trace1 = 0.0; double trace2 = 0.0; int numactive; /* NUMBER OF ACTIVE CONTROL POINTS */ int m, n, i, j; int status; /* CALCULATE THE NUMBER OF VALID CONTROL POINTS */ for (i = numactive = 0; i < cp->count; i++) { if (cp->status[i] > 0) numactive++; } m = numactive; n = ndims; src_mat = G_alloc_matrix(m, n); dest_mat = G_alloc_matrix(m, n); for (i = numactive = 0; i < cp->count; i++) { if (cp->status[i] > 0) { src_mat[numactive][0] = cp->e1[i]; src_mat[numactive][1] = cp->n1[i]; src_mat[numactive][2] = cp->z1[i]; dest_mat[numactive][0] = cp->e2[i]; dest_mat[numactive][1] = cp->n2[i]; dest_mat[numactive][2] = cp->z2[i]; numactive++; } } D_vec = G_alloc_vector(ndims); src_mat_T = G_alloc_matrix(n, m); dest_mat_T = G_alloc_matrix(n, m); src_dest_mat = G_alloc_matrix(n, n); R_mat = G_alloc_matrix(n, n); R_mat_T = G_alloc_matrix(n, n); mat_mn1 = G_alloc_matrix(m, n); mat_mn2 = G_alloc_matrix(m, n); mat_nm1 = G_alloc_matrix(n, m); mat_nm2 = G_alloc_matrix(n, m); mat_nn1 = G_alloc_matrix(n, n); E_mat = G_alloc_matrix(m, m); P_mat = G_alloc_matrix(ndims, ndims); Q_mat = G_alloc_matrix(ndims, ndims); transpose_matrix(m, n, dest_mat, dest_mat_T); for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if (i != j) { E_mat[i][j] = -1.0 / (double)m; } else{ E_mat[i][j] = 1.0 - 1.0 / (double)m; } } } matmult(n, m, m, dest_mat_T, E_mat, mat_nm1); matmult(n, m, n, mat_nm1, src_mat, src_dest_mat); copy_matrix(n, n, src_dest_mat, P_mat); copy_matrix(n, n, src_dest_mat, mat_nn1); status = G_math_svduv(D_vec, mat_nn1, P_mat, n, Q_mat, n); if (status == 0) status = MSUCCESS; transpose_matrix(n, n, P_mat, mat_nn1); /* rotation matrix */ matmult(n, n, n, Q_mat, mat_nn1, R_mat_T); transpose_matrix(n, n, R_mat_T, R_mat); /* scale */ matmult(n, n, n, src_dest_mat, R_mat_T, mat_nn1); trace1 = trace(n, n, mat_nn1); transpose_matrix(m, n, src_mat, src_mat_T); matmult(n, m, m, src_mat_T, E_mat, mat_nm1); matmult(n, m, n, mat_nm1, src_mat, mat_nn1); trace2 = trace(n, n, mat_nn1); OR[14] = trace1 / trace2; /* shifts */ matmult(m, n, n, src_mat, R_mat_T, mat_mn1); scale_matrix(m, n, OR[14], mat_mn1, mat_mn2); subtract_matrix(m, n, dest_mat, mat_mn2, mat_mn1); scale_matrix(m, n, 1.0 / m, mat_mn1, mat_mn2); transpose_matrix(m, n, mat_mn2, mat_nm1); S_vec = G_alloc_vector(n); one_vec = G_alloc_vector(m); for (i = 0; i < m; i++){ one_vec[i] = 1.0; } matrix_multiply(n, m, mat_nm1, one_vec, S_vec); /* matrix to vector */ for (i = 0; i < ndims; i++) { for (j = 0; j < ndims; j++) { OR[i * ndims + j] = R_mat[i][j]; } } G_free_matrix(src_mat); G_free_matrix(src_mat_T); G_free_matrix(dest_mat); G_free_matrix(dest_mat_T); G_free_matrix(src_dest_mat); G_free_vector(D_vec); G_free_matrix(E_mat); G_free_matrix(P_mat); G_free_matrix(Q_mat); G_free_matrix(R_mat); G_free_matrix(R_mat_T); G_free_matrix(mat_mn1); G_free_matrix(mat_mn2); G_free_matrix(mat_nm1); G_free_matrix(mat_nm2); G_free_matrix(mat_nn1); G_free_vector(S_vec); G_free_vector(one_vec); return status; }
void create_RC_matrices(flp_t *flp, int omit_lateral) { int i, j, k = 0, n = flp->n_units; int **border; double **len, *gx, *gy, **g, *c_ver, **t, *gx_sp, *gy_sp; double r_sp1, r_sp2, r_hs; /* lateral resistances to spreader and heatsink */ /* NOTE: *_mid - the vertical R/C from CENTER nodes of spreader * and heatsink. *_per - the vertical R/C from PERIPHERAL (n,s,e,w) nodes */ double r_sp_per, r_hs_mid, r_hs_per, c_sp_per, c_hs_mid, c_hs_per; double gn_sp=0, gs_sp=0, ge_sp=0, gw_sp=0; double w_chip = get_total_width (flp); /* x-axis */ double l_chip = get_total_height (flp); /* y-axis */ border = imatrix(n, 4); len = matrix(n, n); /* len[i][j] = length of shared edge bet. i & j */ gx = vector(n); /* lumped conductances in x direction */ gy = vector(n); /* lumped conductances in y direction */ gx_sp = vector(n); /* lateral conductances in the spreader layer */ gy_sp = vector(n); g = matrix(NL*n+EXTRA, NL*n+EXTRA); /* g[i][j] = conductance bet. nodes i & j */ c_ver = vector(NL*n+EXTRA); /* vertical capacitance */ b = matrix(NL*n+EXTRA, NL*n+EXTRA); /* B, C, INVA and INVB are (NL*n+EXTRA)x(NL*n+EXTRA) matrices */ c = matrix(NL*n+EXTRA, NL*n+EXTRA); inva = matrix(NL*n+EXTRA, NL*n+EXTRA); invb = matrix(NL*n+EXTRA, NL*n+EXTRA); t = matrix (NL*n+EXTRA, NL*n+EXTRA); /* copy of B */ /* compute the silicon fitting factor - see pg 10 of the UVA CS tech report - CS-TR-2003-08 */ factor_chip = C_FACTOR * ((SPEC_HEAT_INT / SPEC_HEAT_SI) * (w_chip + 0.88 * t_interface) \ * (l_chip + 0.88 * t_interface) * t_interface / ( w_chip * l_chip * t_chip) + 1); /* fitting factor for interface - same rationale as above */ factor_int = C_FACTOR * ((SPEC_HEAT_CU / SPEC_HEAT_INT) * (w_chip + 0.88 * t_spreader) \ * (l_chip + 0.88 * t_spreader) * t_spreader / ( w_chip * l_chip * t_interface) + 1); /*printf("fitting factors : %lf, %lf\n", factor_chip, factor_int); */ /* gx's and gy's of blocks */ for (i = 0; i < n; i++) { /* at the silicon layer */ if (omit_lateral) { gx[i] = gy[i] = 0; } else { gx[i] = 1.0/getr(K_SI, flp->units[i].height, flp->units[i].width, l_chip, t_chip); gy[i] = 1.0/getr(K_SI, flp->units[i].width, flp->units[i].height, w_chip, t_chip); } /* at the spreader layer */ gx_sp[i] = 1.0/getr(K_CU, flp->units[i].height, flp->units[i].width, l_chip, t_spreader); gy_sp[i] = 1.0/getr(K_CU, flp->units[i].width, flp->units[i].height, w_chip, t_spreader); } /* shared lengths between blocks */ for (i = 0; i < n; i++) for (j = i; j < n; j++) len[i][j] = len[j][i] = get_shared_len(flp, i, j); /* lateral R's of spreader and sink */ r_sp1 = getr(K_CU, (s_spreader+3*w_chip)/4.0, (s_spreader-w_chip)/4.0, w_chip, t_spreader); r_sp2 = getr(K_CU, (3*s_spreader+w_chip)/4.0, (s_spreader-w_chip)/4.0, (s_spreader+3*w_chip)/4.0, t_spreader); r_hs = getr(K_CU, (s_sink+3*s_spreader)/4.0, (s_sink-s_spreader)/4.0, s_spreader, t_sink); /* vertical R's and C's of spreader and sink */ r_sp_per = RHO_CU * t_spreader * 4.0 / (s_spreader * s_spreader - w_chip*l_chip); c_sp_per = factor_pack * SPEC_HEAT_CU * t_spreader * (s_spreader * s_spreader - w_chip*l_chip) / 4.0; r_hs_mid = RHO_CU * t_sink / (s_spreader*s_spreader); c_hs_mid = factor_pack * SPEC_HEAT_CU * t_sink * (s_spreader * s_spreader); r_hs_per = RHO_CU * t_sink * 4.0 / (s_sink * s_sink - s_spreader*s_spreader); c_hs_per = factor_pack * SPEC_HEAT_CU * t_sink * (s_sink * s_sink - s_spreader*s_spreader) / 4.0; /* short the R's from block centers to a particular chip edge */ for (i = 0; i < n; i++) { if (eq(flp->units[i].bottomy + flp->units[i].height, l_chip)) { gn_sp += gy_sp[i]; border[i][2] = 1; /* block is on northern border */ } if (eq(flp->units[i].bottomy, 0)) { gs_sp += gy_sp[i]; border[i][3] = 1; /* block is on southern border */ } if (eq(flp->units[i].leftx + flp->units[i].width, w_chip)) { ge_sp += gx_sp[i]; border[i][1] = 1; /* block is on eastern border */ } if (eq(flp->units[i].leftx, 0)) { gw_sp += gx_sp[i]; border[i][0] = 1; /* block is on western border */ } } /* overall R and C between nodes */ for (i = 0; i < n; i++) { double area = (flp->units[i].height * flp->units[i].width); /* * amongst functional units in the various layers * resistances in the interface layer are assumed * to be infinite */ for (j = 0; j < n; j++) { double part = 0, part_sp = 0; if (is_horiz_adj(flp, i, j)) { part = gx[i] / flp->units[i].height; part_sp = gx_sp[i] / flp->units[i].height; } else if (is_vert_adj(flp, i,j)) { part = gy[i] / flp->units[i].width; part_sp = gy_sp[i] / flp->units[i].width; } g[i][j] = part * len[i][j]; g[HSP*n+i][HSP*n+j] = part_sp * len[i][j]; } /* vertical g's in the silicon layer */ g[i][IFACE*n+i]=g[IFACE*n+i][i]=2.0/(RHO_SI * t_chip / area); /* vertical g's in the interface layer */ g[IFACE*n+i][HSP*n+i]=g[HSP*n+i][IFACE*n+i]=2.0/(RHO_INT * t_interface / area); /* vertical g's in the spreader layer */ g[HSP*n+i][NL*n+SP_B]=g[NL*n+SP_B][HSP*n+i]=2.0/(RHO_CU * t_spreader / area); /* C's from functional units to ground */ c_ver[i] = factor_chip * SPEC_HEAT_SI * t_chip * area; /* C's from interface portion of the functional units to ground */ c_ver[IFACE*n+i] = factor_int * SPEC_HEAT_INT * t_interface * area; /* C's from spreader portion of the functional units to ground */ c_ver[HSP*n+i] = factor_pack * SPEC_HEAT_CU * t_spreader * area; /* lateral g's from block center (spreader layer) to peripheral (n,s,e,w) spreader nodes */ g[HSP*n+i][NL*n+SP_N]=g[NL*n+SP_N][HSP*n+i]=2.0*border[i][2]/((1.0/gy_sp[i])+r_sp1*gn_sp/gy_sp[i]); g[HSP*n+i][NL*n+SP_S]=g[NL*n+SP_S][HSP*n+i]=2.0*border[i][3]/((1.0/gy_sp[i])+r_sp1*gs_sp/gy_sp[i]); g[HSP*n+i][NL*n+SP_E]=g[NL*n+SP_E][HSP*n+i]=2.0*border[i][1]/((1.0/gx_sp[i])+r_sp1*ge_sp/gx_sp[i]); g[HSP*n+i][NL*n+SP_W]=g[NL*n+SP_W][HSP*n+i]=2.0*border[i][0]/((1.0/gx_sp[i])+r_sp1*gw_sp/gx_sp[i]); } /* max slope (max_power * max_vertical_R / vertical RC time constant) for silicon */ max_slope = MAX_PD / (factor_chip * t_chip * SPEC_HEAT_SI); /* vertical g's and C's between central nodes */ /* between spreader bottom and sink bottom */ g[NL*n+SINK_B][NL*n+SP_B]=g[NL*n+SP_B][NL*n+SINK_B]=2.0/r_hs_mid; /* from spreader bottom to ground */ c_ver[NL*n+SP_B]=c_hs_mid; /* from sink bottom to ground */ c_ver[NL*n+SINK_B] = factor_pack * c_convec; /* g's and C's from peripheral(n,s,e,w) nodes */ for (i = 1; i <= 4; i++) { /* vertical g's between peripheral spreader nodes and spreader bottom */ g[NL*n+SP_B-i][NL*n+SP_B]=g[NL*n+SP_B][NL*n+SP_B-i]=2.0/r_sp_per; /* lateral g's between peripheral spreader nodes and peripheral sink nodes */ g[NL*n+SP_B-i][NL*n+SINK_B-i]=g[NL*n+SINK_B-i][NL*n+SP_B-i]=2.0/(r_hs + r_sp2); /* vertical g's between peripheral sink nodes and sink bottom */ g[NL*n+SINK_B-i][NL*n+SINK_B]=g[NL*n+SINK_B][NL*n+SINK_B-i]=2.0/r_hs_per; /* from peripheral spreader nodes to ground */ c_ver[NL*n+SP_B-i]=c_sp_per; /* from peripheral sink nodes to ground */ c_ver[NL*n+SINK_B-i]=c_hs_per; } /* calculate matrices A, B such that A(dT) + BT = POWER */ for (i = 0; i < NL*n+EXTRA; i++) { for (j = 0; j < NL*n+EXTRA; j++) { if (i==j) { inva[i][j] = 1.0/c_ver[i]; if (i == NL*n+SINK_B) /* sink bottom */ b[i][j] += 1.0 / r_convec; for (k = 0; k < NL*n+EXTRA; k++) { if ((g[i][k]==0.0)||(g[k][i])==0.0) continue; else /* here is why the 2.0 factor comes when calculating g[][] */ b[i][j] += 1.0/((1.0/g[i][k])+(1.0/g[k][i])); } } else { inva[i][j]=0.0; if ((g[i][j]==0.0)||(g[j][i])==0.0) b[i][j]=0.0; else b[i][j]=-1.0/((1.0/g[i][j])+(1.0/g[j][i])); } } } /* we are always going to use the eqn dT + A^-1 * B T = A^-1 * POWER. so, store C = A^-1 * B */ matmult(c, inva, b, NL*n+EXTRA); /* we will also be needing INVB so store it too */ copy_matrix(t, b, NL*n+EXTRA, NL*n+EXTRA); matinv(invb, t, NL*n+EXTRA); /* dump_vector(c_ver, NL*n+EXTRA); */ /* dump_matrix(g, NL*n+EXTRA, NL*n+EXTRA); */ /* dump_matrix(c, NL*n+EXTRA, NL*n+EXTRA); */ /* cleanup */ free_matrix(t, NL*n+EXTRA); free_matrix(g, NL*n+EXTRA); free_matrix(len, n); free_imatrix(border, n); free_vector(c_ver); free_vector(gx); free_vector(gy); free_vector(gx_sp); free_vector(gy_sp); }
int main(int argc, char *argv[]) { int errs = 0; int size, rank; int minsize = 2, count; MPI_Comm comm; int *buf, *bufout; MPI_Op op; MPI_Datatype mattype; int i; MTest_Init(&argc, &argv); MPI_Op_create(matmult, 0, &op); /* A single rotation matrix (3x3, stored as 9 consequetive elements) */ MPI_Type_contiguous(9, MPI_INT, &mattype); MPI_Type_commit(&mattype); /* Sanity check: test that our routines work properly */ { int one = 1; buf = (int *) malloc(4 * 9 * sizeof(int)); initMat(0, 4, 0, &buf[0]); initMat(1, 4, 0, &buf[9]); initMat(2, 4, 0, &buf[18]); initMat(3, 4, 0, &buf[27]); matmult(&buf[0], &buf[9], &one, &mattype); matmult(&buf[9], &buf[18], &one, &mattype); matmult(&buf[18], &buf[27], &one, &mattype); checkResult(1, &buf[27], "Sanity Check"); free(buf); } while (MTestGetIntracommGeneral(&comm, minsize, 1)) { if (comm == MPI_COMM_NULL) continue; MPI_Comm_size(comm, &size); MPI_Comm_rank(comm, &rank); for (count = 1; count < size; count++) { /* Allocate the matrices */ buf = (int *) malloc(count * 9 * sizeof(int)); if (!buf) { MPI_Abort(MPI_COMM_WORLD, 1); } bufout = (int *) malloc(count * 9 * sizeof(int)); if (!bufout) { MPI_Abort(MPI_COMM_WORLD, 1); } for (i = 0; i < count; i++) { initMat(rank, size, i, &buf[i * 9]); } MPI_Allreduce(buf, bufout, count, mattype, op, comm); errs += checkResult(count, bufout, ""); /* Try the same test, but using MPI_IN_PLACE */ for (i = 0; i < count; i++) { initMat(rank, size, i, &bufout[i * 9]); } MPI_Allreduce(MPI_IN_PLACE, bufout, count, mattype, op, comm); errs += checkResult(count, bufout, "IN_PLACE"); free(buf); free(bufout); } MTestFreeComm(&comm); } MPI_Op_free(&op); MPI_Type_free(&mattype); MTest_Finalize(errs); MPI_Finalize(); return 0; }
int main(int argc, char *argv[]) { int i,j/*,k,test*/; int ndomain,total,add; int gottrans; int T_FLAG; /* char c; */ char *env; char *deffile,*keyword,*value; FILE *PARMS,*TRANS,*PDB; struct parameters *parms; struct domain_loc *domain; if(argc<2) exit_error(); /* get environment variable */ if((env=getenv("STAMPDIR"))==NULL) { fprintf(stderr,"error: environment variable STAMPDIR must be set\n"); exit(-1); } parms=(struct parameters*)malloc(sizeof(struct parameters)); strcpy(parms[0].stampdir,env); /* read in default parameters from $STAMPDIR/stamp.defaults */ deffile=(char*)malloc(1000*sizeof(char)); #if defined(_MSC_VER) sprintf(deffile,"%s\\stamp.defaults",env); #else sprintf(deffile,"%s/stamp.defaults",env); #endif if((PARMS=fopen(deffile,"r"))==NULL) { fprintf(stderr,"error: default parameter file %s does not exist\n",deffile); exit(-1); } if(getpars(PARMS,parms)==-1) exit(-1); fclose(PARMS); /* define DSSP directory file name */ sprintf(&parms[0].dsspfile[0],"%s/dssp.directories",env); /* now search the command line for commands */ keyword=(char*)malloc(1000*sizeof(char)); value=(char*)malloc(1000*sizeof(char)); for(i=1; i<argc; ++i) { if(argv[i][0]!='-') exit_error(); strcpy(keyword,&argv[i][1]); if(i+1<argc) strcpy(value,argv[i+1]); else strcpy(value,"none"); for(j=0; j<strlen(keyword); ++j) keyword[j]=ltou(keyword[j]); /* change to upper case */ T_FLAG=(value[0]=='Y' || value[0]=='y' || value[0]=='1' || value[0]=='T' || value[0]=='t' || value[0]=='o' || value[0]=='O'); /* enables one to write '1', 'YES', 'Yes', 'yes', 'T_FLAG', 'True' or 'true' to * set any boolean parmsiable to one */ if((strcmp(&argv[i][1],"l")==0) || (strcmp(&argv[i][1],"f")==0) || (strcmp(&argv[i][1],"p")==0)) { if(i+1>=argc) exit_error(); /* listfile name */ strcpy(parms[0].listfile,argv[i+1]); i++; } else if(strcmp(&argv[i][1],"P")==0) { /* want to read in parameter file */ if(i+1>=argc) exit_error(); if((PARMS=fopen(argv[i+1],"r"))==NULL) { fprintf(stderr,"error opening file %s\n",argv[i+1]); exit(-1); } if(getpars(PARMS,parms)==-1) exit(-1); fclose(PARMS); i++; } else if(strcmp(&argv[i][1],"o")==0) { /* output file */ if(i+1>=argc) exit_error(); strcpy(parms[0].logfile,argv[i+1]); i++; } else if(strcmp(&argv[i][1],"help")==0) { help_exit_error(); } else if((strcmp(&argv[i][1],"V")==0) || (strcmp(&argv[i][1],"v")==0)) { parms[0].verbose=1; strcpy(parms[0].logfile,"stdout"); } else if(strcmp(&argv[i][1],"s")==0) { parms[0].SCAN=1; parms[0].TREEWISE=parms[0].PAIRWISE=0; } else if(strcmp(&argv[i][1],"n")==0) { if(i+1>=argc) exit_error(); sscanf(argv[i+1],"%d",&parms[0].NPASS); i++; if(parms[0].NPASS!=1 && parms[0].NPASS!=2) exit_error(); } else if(strcmp(keyword,"PAIRPEN") == 0 || strcmp(keyword,"PEN")==0 || strcmp(keyword,"SECOND_PAIRPEN")==0) { sscanf(value,"%f",&parms[0].second_PAIRPEN); i++; } else if(strcmp(keyword,"FIRST_PAIRPEN")==0) { sscanf(value,"%f",&parms[0].first_PAIRPEN); i++; } else if(strcmp(keyword,"MAXPITER") == 0 || strcmp(keyword,"MAXSITER") == 0) { sscanf(value,"%d",&parms[0].MAXPITER); i++; } else if(strcmp(keyword,"MAXTITER") == 0) { sscanf(value,"%d",&parms[0].MAXTITER); i++; } else if(strcmp(keyword,"TREEPEN") == 0 || strcmp(keyword,"SECOND_TREEPEN")==0) { sscanf(value,"%f",&parms[0].second_TREEPEN); i++; } else if(strcmp(keyword,"FIRST_TREEPEN")==0) { sscanf(value,"%f",&parms[0].first_TREEPEN); i++; } else if(strcmp(keyword,"SCORETOL") == 0) { sscanf(value,"%f",&parms[0].SCORETOL); i++; } else if(strcmp(keyword,"CLUSTMETHOD") == 0) { sscanf(value,"%d",&parms[0].CLUSTMETHOD); i++; } else if(strcmp(keyword,"E1") == 0 || strcmp(keyword,"SECOND_E1")==0) { sscanf(value,"%f",&parms[0].second_E1); i++; } else if(strcmp(keyword,"E2") == 0 || strcmp(keyword,"SECOND_E2")==0) { sscanf(value,"%f",&parms[0].second_E2); i++; } else if(strcmp(keyword,"FIRST_E1")==0) { sscanf(value,"%f",&parms[0].first_E1); i++; } else if(strcmp(keyword,"FIRST_E2")==0) { sscanf(value,"%f",&parms[0].first_E2); i++; } else if(strcmp(keyword,"NPASS")==0) { sscanf(value,"%d",&parms[0].NPASS); i++; if(parms[0].NPASS!=1 && parms[0].NPASS!=2) { fprintf(stderr,"error: NPASS must be either 1 or 2\n"); return -1; } } else if(strcmp(keyword,"CUTOFF") == 0 || strcmp(keyword,"SECOND_CUTOFF")==0) { sscanf(value,"%f",&parms[0].second_CUTOFF); i++; } else if(strcmp(keyword,"FIRST_CUTOFF")==0) { sscanf(value,"%f",&parms[0].first_CUTOFF); i++; } else if(strcmp(keyword,"TREEPLOT") == 0) { parms[0].TREEPLOT=T_FLAG; i++; } else if(strcmp(keyword,"PAIRPLOT") == 0) { parms[0].PAIRPLOT=T_FLAG; i++; } else if(strcmp(keyword,"NALIGN") == 0) { sscanf(value,"%d",&parms[0].NALIGN); i++; } else if(strcmp(keyword,"DISPALL") == 0) { parms[0].DISPALL=T_FLAG; i++; } else if(strcmp(keyword,"HORIZ") ==0) { parms[0].HORIZ=T_FLAG; i++; } else if(strcmp(keyword,"ADD") ==0) { sscanf(value,"%f",&parms[0].ADD); i++; } else if(strcmp(keyword,"NMEAN") ==0) { sscanf(value,"%f",&parms[0].NMEAN); i++; } else if(strcmp(keyword,"NSD") ==0) { sscanf(value,"%f",&parms[0].NSD); i++; } else if(strcmp(keyword,"STATS") ==0) { parms[0].STATS=T_FLAG; i++; } else if(strcmp(keyword,"NA") == 0) { sscanf(value,"%f",&parms[0].NA); i++; } else if(strcmp(keyword,"NB") == 0) { sscanf(value,"%f",&parms[0].NB); i++; } else if(strcmp(keyword,"NASD") == 0) { sscanf(value,"%f",&parms[0].NASD); i++; } else if(strcmp(keyword,"NBSD") == 0) { sscanf(value,"%f",&parms[0].NBSD); i++; } else if(strcmp(keyword,"PAIRWISE") == 0) { parms[0].PAIRWISE=T_FLAG; i++; } else if(strcmp(keyword,"TREEWISE") == 0) { parms[0].TREEWISE=T_FLAG; i++; } else if(strcmp(keyword,"ORDFILE") == 0) { strcpy(parms[0].ordfile,value); i++; } else if(strcmp(keyword,"TREEFILE") == 0) { strcpy(parms[0].treefile,value); i++; } else if(strcmp(keyword,"PLOTFILE") == 0) { strcpy(parms[0].plotfile,value); i++; } else if(strcmp(keyword,"PREFIX") == 0 || strcmp(keyword,"TRANSPREFIX")==0 || strcmp(keyword,"STAMPPREFIX")==0) { strcpy(parms[0].transprefix,value); i++; } else if(strcmp(keyword,"MATFILE") == 0) { strcpy(parms[0].matfile,value); i++; } else if(strcmp(keyword,"THRESH") ==0) { sscanf(value,"%f",&parms[0].THRESH); i++; } else if(strcmp(keyword,"TREEALIGN")==0) { parms[0].TREEALIGN=T_FLAG; i++; } else if(strcmp(keyword,"TREEALLALIGN")==0) { parms[0].TREEALLALIGN=T_FLAG; i++; } else if(strcmp(keyword,"PAIRALIGN")==0 || strcmp(keyword,"SCANALIGN")==0) { parms[0].PAIRALIGN=T_FLAG; i++; } else if(strcmp(keyword,"PAIRALLALIGN")==0 || strcmp(keyword,"SCANALLALIGN")==0) { parms[0].PAIRALLALIGN=T_FLAG; i++; } else if(strcmp(keyword,"PRECISION")==0) { sscanf(value,"%d",&parms[0].PRECISION); i++; } else if(strcmp(keyword,"MAX_SEQ_LEN")==0) { sscanf(value,"%d",&parms[0].MAX_SEQ_LEN); i++; } else if(strcmp(keyword,"ROUGHFIT")==0) { parms[0].ROUGHFIT=T_FLAG; i++; } else if(strcmp(keyword,"ROUGH")==0) { parms[0].ROUGHFIT=1; } else if(strcmp(keyword,"ROUGHOUT")==0) { parms[0].roughout=1; } else if(strcmp(keyword,"ROUGHOUTFILE")==0) { if(i+1>=argc) exit_error(); strcpy(&parms[0].roughoutfile[0],argv[i+1]); i++; parms[0].roughout=1; } else if(strcmp(keyword,"BOOLCUT")==0 || strcmp(keyword,"SECOND_BOOLCUT")==0) { sscanf(value,"%f",&parms[0].second_BOOLCUT); i++; } else if(strcmp(keyword,"FIRST_BOOLCUT")==0) { sscanf(value,"%f",&parms[0].first_BOOLCUT); i++; } else if(strcmp(keyword,"SCANSLIDE")==0) { sscanf(value,"%d",&parms[0].SCANSLIDE); i++; } else if(strcmp(keyword,"SCAN")==0) { parms[0].SCAN=T_FLAG; i++; if(T_FLAG) parms[0].PAIRWISE=parms[0].TREEWISE=0; } else if(strcmp(keyword,"SCANMODE")==0) { sscanf(value,"%d",&parms[0].SCANMODE); i++; if(parms[0].SCANMODE==1) parms[0].PAIRALIGN=1; } else if(strcmp(keyword,"SCANCUT")==0) { sscanf(value,"%f",&parms[0].SCANCUT); i++; } else if(strcmp(keyword,"SECSCREEN")==0) { parms[0].SECSCREEN=T_FLAG; i++; } else if(strcmp(keyword,"SECSCREENMAX")==0) { sscanf(value,"%f",&parms[0].SECSCREENMAX); i++; } else if(strcmp(keyword,"SCANTRUNC")==0) { parms[0].SCANTRUNC=T_FLAG; i++; } else if(strcmp(keyword,"SCANTRUNCFACTOR")==0) { sscanf(value,"%f",&parms[0].SCANTRUNCFACTOR); i++; } else if(strcmp(keyword,"DATABASE")==0) { strcpy(&parms[0].database[0],value); i++; } else if(strcmp(keyword,"SCANFILE")==0) { strcpy(&parms[0].scanfile[0],value); i++; } else if(strcmp(keyword,"LOGFILE")==0) { strcpy(&parms[0].logfile[0],value); i++; } else if(strcmp(keyword,"SECTYPE")==0) { sscanf(value,"%d",&parms[0].SECTYPE); i++; } else if(strcmp(keyword,"SCANSEC")==0) { sscanf(value,"%d",&parms[0].SCANSEC); i++; } else if(strcmp(keyword,"SECFILE")==0) { strcpy(&parms[0].secfile[0],value); i++; parms[0].SECTYPE=2; } else if(strcmp(keyword,"BOOLEAN")==0) { parms[0].BOOLEAN=T_FLAG; i++; } else if(strcmp(keyword,"BOOLMETHOD")==0) { sscanf(value,"%d",&parms[0].BOOLMETHOD); i++; } else if(strcmp(keyword,"LISTFILE")==0) { strcpy(&parms[0].listfile[0],value); i++; } else if(strcmp(keyword,"STAMPDIR")==0) { strcpy(&parms[0].stampdir[0],value); i++; } else if(strcmp(keyword,"CLUST")==0) { parms[0].CLUST=T_FLAG; i++; } else if(strcmp(keyword,"COLUMNS")==0) { sscanf(value,"%d",&parms[0].COLUMNS); i++; } else if(strcmp(keyword,"SW")==0) { sscanf(value,"%d",&parms[0].SW); i++; } else if(strcmp(keyword,"CCFACTOR")==0) { sscanf(value,"%f",&parms[0].CCFACTOR); i++; } else if(strcmp(keyword,"CCADD")==0) { parms[0].CCADD=T_FLAG; i++; } else if(strcmp(keyword,"MINFIT")==0) { sscanf(value,"%d",&parms[0].MINFIT); i++; } else if(strcmp(keyword,"ROUGHALIGN")==0) { strcpy(parms[0].roughalign,value); i++; } else if(strcmp(keyword,"FIRST_THRESH")==0) { sscanf(value,"%f",&parms[0].first_THRESH); i++; } else if(strcmp(keyword,"MIN_FRAC")==0) { sscanf(value,"%f",&parms[0].MIN_FRAC); i++; } else if(strcmp(keyword,"SCORERISE")==0) { parms[0].SCORERISE=T_FLAG; i++; } else if(strcmp(keyword,"SKIPAHEAD")==0) { parms[0].SKIPAHEAD=T_FLAG; i++; } else if(strcmp(keyword,"SCANSCORE")==0) { sscanf(value,"%d",&parms[0].SCANSCORE); i++; } else if(strcmp(keyword,"PAIROUTPUT")==0) { parms[0].PAIROUTPUT=T_FLAG; i++; } else if(strcmp(keyword,"ALLPAIRS")==0) { parms[0].ALLPAIRS=T_FLAG; i++; } else if (strcmp(keyword,"ATOMTYPE")==0) { parms[0].ATOMTYPE=T_FLAG; i++; } else if(strcmp(keyword,"DSSP")==0) { parms[0].DSSP=T_FLAG; i++; } else if(strcmp(keyword,"SLOWSCAN")==0) { parms[0].SLOWSCAN=T_FLAG; i++; } else if(strcmp(keyword,"SLOW")==0) { parms[0].SLOWSCAN=1; } else if(strcmp(keyword,"CUT")==0) { parms[0].CO=1; } else if(strcmp(&argv[i][1],"slide")==0) { if(i+1>=argc) exit_error(); sscanf(argv[i+1],"%d",&parms[0].SCANSLIDE); i++; } else if(strcmp(&argv[i][1],"d")==0) { /* database file */ if(i+1>=argc) exit_error(); strcpy(&parms[0].database[0],argv[i+1]); i++; } else if(strcmp(&argv[i][1],"pen1")==0) { if(i+1>=argc) exit_error(); sscanf(argv[i+1],"%f",&parms[0].first_PAIRPEN); i++; } else if(strcmp(&argv[i][1],"pen2")==0) { if(i+1>=argc) exit_error(); sscanf(argv[i+1],"%f",&parms[0].second_PAIRPEN); i++; } else if(strcmp(&argv[i][1],"prefix")==0) { if(i+1>=argc) exit_error(); strcpy(&parms[0].transprefix[0],argv[i+1]); i++; } else if(strcmp(&argv[i][1],"scancut")==0) { if(i+1>=argc) exit_error(); sscanf(argv[i+1],"%f",&parms[0].SCANCUT); i++; } else if(strcmp(&argv[i][1],"opd")==0) { parms[0].opd=1; } else { exit_error(); } } free(keyword); free(value); /* make the names of all the output files using the prefix */ sprintf(&parms[0].ordfile[0],"%s.ord",parms[0].transprefix); sprintf(&parms[0].treefile[0],"%s.tree",parms[0].transprefix); sprintf(&parms[0].plotfile[0],"%s.plot",parms[0].transprefix); sprintf(&parms[0].matfile[0],"%s.mat",parms[0].transprefix); sprintf(&parms[0].roughalign[0],"%s_align.rough",parms[0].transprefix); sprintf(&parms[0].scanfile[0],"%s.scan",parms[0].transprefix); if(strcmp(parms[0].logfile,"stdout")==0 || strcmp(parms[0].logfile,"STDOUT")==0) { parms[0].LOG=stdout; } else if(strcmp(parms[0].logfile,"silent")==0 || strcmp(parms[0].logfile,"SILENT")==0) { #if defined(_MSC_VER) parms[0].LOG=stdout; #else parms[0].LOG=fopen("/dev/null","w"); #endif } else { if((parms[0].LOG=fopen(parms[0].logfile,"w"))==NULL) { fprintf(stderr,"error opening file %s\n",parms[0].logfile); exit(-1); } } if(strcmp(parms[0].logfile,"silent")==0) { printf("\nSTAMP Structural Alignment of Multiple Proteins\n"); printf(" by Robert B. Russell & Geoffrey J. Barton \n"); printf(" Please cite PROTEINS, v14, 309-323, 1992\n\n"); } fprintf(parms[0].LOG,"-------------------------------------------------------------------------------\n"); fprintf(parms[0].LOG," S t A M P\n"); fprintf(parms[0].LOG," Structural Alignment of\n"); fprintf(parms[0].LOG," Multiple Proteins\n"); fprintf(parms[0].LOG," By Robert B. Russell & Geoffrey J. Barton \n"); fprintf(parms[0].LOG," Last Modified: %s\n",lastmod); fprintf(parms[0].LOG," Please cite Ref: Russell and GJ Barton, PROTEINS, v14, 309-323, 1992\n"); fprintf(parms[0].LOG,"-------------------------------------------------------------------------------\n\n"); fprintf(parms[0].LOG,"STAMPDIR has been set to %s\n\n\n",parms[0].stampdir); /* read in coordinate locations and initial transformations */ if((TRANS = fopen(parms[0].listfile,"r")) == NULL) { fprintf(stderr,"error: file %s does not exist\n",parms[0].listfile); exit(-1); } /* determine the number of domains specified */ ndomain=count_domain(TRANS); domain=(struct domain_loc*)malloc(ndomain*sizeof(struct domain_loc)); rewind(TRANS); if(getdomain(TRANS,domain,&ndomain,ndomain,&gottrans,parms[0].stampdir,parms[0].DSSP,parms[0].LOG)==-1) exit(-1); fclose(TRANS); fprintf(parms[0].LOG,"Details of this run:\n"); if(parms[0].PAIRWISE) fprintf(parms[0].LOG,"PAIRWISE mode specified\n"); if(parms[0].TREEWISE) fprintf(parms[0].LOG,"TREEWISE mode specified\n"); if(parms[0].SCAN) fprintf(parms[0].LOG,"SCAN mode specified\n"); if(!parms[0].SCAN) { /* if no MINFIT has been given, then take the smallest length and divide it by two */ if(parms[0].MINFIT==-1) { parms[0].MINFIT=parms[0].MAXLEN; for(i=0; i<ndomain; ++i) if(domain[i].ncoords<parms[0].MINFIT) parms[0].MINFIT=domain[i].ncoords; parms[0].MINFIT/=2; } fprintf(parms[0].LOG," pairwise score file: %s\n",parms[0].matfile); if(parms[0].TREEWISE) { fprintf(parms[0].LOG," tree order file: %s\n",parms[0].ordfile); fprintf(parms[0].LOG," tree file: %s\n",parms[0].treefile); fprintf(parms[0].LOG," tree plot file: %s\n",parms[0].plotfile); } } else { fprintf(parms[0].LOG," SCANMODE set to %d\n",parms[0].SCANMODE); fprintf(parms[0].LOG," SCANSCORE set to %d\n",parms[0].SCANSCORE); fprintf(parms[0].LOG," (see documentation for an explanation)\n"); if(parms[0].opd==1) fprintf(parms[0].LOG," Domains will be skipped after the first match is found\n"); if(parms[0].SCANMODE==1) { fprintf(parms[0].LOG," Transformations for Sc values greater than %f are to be output\n",parms[0].SCANCUT); fprintf(parms[0].LOG," to the file %s\n",parms[0].transprefix); } else { fprintf(parms[0].LOG," Only the scores are to be output to the file %s\n",parms[0].scanfile); } fprintf(parms[0].LOG," secondary structures are "); switch(parms[0].SCANSEC) { case 0: fprintf(parms[0].LOG," not to be considered\n"); break; case 1: fprintf(parms[0].LOG," to be from DSSP\n"); break; case 2: fprintf(parms[0].LOG," to be read in from %s\n",parms[0].secfile); break; default: fprintf(parms[0].LOG," not to be considered\n"); } if(parms[0].SECSCREEN) { fprintf(parms[0].LOG," An initial screen on secondary structure content is to performed when possible\n"); fprintf(parms[0].LOG," Secondary structure summaries farther than %6.2f %% apart result in\n",parms[0].SECSCREENMAX); fprintf(parms[0].LOG," a comparison being ignored\n"); } fprintf(parms[0].LOG," Initial fits are to be performed by aligning the N-terminus of the query\n with every %d residue of the database sequence\n",parms[0].SCANSLIDE); fprintf(parms[0].LOG," of the query along the database structure.\n"); if(parms[0].SCANTRUNC) { fprintf(parms[0].LOG," If sequences in the database are > %5.3f x the query sequence length\n",parms[0].SCANTRUNCFACTOR); fprintf(parms[0].LOG," then a fraction of the the database structure, corresponding to this\n"); fprintf(parms[0].LOG," of length %5.3f x the query, will be considered\n",parms[0].SCANTRUNCFACTOR); fprintf(parms[0].LOG," comparisons are to be ignored if the database structure is less than\n %6.4f x the length of the query structure\n",parms[0].MIN_FRAC); } fprintf(parms[0].LOG," Domain database file to be scanned %s\n",parms[0].database); } if(parms[0].TREEWISE) fprintf(parms[0].LOG," output files prefix: %s\n",parms[0].transprefix); fprintf(parms[0].LOG,"\n\nParameters:\n"); fprintf(parms[0].LOG,"Rossmann and Argos parameters:\n"); if(parms[0].NPASS==2) { fprintf(parms[0].LOG," Two fits are to be performed, the first fit with:\n"); fprintf(parms[0].LOG," E1=%7.3f,",parms[0].first_E1); fprintf(parms[0].LOG," E2=%7.3f,",parms[0].first_E2); fprintf(parms[0].LOG," CUT=%7.3f,",parms[0].first_CUTOFF); fprintf(parms[0].LOG," PAIRPEN=%7.3f,",parms[0].first_PAIRPEN); fprintf(parms[0].LOG," TREEPEN=%7.3f\n",parms[0].first_TREEPEN); /* fprintf(parms[0].LOG," E1=%7.3f, E2=%7.3f, CUT=%7.3f, PAIRPEN=%7.3f, TREEPEN=%7.3f\n", parms[0].first_E1,parms[0].first_E2,parms[0].first_CUTOFF,parms[0].first_PAIRPEN,parms[0].first_TREEPEN); */ fprintf(parms[0].LOG," The second fit with:\n"); } else fprintf(parms[0].LOG," One fit is to performed with:\n"); fprintf(parms[0].LOG," E1=%7.3f, E2=%7.3f, CUT=%7.3f, PAIRPEN=%7.3f, TREEPEN=%7.3f\n", parms[0].second_E1,parms[0].second_E2,parms[0].second_CUTOFF,parms[0].second_PAIRPEN,parms[0].second_TREEPEN); if(parms[0].BOOLEAN) { fprintf(parms[0].LOG," BOOLEAN mode specified\n"); fprintf(parms[0].LOG," A boolean matrix will be calculated corresponding to whether\n"); fprintf(parms[0].LOG," positions have Pij values greater than:\n"); if(parms[0].NPASS==2) fprintf(parms[0].LOG," %7.3f, for the first fit and\n",parms[0].first_BOOLCUT); fprintf(parms[0].LOG," %7.3f",parms[0].second_BOOLCUT); if(parms[0].NPASS==2) fprintf(parms[0].LOG," for the second fit.\n"); else fprintf(parms[0].LOG,".\n"); fprintf(parms[0].LOG," In the multiple case, this criteria must be satisfied for *all*\n"); fprintf(parms[0].LOG," possible pairwise comparisons\n"); } if(parms[0].SW==1) { fprintf(parms[0].LOG," Corner cutting is to be performed\n"); fprintf(parms[0].LOG," Corner cutting length: %6.2f\n",parms[0].CCFACTOR); if(parms[0].CCADD) fprintf(parms[0].LOG," The length difference is to be added to this value\n"); } else { fprintf(parms[0].LOG," The entire SW matrix is to be calculated and used\n"); } fprintf(parms[0].LOG," The minimum length of alignment to be evaluated further is %3d residues\n",parms[0].MINFIT); fprintf(parms[0].LOG,"\n"); fprintf(parms[0].LOG," Convergence tolerance SCORETOL= %f %%\n", parms[0].SCORETOL); fprintf(parms[0].LOG," Other parameters:\n"); fprintf(parms[0].LOG," MAX_SEQ_LEN=%d, MAXPITER=%d, MAXTITER=%d\n", parms[0].MAX_SEQ_LEN,parms[0].MAXPITER,parms[0].MAXTITER); fprintf(parms[0].LOG," PAIRPLOT (SCANPLOT) = %d, TREEPLOT = %d, PAIRALIGN (SCANALIGN) = %d, TREEALIGN = %d\n", parms[0].PAIRPLOT,parms[0].TREEPLOT,parms[0].PAIRALIGN,parms[0].TREEALIGN); fprintf(parms[0].LOG," PAIRALLALIGN (SCANALLALIGN) = %d, TREEALLALIGN = %d\n",parms[0].PAIRALLALIGN,parms[0].TREEALLALIGN); if(!parms[0].BOOLEAN) { fprintf(parms[0].LOG,"\n\nDetails of Confidence value calculations:\n"); if(parms[0].STATS) fprintf(parms[0].LOG," actual mean and standard deviations are to be\n used for determination of Pij' values.\n"); else { fprintf(parms[0].LOG," pre-set mean and standard deviations are to be used\n and multiple comparisons are to be corrected.\n"); fprintf(parms[0].LOG," mean Xt = %f, standard deviation SDt = %f\n", parms[0].NMEAN,parms[0].NSD); fprintf(parms[0].LOG," for the multiple case:\n"); fprintf(parms[0].LOG," pairwise means are to be calculated from:\n Xp = exp(%6.4f * log(length) + %6.4f)\n",parms[0].NA,parms[0].NB); fprintf(parms[0].LOG," and pairwise standard deviations from:\n SDp = exp(%6.4f * log(length) + %6.4f)\n",parms[0].NASD,parms[0].NBSD); fprintf(parms[0].LOG," the mean to be used is calculated from: \n Xc = (Xm/Xp) * Xt).\n"); fprintf(parms[0].LOG," and the standard deviation from: \n SDc = (SDm/SDp)*SDt).\n"); } /* End of if(parms[0].STATS) */ } else { fprintf(parms[0].LOG," Positional values will consist of one's or zeros depending on whether\n"); fprintf(parms[0].LOG," a position satisfies the BOOLEAN criterion above\n"); fprintf(parms[0].LOG," The score (Sp) for each alignment will be a sum of these positions.\n"); } /* end of if(parms[0].BOOLEAN */ if(!parms[0].SCAN && parms[0].TREEWISE) { fprintf(parms[0].LOG,"\n\nTree is to be generated by "); if(parms[0].CLUSTMETHOD==0) fprintf(parms[0].LOG,"1/rms values.\n"); if(parms[0].CLUSTMETHOD==1) { fprintf(parms[0].LOG,"scores from path tracings modified as follows:\n"); fprintf(parms[0].LOG," Sc = (Sp/Lp) * ((Lp-ia)/La) * ((Lp-ib)/Lb),\n"); fprintf(parms[0].LOG," where Sp is the actual score, Lp is the path length.\n"); fprintf(parms[0].LOG," and La & Lb are the lengths of the structures considered.\n"); } /* End of if(parms[0].METHOD==2) */ } fprintf(parms[0].LOG,"\n\n"); fprintf(parms[0].LOG,"Reading coordinates...\n"); for(i=0; i<ndomain; ++i) { fprintf(parms[0].LOG,"Domain %3d %s %s\n ",i+1,domain[i].filename,domain[i].id); if((PDB=openfile(domain[i].filename,"r"))==NULL) { fprintf(stderr,"error opening file %s\n",domain[i].filename); exit(-1); } domain[i].ncoords=0; domain[i].coords=(int**)malloc(parms[0].MAX_SEQ_LEN*sizeof(int*)); domain[i].aa=(char*)malloc((parms[0].MAX_SEQ_LEN+1)*sizeof(char)); domain[i].numb=(struct brookn*)malloc((parms[0].MAX_SEQ_LEN)*sizeof(struct brookn)); total=0; fprintf(parms[0].LOG," "); for(j=0; j<domain[i].nobj; ++j) { if(!parms[0].DSSP) { if(igetca(PDB,&domain[i].coords[total],&domain[i].aa[total],&domain[i].numb[total], &add,domain[i].start[j],domain[i].end[j],domain[i].type[j],(parms[0].MAX_SEQ_LEN-total), domain[i].reverse[j],parms[0].PRECISION,parms[0].ATOMTYPE,parms[0].LOG)==-1) { fprintf(stderr,"Error in domain %s object %d \n",domain[i].id,j+1); exit(-1); } } else { if(igetcadssp(PDB,&domain[i].coords[total],&domain[i].aa[total],&domain[i].numb[total], &add,domain[i].start[j],domain[i].end[j],domain[i].type[j],(parms[0].MAX_SEQ_LEN-total), domain[i].reverse[j],parms[0].PRECISION,parms[0].LOG)==-1) exit(-1); } switch(domain[i].type[j]) { case 1: fprintf(parms[0].LOG," all residues"); break; case 2: fprintf(parms[0].LOG," chain %c",domain[i].start[j].cid); break; case 3: fprintf(parms[0].LOG," from %c %4d %c to %c %4d %c", domain[i].start[j].cid,domain[i].start[j].n,domain[i].start[j].in, domain[i].end[j].cid,domain[i].end[j].n,domain[i].end[j].in); break; } fprintf(parms[0].LOG,"%4d CAs ",add); total+=add; closefile(PDB,domain[i].filename); PDB=openfile(domain[i].filename,"r"); } domain[i].ncoords=total; fprintf(parms[0].LOG,"=> %4d CAs in total\n",domain[i].ncoords); fprintf(parms[0].LOG,"Applying the transformation... \n"); printmat(domain[i].R,domain[i].V,3,parms[0].LOG); fprintf(parms[0].LOG," ...to these coordinates.\n"); matmult(domain[i].R,domain[i].V,domain[i].coords,domain[i].ncoords,parms[0].PRECISION); closefile(PDB,domain[i].filename); } fprintf(parms[0].LOG,"\n\n"); fprintf(parms[0].LOG,"Secondary structure...\n"); for(i=0; i<ndomain; ++i) domain[i].sec=(char*)malloc(parms[0].MAX_SEQ_LEN*sizeof(char)); switch(parms[0].SECTYPE) { case 0: { fprintf(parms[0].LOG,"No secondary structure assignment will be considered\n"); for(i=0; i<ndomain; ++i) { for(j=0; j<domain[i].ncoords; ++j) domain[i].sec[j]='?'; domain[i].sec[j]='\0'; } parms[0].SECSCREEN=0; } break; case 1: { fprintf(parms[0].LOG,"Will try to find Kabsch and Sander DSSP assignments\n"); if(getks(domain,ndomain,parms)!=0) parms[0].SECSCREEN=0; } break; case 2: { fprintf(parms[0].LOG,"Reading in secondary structure assignments from file: %s\n",parms[0].secfile); if(getsec(domain,ndomain,parms)!=0) parms[0].SECSCREEN=0; } break; default: { fprintf(stderr,"error: unrecognised secondary structure assignment option\n"); exit(-1); } } fprintf(parms[0].LOG,"\n\n"); if(parms[0].SCAN) { i=0; fprintf(parms[0].LOG,"Scanning with domain %s\n",&(domain[i].id[0])); if(strcmp(parms[0].logfile,"silent")==0) { printf("Results of scan will be written to file %s\n",parms[0].scanfile); printf("Fits = no. of fits performed, Sc = STAMP score, RMS = RMS deviation\n"); printf("Align = alignment length, Nfit = residues fitted, Eq. = equivalent residues\n"); printf("Secs = no. equiv. secondary structures, %%I = seq. identity, %%S = sec. str. identity\n"); printf("P(m) = P value (p=1/10) calculated after Murzin (1993), JMB, 230, 689-694\n"); printf("\n"); printf(" Domain1 Domain2 Fits Sc RMS Len1 Len2 Align Fit Eq. Secs %%I %%S P(m)\n"); } if(parms[0].SLOWSCAN==1) { if(slow_scan(domain[i],parms)==-1) exit(-1); } else { if(scan(domain[i],parms)==-1) exit(-1); } if(strcmp(parms[0].logfile,"silent")==0) printf("See the file %s.scan\n",parms[0].transprefix); fprintf(parms[0].LOG,"\n"); } else { if(parms[0].ROUGHFIT) if(roughfit(domain,ndomain,parms)==-1) exit(-1); if(parms[0].PAIRWISE) if(pairwise(domain,ndomain,parms)==-1) exit(-1); if(parms[0].TREEWISE) if(treewise(domain,ndomain,parms)==-1) exit(-1); } /* end of if(parms[0].SCAN... */ /* freeing memory to keep purify happy */ /* for(i=0; i<ndomain; ++i) { free(domain[i].aa); free(domain[i].sec); free(domain[i].v); free(domain[i].V); for(j=0; j<3; ++j) { free(domain[i].R[j]); free(domain[i].r[j]); } free(domain[i].R); free(domain[i].r); for(j=0; j<domain[i].ncoords; ++j) free(domain[i].coords[j]); free(domain[i].coords); free(domain[i].type); free(domain[i].start); free(domain[i].end); free(domain[i].reverse); free(domain[i].numb); } */ free(domain); exit(0); }
int roughfit(struct domain_loc *domain, int ndomain, struct parameters *parms) { int i,j,k/*,l,m*/; /* int counter,ndone; */ int ntofit; float rmsd; /* char sys[200]; */ struct brookn tmps,tmpe; FILE *ROUGHOUT=0; printf("Running roughfit.\n"); fprintf(parms[0].LOG,"\n\nROUGH FIT has been requested.\n"); fprintf(parms[0].LOG," The sequences will be aligned from their N-terminal ends, and\n"); fprintf(parms[0].LOG," the resulting equivalences will be used to generate an inital\n"); fprintf(parms[0].LOG," superposition.\n"); if(parms[0].roughout == 1 ) { /* output the transformations */ fprintf(parms[0].LOG,"\nROUGH FIT transformations will be output to the file %s\n",parms[0].roughoutfile); if((ROUGHOUT=fopen(parms[0].roughoutfile,"w"))==NULL) { fprintf(stderr,"Error opening file %s for writing\n",parms[0].roughoutfile); exit(-1); } fprintf(ROUGHOUT,"%% Output from STAMP ROUGH FIT routine\n"); fprintf(ROUGHOUT,"%% The sequences from the file %s have been aligned from their\n",parms[0].listfile); fprintf(ROUGHOUT,"%% N-terminal ends, andthe resulting equivalences were be used \n"); fprintf(ROUGHOUT,"%% to generate the superpositions given below.\n"); } /* We will fit all domains onto the first domain */ for(i=1; i<ndomain; ++i) { if(domain[i].ncoords>domain[0].ncoords) ntofit=domain[0].ncoords; else ntofit=domain[i].ncoords; rmsd=matfit(domain[0].coords,domain[i].coords,domain[i].R,domain[i].V,ntofit,1,parms[0].PRECISION); fprintf(parms[0].LOG,"Domains %s onto %s RMS of %f on %d atoms\n",domain[0].id,domain[i].id,rmsd,ntofit); if(parms[0].roughout == 1) { fprintf(ROUGHOUT,"%% Domains %s onto %s RMS of %f on %d atoms\n",domain[0].id,domain[i].id,rmsd,ntofit); } } for(i=0; i<ndomain; ++i) { fprintf(parms[0].LOG,"\nDomain %2d, %s, %d coordinates\n",i+1,domain[i].id,domain[i].ncoords); fprintf(parms[0].LOG,"Applying the transformation...\n"); for(j=0; j<3; ++j) { fprintf(parms[0].LOG,"| "); for(k=0; k<3; ++k) fprintf(parms[0].LOG,"%8.5f ",domain[i].R[j][k]); fprintf(parms[0].LOG," | %8.5f\n",domain[i].V[j]); } fprintf(parms[0].LOG," ...to these coordinates.\n"); if(parms[0].roughout == 1) { fprintf(ROUGHOUT,"%s %s { ",domain[i].filename,domain[i].id); for(j=0; j<domain[i].nobj; ++j) { if(domain[i].start[j].cid!=' ') tmps.cid=domain[i].start[j].cid; else tmps.cid='_'; if(domain[i].end[j].cid!=' ') tmpe.cid=domain[i].start[j].cid; else tmpe.cid='_'; if(domain[i].start[j].in!=' ') tmps.in=domain[i].start[j].in; else tmps.in='_'; if(domain[i].end[j].in!=' ') tmpe.in=domain[i].start[j].in; else tmpe.in='_'; if(domain[i].type[j]==1) fprintf(ROUGHOUT,"ALL"); else if(domain[i].type[j]==2) fprintf(ROUGHOUT,"CHAIN %c",domain[i].start[j].cid); else fprintf(ROUGHOUT,"%c %d %c to %c %d %c", tmps.cid,domain[i].start[j].n,tmps.in, tmps.cid,domain[i].end[j].n,tmpe.in); fprintf(ROUGHOUT," "); } fprintf(ROUGHOUT,"\n"); for(j=0; j<3; ++j) { fprintf(ROUGHOUT,"%10.4f %10.4f %10.4f %10.4f ", domain[i].R[j][0],domain[i].R[j][1],domain[i].R[j][2],domain[i].V[j]); if(j==2) fprintf(ROUGHOUT," } "); fprintf(ROUGHOUT,"\n"); } } matmult(domain[i].R,domain[i].V,domain[i].coords,domain[i].ncoords,parms[0].PRECISION); } /* and we are done */ if(parms[0].roughout == 1) fclose(ROUGHOUT); fprintf(parms[0].LOG,"\n"); return 0; }
int main(int argc, char *argv[]) { /* Fonksiyonlarin test edilmesi */ double _X[4 * 4] = {1, 2, 3, 1, -1, 1, 2, 3, 0, 4, 5, -3, -1, 1, 2, 3}; double _Y[4 * 4] = {1, 2, 3, 4, 4, 3, 2, 1, -1, -1, 2, 2, 3, 0, 1, 2}; double _Z[4 * 4] = {0}; matmult(4, 4, _X, 4, _Y, 4, _Z); matprint(4, 4, _Z); printf("=========================\n"); double _Zfast[4 * 4] = {0}; matmult_fast(4, 4, _X, 4, _Y, 4, _Zfast, 2); matprint(4, 4, _Zfast); double err = 0.0; for (int i = 0; i < sizeof(_Z) / sizeof(double); ++i) { err += _Z[i] - _Zfast[i]; } printf("Error between methods: %.5f\n", err); assert(err < 0.0000001); ///////////////////////////////////////////////////////////// /* Matris boyutu (int) : argv[1] */ /* recursion base case (int): argv[2] */ if (argc != 3) { printf("Usage: %s <matrix dimension> <base recursion case>\n", argv[0]); exit(1); } /* Komut satirindan verilen matris boyutu ve ozyinelemenin * sonlandirilacagi temel durum. */ int mat_size = atoi(argv[1]); int min_mat_recurse = atoi(argv[2]); /* Zaman olcumleri icin gerekli */ struct timeval tvBegin, tvEnd, tvDiff; double *X, *Y, *Z, *Zfast; /* TODO: 4 gosterici icin ilgili yerleri ayirin */ /* TODO: Gostericilerden birisi NULL ise bellek hatasi verip * programi 1 donus degeriyle sonlandirin. */ if () { fprintf(stderr, "Error allocating memory.\n"); exit(1); } /* TODO: X ve Y matrislerini rasgele doldurun */ /* Klasik carpim algoritmasinin olcumu */ gettimeofday(&tvBegin, NULL); matmult(mat_size, mat_size, X, mat_size, Y, mat_size, Z); gettimeofday(&tvEnd, NULL); timeval_subtract(&tvDiff, &tvEnd, &tvBegin); printf("matmult (%dx%d) --> %ld.%06ld\n", mat_size, mat_size, (long int)tvDiff.tv_sec, (long int)tvDiff.tv_usec); /* Strassen carpim algoritmasinin olcumu */ gettimeofday(&tvBegin, NULL); matmult_fast(mat_size, mat_size, X, mat_size, Y, mat_size, Zfast, min_mat_recurse); gettimeofday(&tvEnd, NULL); timeval_subtract(&tvDiff, &tvEnd, &tvBegin); printf("matmult_fast (%dx%d - base_case: %d) --> %ld.%06ld\n", mat_size, mat_size, min_mat_recurse, (long int)tvDiff.tv_sec, (long int)tvDiff.tv_usec); /* TODO: 4 gostericiye ayrilan yerleri free() edin. */ return 0; }
int main(int argc, char **argv) { int i; unsigned char *ba = (void*)0x30000000; int rc; while(1); x = y = 0; if (!(rc=dput(0, DET_START | DET_SNAP,0,0,0))) { x = 0xdeadbeef; dret(); } if (!(rc=dput(1, DET_START | DET_SNAP,0,0,0))) { y = 0xabadcafe; dret(); } assert(0 == x); assert(0 == y); assert(0 < dget(0, DET_MERGE, (unsigned long)&x, sizeof(x), 0)); assert(0 < dget(1, DET_MERGE, (unsigned long)&y, sizeof(y), 0)); assert(0xdeadbeef == x); assert(0xabadcafe == y); dput(0, DET_KILL, 0, 0, 0); dput(1, DET_KILL, 0, 0, 0); x=0xdeadbeef; y=0xabadcafe; if (!dput(0, DET_START | DET_SNAP,0,0,0)) { x = y; dret(); } if (!dput(1, DET_START | DET_SNAP,0,0,0)) { y = x; dret(); } assert(0xdeadbeef == x); assert(0xabadcafe == y); assert(0 < dget(0, DET_MERGE, (unsigned long)&x, sizeof(x), 0)); assert(0 < dget(1, DET_MERGE, (unsigned long)&y, sizeof(y), 0)); iprintf("%lx %lx\n",x,y); assert(0xdeadbeef == y); assert(0xabadcafe == x); dput(0, DET_KILL, 0, 0, 0); dput(1, DET_KILL, 0, 0, 0); f1: /* More complicated merge opportunities. */ pqsort(&randints[0], &randints[SORT_SIZE-1]); assert(0 == memcmp(randints, sortints, SORT_SIZE*sizeof(int))); iprintf("PQsort success\n"); /* Matrix multiplication. */ matmult(ma, mb, mr); assert(sizeof(mr) == sizeof(int)*8*8); /* These can be determined statically...? */ assert(sizeof(mc) == sizeof(int)*8*8); assert(0 == memcmp(mr, mc, sizeof(mr))); iprintf("Mat mult success\n"); return 0; iprintf("Start large\n"); /* Merge N processes where the N-th process touches bytes in the address space modulo N. */ /*ba = mmap(ba, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, -1, 0); assert(ba==(void*)0x10000000); for (i = 0; i < MEM_SIZE; ++i) ba[i] = 0;*/ for (i = 0; i < N; ++i) { fillpage(i); } //dput(0, DETERMINE_DEBUG, (void*)1, 0, NULL); //dput(0, DETERMINE_DEBUG, (void*)2, 0, NULL); //dput(1, DETERMINE_DEBUG, (void*)2, 0, NULL); dput(0, 0, 0, 0, 0); dput(1, 0, 0, 0, 0); iprintf("Starting\n"); for (i = 0; i < N; ++i) { assert(0 < dget(i, DET_MERGE, 0x30000000, MEM_SIZE, 0)); } iprintf("Done\n"); for (i = 0; i < MEM_SIZE; ++i) { assert(!!ba[i]); } #if 0 /* If we merge the same child again we should get a conflict! */ assert(-1 == dget(0, DET_MERGE, (void*)0x30000000, MEM_SIZE, NULL)); munmap((void*)0x30000000, MEM_SIZE); /* Merge N processes that have set a distinct page of memory to be filled uniquely. */ for (i = 0; i < N; ++i) { fillonepage(i); } for (i = 0; i < N; ++i) { assert(-1 != dget(i, DET_MERGE, (void*)0x30000000, 0x1000 * N, NULL)); } for (i = 0; i < N; ++i) { int j; unsigned long *la = (void*)(0x30000000 + i * 0x1000); for (j = 0; j < 0x1000/sizeof(unsigned long); ++j) { assert((0xdeadbeef * i * j) == la[j]); } } #endif iprintf("Merge success.\n"); return 0; }
int transform(const char * source, const char * destination, const char * output){ char src_pts_name[256]; char dest_pts_name[256]; char out_param_name[256]; int n=3; int m=0; int m2=0; int k,l; double **src_mat=NULL; double **dest_mat=NULL; double **dest_mat_T=NULL; double **src_mat_T=NULL; double **E_mat=NULL; double **C_mat=NULL; double **C_mat_interm=NULL; double **D_mat_interm=NULL; double **P_mat=NULL; double *D_vec=NULL; double *T_vec=NULL; double *one_vec=NULL; double **D_mat=NULL; double **Q_mat=NULL; double **P_mat_T=NULL; double **R_mat=NULL; double trace1=0.0; double trace2=0.0; double scal=0.0; double ppm=0.0; FILE *outfile; printf("\n*******************************\n"); printf( "* helmparms3d v%1.2f *\n",VERS); printf( "* (c) U. Niethammer 2011 *\n"); printf( "* http://helmparms3d.sf.net *\n"); printf( "*******************************\n"); memset(src_pts_name,0,sizeof(src_pts_name)); memset(dest_pts_name,0,sizeof(dest_pts_name)); memset(out_param_name,0,sizeof(out_param_name)); strcpy(src_pts_name, source); strcpy(dest_pts_name, destination); strcpy(out_param_name, output); m=get_m_size(src_pts_name); m2=get_m_size(dest_pts_name); if(m2!=m){ printf("Error, number of source and destination points is not equal!\n"); } else { src_mat=matrix(m,m, src_mat); dest_mat=matrix(m,m, dest_mat); read_points(src_pts_name, src_mat); read_points(dest_pts_name, dest_mat); D_vec=vector(n, D_vec); E_mat=matrix(m, m, E_mat); P_mat=matrix(m, m, P_mat); D_mat=matrix(m, m, D_mat); Q_mat=matrix(m, m, Q_mat); P_mat_T=matrix(m, m, P_mat_T); R_mat=matrix(m, m, R_mat); dest_mat_T=matrix(m, m, dest_mat_T); C_mat=matrix(m, m, C_mat); C_mat_interm=matrix(m, m, C_mat_interm); src_mat_T=matrix(m, m, src_mat_T); D_mat_interm=matrix(m, m, D_mat_interm); transpose_matrix(m, m, dest_mat, dest_mat_T); if(debug)printf("%s_T:\n",dest_pts_name); if(debug)plot_matrix(stdout, n, m, dest_mat_T); for(k=0;k<m;k++){ for(l=0;l<m;l++){ if(k!=l){ E_mat[k][l]=-1.0/(double)m; } else{ E_mat[k][l]=1.0-1.0/(double)m; } } } if(debug)printf("E:\n"); if(debug)plot_matrix(stdout, m, m, E_mat); if(debug)printf("dest_mat_T:\n"); if(debug)plot_matrix(stdout, n, m, dest_mat_T); matmult(dest_mat_T, m, m, E_mat, m, m, C_mat_interm, m, n); if(debug)printf("C_interm:\n"); if(debug)plot_matrix(stdout, n, m, C_mat_interm); matmult(C_mat_interm, n, m, src_mat, m, n, C_mat, n, n); if(debug)printf("C:\n"); if(debug)plot_matrix(stdout, n, n, C_mat); copy_matrix(n,n,C_mat,P_mat); if(debug)printf("P:\n"); if(debug)plot_matrix(stdout, n, n, P_mat); //Given matrix C[m][n], m>=n, using svd decomposition C = P D Q' to get P[m][n], diag D[n] and Q[n][n]. svd(n, n, C_mat, P_mat, D_vec, Q_mat); transpose_matrix(n, n, P_mat, P_mat_T); if(debug)printf("P\n"); if(debug)plot_matrix(stdout, n, n, P_mat); if(debug)printf("P_T\n"); if(debug)plot_matrix(stdout, n, n, P_mat_T); if(debug)printf("D_vec\n"); if(debug)plot_vector(stdout, n, D_vec); for(k=0;k<n;k++){ for(l=0;l<n;l++){ D_mat[k][l]=0.0; D_mat[l][l]=D_vec[l]; } } if(debug)printf("D\n"); if(debug)plot_matrix(stdout, n, n, D_mat); matmult(Q_mat, n, n, P_mat_T, n, n, R_mat, n, n); if(debug)printf("R_trans:\n"); if(debug)plot_matrix(stdout, n, n, R_mat); matmult(C_mat, m, n, R_mat, n, m, C_mat_interm, m, n); if(debug)printf("C_interm:\n"); if(debug)plot_matrix(stdout, n, n, C_mat_interm); trace1=trace(n,n,C_mat_interm); if(debug)printf("\ntra=%lf\n\n",trace1); transpose_matrix(m, m, src_mat, src_mat_T); if(debug)printf("%s_T:\n",src_pts_name); if(debug)plot_matrix(stdout, n, m, src_mat_T); init_matrix(m,m,C_mat); init_matrix(m,m,C_mat_interm); matmult(src_mat_T, m, m, E_mat, m, m, C_mat_interm, n, n); if(debug)printf("C_interm:\n"); if(debug)plot_matrix(stdout, n, m, C_mat_interm); matmult(C_mat_interm, n, m, src_mat, m, n, C_mat, n, n); if(debug)printf("C:\n"); if(debug)plot_matrix(stdout, n, n, C_mat); trace2=trace(n,n,C_mat); if(debug)printf("\ntra=%lf\n\n",trace2); scal=trace1/trace2; ppm=scal-1.0; if(debug)printf("\nscal = %10.10lf\nscal = %10.10lf ppm\n\n",scal, ppm); init_matrix(m,m,C_mat); init_matrix(m,m,C_mat_interm); matmult(src_mat, m, n, R_mat, n,m, D_mat_interm, m, n); if(debug)printf("C_mat_interm:\n"); if(debug)plot_matrix(stdout, m, n, D_mat_interm); scal_matrix(m, n, scal, D_mat_interm, C_mat_interm); if(debug)printf("C_mat_interm:\n"); if(debug)plot_matrix(stdout, m, n, C_mat_interm); subtract_matrix(m, n, dest_mat, C_mat_interm, D_mat_interm); if(debug)plot_matrix(stdout, m, n, D_mat_interm); scal_matrix(m, n, 1.0/m, D_mat_interm, C_mat_interm); if(debug)plot_matrix(stdout, m, n, C_mat_interm); init_matrix(m,m,src_mat_T); transpose_matrix(m, m, C_mat_interm, src_mat_T); if(debug)plot_matrix(stdout, n, m, src_mat_T); T_vec=vector(m, T_vec); one_vec=vector(m, one_vec); for(k=0;k<m;k++){ one_vec[k]=1.0; } matrix_multiply(n, m, src_mat_T, one_vec, T_vec); if(debug)printf("T:\n"); if(debug)plot_vector(stdout, 3, T_vec); outfile = fopen(out_param_name, "w"); if(outfile == NULL){ printf("Error writing %s\r\n",out_param_name); exit(-1); } init_matrix(m,m,src_mat_T); transpose_matrix(m, m, R_mat, src_mat_T); plot_matrix(outfile, n, n, src_mat_T); printf("R =\n");fflush(stdout); plot_matrix(stdout, n, n, src_mat_T); printf("\n");fflush(stdout); plot_vector(outfile, 3, T_vec); printf("T =\n");fflush(stdout); plot_vector(stdout, 3, T_vec); printf("\n");fflush(stdout); fprintf(outfile, "%10.10lf\n", scal); printf("s = %10.10lf (= %10.10lf ppm)\n\n",scal, ppm);fflush(stdout); fclose(outfile); freevector(D_vec); freevector(T_vec); freevector(one_vec); freematrix(m, src_mat); freematrix(m, dest_mat); freematrix(m, E_mat); freematrix(m, P_mat); freematrix(m, D_mat); freematrix(m, Q_mat); freematrix(m, P_mat_T); freematrix(m, R_mat); freematrix(m, dest_mat_T); freematrix(m, C_mat); freematrix(m, C_mat_interm); freematrix(m, src_mat_T); freematrix(m, D_mat_interm); printf("\n...done\n"); } }