1 /*-------------------------------------------------------------------
2 Copyright 2011 Ravishankar Sundararaman, Kendra Letchworth Weaver
3 
4 This file is part of JDFTx.
5 
6 JDFTx is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10 
11 JDFTx is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with JDFTx.  If not, see <http://www.gnu.org/licenses/>.
18 -------------------------------------------------------------------*/
19 
20 #include <electronic/Everything.h>
21 #include <electronic/ColumnBundle.h>
22 #include <core/matrix.h>
23 #include <electronic/Dump.h>
24 #include <electronic/ElecMinimizer.h>
25 #include <electronic/LatticeMinimizer.h>
26 #include <electronic/Vibrations.h>
27 #include <electronic/IonicDynamics.h>
28 #include <fluid/FluidSolver.h>
29 #include <core/Util.h>
30 #include <commands/parser.h>
31 
32 //Program entry point
main(int argc,char ** argv)33 int main(int argc, char** argv)
34 {	//Parse command line, initialize system and logs:
35 	Everything e; //the parent data structure for, well, everything
36 	InitParams ip("Performs Joint Density Functional Theory calculations.", &e);
37 	initSystemCmdline(argc, argv, ip);
38 
39 	//Parse input file and setup
40 	ElecVars& eVars = e.eVars;
41 	parse(readInputFile(ip.inputFilename), e, ip.printDefaults);
42 	if(ip.dryRun) eVars.skipWfnsInit = true;
43 	e.setup();
44 	e.dump(DumpFreq_Init, 0);
45 	Citations::print();
46 	if(ip.dryRun)
47 	{	logPrintf("Dry run successful: commands are valid and initialization succeeded.\n");
48 		finalizeSystem();
49 		return 0;
50 	}
51 	else logPrintf("Initialization completed successfully at t[s]: %9.2lf\n\n", clock_sec());
52 	logFlush();
53 
54 	if(e.cntrl.dumpOnly)
55 	{	//Single energy calculation so that all dependent quantities have been initialized:
56 		if(eVars.isRandom) die("Electronic state required for dump-only mode has not been read in (using initial-state or wavefunction).\n\n");
57 		logPrintf("\n----------- Energy evaluation at fixed state -------------\n"); logFlush();
58 		eVars.elecEnergyAndGrad(e.ener, 0, 0, true); //calculate Hsub so that eigenvalues are available (used by many dumps)
59 		logPrintf("# Energy components:\n"); e.ener.print(); logPrintf("\n");
60 	}
61 	else if(e.cntrl.fixed_H)
62 	{	//Band structure calculation - ion and fluid minimization need to be handled differently
63 		if(eVars.nFilenamePattern.length())
64 		{	//If starting from density, compute potential:
65 			eVars.EdensityAndVscloc(e.ener);
66 			if(eVars.fluidSolver && eVars.fluidSolver->useGummel())
67 			{	//Relies on the gummel loop, so EdensityAndVscloc would not have invoked minimize
68 				eVars.fluidSolver->minimizeFluid();
69 				eVars.EdensityAndVscloc(e.ener); //update Vscloc
70 			}
71 		}
72 		if(e.exCorr.exxFactor() and e.eVars.isRandom)
73 			die("Fixed Hamiltonian calculations with EXX require occupied wavefunctions to be read in (use initial-state or wavefunction commands).\n");
74 		e.iInfo.augmentDensityGridGrad(eVars.Vscloc); //update Vscloc atom projections for ultrasoft psp's
75 		logPrintf("\n----------- Band structure minimization -------------\n"); logFlush();
76 		bandMinimize(e); // Do the band-structure minimization
77 		//Update fillings if necessary:
78 		if(e.eInfo.fillingsUpdate == ElecInfo::FillingsHsub)
79 		{	//Calculate mu from nElectrons:
80 			double Bz, mu = e.eInfo.findMu(eVars.Hsub_eigs, e.eInfo.nElectrons, Bz);
81 			//Update fillings:
82 			for(int q=e.eInfo.qStart; q<e.eInfo.qStop; q++)
83 				eVars.F[q] = e.eInfo.smear(e.eInfo.muEff(mu,Bz,q), eVars.Hsub_eigs[q]);
84 			//Update TS and muN:
85 			e.eInfo.updateFillingsEnergies(eVars.Hsub_eigs, e.ener);
86 			e.eInfo.smearReport();
87 		}
88 	}
89 	else if(e.vibrations) //Bypasses ionic/lattice minimization, calls electron/fluid minimization loops at various ionic configurations
90 	{	e.vibrations->calculate();
91 	}
92 	else if(e.ionicDynParams.nSteps)
93 	{	//Born-Oppenheimer molecular dynamics
94 		IonicDynamics idyn(e);
95 		idyn.run();
96 	}
97 	else if(e.latticeMinParams.nIterations)
98 	{	//Lattice minimization loop (which invokes the ionic minimization loop)
99 		LatticeMinimizer lmin(e);
100 		lmin.minimize(e.latticeMinParams);
101 	}
102 	else
103 	{	//Ionic minimization loop (which calls electron/fluid minimization loops)
104 		IonicMinimizer imin(e);
105 		imin.minimize(e.ionicMinParams);
106 	}
107 
108 	//Final dump:
109 	e.dump(DumpFreq_End, 0);
110 
111 	finalizeSystem();
112 	return 0;
113 }
114