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 <commands/command.h>
21 #include <electronic/Everything.h>
22 #include <electronic/ColumnBundle.h>
23 
24 struct CommandInitialState : public Command
25 {
CommandInitialStateCommandInitialState26 	CommandInitialState() : Command("initial-state", "jdftx/Initialization")
27 	{
28 		format = "<filename-pattern>";
29 		comments = "Initialize state from a filename pattern which contains a $VAR,\n"
30 			"equivalent to invoking the following commands:\n"
31 			"+ wavefunction          read  <filename-pattern>/$VAR/wfns\n"
32 			"+ elec-initial-fillings read  <filename-pattern>/$VAR/fillings\n"
33 			"+ elec-initial-Haux           <filename-pattern>/$VAR/Haux\n"
34 			"+ fluid-initial-state         <filename-pattern>/$VAR/fluidState\n"
35 			"\n"
36 			"(where A/x/y is sed for 'find x in A and replace it with y'.)\n"
37 			"This command will invoke the read only for those state variables for which\n"
38 			"the corresponding files exist, leaving the rest with default initialization.\n"
39 			"When using SCF, this will also read scfHistory and eigenvalues if available.";
40 
41 		forbid("wavefunction");
42 		forbid("elec-initial-fillings");
43 		forbid("elec-initial-eigenvals");
44 		forbid("fluid-initial-state");
45 	}
46 
processCommandInitialState47 	void process(ParamList& pl, Everything& e)
48 	{	pl.get(filenamePattern, string(), "filename-pattern", true);
49 		setAvailableFilenames(filenamePattern, e);
50 	}
51 
printStatusCommandInitialState52 	void printStatus(Everything& e, int iRep)
53 	{	fputs(filenamePattern.c_str(), globalLog);
54 	}
55 
56 private:
57 	string filenamePattern;
58 }
59 commandInitialState;
60 
setAvailableFilename(string filenamePattern,string varName,string & target)61 void setAvailableFilename(string filenamePattern, string varName, string& target)
62 {	string filename = filenamePattern;
63 	filename.replace(filename.find("$VAR"),4, varName);
64 	if(isReadable(filename))
65 		target = filename; //file exists and is readable
66 }
67 
setAvailableFilenames(string filenamePattern,Everything & e)68 void setAvailableFilenames(string filenamePattern, Everything& e)
69 {	if(filenamePattern.find("$VAR")==string::npos)
70 		throw "<filename-pattern> = " + filenamePattern + " doesn't contain '$VAR'";
71 	setAvailableFilename(filenamePattern, "wfns", e.eVars.wfnsFilename);
72 	setAvailableFilename(filenamePattern, "fillings", e.eInfo.initialFillingsFilename);
73 	if(!e.eInfo.initialFillingsFilename.length())
74 		setAvailableFilename(filenamePattern, "fill", e.eInfo.initialFillingsFilename); //alternate naming convention
75 	setAvailableFilename(filenamePattern, "fluidState", e.eVars.fluidInitialStateFilename);
76 	if(!e.eVars.fluidInitialStateFilename.length())
77 		setAvailableFilename(filenamePattern, "fS", e.eVars.fluidInitialStateFilename); //alternate naming convention
78 	setAvailableFilename(filenamePattern, "scfHistory", e.scfParams.historyFilename);
79 	setAvailableFilename(filenamePattern, "eigenvals", e.eVars.eigsFilename);
80 }
81 
82 //-----------------------------------------------------------------------
83 
84 enum WfnsInit { WfnsLCAO, WfnsRandom, WfnsRead, WfnsReadRS };
85 
86 EnumStringMap<WfnsInit> wfnsInitMap(
87 	WfnsLCAO, "lcao",
88 	WfnsRandom, "random",
89 	WfnsRead, "read",
90 	WfnsReadRS, "read-rs" );
91 
92 struct CommandWavefunction : public Command
93 {
CommandWavefunctionCommandWavefunction94 	CommandWavefunction() : Command("wavefunction", "jdftx/Initialization")
95 	{
96 		format =
97 			"lcao\n"
98 			"           | random\n"
99 			"           | read <filename> [<nBandsOld>] [<EcutOld>]\n"
100 			"           | read-rs <filename-pattern> [<nBandsOld>] [<NxOld>] [<NyOld>] [<NzOld>]";
101 		comments =
102 			"Wavefunction initialization: use atomic orbitals (default), randomize or read from files:\n"
103 			"+ read expects <filename> to point to a single file with fourier-space G-sphere wavefunctions.\n"
104 			"+ read-rs expects <filename> to be a printf format with 2 %%d's, the first for state index and\n"
105 			"   the second for band. Each 'column' will be loaded from a separate file accordingly.\n"
106 			"   For spinor wavefunctions, each spinor component has a separate second index, so that\n"
107 			"   the first band is read from 0 and 1, the second one from 2 and 3 and so on.\n"
108 			"+ <nBandsOld> can be used to specify a wavefunction which has different bands\n"
109 			"   extra bands will be discarded, unspecified bands will be randomized and orthogonalized.\n"
110 			"   Reminder: nBandsOlds for fillings file is specified separately in elec-initial-fillings.\n"
111 			"   Default: 0 => old and current nBands must match exactly.\n"
112 			"+ <EcutOld> can be used to specify a wavefunction with different planewave cutoff.\n"
113 			"   The wavefunction will be appropriately up/down-sampled in Fourier space.\n"
114 			"   Default: 0.0 => old and current Ecut must match exactly.\n"
115 			"+ <N*old> specify fftbox dimensions of the input data when reading real-space wavefunctions.\n"
116 			"   The wavefunction will be appropriately up/down-sampled in Fourier space.\n"
117 			"   Default: 0 => old and current fftbox must match exactly.";
118 		hasDefault = false;
119 
120 		forbid("initial-state");
121 	}
122 
processCommandWavefunction123 	void process(ParamList& pl, Everything& e)
124 	{	WfnsInit wfnsInit; pl.get(wfnsInit, WfnsLCAO, wfnsInitMap, "option", true);
125 		switch(wfnsInit)
126 		{	case WfnsLCAO:
127 				e.eVars.initLCAO = true;
128 				break;
129 			case WfnsRandom:
130 				//ElecVars constructor defaults to initLCAO=false with no wfnsFilename
131 				e.eVars.initLCAO = false;
132 				break;
133 			case WfnsRead:
134 			{	pl.get(e.eVars.wfnsFilename, string(), "filename", true);
135 				auto conversion = std::make_shared<ElecInfo::ColumnBundleReadConversion>();
136 				conversion-> realSpace = false;
137 				pl.get(conversion->nBandsOld, -1, "nBandsOld");
138 				pl.get(conversion->EcutOld, 0.0, "EcutOld");
139 				if(conversion->nBandsOld>=0) e.eVars.readConversion = conversion;
140 				break;
141 			}
142 			case WfnsReadRS:
143 			{	pl.get(e.eVars.wfnsFilename, string(), "filename", true);
144 				auto conversion = std::make_shared<ElecInfo::ColumnBundleReadConversion>();
145 				conversion-> realSpace = true;
146 				pl.get(conversion->nBandsOld, 0, "nBandsOld");
147 				pl.get(conversion->S_old[0], 0, "NxOld");
148 				pl.get(conversion->S_old[1], 0, "NyOld");
149 				pl.get(conversion->S_old[2], 0, "NzOld");
150 				e.eVars.readConversion = conversion;
151 				break;
152 			}
153 		}
154 	}
155 
printStatusCommandWavefunction156 	void printStatus(Everything& e, int iRep)
157 	{	if(!e.eVars.wfnsFilename.length())
158 			logPrintf(e.eVars.initLCAO ? "lcao" : "random");
159 		else if(!e.eVars.readConversion)
160 			logPrintf("read %s", e.eVars.wfnsFilename.c_str());
161 		else if(!e.eVars.readConversion->realSpace)
162 			logPrintf("read %s %d %lf", e.eVars.wfnsFilename.c_str(),
163 				e.eVars.readConversion->nBandsOld, e.eVars.readConversion->EcutOld);
164 		else
165 			logPrintf("read-rs %s %d %d %d %d", e.eVars.wfnsFilename.c_str(), e.eVars.readConversion->nBandsOld,
166 				e.eVars.readConversion->S_old[0], e.eVars.readConversion->S_old[1], e.eVars.readConversion->S_old[2]);
167 	}
168 }
169 commandWavefunction;
170