static inline void CalcForces() { double rij[3]; utot = 0; double r2; for (int i = 1; i < N; ++i) { for (int j = 0; j < i; ++j) { r2 = 0; for (int k = 0; k < 3; ++k) { rij[k] = rn[i][k] - rn[j][k]; if (rij[k] > L2[k]) { rij[k] -= L[k]; } else if (rij[k] < -L2[k]) { rij[k] += L[k]; } r2 += rij[k] * rij[k]; } double f_r = 0; utot += Potential(r2); f_r = ForceDivByRange(r2); for (int k = 0; k < 3; ++k) { assert(f_r == f_r); f[i][k] += (float)f_r * (float)rij[k]; f[j][k] -= (float)f_r * (float)rij[k]; } } } }
bool NEURON::Excite(NEU_POTENT pot) { PotentialAdd(pot); if (Potential() > THRESH_BASE && _state != HYPER ) { _state = HYPER; } return (_state == HYPER); }
void MarchingCube::updateDensityField() { btVector3 stepSize(bMax - bMin); stepSize.setX(stepSize.x() / ncellsX); stepSize.setY(stepSize.y() / ncellsY); stepSize.setZ(stepSize.z() / ncellsZ); for(int i=0; i < ncellsX+1; i++) for(int j=0; j < ncellsY+1; j++) for(int k=0; k < ncellsZ+1; k++) { mp4Vector vert(bMin.x()+i*stepSize.x(), bMin.y()+j*stepSize.y(), bMin.z()+k*stepSize.z(), 0); vert.val = Potential((mpVector)vert, fluid->internalGetParticles()); mcPoints[i*(ncellsY+1)*(ncellsZ+1) + j*(ncellsZ+1) + k] = vert; } }
int main(int argc, char* argv[]) { int nsteps; Box box = Box(20.0); Particles particles = Particles(400, 400, box); Potential potential = Potential(); Integrator integrator = Integrator(0.0005); std::ofstream file; file.open("dump.lammpstrj"); Dump dump = Dump(100, &file); std::ofstream file2; file2.open("thermo.out"); Thermo thermo = Thermo(100, &file2); if (argc != 2) { std::cerr << "usage: " << argv[0] << " nsteps" << std::endl; exit(1); } nsteps = atoi(argv[1]); std::cout << "Initializing system..." << std::endl; System sys = System(&box, &particles, &potential, &integrator, &dump, &thermo); sys.run(nsteps); }
void Forces::CalcTens(){ if(!VAR_IF_TYPE(SysAlloc,ALL_FORCES)){ printf("Forces not allocated\n"); return; } ClearDens(); AddDens(0,pNPart()); SumDens(0,pNPart()); double Dist = 0.; double DistRelBA[4]; for(int p=0;p<pNPart();p++){ for(int d=0;d<3;d++){ Fm[p].Dir[d] = 0.; } } // non bonded double Pos[3]; for(int p1=0;p1<pNPart();p1++){ for(Pc->SetCurr(p1);Pc->IfCurr();Pc->NextCurr()){ int p2 = Pc->p2Curr; if(p2 <= p1) continue; Pc->Dist2Curr(DistRelBA); if(DistRelBA[3] > Kf.CutOff2) continue; double Dist = sqrt(DistRelBA[3]); double Force = 0.; for(int t=0;t<pNType();t++){ Force += MInt->Coeff(pType(p1),pType(p2),t)*(Dens3[p1*pNType()+t]+Dens3[p2*pNType()+t]); } Force *= DerWei3(Dist,pWei3Par())*2./3.; Force += DerWei2(Dist,pWei2Par())*MInt->Coeff(pType(p1),pType(p2)); SumTens(p1,p2,Force,DistRelBA); Force /= -Dist; SigErr(Force > 5000.,"Forces over the limit %lf\n",Force); for(int d=0;d<3;d++){ Fm[p1].Dir[d] += Force*DistRelBA[d]; Fm[p2].Dir[d] -= Force*DistRelBA[d]; } } } // bonded double DistRelBC[4]; double Pre[12]; for(int b=0;b<pNBlock();b++){ for(int c=0;c<pNChain(b);c++){ for(int p=c*pNPCh(b);p<(c+1)*pNPCh(b)-1;p++){ TwoPartDist(p+1,p,DistRelBA); double ForceSp = pkSpr()*(1. - pSprRest()/DistRelBA[3]); SumTens(p,p+1,ForceSp,DistRelBA); for(int d=0;d<3;d++){ Fm[p].Dir[d] += ForceSp*DistRelBA[d]; Fm[p+1].Dir[d] -= ForceSp*DistRelBA[d]; } if(p < (c+1)*pNPCh(b)-2){ TwoPartDist(p+2,p+1,DistRelBC); double CosAngle = 0.; for(int d=0;d<3;d++){ DistRelBA[d] /= DistRelBA[3]; DistRelBC[d] /= DistRelBC[3]; CosAngle += DistRelBA[d]*DistRelBC[d]; } double PreFactBA = pkBen()/DistRelBA[3]; double PreFactBC = pkBen()/DistRelBC[3]; for(int d=0;d<3;d++){ Fm[p+0].Dir[d] += PreFactBA*(DistRelBC[d]-DistRelBA[d]*CosAngle); Fm[p+1].Dir[d] -= PreFactBA*(DistRelBC[d]-DistRelBA[d]*CosAngle); Fm[p+1].Dir[d] += PreFactBC*(DistRelBA[d]-DistRelBC[d]*CosAngle); Fm[p+2].Dir[d] -= PreFactBC*(DistRelBA[d]-DistRelBC[d]*CosAngle); Pre[d ] = DistRelBA[d]*pkBen()*(DistRelBC[d]-DistRelBA[d]*CosAngle); Pre[d+6] = DistRelBC[d]*pkBen()*(DistRelBA[d]-DistRelBC[d]*CosAngle); } Pre[ 3] = DistRelBA[0]*pkBen()*(DistRelBC[1]-DistRelBA[1]*CosAngle); Pre[ 4] = DistRelBA[0]*pkBen()*(DistRelBC[2]-DistRelBA[2]*CosAngle); Pre[ 5] = DistRelBA[1]*pkBen()*(DistRelBC[2]-DistRelBA[2]*CosAngle); Pre[ 9] = DistRelBC[0]*pkBen()*(DistRelBA[1]-DistRelBC[1]*CosAngle); Pre[10] = DistRelBC[0]*pkBen()*(DistRelBA[2]-DistRelBC[2]*CosAngle); Pre[11] = DistRelBC[1]*pkBen()*(DistRelBA[2]-DistRelBC[2]*CosAngle); SumTens(p,p+1,Pre); SumTens(p+1,p+2,Pre+6); } } } } return; //external double Pot[3]; double PosBf[3]; double dr[4]; double NPos[3]; for(int n=0;n<pNNano();n++){ Point2Shape(Nano[n].Shape); for(int p=0;p<pNPart();p++){ pPos(p,Pos); double Dr2 = NanoDist2(Pos,n); double InvDist = 1./Dr2; double Cons = Potential(Dr2,0,pType(p),Pot); for(int d=0;d<3;d++){ dr[d] = Nano[n].Pos[d] - pPos(p,d); if(dr[d] > .5*pInvEdge(d)) dr[d] -= pEdge(d); if(dr[d] < -.5*pInvEdge(d)) dr[d] += pEdge(d); } double Norm = SQR(Nano[n].Rad)/(SQR(dr[0]) + SQR(dr[1]) + SQR(dr[2])); Norm = sqrt(Norm); for(int d=0;d<3;d++){ NPos[d] = NPos[d] + dr[d]*Norm; Fm[p].Dir[d] += Cons*dr[d]*InvDist; Pre[d ] = Cons*dr[d]*dr[d]*InvDist; } Pre[3] = Cons*dr[0]*dr[1]*InvDist; Pre[4] = Cons*dr[0]*dr[2]*InvDist; Pre[5] = Cons*dr[1]*dr[2]*InvDist; SumTens(NPos,Pos,Pre); } } }
int main(int argc, char *argv[]){ int flag = 0; int c; //the character used to process commandline argument double hx, ht; // increment for time and space int nx, nt; // length for space and time boundary double vh, vw; // potential height and width double sigma, height, k0; // gaussian wave packet width and height double energy=0.501; nx = 200; hx = 1; ht = 0.02; vw = 5; vh = 1; nx *= 2; // we make the meshgrid center on 0; double* meshXI = (double *)malloc(nx*sizeof(double)); //image part of meshgrid double* meshXR = (double *)malloc(nx*sizeof(double)); //real part of meshgrid double* meshX = (double *)malloc(nx*sizeof(double)); double* V = (double *)malloc(nx*sizeof(double)); //Potential grid while ((c = getopt(argc, argv, "n:h:e:s:w:por")) != -1) switch(c) { case 'n': nt = atof(optarg); break; case 'h': vh = atof(optarg); break; case 'e': energy = atof(optarg); break; case 's': sigma = atof(optarg); break; case 'p': flag = 1; break; case 'w': vw = atof(optarg); break; case 'o': break; case 'r': break; } // process commandline arguments sigma = 5; height = 1; k0 = sqrt(2*energy-1.0/(2*sigma*sigma)); nt = (int)30*nx*1.0/k0; GaussianWave(sigma, height, k0, meshXR, meshXI, nx, hx); GaussianCombine(meshXR, meshXI, meshX,nx); // construct a gaussian wavepacket Potential(V, nx, vh, vw); // construct a potential step ofstream fp; fp.open("V.dat"); for (int m=0; m<nx; m++){ fp<<m<<" "<<V[m]<<endl; } fp.close(); cout<<"Simulation running..."<<endl; FILE *pipe = popen("gnuplot -persist","w"); fflush(pipe); for (int i=0; i<nt-1; i++){ for (int j=1; j<nx; j++){ meshXI[j] += 1.0*ht/(2*hx*hx) * (meshXR[j+1]-2*meshXR[j]+meshXR[j-1])-ht*V[j]*meshXR[j]; meshXR[j] += -1.0*ht/(2*hx*hx) * (meshXI[j+1]-2*meshXI[j]+meshXI[j-1])+ht*V[j]*meshXI[j]; GaussianCombine(meshXR, meshXI, meshX, nx); } if ( (flag==1) && (i%100==0) ){ fp.open("plot.dat"); for (int m=0; m<nx; m++){ fp<<m<<" "<<meshX[m]<<endl; } fp.close(); } if ( (flag==1) && (i%100==0) ){ fprintf(pipe,"set multiplot\n"); fflush(pipe); fprintf(pipe,"set xrange [0:%d]\n",nx); fflush(pipe); fprintf(pipe,"set title 't=%d' \n", i); fflush(pipe); fprintf(pipe,"plot 'V.dat' with lines\n"); fflush(pipe); fprintf(pipe,"plot 'plot.dat' with lines\n"); fflush(pipe); fprintf(pipe,"set nomultiplot\n"); fflush(pipe); //sleep(0.5); } } double reflect=0; double transmit=0; for (int i=0; i<nx/2+50; i++){ reflect += meshX[i]; } for (int i=nx/2+50+vw; i<nx; i++){ transmit += meshX[i]; } free(meshXR); free(meshXI); free(meshX); // clean our workspace cout<<"------------------Summary----------------------"<<endl; cout<<"Particle Energy: " << energy<<" (-e)"<<endl; cout<<"Wavepacket sigma: "<< sigma<<" (-s)"<<endl; cout<<"Step potential Height: "<<vh<<" (-h)"<<endl; cout<<"Step potential Width: "<<vw<<" (-w)"<<endl; cout<<"Simulation steps: "<<nt<<" (-n)"<<endl; cout<<"Bounced back fraction: "<<reflect/(reflect+transmit)<<endl; cout<<"Transmitted fraction: "<<transmit/(transmit+reflect)<<endl; cout<<"-----------------------------------------------"<<endl; return 0; }