bool ExponentialMedium::sampleDistance(PathSampleGenerator &sampler, const Ray &ray, MediumState &state, MediumSample &sample) const { if (state.bounce > _maxBounce) return false; float x = _falloffScale*(ray.pos() - _unitPoint).dot(_unitFalloffDirection); float dx = _falloffScale*ray.dir().dot(_unitFalloffDirection); float maxT = ray.farT(); if (_absorptionOnly) { if (maxT == Ray::infinity() && dx <= 0.0f) return false; sample.t = maxT; sample.weight = std::exp(-_sigmaT*densityIntegral(x, dx, ray.farT())); sample.pdf = 1.0f; sample.exited = true; } else { int component = sampler.nextDiscrete(3); float sigmaTc = _sigmaT[component]; float xi = 1.0f - sampler.next1D(); float logXi = std::log(xi); float t = inverseOpticalDepth(x, dx, sigmaTc, logXi); sample.t = min(t, maxT); sample.weight = std::exp(-_sigmaT*densityIntegral(x, dx, sample.t)); sample.exited = (t >= maxT); if (sample.exited) { sample.pdf = sample.weight.avg(); } else { float rho = density(x, dx, sample.t); sample.pdf = (rho*_sigmaT*sample.weight).avg(); sample.weight *= rho*_sigmaS; } sample.weight /= sample.pdf; state.advance(); } sample.p = ray.pos() + sample.t*ray.dir(); sample.phase = _phaseFunction.get(); return true; }
int main() { FILE *planets,*results; int i; struct planet ss[9]; if((planets=fopen("planets.txt","r"))==NULL) { printf("Error loading planets.txt\n"); printf("Please press enter to continue"); getchar(); return(0); } if((results=fopen("results.txt","w"))==NULL) { printf("Error loading results.txt\n"); printf("Please press enter to continue"); getchar(); return(0); } for(i=0;i<9;i++) { fscanf(planets,"%s %lf %lf %lf",&ss[i].name,&ss[i].distance,&ss[i].mass,&ss[i].radius); } printf("Name Distance Mass Radius\n"); for(i=0;i<9;i++) { printf("%10s %g %g %g\n",ss[i].name,ss[i].distance,ss[i].mass,ss[i].radius); } for(i=0;i<9;i++) { fprintf(results,"%s %g %g\n",ss[i].name,ylength(&ss[i]),density(&ss[i])); } printf("Please press enter to continue"); getchar(); return(0); }
doublereal WaterPropsIAPWS::psat(doublereal temperature, int waterState) { doublereal densLiq = -1.0, densGas = -1.0, delGRT = 0.0; doublereal dp, pcorr; if (temperature >= T_c) { densGas = density(temperature, P_c, WATER_SUPERCRIT); setState_TR(temperature, densGas); return P_c; } doublereal p = psat_est(temperature); bool conv = false; for (int i = 0; i < 30; i++) { if (method == 1) { corr(temperature, p, densLiq, densGas, delGRT); doublereal delV = M_water * (1.0/densLiq - 1.0/densGas); dp = - delGRT * Rgas * temperature / delV; } else { corr1(temperature, p, densLiq, densGas, pcorr); dp = pcorr - p; } p += dp; if ((method == 1) && delGRT < 1.0E-8) { conv = true; break; } else { if (fabs(dp/p) < 1.0E-9) { conv = true; break; } } } // Put the fluid in the desired end condition if (waterState == WATER_LIQUID) { setState_TR(temperature, densLiq); } else if (waterState == WATER_GAS) { setState_TR(temperature, densGas); } else { throw Cantera::CanteraError("WaterPropsIAPWS::psat", "unknown water state input: " + Cantera::int2str(waterState)); } return p; }
bool AtmosphericMedium::sampleDistance(PathSampleGenerator &sampler, const Ray &ray, MediumState &state, MediumSample &sample) const { if (state.bounce > _maxBounce) return false; Vec3f p = (ray.pos() - _center); float t0 = p.dot(ray.dir()); float h = (p - t0*ray.dir()).length(); float maxT = ray.farT() + t0; if (_absorptionOnly) { sample.t = ray.farT(); sample.weight = std::exp(-_sigmaT*densityIntegral(h, t0, maxT)); sample.pdf = 1.0f; sample.exited = true; } else { int component = sampler.nextDiscrete(3); float sigmaTc = _sigmaT[component]; float xi = 1.0f - sampler.next1D(); float t = inverseOpticalDepth(h, t0, sigmaTc, xi); sample.t = min(t, maxT); sample.weight = std::exp(-_sigmaT*densityIntegral(h, t0, sample.t)); sample.exited = (t >= maxT); if (sample.exited) { sample.pdf = sample.weight.avg(); } else { float rho = density(h, sample.t); sample.pdf = (rho*_sigmaT*sample.weight).avg(); sample.weight *= rho*_sigmaS; } sample.weight /= sample.pdf; sample.t -= t0; state.advance(); } sample.p = ray.pos() + sample.t*ray.dir(); sample.phase = _phaseFunction.get(); return true; }
double l1Distance(const Kernel &rhs, long nr = 1000) { double a,b,x; a = min_ - 4.0*width_; b = rhs.min_ - 4.0*rhs.width_; double dm = (a < b ? a : b); a = max_ + 4.0*width_; b = rhs.max_ + 4.0*rhs.width_; double dM = (a > b ? a : b); double dD=(dM-dm)/(nr-1); const function::Linear<double> lt(0, dm, nr-1, dM); dM = 0.0; for (long i=0; i<nr; ++i) { x = lt(i); dM += fabs(density(x)-rhs.density(x))*dD; } return dM; }
int MixtureFugacityTP::phaseState(bool checkState) const { int state = iState_; if (checkState) { double t = temperature(); double tcrit = critTemperature(); double rhocrit = critDensity(); if (t >= tcrit) { return FLUID_SUPERCRIT; } double tmid = tcrit - 100.; if (tmid < 0.0) { tmid = tcrit / 2.0; } double pp = psatEst(tmid); double mmw = meanMolecularWeight(); double molVolLiqTmid = liquidVolEst(tmid, pp); double molVolGasTmid = GasConstant * tmid / (pp); double densLiqTmid = mmw / molVolLiqTmid; double densGasTmid = mmw / molVolGasTmid; double densMidTmid = 0.5 * (densLiqTmid + densGasTmid); doublereal rhoMid = rhocrit + (t - tcrit) * (rhocrit - densMidTmid) / (tcrit - tmid); double rho = density(); int iStateGuess = FLUID_LIQUID_0; if (rho < rhoMid) { iStateGuess = FLUID_GAS; } double molarVol = mmw / rho; double presCalc; double dpdv = dpdVCalc(t, molarVol, presCalc); if (dpdv < 0.0) { state = iStateGuess; } else { state = FLUID_UNSTABLE; } } return state; }
void XZHydrostatic_Omega<EvalT, Traits>:: evaluateFields(typename Traits::EvalData workset) { #ifndef ALBANY_KOKKOS_UNDER_DEVELOPMENT for (int cell=0; cell < workset.numCells; ++cell) { for (int qp=0; qp < numQPs; ++qp) { for (int level=0; level < numLevels; ++level) { ScalarT sum = -0.5*divpivelx(cell,qp,level) * E.delta(level); for (int j=0; j<level; ++j) sum -= divpivelx(cell,qp,j) * E.delta(j); for (int dim=0; dim < numDims; ++dim) sum += Velocity(cell,qp,level,dim)*gradp(cell,qp,level,dim); omega(cell,qp,level) = sum/(Cpstar(cell,qp,level)*density(cell,qp,level)); } } } #else Kokkos::parallel_for(XZHydrostatic_Omega_Policy(0,workset.numCells),*this); cudaCheckError(); #endif }
result_type result(Args const &args) const { if (this->is_dirty) { this->is_dirty = false; std::size_t cnt = count(args); range_type histogram = density(args); typename range_type::iterator it = histogram.begin(); while (this->sum < 0.5 * cnt) { this->sum += it->second * cnt; ++it; } --it; float_type over = numeric::average(this->sum - 0.5 * cnt, it->second * cnt); this->median = it->first * over + (it + 1)->first * (1. - over); } return this->median; }
double N_from_mu(Minimizer *min, Grid *potential, const Grid &constraint, double mu) { Functional f = constrain(constraint, OfEffectivePotential(HS + IdealGas() + ChemicalPotential(mu))); double Nnow = 0; min->minimize(f, potential->description()); for (int i=0;i<numiters && min->improve_energy(false);i++) { Grid density(potential->description(), EffectivePotentialToDensity()(1, potential->description(), *potential)); Nnow = density.sum()*potential->description().dvolume; printf("Nnow is %g vs %g\n", Nnow, N); fflush(stdout); density.epsNativeSlice("papers/contact/figs/box.eps", Cartesian(0,ymax+2,0), Cartesian(0,0,zmax+2), Cartesian(0,-ymax/2-1,-zmax/2-1)); density.epsNativeSlice("papers/contact/figs/box-diagonal.eps", Cartesian(xmax+2,0,zmax+2), Cartesian(0,ymax+2,0), Cartesian(-xmax/2-1,-ymax/2-1,-zmax/2-1)); //sleep(3); } return Nnow; }
doublereal SingleSpeciesTP::cv_mole() const { /* * For single species, we go directory to the general Cp - Cv relation * * Cp = Cv + alpha**2 * V * T / beta * * where * alpha = volume thermal expansion coefficient * beta = isothermal compressibility */ doublereal cvbar = cp_mole(); doublereal alpha = thermalExpansionCoeff(); doublereal beta = isothermalCompressibility(); doublereal V = molecularWeight(0)/density(); doublereal T = temperature(); if (beta != 0.0) { cvbar -= alpha * alpha * V * T / beta; } return cvbar; }
void WaterSSTP::getEntropy_R_ref(doublereal* sr) const { doublereal p = pressure(); double T = temperature(); double dens = density(); int waterState = WATER_GAS; double rc = m_sub.Rhocrit(); if (dens > rc) { waterState = WATER_LIQUID; } doublereal dd = m_sub.density(T, OneAtm, waterState, dens); if (dd <= 0.0) { throw CanteraError("setPressure", "error"); } m_sub.setState_TR(T, dd); doublereal s = m_sub.entropy(); *sr = (s + SW_Offset)/ GasConstant; dd = m_sub.density(T, p, waterState, dens); }
void IonFlow::electricFieldMethod(const double* x, size_t j0, size_t j1) { for (size_t j = j0; j < j1; j++) { double wtm = m_wtm[j]; double rho = density(j); double dz = z(j+1) - z(j); // mixture-average diffusion double sum = 0.0; for (size_t k = 0; k < m_nsp; k++) { m_flux(k,j) = m_wt[k]*(rho*m_diff[k+m_nsp*j]/wtm); m_flux(k,j) *= (X(x,k,j) - X(x,k,j+1))/dz; sum -= m_flux(k,j); } // ambipolar diffusion double E_ambi = E(x,j); for (size_t k : m_kCharge) { double Yav = 0.5 * (Y(x,k,j) + Y(x,k,j+1)); double drift = rho * Yav * E_ambi * m_speciesCharge[k] * m_mobility[k+m_nsp*j]; m_flux(k,j) += drift; } // correction flux double sum_flux = 0.0; for (size_t k = 0; k < m_nsp; k++) { sum_flux -= m_flux(k,j); // total net flux } double sum_ion = 0.0; for (size_t k : m_kCharge) { sum_ion += Y(x,k,j); } // The portion of correction for ions is taken off for (size_t k : m_kNeutral) { m_flux(k,j) += Y(x,k,j) / (1-sum_ion) * sum_flux; } } }
// Calculate the modes of the density for the distribution // and their associated errors double vpScale::MeanShift(vpColVector &error) { int n = error.getRows()/dimension; vpColVector density(n); vpColVector density_gradient(n); vpColVector mean_shift(n); int increment=1; // choose smallest error as start point int i=0; while(error[i]<0 && error[i]<error[i+1]) i++; // Do mean shift until no shift while(increment >= 1 && i<n) { increment=0; density[i] = KernelDensity(error, i); density_gradient[i] = KernelDensityGradient(error, i); mean_shift[i]=vpMath::sqr(bandwidth)*density_gradient[i]/((dimension+2)*density[i]); double tmp_shift = mean_shift[i]; // Do mean shift while(tmp_shift>0 && tmp_shift>error[i]-error[i+1]) { i++; increment++; tmp_shift-=(error[i]-error[i-1]); } } return error[i]; }
ShardDefinition::ShardDefinition(std::istream& input) { CsvTsv::InputColumn<size_t> minPostings("MinPostings", "Minimum number of postings for any document in shard."); CsvTsv::InputColumn<double> density("Density", "Target density for RowTables in the shard."); CsvTsv::CsvTableParser parser(input); CsvTsv::TableReader reader(parser); reader.DefineColumn(minPostings); reader.DefineColumn(density); reader.ReadPrologue(); while (!reader.AtEOF()) { reader.ReadDataRow(); AddShard(minPostings, density); } reader.ReadEpilogue(); }
Real SodProblem::valueExact(Real t, const Point& p, int eq) { switch (eq) { case 0: return density(t, p); break; case 1: return momentumX(t, p); break; case 2: return momentumY(t, p); break; case 3: return momentumZ(t, p); case 4: return energyTotal(t, p); break; default: return 0.0; mooseError("不可用的分量" << eq); break; } }
void XZHydrostatic_GeoPotential<EvalT, Traits>:: evaluateFields(typename Traits::EvalData workset) { const Eta<EvalT> &E = Eta<EvalT>::self(); ScalarT sum; for (int cell=0; cell < workset.numCells; ++cell) { for (int node=0; node < numNodes; ++node) { for (int level=0; level < numLevels; ++level) { sum = PhiSurf(cell,node) + 0.5 * Pi(cell,node,level) * E.delta(level) / density(cell,node,level); for (int j=level+1; j < numLevels; ++j) sum += Pi(cell,node,j) * E.delta(j) / density(cell,node,j); Phi(cell,node,level) = sum; //std::cout <<"Inside GeoP, cell, node, PhiSurf(cell,node)="<<cell<< //", "<<node<<", "<<PhiSurf(cell,node) <<std::endl; } } } }
void ShardDefinition::Write(std::ostream& output) const { CsvTsv::OutputColumn<size_t> minPostings("MinPostings", "Minimum number of postings for any document in shard."); CsvTsv::OutputColumn<double> density("Density", "Target density for RowTables in the shard."); CsvTsv::CsvTableFormatter formatter(output); CsvTsv::TableWriter writer(formatter); writer.DefineColumn(minPostings); writer.DefineColumn(density); writer.WritePrologue(); for (unsigned i = 0; i < m_shards.size(); ++i) { minPostings = m_shards[i].first; density = m_shards[i].second; writer.WriteDataRow(); } writer.WriteEpilogue(); }
Real RichardsDensityConstBulk::d2density(Real p) const { return density(p)/_bulk/_bulk; }
/* * Format a summary of the mixture state for output. */ void MolalityVPSSTP::reportCSV(std::ofstream& csvFile) const { csvFile.precision(3); int tabS = 15; int tabM = 30; int tabL = 40; try { if (name() != "") { csvFile << "\n"+name()+"\n\n"; } csvFile << setw(tabL) << "temperature (K) =" << setw(tabS) << temperature() << endl; csvFile << setw(tabL) << "pressure (Pa) =" << setw(tabS) << pressure() << endl; csvFile << setw(tabL) << "density (kg/m^3) =" << setw(tabS) << density() << endl; csvFile << setw(tabL) << "mean mol. weight (amu) =" << setw(tabS) << meanMolecularWeight() << endl; csvFile << setw(tabL) << "potential (V) =" << setw(tabS) << electricPotential() << endl; csvFile << endl; csvFile << setw(tabL) << "enthalpy (J/kg) = " << setw(tabS) << enthalpy_mass() << setw(tabL) << "enthalpy (J/kmol) = " << setw(tabS) << enthalpy_mole() << endl; csvFile << setw(tabL) << "internal E (J/kg) = " << setw(tabS) << intEnergy_mass() << setw(tabL) << "internal E (J/kmol) = " << setw(tabS) << intEnergy_mole() << endl; csvFile << setw(tabL) << "entropy (J/kg) = " << setw(tabS) << entropy_mass() << setw(tabL) << "entropy (J/kmol) = " << setw(tabS) << entropy_mole() << endl; csvFile << setw(tabL) << "Gibbs (J/kg) = " << setw(tabS) << gibbs_mass() << setw(tabL) << "Gibbs (J/kmol) = " << setw(tabS) << gibbs_mole() << endl; csvFile << setw(tabL) << "heat capacity c_p (J/K/kg) = " << setw(tabS) << cp_mass() << setw(tabL) << "heat capacity c_p (J/K/kmol) = " << setw(tabS) << cp_mole() << endl; csvFile << setw(tabL) << "heat capacity c_v (J/K/kg) = " << setw(tabS) << cv_mass() << setw(tabL) << "heat capacity c_v (J/K/kmol) = " << setw(tabS) << cv_mole() << endl; csvFile.precision(8); vector<std::string> pNames; vector<vector_fp> data; vector_fp temp(nSpecies()); getMoleFractions(&temp[0]); pNames.push_back("X"); data.push_back(temp); try { getMolalities(&temp[0]); pNames.push_back("Molal"); data.push_back(temp); } catch (CanteraError& err) { err.save(); } try { getChemPotentials(&temp[0]); pNames.push_back("Chem. Pot. (J/kmol)"); data.push_back(temp); } catch (CanteraError& err) { err.save(); } try { getStandardChemPotentials(&temp[0]); pNames.push_back("Chem. Pot. SS (J/kmol)"); data.push_back(temp); } catch (CanteraError& err) { err.save(); } try { getMolalityActivityCoefficients(&temp[0]); pNames.push_back("Molal Act. Coeff."); data.push_back(temp); } catch (CanteraError& err) { err.save(); } try { getActivities(&temp[0]); pNames.push_back("Molal Activity"); data.push_back(temp); size_t iHp = speciesIndex("H+"); if (iHp != npos) { double pH = -log(temp[iHp]) / log(10.0); csvFile << setw(tabL) << "pH = " << setw(tabS) << pH << endl; } } catch (CanteraError& err) { err.save(); } try { getPartialMolarEnthalpies(&temp[0]); pNames.push_back("Part. Mol Enthalpy (J/kmol)"); data.push_back(temp); } catch (CanteraError& err) { err.save(); } try { getPartialMolarEntropies(&temp[0]); pNames.push_back("Part. Mol. Entropy (J/K/kmol)"); data.push_back(temp); } catch (CanteraError& err) { err.save(); } try { getPartialMolarIntEnergies(&temp[0]); pNames.push_back("Part. Mol. Energy (J/kmol)"); data.push_back(temp); } catch (CanteraError& err) { err.save(); } try { getPartialMolarCp(&temp[0]); pNames.push_back("Part. Mol. Cp (J/K/kmol"); data.push_back(temp); } catch (CanteraError& err) { err.save(); } try { getPartialMolarVolumes(&temp[0]); pNames.push_back("Part. Mol. Cv (J/K/kmol)"); data.push_back(temp); } catch (CanteraError& err) { err.save(); } csvFile << endl << setw(tabS) << "Species,"; for (size_t i = 0; i < pNames.size(); i++) { csvFile << setw(tabM) << pNames[i] << ","; } csvFile << endl; /* csvFile.fill('-'); csvFile << setw(tabS+(tabM+1)*pNames.size()) << "-\n"; csvFile.fill(' '); */ for (size_t k = 0; k < nSpecies(); k++) { csvFile << setw(tabS) << speciesName(k) + ","; if (data[0][k] > SmallNumber) { for (size_t i = 0; i < pNames.size(); i++) { csvFile << setw(tabM) << data[i][k] << ","; } csvFile << endl; } else { for (size_t i = 0; i < pNames.size(); i++) { csvFile << setw(tabM) << 0 << ","; } csvFile << endl; } } } catch (CanteraError& err) { err.save(); } }
/** * Format a summary of the mixture state for output. */ std::string MolalityVPSSTP::report(bool show_thermo) const { char p[800]; string s = ""; try { if (name() != "") { sprintf(p, " \n %s:\n", name().c_str()); s += p; } sprintf(p, " \n temperature %12.6g K\n", temperature()); s += p; sprintf(p, " pressure %12.6g Pa\n", pressure()); s += p; sprintf(p, " density %12.6g kg/m^3\n", density()); s += p; sprintf(p, " mean mol. weight %12.6g amu\n", meanMolecularWeight()); s += p; doublereal phi = electricPotential(); sprintf(p, " potential %12.6g V\n", phi); s += p; size_t kk = nSpecies(); vector_fp x(kk); vector_fp molal(kk); vector_fp mu(kk); vector_fp muss(kk); vector_fp acMolal(kk); vector_fp actMolal(kk); getMoleFractions(&x[0]); getMolalities(&molal[0]); getChemPotentials(&mu[0]); getStandardChemPotentials(&muss[0]); getMolalityActivityCoefficients(&acMolal[0]); getActivities(&actMolal[0]); size_t iHp = speciesIndex("H+"); if (iHp != npos) { double pH = -log(actMolal[iHp]) / log(10.0); sprintf(p, " pH %12.4g \n", pH); s += p; } if (show_thermo) { sprintf(p, " \n"); s += p; sprintf(p, " 1 kg 1 kmol\n"); s += p; sprintf(p, " ----------- ------------\n"); s += p; sprintf(p, " enthalpy %12.6g %12.4g J\n", enthalpy_mass(), enthalpy_mole()); s += p; sprintf(p, " internal energy %12.6g %12.4g J\n", intEnergy_mass(), intEnergy_mole()); s += p; sprintf(p, " entropy %12.6g %12.4g J/K\n", entropy_mass(), entropy_mole()); s += p; sprintf(p, " Gibbs function %12.6g %12.4g J\n", gibbs_mass(), gibbs_mole()); s += p; sprintf(p, " heat capacity c_p %12.6g %12.4g J/K\n", cp_mass(), cp_mole()); s += p; try { sprintf(p, " heat capacity c_v %12.6g %12.4g J/K\n", cv_mass(), cv_mole()); s += p; } catch (CanteraError& err) { err.save(); sprintf(p, " heat capacity c_v <not implemented> \n"); s += p; } } sprintf(p, " \n"); s += p; if (show_thermo) { sprintf(p, " X " " Molalities Chem.Pot. ChemPotSS ActCoeffMolal\n"); s += p; sprintf(p, " " " (J/kmol) (J/kmol) \n"); s += p; sprintf(p, " ------------- " " ------------ ------------ ------------ ------------\n"); s += p; for (size_t k = 0; k < kk; k++) { if (x[k] > SmallNumber) { sprintf(p, "%18s %12.6g %12.6g %12.6g %12.6g %12.6g\n", speciesName(k).c_str(), x[k], molal[k], mu[k], muss[k], acMolal[k]); } else { sprintf(p, "%18s %12.6g %12.6g N/A %12.6g %12.6g \n", speciesName(k).c_str(), x[k], molal[k], muss[k], acMolal[k]); } s += p; } } else { sprintf(p, " X" "Molalities\n"); s += p; sprintf(p, " -------------" " ------------\n"); s += p; for (size_t k = 0; k < kk; k++) { sprintf(p, "%18s %12.6g %12.6g\n", speciesName(k).c_str(), x[k], molal[k]); s += p; } } } catch (CanteraError& err) { err.save(); } return s; }
void SingleSpeciesTP::getStandardVolumes(doublereal* vbar) const { vbar[0] = molecularWeight(0) / density(); }
void SingleSpeciesTP::getPartialMolarVolumes(doublereal* vbar) const { vbar[0] = molecularWeight(0) / density(); }
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 }
doublereal Phase::molarDensity() const { return density()/meanMolecularWeight(); }
void TGaussianMixture::expectation_maximization(const double *x, const double *w_n, unsigned int N, unsigned int iterations) { double *p_kn = new double[nclusters*N]; // Determine total weight double sum_w = 0.; for(unsigned int n=0; n<N; n++) { sum_w += w_n[n]; } // Choose means randomly from given points unsigned int *index = new unsigned int[nclusters]; bool repeated_index; for(unsigned int k=0; k<nclusters; k++) { repeated_index = true; while(repeated_index) { index[k] = gsl_rng_uniform_int(r, N); repeated_index = false; for(unsigned int i=0; i<k; i++) { if(index[i] == index[k]) { repeated_index = true; break; } } } for(unsigned int i=0; i<ndim; i++) { mu[k*ndim + i] = x[index[k]*ndim + i]; } } delete[] index; // Assign points to nearest cluster center double sum, tmp, min_dist; unsigned int nearest_cluster; for(unsigned int n=0; n<N; n++) { // Find nearest cluster center min_dist = inf_replacement; for(unsigned int k=0; k<nclusters; k++) { sum = 0.; for(unsigned int i=0; i<ndim; i++) { tmp = x[n*ndim + i] - mu[k*ndim + i]; sum += tmp*tmp; } if(sum < min_dist) { min_dist = sum; nearest_cluster = k; } } for(unsigned int k=0; k<nclusters; k++) { p_kn[n*nclusters + k] = 0.; } p_kn[n*nclusters + nearest_cluster] = 1.; w[nearest_cluster] += w_n[n]; } sum = 0.; for(unsigned int k=0; k<nclusters; k++) { sum += w[k]; } for(unsigned int k=0; k<nclusters; k++) { w[k] /= sum; } // Iterate for(unsigned int count=0; count<=iterations; count++) { // Assign probability for each point to be in each cluster if(count != 0) { density(x, N, p_kn); for(unsigned int n=0; n<N; n++) { // Normalize probability for point to be in some cluster to unity sum = 0.; for(unsigned int k=0; k<nclusters; k++) { sum += p_kn[n*nclusters + k]; } for(unsigned int k=0; k<nclusters; k++) { p_kn[n*nclusters + k] /= sum; } } } // Determine cluster properties from members if(count != 0) { for(unsigned int k=0; k<nclusters; k++) { // Strength of Gaussian w[k] = 0.; for(unsigned int n=0; n<N; n++) { w[k] += w_n[n] * p_kn[n*nclusters + k]; } w[k] /= sum_w; } } for(unsigned int k=0; k<nclusters; k++) { // Mean of Gaussian for(unsigned int j=0; j<ndim; j++) { mu[k*ndim + j] = 0.; for(unsigned int n=0; n<N; n++) { mu[k*ndim + j] += w_n[n] * p_kn[n*nclusters + k] * x[n*ndim + j] / w[k]; } mu[k*ndim + j] /= sum_w; } } for(unsigned int k=0; k<nclusters; k++) { // Covariance for(unsigned int i=0; i<ndim; i++) { for(unsigned int j=i; j<ndim; j++) { sum = 0.; tmp = 0.; for(unsigned int n=0; n<N; n++) { sum += p_kn[n*nclusters + k] * w_n[n] * (x[n*ndim + i] - mu[k*ndim + i]) * (x[n*ndim + j] - mu[k*ndim + j]); tmp += w_n[n] * p_kn[n*nclusters + k]; } sum /= tmp; if(i == j) { gsl_matrix_set(cov[k], i, j, 1.01*sum + 0.01); } else { gsl_matrix_set(cov[k], i, j, sum); gsl_matrix_set(cov[k], j, i, sum); } } } } invert_covariance(); /*std::cout << "Iteration #" << count << std::endl; std::cout << "=======================================" << std::endl; for(unsigned int k=0; k<nclusters; k++) { std::cout << "Cluster #" << k+1 << std::endl; std::cout << "w = " << w[k] << std::endl; std::cout << "mu ="; for(unsigned int i=0; i<ndim; i++) { std::cout << " " << mu[k*ndim + i]; } std::cout << std::endl; std::cout << "Covariance:" << std::endl; for(unsigned int i=0; i<ndim; i++) { for(unsigned int j=0; j<ndim; j++) { std::cout << " " << gsl_matrix_get(cov[k], i, j); } std::cout << std::endl; } std::cout << std::endl; }*/ //w_k = np.einsum('n,kn->k', w, p_kn) / N //mu = np.einsum('k,n,kn,nj->kj', 1./w_k, w, p_kn, x) / N //for j in xrange(k): // Delta[j] = x - mu[j] //cov = np.einsum('kn,n,kni,knj->kij', p_kn, w, Delta, Delta) //cov = np.einsum('kij,k->kij', cov, 1./np.sum(p_kn, axis=1)) } // Find the vector sqrt_cov s.t. sqrt_cov sqrt_cov^T = cov. for(unsigned int k=0; k<nclusters; k++) { sqrt_matrix(cov[k], sqrt_cov[k], esv, eival, eivec, sqrt_eival); } // Cleanup delete[] p_kn; }
void WaterSSTP::setTemperature(const doublereal temp) { Phase::setTemperature(temp); m_sub.setState_TR(temp, density()); }
void Phase::saveState(size_t lenstate, doublereal* state) const { state[0] = temperature(); state[1] = density(); getMassFractions(state + 2); }
std::string ThermoPhase::report(bool show_thermo, doublereal threshold) const { fmt::MemoryWriter b; try { if (name() != "") { b.write("\n {}:\n", name()); } b.write("\n"); b.write(" temperature {:12.6g} K\n", temperature()); b.write(" pressure {:12.6g} Pa\n", pressure()); b.write(" density {:12.6g} kg/m^3\n", density()); b.write(" mean mol. weight {:12.6g} amu\n", meanMolecularWeight()); doublereal phi = electricPotential(); if (phi != 0.0) { b.write(" potential {:12.6g} V\n", phi); } if (show_thermo) { b.write("\n"); b.write(" 1 kg 1 kmol\n"); b.write(" ----------- ------------\n"); b.write(" enthalpy {:12.5g} {:12.4g} J\n", enthalpy_mass(), enthalpy_mole()); b.write(" internal energy {:12.5g} {:12.4g} J\n", intEnergy_mass(), intEnergy_mole()); b.write(" entropy {:12.5g} {:12.4g} J/K\n", entropy_mass(), entropy_mole()); b.write(" Gibbs function {:12.5g} {:12.4g} J\n", gibbs_mass(), gibbs_mole()); b.write(" heat capacity c_p {:12.5g} {:12.4g} J/K\n", cp_mass(), cp_mole()); try { b.write(" heat capacity c_v {:12.5g} {:12.4g} J/K\n", cv_mass(), cv_mole()); } catch (NotImplementedError&) { b.write(" heat capacity c_v <not implemented> \n"); } } vector_fp x(m_kk); vector_fp y(m_kk); vector_fp mu(m_kk); getMoleFractions(&x[0]); getMassFractions(&y[0]); getChemPotentials(&mu[0]); int nMinor = 0; doublereal xMinor = 0.0; doublereal yMinor = 0.0; b.write("\n"); if (show_thermo) { b.write(" X " " Y Chem. Pot. / RT\n"); b.write(" ------------- " "------------ ------------\n"); for (size_t k = 0; k < m_kk; k++) { if (abs(x[k]) >= threshold) { if (abs(x[k]) > SmallNumber) { b.write("{:>18s} {:12.6g} {:12.6g} {:12.6g}\n", speciesName(k), x[k], y[k], mu[k]/RT()); } else { b.write("{:>18s} {:12.6g} {:12.6g}\n", speciesName(k), x[k], y[k]); } } else { nMinor++; xMinor += x[k]; yMinor += y[k]; } } } else { b.write(" X Y\n"); b.write(" ------------- ------------\n"); for (size_t k = 0; k < m_kk; k++) { if (abs(x[k]) >= threshold) { b.write("{:>18s} {:12.6g} {:12.6g}\n", speciesName(k), x[k], y[k]); } else { nMinor++; xMinor += x[k]; yMinor += y[k]; } } } if (nMinor) { b.write(" [{:+5d} minor] {:12.6g} {:12.6g}\n", nMinor, xMinor, yMinor); } } catch (CanteraError& err) { return b.str() + err.what(); } return b.str(); }
void MetalSHEelectrons::getParameters(int& n, doublereal* const c) const { n = 1; c[0] = density(); }
void run_with_eta(double eta, const char *name, Functional fhs) { // Generates a data file for the pair distribution function, for filling fraction eta // and distance of first sphere from wall of z0. Data saved in a table such that the // columns are x values and rows are z1 values. printf("Now starting run_with_eta with eta = %g name = %s\n", eta, name); Functional f = OfEffectivePotential(fhs + IdealGas()); double mu = find_chemical_potential(f, 1, eta/(4*M_PI/3)); f = OfEffectivePotential(fhs + IdealGas() + ChemicalPotential(mu)); Lattice lat(Cartesian(width,0,0), Cartesian(0,width,0), Cartesian(0,0,width)); GridDescription gd(lat, dx); Grid potential(gd); Grid constraint(gd); constraint.Set(notinsphere); f = constrain(constraint, f); potential = (eta*constraint + 1e-4*eta*VectorXd::Ones(gd.NxNyNz))/(4*M_PI/3); potential = -potential.cwise().log(); const double approx_energy = (fhs + IdealGas() + ChemicalPotential(mu))(1, eta/(4*M_PI/3))*uipow(width,3); const double precision = fabs(approx_energy*1e-10); //printf("Minimizing to %g absolute precision...\n", precision); { // Put mimizer in block so as to free it when we finish minimizing to save memory. Minimizer min = Precision(precision, PreconditionedConjugateGradient(f, gd, 1, &potential, QuadraticLineMinimizer)); for (int i=0;min.improve_energy(true) && i<100;i++) { 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); fflush(stdout); } took("Doing the minimization"); } Grid density(gd, EffectivePotentialToDensity()(1, gd, potential)); Grid gsigma(gd, gSigmaA(1.0)(1, gd, density)); Grid nA(gd, ShellConvolve(2)(1, density)/(4*M_PI*4)); Grid n3(gd, StepConvolve(1)(1, density)); Grid nbar_sokolowski(gd, StepConvolve(1.6)(1, density)); nbar_sokolowski /= (4.0/3.0*M_PI*ipow(1.6, 3)); // Create the walls directory if it doesn't exist. if (mkdir("papers/pair-correlation/figs/walls", 0777) != 0 && errno != EEXIST) { // We failed to create the directory, and it doesn't exist. printf("Failed to create papers/pair-correlation/figs/walls: %s", strerror(errno)); exit(1); // fail immediately with error code } // here you choose the values of z0 to use // dx is the resolution at which we compute the density. char *plotname = new char[4096]; for (double z0 = 2.1; z0 < 4.5; z0 += 2.1) { // For each z0, we now pick one of our methods for computing the // pair distribution function: for (int version = 0; version < numplots; version++) { sprintf(plotname, "papers/pair-correlation/figs/triplet%s-%s-%04.2f-%1.2f.dat", name, fun[version], eta, z0); FILE *out = fopen(plotname,"w"); FILE *xfile = fopen("papers/pair-correlation/figs/triplet-x.dat","w"); FILE *zfile = fopen("papers/pair-correlation/figs/triplet-z.dat","w"); // the +1 for z0 and z1 are to shift the plot over, so that a sphere touching the wall // is at z = 0, to match with the monte carlo data const Cartesian r0(0,0,z0); for (double x = 0; x < 4; x += dx) { for (double z1 = -4; z1 <= 9; z1 += dx) { const Cartesian r1(x,0,z1); double g2 = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out, "%g\t", g3); fprintf(xfile, "%g\t", x); fprintf(zfile, "%g\t", z1); } fprintf(out, "\n"); fprintf(xfile, "\n"); fprintf(zfile, "\n"); } fclose(out); fclose(xfile); fclose(zfile); } } delete[] plotname; took("Dumping the triplet dist plots"); const double ds = 0.01; // step size to use in path plots, FIXME increase for publication! const double delta = .1; //this is the value of radius of the //particle as it moves around the contact //sphere on its path char *plotname_path = new char[4096]; for (int version = 0; version < numplots; version++) { sprintf(plotname_path, "papers/pair-correlation/figs/triplet%s-path-%s-%04.2f.dat", name, fun[version], eta); FILE *out_path = fopen(plotname_path, "w"); if (!out_path) { fprintf(stderr, "Unable to create file %s!\n", plotname_path); return; } sprintf(plotname_path, "papers/pair-correlation/figs/triplet-back-contact-%s-%04.2f.dat", fun[version], eta); FILE *out_back = fopen(plotname_path, "w"); if (!out_back) { fprintf(stderr, "Unable to create file %s!\n", plotname_path); return; } fprintf(out_path, "# unused\tg3\tz\tx\n"); fprintf(out_back, "# unused\tg3\tz\tx\n"); const Cartesian r0(0,0, 2.0+delta); const double max_theta = M_PI*2.0/3; for (double z = 7; z >= 2*(2.0 + delta); z-=ds) { const Cartesian r1(0,0,z); double g2_path = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2_path*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out_path,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); } for (double z = -7; z <= -(2.0 + delta); z+=ds) { const Cartesian r1(0,0,z); double g2_path = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2_path*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out_back,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); } const double dtheta = ds/2; for (double theta = 0; theta <= max_theta; theta += dtheta){ const Cartesian r1((2.0+delta)*sin(theta), 0, (2.0+delta)*(1+cos(theta))); double g2_path = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2_path*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out_path,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); } for (double theta = 0; theta <= max_theta; theta += dtheta){ const Cartesian r1((2.0+delta)*sin(theta), 0,-(2.0+delta)*cos(theta)); double g2_path = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2_path*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out_back,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); } for (double x = (2.0+delta)*sqrt(3)/2; x<=6; x+=ds){ const Cartesian r1(x, 0, 1.0+delta/2); double g2_path = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2_path*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out_path,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); fprintf(out_back,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); } fclose(out_path); fclose(out_back); } for (int version = 0; version < numplots; version++) { sprintf(plotname_path, "papers/pair-correlation/figs/triplet-path-inbetween-%s-%04.2f.dat", fun[version], eta); FILE *out_path = fopen(plotname_path, "w"); if (!out_path) { fprintf(stderr, "Unable to create file %s!\n", plotname_path); return; } sprintf(plotname_path, "papers/pair-correlation/figs/triplet-back-inbetween-%s-%04.2f.dat", fun[version], eta); FILE *out_back = fopen(plotname_path, "w"); if (!out_back) { fprintf(stderr, "Unable to create file %s!\n", plotname_path); return; } fprintf(out_path, "# unused\tg3\tz\tx\n"); fprintf(out_back, "# unused\tg3\tz\tx\n"); const Cartesian r0(0,0, 4.0+2*delta); const double max_theta = M_PI; for (double z = 11; z >= 3*(2.0 + delta); z-=ds) { const Cartesian r1(0,0,z); double g2_path = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2_path*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out_path,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); } for (double z = -10; z <= -(2.0 + delta); z+=ds) { const Cartesian r1(0,0,z); double g2_path = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2_path*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out_back,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); } const double dtheta = ds/2; for (double theta = 0; theta <= max_theta; theta += dtheta){ const Cartesian r1((2.0+delta)*sin(theta), 0, (2.0+delta)*(2+cos(theta))); double g2_path = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2_path*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out_path,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); } for (double theta = 0; theta <= max_theta; theta += dtheta){ const Cartesian r1((2.0+delta)*sin(theta), 0, -(2.0+delta)*cos(theta)); double g2_path = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2_path*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out_back,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); } for (double x = 0; x>=-6; x-=ds){ const Cartesian r1(x, 0, 2.0+delta); double g2_path = pairdists[version](gsigma, density, nA, n3, nbar_sokolowski, r0, r1); double n_bulk = (3.0/4.0/M_PI)*eta; double g3 = g2_path*density(r0)*density(r1)/n_bulk/n_bulk; fprintf(out_path,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); fprintf(out_back,"0\t%g\t%g\t%g\n", g3, r1[2], r1[0]); } fclose(out_path); fclose(out_back); } delete[] plotname_path; }