void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { clock_t start,finish; double duration; srand(unsigned(time(NULL))); //-------------------- // Initialization //-------------------- start = clock(); Matrix MatA(mxGetM(prhs[0]),mxGetN(prhs[0]),mxGetPr(prhs[0]),MATRIX_COL_SUM); Matrix MatB(mxGetM(prhs[1]),mxGetN(prhs[1]),mxGetPr(prhs[1]),MATRIX_COL_SUM); const size_t budget = (size_t)mxGetPr(prhs[2])[0]; const size_t NumSample = (size_t)mxGetPr(prhs[3])[0]; const uint top_t = (uint)mxGetPr(prhs[4])[0]; finish = clock(); duration = (double)(finish-start) / CLOCKS_PER_SEC; // value plhs[0] = mxCreateDoubleMatrix(top_t, 1, mxREAL); double *plhs_result = mxGetPr(plhs[0]); // result for time plhs[1] = mxCreateDoubleMatrix(1, 1, mxREAL); double *tsec = mxGetPr(plhs[1]); *tsec = duration; // pair plhs[2] = mxCreateNumericMatrix(top_t, 2, mxUINT64_CLASS, mxREAL); uint64_T* plhs_pr = (uint64_T*)mxGetData(plhs[2]); mexPrintf("Starting Wedge Sampling:"); mexPrintf("Top:%d,Samples:1e%d,Budget:1e%d\n",top_t,(int)log10(NumSample),(int)log10(budget)); mexEvalString("drawnow"); //------------------------------------- // Compute weight //------------------------------------- double SumofW = 0; //weight has the same size of A double *weight = (double*)malloc(MatA.row*MatA.col*sizeof(double)); memset(weight, 0, MatA.row*MatA.col*sizeof(double)); start = clock(); for (uint r = 0; r < MatA.row; ++r){ for(uint i = 0; i < MatA.col; ++i){ double tempW = abs(MatA.GetElement(r,i)) * MatB.SumofCol[r]; weight[r*MatA.col + i] = tempW; SumofW += tempW; } } finish = clock(); duration = (double)(finish-start) / CLOCKS_PER_SEC; *tsec += duration; //------------------------- // Do Sampling //------------------------- start = clock(); // sampled r, i, j, r' uint *IdxR = (uint*)malloc(NumSample*sizeof(uint)); memset(IdxR, 0, NumSample*sizeof(uint)); uint *IdxI = (uint*)malloc(NumSample*sizeof(uint)); memset(IdxI, 0, NumSample*sizeof(uint)); uint *IdxJ = (uint*)malloc(NumSample*sizeof(uint)); memset(IdxJ, 0, NumSample*sizeof(uint)); // sampled r's frequency size_t *freq_r = (size_t*)malloc(MatA.row*sizeof(size_t)); memset(freq_r, 0, MatA.row*sizeof(size_t)); // sample pairs (i,r) , sort_sample(NumSample, \ IdxI, IdxR, \ freq_r, \ MatA.row, MatA.col, \ weight, SumofW); // sample j; for (uint r = 0,offset = 0; r < MatA.row; ++r){ vose_alias( freq_r[r], (IdxJ + offset), \ MatB.row, \ (MatB.element + r*MatB.row), \ MatB.SumofCol[r]); offset += freq_r[r]; } // sample rp and get score TPoint2DMap IrJc; for (size_t s = 0; s < NumSample ; ++s){ uint r = IdxR[s]; uint i = IdxI[s]; uint j = IdxJ[s]; double valueSampled = sgn_foo(MatA.GetElement(r,i)) \ * sgn_foo(MatB.GetElement(j,r)); // Update the element in coordinate IrJc[point2D(i,j)] += valueSampled; } finish = clock(); duration = (double)(finish-start) / CLOCKS_PER_SEC; *tsec += duration; //----------------------------------- //sort the values have been sampled //----------------------------------- std::vector<pidx2d> sortVec; std::vector<pidx2d> tempSortedVec; // sort the sampled value for (auto mapItr = IrJc.begin(); mapItr != IrJc.end() ; ++mapItr){ tempSortedVec.push_back(std::make_pair(mapItr->first,mapItr->second)); } start = clock(); sort(tempSortedVec.begin(), tempSortedVec.end(), compgt<pidx2d>); finish = clock(); *tsec += duration; // compute the actual of top-t'(budget) for(size_t m = 0; m < tempSortedVec.size() && m < budget; ++m){ double true_value = vectors_mul(tempSortedVec[m].first, MatA, MatB); sortVec.push_back(std::make_pair(tempSortedVec[m].first,true_value)); } sort(sortVec.begin(), sortVec.end(), compgt<pidx2d>); finish = clock(); duration = (double)(finish-start) / CLOCKS_PER_SEC; *tsec += duration; //-------------------------------- // Converting to Matlab //-------------------------------- for(uint m = 0; m < sortVec.size() && m < top_t; ++m){ //value plhs_result[m] = sortVec[m].second; //i plhs_pr[m] = (sortVec[m].first.x + 1); //j plhs_pr[m + top_t] = (sortVec[m].first.y + 1); } mexPrintf("Done!\n"); //--------------- // free //--------------- free(weight); free(IdxI); free(IdxJ); free(IdxR); free(freq_r); }
int LocalMove(struct s_polymer *p, struct s_polymer *oldp,struct s_polymer *fragment,struct s_potential *pot,int nmul,struct s_mc_parms *parms, double t) { int ok=0,nang=0; int iw,ip,iapdbtocheck,i,m; double deltaE; double detL1,detL2,detA1,detA2,W1,W2,psisquared,e; ip=irand(parms->npol); iw=(nmul-1)+irand( (p+ip)->nback-nmul+1 ); //iw=20; for(i=0; i<nmul; i++) { nang+=((((p+ip)->back)+iw-nmul+i)+1)->move; } deltaE = -GetEnergyMonomerRange(p,iw-nmul+1,iw+1,ip); if(iw==((p+ip)->nback-1)) //pivot forward OK { for(i=0; i<nang; i++) (fragment+ip)->d_ang[i]=parms->dw_mpivot*(0.05-frand()); if (!parms->nodihpot) for(i=0; i<nmul+3; i++) { deltaE-=(((p+ip)->back)+iw-nmul+i+1)->e_dih; } m=0; for(i=0; i<nmul; i++) { if( (((p+ip)->back)+iw+i-nmul+2)->move==1) { ok*=PivotForward((p+ip),iw-nmul+i+1,(fragment+ip)->d_ang[m],nmul-i-2,parms); m++; if(m==nang) break; } } if(!parms->nosidechains) { ok*=AddSidechain(p,iw-nmul+1,iw+1,ip); } deltaE += EnergyMonomerRange(p,pot,iw-nmul+1,iw+1,ip,parms->npol,parms->shell,1,parms->nosidechains,parms->disentangle,parms->hb); if (!parms->nodihpot) for(i=0; i<nmul+3; i++) { deltaE+=EnergyDihedrals(p,pot,iw-nmul+i+1,ip,1); } ok*=Metropolis(deltaE,t,p->tables); if(ok==0) { UpdateMonomerRange(oldp,p,iw-nmul+1,iw+1,ip,parms->shell); } else { //move accepted UpdateMonomerRange(p,oldp,iw-nmul+1,iw+1,ip,parms->shell); p->etot+=deltaE; parms->acc++; } } //end of forward else if(iw==nmul-1) //pivot backward OK { for(i=0; i<nang; i++) (fragment+ip)->d_ang[i]=parms->dw_mpivot*(0.05-frand()); if (!parms->nodihpot) for(i=0; i<nmul+3; i++) { deltaE-=(((p+ip)->back)+i)->e_dih; } m=0; for(i=0; i<nmul; i++) { if( (((p+ip)->back)+iw-i)->move==1) { ok*=PivotBackward((p+ip),iw-i,(fragment+ip)->d_ang[m],nmul-i-2,parms); m++; if(m==nang) break; } } if(!parms->nosidechains) ok*=AddSidechain(p,0,iw+1,ip); deltaE += EnergyMonomerRange(p,pot,0,iw+1,ip,parms->npol,parms->shell,1,parms->nosidechains,parms->disentangle,parms->hb); if (!parms->nodihpot) for(i=0; i<nmul+3; i++) { deltaE+=EnergyDihedrals(p,pot,i,ip,1); } ok*=Metropolis(deltaE,t,p->tables); if(ok==0) { UpdateMonomerRange(oldp,p,0,iw+1,ip,parms->shell); } else { UpdateMonomerRange(p,oldp,0,iw+1,ip,parms->shell); p->etot+=deltaE; parms->acc++; } } //end of backward else if(iw!=((p+ip)->nback-2))//pivot local { if((((p+ip)->back)+iw+1)->iapdb==0) iapdbtocheck=1; if((((p+ip)->back)+iw+1)->iapdb==1) iapdbtocheck=2; if((((p+ip)->back)+iw+1)->iapdb==2) iapdbtocheck=0; int out; Gaussian_Angles((fragment+ip)->g_ang,nang); Compute_G(fragment,p,ip,iw-nmul+1,nmul,nang,parms); MatA((fragment+ip)->A,(fragment+ip)->G,nang,parms->bgs_a,parms->bgs_b); Cholesky_2((fragment+ip)->L,(fragment+ip)->A,nang); psisquared=Squared_n_Norma((fragment+ip)->g_ang,nang); e=exp(-psisquared); detL1=DetTriang((fragment+ip)->L,nang); detA1=detL1*detL1; W1=e * sqrt(detA1); InvertTriang((fragment+ip)->Y,(fragment+ip)->L,nang); TransposedMatOnVect((fragment+ip)->Y,(fragment+ip)->g_ang,(fragment+ip)->d_ang,nang); if(!parms->nodihpot) for(i=0; i<nmul+3; i++) { deltaE -= (((p+ip)->back)+iw-nmul+i+1)->e_dih; } if(!parms->noangpot) { deltaE-=(((p+ip)->back)+iw)->e_ang; deltaE-=(((p+ip)->back)+iw+1)->e_ang; } m=0; for(i=0; i<nmul; i++) { if( (((p+ip)->back)+iw+i-nmul+2)->move==1) { ok*=PivotForward((p+ip),iw-nmul+i+1,(fragment+ip)->d_ang[m],nmul-i-2,parms); m++; if(m==nang) break; } } double rc2=parms->r_cloose*parms->r_cloose; double dihedral=Dihedral( (((p+ip)->back)+iw-iapdbtocheck)->pos, (((p+ip)->back)+iw+1-iapdbtocheck)->pos, (((p+ip)->back)+iw+2-iapdbtocheck)->pos, (((p+ip)->back)+iw+3-iapdbtocheck)->pos, p->tables, &out ); // fprintf(stderr,"Checking Dihedral: backbone atoms %d,%d,%d,%d\n",+iw-iapdbtocheck,+iw+1-iapdbtocheck,+iw+2-iapdbtocheck,+iw+3-iapdbtocheck); if( DAbs (Dist2( (((p+ip)->back)+iw)->pos,(((p+ip)->back)+iw+1)->pos ) -(((p+ip)->back)+iw)->d2_next < rc2 ) && DAbs( Angle( (((p+ip)->back)+iw-1)->pos, (((p+ip)->back)+iw)->pos, (((p+ip)->back)+iw+1)->pos, (p+ip)->tables, &out) - (((p+ip)->back)+iw-1)->a_next) < 0.05 && DAbs(180-DAbs(dihedral)) < DELTAOMEGA) { if(!parms->nosidechains) { ok *= AddSidechain(p,iw-nmul+1,iw+1,ip); } deltaE += EnergyMonomerRange(p,pot,iw-nmul+1,iw+1,ip,parms->npol,parms->shell,1,parms->nosidechains,parms->disentangle,parms->hb); if (!parms->nodihpot) for(i=0; i<nmul+3; i++) { deltaE+=EnergyDihedrals(p,pot,iw-nmul+i+1,ip,1); } if(!parms->noangpot) { // fprintf(stderr,"\nCOMPUTING ANGLE ENERGY deltaE=%lf\t",deltaE); deltaE+=EnergyAngles(p,pot,iw,ip,1); deltaE+=EnergyAngles(p,pot,iw+1,ip,1); // fprintf(stderr,"-> %lf\n",deltaE); } Compute_G(fragment,p,ip,iw-nmul+1,nmul,nang,parms); MatA((fragment+ip)->A,(fragment+ip)->G,nang,parms->bgs_a,parms->bgs_b); Cholesky_2((fragment+ip)->L,(fragment+ip)->A,nang); detL2=DetTriang((fragment+ip)->L,nang); detA2=detL2*detL2; TransposedMatOnVect((fragment+ip)->L,(fragment+ip)->d_ang,(fragment+ip)->g_ang,nang); psisquared=Squared_n_Norma((fragment+ip)->g_ang,nang); e=exp(-psisquared); W2=e*sqrt(detA2); ok*=B_Metropolis(deltaE,t,W2,W1,p->tables); if(ok==0) //move rejected { UpdateMonomerRange(oldp,p,iw-nmul+1,iw+1,ip,parms->shell); } else //move accepted { UpdateMonomerRange(p,oldp,iw-nmul+1,iw+1,ip,parms->shell); p->etot+=deltaE; parms->acc++; } }//end of loose condition else //if not loose pivot { ok=0; UpdateMonomerRange(oldp,p,iw-nmul+1,iw+1,ip,parms->shell); } }//end of local pivot parms->mov++; return ok; }