double run_soft_sphere(double reduced_density, double temp) { Functional f = SoftFluid(sigma, 1, 0); const double mu = find_chemical_potential(OfEffectivePotential(f), temp, reduced_density*pow(2,-5.0/2.0)); printf("mu is %g for reduced_density = %g at temperature %g\n", mu, reduced_density, temp); //printf("Filling fraction is %g with functional %s at temperature %g\n", reduced_density, teff); //fflush(stdout); temperature = temp; //if (kT == 0) kT = ;1 Lattice lat(Cartesian(xmax,0,0), Cartesian(0,ymax,0), Cartesian(0,0,zmax)); GridDescription gd(lat, dx); Grid softspherepotential(gd); softspherepotential.Set(soft_sphere_potential); f = SoftFluid(sigma, 1, mu); // compute approximate energy with chemical potential mu const double approx_energy = f(temperature, reduced_density*pow(2,-5.0/2.0))*xmax*ymax*zmax; const double precision = fabs(approx_energy*1e-9); f = OfEffectivePotential(SoftFluid(sigma, 1, mu) + ExternalPotential(softspherepotential)); static Grid *potential = 0; potential = new Grid(gd); *potential = softspherepotential - temperature*log(reduced_density*pow(2,-5.0/2.0)/(1.0*radius*radius*radius))*VectorXd::Ones(gd.NxNyNz); // Bad starting guess printf("\tMinimizing to %g absolute precision from %g from %g...\n", precision, approx_energy, temperature); fflush(stdout); Minimizer min = Precision(precision, PreconditionedConjugateGradient(f, gd, temperature, potential, QuadraticLineMinimizer)); took("Setting up the variables"); for (int i=0; min.improve_energy(true) && i<100; i++) { } took("Doing the minimization"); min.print_info(); Grid density(gd, EffectivePotentialToDensity()(temperature, gd, *potential)); //printf("# per area is %g at filling fraction %g\n", density.sum()*gd.dvolume/dw/dw, reduced_density); char *plotname = (char *)malloc(1024); sprintf(plotname, "papers/fuzzy-fmt/figs/radial-wca-%06.4f-%04.2f.dat", temp, reduced_density); z_plot(plotname, Grid(gd, pow(2,5.0/2.0)*density)); free(plotname); { //double peak = peak_memory()/1024.0/1024; //double current = current_memory()/1024.0/1024; //printf("Peak memory use is %g M (current is %g M)\n", peak, current); } took("Plotting stuff"); printf("density %g gives ff %g for reduced_density = %g and T = %g\n", density(0,0,gd.Nz/2), density(0,0,gd.Nz/2)*4*M_PI/3, reduced_density, temp); return density(0, 0, gd.Nz/2)*4*M_PI/3; // return bulk filling fraction }
int main(int, char **) { FILE *o = fopen("paper/figs/constrained-water.dat", "w"); Functional f = OfEffectivePotential(SaftFluidSlow(water_prop.lengthscale, water_prop.epsilonAB, water_prop.kappaAB, water_prop.epsilon_dispersion, water_prop.lambda_dispersion, water_prop.length_scaling, 0)); double mu_satp = find_chemical_potential(f, water_prop.kT, water_prop.liquid_density); Lattice lat(Cartesian(width,0,0), Cartesian(0,width,0), Cartesian(0,0,zmax)); GridDescription gd(lat, 0.1); Grid potential(gd); Grid constraint(gd); constraint.Set(notinwall); f = constrain(constraint, OfEffectivePotential(SaftFluidSlow(water_prop.lengthscale, water_prop.epsilonAB, water_prop.kappaAB, water_prop.epsilon_dispersion, water_prop.lambda_dispersion, water_prop.length_scaling, mu_satp))); Minimizer min = Precision(0, PreconditionedConjugateGradient(f, gd, water_prop.kT, &potential, QuadraticLineMinimizer)); potential = water_prop.liquid_density*constraint + water_prop.vapor_density*VectorXd::Ones(gd.NxNyNz); //potential = water_prop.liquid_density*VectorXd::Ones(gd.NxNyNz); potential = -water_prop.kT*potential.cwise().log(); const int numiters = 50; for (int i=0;i<numiters && min.improve_energy(true);i++) { fflush(stdout); Grid density(gd, EffectivePotentialToDensity()(water_prop.kT, gd, potential)); density.epsNative1d("paper/figs/1d-constrained-plot.eps", Cartesian(0,0,0), Cartesian(0,0,zmax), water_prop.liquid_density, 1, "Y axis: , x axis: "); } min.print_info(); double energy = min.energy()/width/width; printf("Energy is %.15g\n", energy); double N = 0; { Grid density(gd, EffectivePotentialToDensity()(water_prop.kT, gd, potential)); for (int i=0;i<gd.NxNyNz;i++) N += density[i]*gd.dvolume; } N = N/width/width; printf("N is %.15g\n", N); Grid density(gd, EffectivePotentialToDensity()(water_prop.kT, gd, potential)); density.epsNative1d("paper/figs/1d-constrained-plot.eps", Cartesian(0,0,0), Cartesian(0,0,zmax), water_prop.liquid_density, 1, "Y axis: , x axis: "); //potential.epsNative1d("hard-wall-potential.eps", Cartesian(0,0,0), Cartesian(0,0,zmax), 1, 1); fclose(o); }
double run_walls(double reduced_density, const char *name, Functional fhs, double teff) { double kT = teff; if (kT == 0) kT = 1; Functional f = OfEffectivePotential(fhs); const double zmax = width + 2*spacing; Lattice lat(Cartesian(dw,0,0), Cartesian(0,dw,0), Cartesian(0,0,zmax)); GridDescription gd(lat, dx); Grid constraint(gd); constraint.Set(notinwall); f = constrain(constraint, f); Grid potential(gd); potential = pow(2,-5.0/2.0)*(reduced_density*constraint + 1e-4*reduced_density*VectorXd::Ones(gd.NxNyNz)); potential = -kT*potential.cwise().log(); const double approx_energy = fhs(kT, reduced_density*pow(2,-5.0/2.0))*dw*dw*width; const double precision = fabs(approx_energy*1e-11); printf("\tMinimizing to %g absolute precision from %g from %g...\n", precision, approx_energy, kT); fflush(stdout); Minimizer min = Precision(precision, PreconditionedConjugateGradient(f, gd, kT, &potential, QuadraticLineMinimizer)); took("Setting up the variables"); if (strcmp(name, "hard") != 0 && false) { printf("For now, SoftFluid doesn't work properly, so we're skipping the\n"); printf("minimization at temperature %g.\n", teff); } else { for (int i=0;min.improve_energy(false) && i<100;i++) { } } took("Doing the minimization"); min.print_info(); Grid density(gd, EffectivePotentialToDensity()(kT, gd, potential)); //printf("# per area is %g at filling fraction %g\n", density.sum()*gd.dvolume/dw/dw, eta); char *plotname = (char *)malloc(1024); sprintf(plotname, "papers/fuzzy-fmt/figs/walls%s-%06.4f-%04.2f.dat", name, teff, reduced_density); z_plot(plotname, Grid(gd, density*pow(2,5.0/2.0))); free(plotname); took("Plotting stuff"); printf("density %g gives ff %g for reduced density = %g and T = %g\n", density(0,0,gd.Nz/2), density(0,0,gd.Nz/2)*4*M_PI/3, reduced_density, teff); return density(0, 0, gd.Nz/2)*4*M_PI/3; // return bulk filling fraction }
bool ConjugateGradientType::improve_energy(bool verbose) { iter++; //printf("I am running ConjugateGradient::improve_energy\n"); const double E0 = energy(); if (E0 != E0) { // There is no point continuing, since we're starting with a NaN! // So we may as well quit here. if (verbose) { printf("The initial energy is a NaN, so I'm quitting early from ConjugateGradientType::improve_energy.\n"); f.print_summary("has nan:", E0); fflush(stdout); } return false; } double gdotd; { const VectorXd g = -grad(); // Let's immediately free the cached gradient stored internally! invalidate_cache(); // Note: my notation vaguely follows that of // [wikipedia](http://en.wikipedia.org/wiki/Nonlinear_conjugate_gradient_method). // I use the Polak-Ribiere method, with automatic direction reset. // Note that we could save some memory by using Fletcher-Reeves, and // it seems worth implementing that as an option for // memory-constrained problems (then we wouldn't need to store oldgrad). double beta = g.dot(g - oldgrad)/oldgradsqr; oldgrad = g; if (beta < 0 || beta != beta || oldgradsqr == 0) beta = 0; oldgradsqr = oldgrad.dot(oldgrad); direction = g + beta*direction; gdotd = oldgrad.dot(direction); if (gdotd < 0) { direction = oldgrad; // If our direction is uphill, reset to gradient. if (verbose) printf("reset to gradient...\n"); gdotd = oldgrad.dot(direction); } } Minimizer lm = linmin(f, gd, kT, x, direction, -gdotd, &step); for (int i=0; i<100 && lm.improve_energy(verbose); i++) { if (verbose) lm.print_info("\t"); } if (verbose) { //lm->print_info(); print_info(); printf("grad*dir/oldgrad*dir = %g\n", grad().dot(direction)/gdotd); } return (energy() < E0); }
bool PreconditionedConjugateGradientType::improve_energy(bool verbose) { iter++; //printf("I am running ConjugateGradient::improve_energy\n"); const double E0 = energy(); if (isnan(E0)) { // There is no point continuing, since we're starting with a NaN! // So we may as well quit here. if (verbose) { printf("The initial energy is a NaN, so I'm quitting early.\n"); fflush(stdout); } return false; } double beta; { // Note: my notation vaguely follows that of // [wikipedia](http://en.wikipedia.org/wiki/Nonlinear_conjugate_gradient_method). // I use the Polak-Ribiere method, with automatic direction reset. // Note that we could save some memory by using Fletcher-Reeves, and // it seems worth implementing that as an option for // memory-constrained problems (then we wouldn't need to store oldgrad). pgrad(); // compute pgrad first, since that computes both. beta = -pgrad().dot(-grad() - oldgrad)/oldgradsqr; oldgrad = -grad(); if (beta < 0 || beta != beta || oldgradsqr == 0) beta = 0; if (verbose) printf("beta = %g\n", beta); oldgradsqr = -pgrad().dot(oldgrad); direction = -pgrad() + beta*direction; // Let's immediately free the cached gradient stored internally! invalidate_cache(); } // free g and pg! const double gdotd = oldgrad.dot(direction); Minimizer lm = linmin(f, gd, kT, x, direction, -gdotd, &step); for (int i=0; i<100 && lm.improve_energy(verbose); i++) { if (verbose) lm.print_info("\t"); } if (verbose) { //lm->print_info(); print_info(); printf("grad*oldgrad = %g\n", grad().dot(direction)/gdotd); } return (energy() < E0 || beta != 0); }
double run_walls(double eta, const char *name, Functional fhs, double teff) { //printf("Filling fraction is %g with functional %s at temperature %g\n", eta, name, teff); //fflush(stdout); double kT = teff; if (kT == 0) kT = 1; Functional f = OfEffectivePotential(fhs); const double zmax = width + 2*spacing; Lattice lat(Cartesian(dw,0,0), Cartesian(0,dw,0), Cartesian(0,0,zmax)); GridDescription gd(lat, dx); Grid constraint(gd); constraint.Set(notinwall); f = constrain(constraint, f); // We reuse the potential, which should give us a better starting // guess on each calculation. static Grid *potential = 0; if (strcmp(name, "hard") == 0) { // start over for each potential delete potential; potential = 0; } if (!potential) { potential = new Grid(gd); *potential = (eta*constraint + 1e-4*eta*VectorXd::Ones(gd.NxNyNz))/(4*M_PI/3); *potential = -kT*potential->cwise().log(); } // FIXME below I use the HS energy because of issues with the actual // functional. const double approx_energy = fhs(kT, eta/(4*M_PI/3))*dw*dw*width; const double precision = fabs(approx_energy*1e-5); printf("\tMinimizing to %g absolute precision from %g from %g...\n", precision, approx_energy, kT); fflush(stdout); Minimizer min = Precision(precision, PreconditionedConjugateGradient(f, gd, kT, potential, QuadraticLineMinimizer)); took("Setting up the variables"); for (int i=0;min.improve_energy(false) && i<100;i++) { } took("Doing the minimization"); min.print_info(); Grid density(gd, EffectivePotentialToDensity()(kT, gd, *potential)); //printf("# per area is %g at filling fraction %g\n", density.sum()*gd.dvolume/dw/dw, eta); char *plotname = (char *)malloc(1024); sprintf(plotname, "papers/fuzzy-fmt/figs/walls%s-%06.4f-%04.2f.dat", name, teff, eta); z_plot(plotname, Grid(gd, 4*M_PI*density/3)); free(plotname); { GridDescription gdj = density.description(); double sep = gdj.dz*gdj.Lat.a3().norm(); int div = gdj.Nz; int mid = int (div/2.0); double Ntot_per_A = 0; double mydist = 0; for (int j=0; j<mid; j++){ Ntot_per_A += density(0,0,j)*sep; mydist += sep; } double Extra_per_A = Ntot_per_A - eta/(4.0/3.0*M_PI)*width/2; FILE *fout = fopen("papers/fuzzy-fmt/figs/wallsfillingfracInfo.txt", "a"); fprintf(fout, "walls%s-%04.2f.dat - If you want to match the bulk filling fraction of figs/walls%s-%04.2f.dat, than the number of extra spheres per area to add is %04.10f. So you'll want to multiply %04.2f by your cavity volume and divide by (4/3)pi. Then add %04.10f times the Area of your cavity to this number\n", name, eta, name, eta, Extra_per_A, eta, Extra_per_A); int wallslen = 20; double Extra_spheres = (eta*wallslen*wallslen*wallslen/(4*M_PI/3) + Extra_per_A*wallslen*wallslen); fprintf (fout, "For filling fraction %04.02f and walls of length %d you'll want to use %.0f spheres.\n\n", eta, wallslen, Extra_spheres); fclose(fout); } { //double peak = peak_memory()/1024.0/1024; //double current = current_memory()/1024.0/1024; //printf("Peak memory use is %g M (current is %g M)\n", peak, current); } took("Plotting stuff"); printf("density %g gives ff %g for eta = %g and T = %g\n", density(0,0,gd.Nz/2), density(0,0,gd.Nz/2)*4*M_PI/3, eta, teff); return density(0, 0, gd.Nz/2)*4*M_PI/3; // return bulk filling fraction }