00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "QMCPropertyArrays.h"
00014 #include "QMCDerivativeProperties.h"
00015
00016 int QMCPropertyArrays::numFutureWalking = -1;
00017
00018 QMCPropertyArrays::QMCPropertyArrays()
00019 {
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 if(globalInput.flags.input_file_name == "")
00041 {
00042 cerr << "Error: The global QMCInput needs to have been initiated by this point!\n";
00043 exit(1);
00044 }
00045
00046
00047 numFutureWalking = globalInput.flags.future_walking.size();
00048
00049 names.allocate(NUM_PROPS);
00050
00051
00052 names[FW_It] = "Normalization_I(t)";
00053 names[FW_TE] = "Total_Energy";
00054 names[FW_KE] = "Kinetic_Energy";
00055 names[FW_KEg] = "Kinetic_Energy_g";
00056 names[FW_PE] = "Potential_Energy";
00057 names[FW_R12] = "R12";
00058 names[FW_R2] = "R2";
00059 names[FW_iR] = "Inverse_R";
00060 names[FW_iR12]= "Inverse_R12";
00061
00062 names[FW_TE_2] = "Total_Energy^2";
00063 names[FW_KE_2] = "Kinetic_Energy^2";
00064 names[FW_KEg_2] = "Kinetic_Energy_g^2";
00065 names[FW_PE_2] = "Potential_Energy^2";
00066 names[FW_R12_2] = "R12^2";
00067 names[FW_R2_2] = "R2^2";
00068 names[FW_iR_2] = "Inverse_R^2";
00069 names[FW_iR12_2]= "Inverse_R12^2";
00070
00071 props.allocate(NUM_PROPS);
00072 for(int i=0; i<props.size(); i++)
00073 props(i).allocate(numFutureWalking);
00074
00075 nBasisFunc = 0;
00076 calc_density = false;
00077 calc_forces = false;
00078 zeroOut();
00079 }
00080
00081 QMCPropertyArrays::~QMCPropertyArrays()
00082 {
00083 if(calc_forces)
00084 {
00085 for (int i=0; i<nuclearForces.dim1(); i++)
00086 nuclearForces(i).deallocate();
00087 nuclearForces.deallocate();
00088 }
00089
00090 for(int i=0; i<props.size(); i++)
00091 props(i).deallocate();
00092 props.deallocate();
00093
00094 der.deallocate();
00095 for(int i=0; i<hess.dim1(); i++)
00096 hess(i).deallocate();
00097 hess.deallocate();
00098
00099 chiDensity.deallocate();
00100 }
00101
00102 void QMCPropertyArrays::setCalcDensity(bool calcDensity, int nbasisfunctions)
00103 {
00104 calc_density = calcDensity;
00105
00106 if(calc_density == true)
00107 {
00108 nBasisFunc = nbasisfunctions;
00109 chiDensity.allocate(nBasisFunc);
00110 }
00111 else
00112 {
00113 nBasisFunc = 0;
00114 chiDensity.deallocate();
00115 }
00116 }
00117
00118 void QMCPropertyArrays::setCalcForces(bool calcForces, int dim1, int dim2)
00119 {
00120 calc_forces = calcForces;
00121
00122 if(calc_forces == true)
00123 {
00124 nuclearForces.allocate(numFutureWalking);
00125 for (int fw=0; fw<nuclearForces.dim1(); fw++)
00126 nuclearForces(fw).allocate(dim1,dim2);
00127 }
00128 else
00129 {
00130 for (int i=0; i<nuclearForces.dim1(); i++)
00131 nuclearForces(i).deallocate();
00132 nuclearForces.deallocate();
00133 }
00134 }
00135
00136 void QMCPropertyArrays::zeroOut()
00137 {
00138 for(int fw=0; fw<numFutureWalking; fw++)
00139 {
00140 for(int i=0; i<props.size(); i++)
00141 (props(i))(fw).zeroOut();
00142
00143 if (calc_forces)
00144 for (int i=0; i<nuclearForces(fw).dim1(); i++)
00145 for (int j=0; j<nuclearForces(fw).dim2(); j++)
00146 (nuclearForces(fw))(i,j).zeroOut();
00147 }
00148
00149 if(globalInput.cs_Parameters.dim1() > 1)
00150 cs_Energies.allocate(globalInput.cs_Parameters.dim1());
00151 else
00152 cs_Energies.deallocate();
00153
00154 int numAI = globalInput.getNumberAIParameters();
00155
00156 if(globalInput.flags.calculate_Derivatives != 0)
00157 {
00158
00159
00160 der.allocate(numAI,5);
00161 der = 0.0;
00162 } else {
00163 der.deallocate();
00164 }
00165
00166 if(globalInput.flags.calculate_Derivatives != 0)
00167 {
00168 if( globalInput.flags.optimize_Psi_criteria == "analytical_energy_variance")
00169 {
00170 hessIsSymmetric = true;
00171 hess.allocate(1);
00172 hess(0).allocate(numAI,numAI);
00173 } else if(globalInput.flags.optimize_Psi_criteria == "generalized_eigenvector")
00174 {
00175 hessIsSymmetric = false;
00176 hess.allocate(3);
00177 for(int i=0; i<hess.dim1(); i++)
00178 hess(i).allocate(numAI,numAI);
00179 } else {
00180 clog << "Error: unknown optimize_Psi_criteria\n";
00181 }
00182
00183 for(int i=0; i<hess.dim1(); i++)
00184 hess(i) = 0.0;
00185 } else {
00186 for(int i=0; i<hess.dim1(); i++)
00187 hess(i).deallocate();
00188 hess.deallocate();
00189 }
00190
00191 for(int i=0; i<cs_Energies.dim1(); i++)
00192 cs_Energies(i).zeroOut();
00193
00194 numDerHessSamples = 0;
00195
00196 if (calc_density)
00197 for (int i=0; i<nBasisFunc; i++)
00198 chiDensity(i).zeroOut();
00199 }
00200
00201 void QMCPropertyArrays::newSample(QMCPropertyArrays* newProperties, double weight,
00202 int nwalkers)
00203 {
00204 for(int fw=0; fw<numFutureWalking; fw++)
00205 {
00206 if (calc_forces && (newProperties->nuclearForces(fw))(0,0).getNumberSamples() > 0)
00207 {
00208 for (int i=0; i<nuclearForces(fw).dim1(); i++)
00209 for (int j=0; j<nuclearForces(fw).dim2(); j++)
00210 (nuclearForces(fw))(i,j).newSample((newProperties->nuclearForces(fw))(i,j).getAverage(),
00211 weight);
00212 }
00213
00214 for(int i=0; i<props.size(); i++)
00215 if((newProperties->props(i))(fw).getNumberSamples() > 0)
00216 (props(i))(fw).newSample( (newProperties->props(i))(fw).getAverage(), weight);
00217 }
00218
00219 for(int i=0; i<cs_Energies.dim1(); i++)
00220 if(newProperties->cs_Energies(i).getNumberSamples() > 0)
00221 cs_Energies(i).newSample(newProperties->cs_Energies(i).getAverage(), weight);
00222
00223 if (calc_density)
00224 for (int i=0; i<nBasisFunc; i++)
00225 if(newProperties->chiDensity(i).getNumberSamples() > 0)
00226 chiDensity(i).newSample(newProperties->chiDensity(i).getAverage(),weight);
00227 }
00228
00229 void QMCPropertyArrays::matchParametersTo( const QMCPropertyArrays &rhs )
00230 {
00231 if(rhs.calc_forces)
00232 setCalcForces(rhs.calc_forces,rhs.nuclearForces.get(0).dim1(),rhs.nuclearForces.get(0).dim2());
00233 if(rhs.calc_density)
00234 setCalcDensity(rhs.calc_density,rhs.nBasisFunc);
00235
00236 zeroOut();
00237 }
00238
00239 void QMCPropertyArrays::operator = ( const QMCPropertyArrays &rhs )
00240 {
00241 matchParametersTo(rhs);
00242
00243
00244 props = rhs.props;
00245
00246 if (calc_forces)
00247 {
00248 nuclearForces = rhs.nuclearForces;
00249 }
00250 if (calc_density)
00251 {
00252 chiDensity = rhs.chiDensity;
00253 }
00254
00255 der = rhs.der;
00256 hess = rhs.hess;
00257 cs_Energies = rhs.cs_Energies;
00258 numDerHessSamples = rhs.numDerHessSamples;
00259 }
00260
00261 QMCPropertyArrays QMCPropertyArrays::operator + ( QMCPropertyArrays &rhs )
00262 {
00263 QMCPropertyArrays result;
00264
00265 matchParametersTo(rhs);
00266 result.matchParametersTo(rhs);
00267
00268
00269 for(int fw=0; fw<numFutureWalking; fw++)
00270 {
00271 if (calc_forces)
00272 {
00273 for (int i=0; i<nuclearForces(fw).dim1(); i++)
00274 for (int j=0; j<nuclearForces(fw).dim2(); j++)
00275 (result.nuclearForces(fw))(i,j) = (nuclearForces(fw))(i,j)
00276 + (rhs.nuclearForces(fw))(i,j);
00277 }
00278
00279 for(int i=0; i<props.size(); i++)
00280 (result.props(i))(fw) = (props(i))(fw) + (rhs.props(i))(fw);
00281 }
00282
00283 return result;
00284 }
00285
00286 void QMCPropertyArrays::toXML(ostream& strm)
00287 {
00288
00289 strm << "<QMCPropertyArrays>" << endl;
00290
00291
00292 if (calc_density)
00293 {
00294 strm << "<ChiDensity>" << endl;
00295 for (int i=0; i<nBasisFunc; i++)
00296 chiDensity(i).toXML(strm);
00297 strm << "</ChiDensity>" << endl;
00298 }
00299
00300
00301 if (calc_forces)
00302 {
00303 for(int fw=0; fw<numFutureWalking; fw++)
00304 {
00305 stringstream temp;
00306 temp << globalInput.flags.future_walking[fw];
00307 strm << "<NuclearForces" << temp.str() << ">" << endl;
00308 for (int i=0; i<nuclearForces(fw).dim1(); i++)
00309 for (int j=0; j<nuclearForces(fw).dim2(); j++)
00310 (nuclearForces(fw))(i,j).toXML(strm);
00311 strm << "</NuclearForces" << temp.str() << ">" << endl;
00312 }
00313 }
00314
00315 for(int fw=0; fw<numFutureWalking; fw++)
00316 {
00317 stringstream temp;
00318 temp << globalInput.flags.future_walking[fw];
00319
00320 for(int i=0; i<props.size(); i++)
00321 {
00322
00323 strm << "<FW_" << names[i] << "_" << temp.str() << ">" << endl;
00324 (props(i))(fw).toXML(strm);
00325 strm << "</FW_" << names[i] << "_" << temp.str() << ">" << endl;
00326 }
00327 }
00328
00329
00330 strm << "</QMCPropertyArrays>" << endl;
00331 }
00332
00333 bool QMCPropertyArrays::readXML(istream& strm)
00334 {
00335 string temp;
00336
00337
00338 strm >> temp;
00339
00340
00341 if (calc_density)
00342 {
00343 strm >> temp;
00344 for (int i=0; i<nBasisFunc; i++)
00345 chiDensity(i).readXML(strm);
00346 strm >> temp;
00347 }
00348
00349
00350 if (calc_forces)
00351 {
00352 for(int fw=0; fw<numFutureWalking; fw++)
00353 {
00354 strm >> temp;
00355 for (int i=0; i<nuclearForces(fw).dim1(); i++)
00356 for (int j=0; j<nuclearForces(fw).dim2(); j++)
00357 (nuclearForces(fw))(i,j).readXML(strm);
00358 strm >> temp;
00359 }
00360 }
00361
00362 for(int fw=0; fw<numFutureWalking; fw++)
00363 {
00364 for(int i=0; i<props.size(); i++)
00365 {
00366 strm >> temp;
00367 (props(i))(fw).readXML(strm);
00368 strm >> temp;
00369 }
00370 }
00371
00372
00373 strm >> temp;
00374 if(temp != "</QMCPropertyArrays>")
00375 {
00376 clog << "Error: checkpoint read failed in QMCPropertyArrays."
00377 << " We expected to read a \"</QMCPropertyArrays>\""
00378 << " tag, but found \"" << temp << "\"." << endl;
00379 return false;
00380 }
00381 return true;
00382 }
00383
00384 ostream& operator <<(ostream& strm, QMCPropertyArrays &rhs)
00385 {
00386 int w1 = 10;
00387 int w2 = 10;
00388 int p1 = 2;
00389
00390 if(rhs.cs_Energies.dim1() > 1)
00391 {
00392 strm << endl << "------ Correlated Sampling Energies --------" << endl;
00393 for(int i=0; i<rhs.cs_Energies.dim1(); i++)
00394 {
00395 strm << "Set " << setw(3) << i << ": Sample variance = " << rhs.cs_Energies(i).getSeriallyCorrelatedVariance() << endl;
00396 strm << rhs.cs_Energies(i);
00397
00398 strm << endl;
00399 }
00400 }
00401
00402
00403
00404 if(false)
00405 {
00406 if(rhs.der.dim1() > 0)
00407 {
00408 strm << endl << "------ Objective Function Derivatives --------" << endl;
00409 for(int i=0; i<rhs.der.dim1(); i++)
00410 {
00411 for(int j=0; j<rhs.der.dim2(); j++)
00412 {
00413 strm << "Param " << i << " term " << j << ": " << rhs.der(i,j);
00414 }
00415 strm << endl;
00416 }
00417 }
00418 }
00419
00420 if(false)
00421 {
00422 if(rhs.hess.dim1() > 0)
00423 {
00424 strm << endl << "------ Objective Function Hessian --------" << endl;
00425
00426 for (int i=0; i<rhs.hess.dim1(); i++)
00427 {
00428 for(int j=0; j<rhs.hess(i).dim1(); j++)
00429 {
00430 int max = rhs.hess(i).dim2();
00431 if(rhs.hessIsSymmetric)
00432 max = j+1;
00433
00434 for(int k=0; k<max; k++)
00435 strm << "(" << j << "," << k << "): " << (rhs.hess(i))(j,k);
00436 }
00437 strm << endl;
00438 }
00439 strm << endl << endl;
00440 }
00441 }
00442
00443 if(rhs.numFutureWalking <= 1) return strm;
00444
00445 for(int i=0; i<rhs.props.size(); i++)
00446 {
00447 strm << endl << "-------------------------- FW " << rhs.names[i] << endl;
00448 for(int fw=0; fw<rhs.numFutureWalking; fw++)
00449 {
00450 if((rhs.props(i))(fw).getNumberSamples() <= 0) break;
00451 strm.width(w1);
00452 strm.precision(p1);
00453 strm << globalInput.flags.dt_effective * globalInput.flags.future_walking[fw] << " <=> ";
00454 strm.width(w2);
00455 strm << globalInput.flags.future_walking[fw]
00456 << ": " << (rhs.props(i))(fw);
00457 }
00458 }
00459
00460
00461 if (rhs.calc_forces)
00462 {
00463 strm << endl << "------------ FW Nuclear Forces ---------------" << endl;
00464 for(int fw=0; fw<rhs.numFutureWalking; fw++)
00465 {
00466 if((rhs.nuclearForces(fw))(0,0).getNumberSamples() <= 0) break;
00467 for (int i=0; i<rhs.nuclearForces(fw).dim1(); i++)
00468 {
00469 for (int j=0; j<rhs.nuclearForces(fw).dim2(); j++)
00470 {
00471
00472 strm.width(5);
00473 if(globalInput.flags.nuclear_derivatives == "bin_force_density")
00474 {
00475 strm << i;
00476 } else {
00477 strm << globalInput.Molecule.Atom_Labels(i) << i;
00478 switch(j)
00479 {
00480 case 0: strm << "_x"; break;
00481 case 1: strm << "_y"; break;
00482 case 2: strm << "_z"; break;
00483 }
00484 }
00485
00486 strm.width(w1);
00487 strm.precision(p1);
00488 strm << globalInput.flags.dt_effective * globalInput.flags.future_walking[fw] << " <=> ";
00489 strm.width(w2);
00490 strm << globalInput.flags.future_walking[fw]
00491 << ": " << (rhs.nuclearForces(fw))(i,j);
00492 }
00493 }
00494 }
00495 }
00496
00497 return strm;
00498 }
00499