source: pcp/src/perturbed.c@ 34b70c

Last change on this file since 34b70c was 34b70c, checked in by Frederik Heber <heber@…>, 17 years ago

CalculateChemicalShieldingByReciprocalCurrentDensity(): NICS map written to file

fabs of sigma_real is taken (only positive values can be displayed), then B (sigma is basically B_Induced) is diagonalised and absolute values are delivered to OutputVis().

  • Property mode set to 100644
File size: 218.2 KB
Line 
1/** \file perturbed.c
2 * Perturbation calculation due to external magnetic field.
3 *
4 * Central function is MinimisePerturbed() wherein the actual minimisation of the two different operators with each
5 * three components takes place subsequently. Helpful routines are CalculatePerturbationOperator_P() - which applies a
6 * specified component of p on the current wave function - and CalculatePerturbationOperator_RxP() - which does the
7 * same for the RxP operator.
8 * The actual minimisation loop FindPerturbedMinimum() depends on the same routines also used for the occupied orbitals,
9 * however with a different energy functional and derivatives, evaluated in Calculate1stPerturbedDerivative() and
10 * Calculate2ndPerturbedDerivative(). InitPerturbedEnergyCalculation() calculates the total energy functional
11 * perturbed in second order for all wave functions, UpdatePerturbedEnergyCalculation() just updates the one
12 * for the wave function after it has been minimised during the line search. Both use CalculatePerturbedEnergy() which
13 * evaluates the energy functional (and the gradient if specified).
14 * Finally, FillCurrentDensity() evaluates the current density at a given point in space using the perturbed
15 * wave functions. Afterwards by calling CalculateMagneticSusceptibility() or
16 * CalculateChemicalShieldingByReciprocalCurrentDensity() susceptibility respectively shielding tensor are possible uses
17 * of this current density.
18 *
19 * There are also some test routines: TestCurrent() checks whether the integrated current is zero in each component.
20 * test_fft_symmetry() tests the "pulling out imaginary unit" before fourier transformation on a given wave function.
21 * CheckOrbitalOverlap() outputs the overlap matrix for the wave functions of a given minimisation state, this might
22 * be important for the additional \f$\Delta J{ij}\f$ contribution to the current density, which is non-zero for
23 * non-zero mutual overlap, which is evaluated if FillDeltaCurrentDensity() is called.
24 *
25 * Finally, there are also some smaller routines: truedist() gives the correct relative distance between two points
26 * in the unit cell under periodic boundary conditions with minimum image convention. ApplyTotalHamiltonian() returns
27 * the hamiltonian applied to a given wave function. sawtooth() is a sawtooth implementation which is needed in order
28 * to avoid flipping of position eigenvalues for nodes close to or on the cell boundary. CalculateOverlap()
29 * is used in the energy functional derivatives, keeping an overlap table between perturbed wave functions up to date.
30 * fft_Psi() is very similar to CalculateOneDensityR(), it does the extension of the wave function to the upper level
31 * RunStruct#Lev0 while fouriertransforming it to real space. cross() gives correct indices in evaluating a vector cross
32 * product. AllocCurrentDensity() and DisAllocCurrentDensity() mark the current density arrays as currently being in use or not.
33 *
34 Project: ParallelCarParrinello
35 \author Frederik Heber
36 \date 2006
37
38*/
39
40#include <stdlib.h>
41#include <stdio.h>
42#include <math.h>
43#include <string.h>
44#include <time.h>
45#include <gsl/gsl_matrix.h>
46#include <gsl/gsl_eigen.h>
47#include <gsl/gsl_complex.h>
48#include <gsl/gsl_complex_math.h>
49#include <gsl/gsl_sort_vector.h>
50#include <gsl/gsl_linalg.h>
51#include <gsl/gsl_multimin.h>
52
53#include "data.h"
54#include "density.h"
55#include "energy.h"
56#include "excor.h"
57#include "errors.h"
58#include "grad.h"
59#include "gramsch.h"
60#include "mergesort2.h"
61#include "helpers.h"
62#include "init.h"
63#include "myfft.h"
64#include "mymath.h"
65#include "output.h"
66#include "pcp.h"
67#include "perturbed.h"
68#include "run.h"
69#include "wannier.h"
70
71
72/** Minimisation of the PsiTypeTag#Perturbed_RxP0, PsiTypeTag#Perturbed_P0 and other orbitals.
73 * For each of the above PsiTypeTag we go through the following before the minimisation loop:
74 * -# ResetGramSchTagType() resets current type that is to be minimised to NotOrthogonal.
75 * -# UpdateActualPsiNo() steps on to next perturbed of current PsiTypeTag type.
76 * -# GramSch() orthonormalizes perturbed wave functions.
77 * -# TestGramSch() tests if orthonormality was achieved.
78 * -# InitDensityCalculation() gathers densities from all wave functions (and all processes), within SpeedMeasure() DensityTime.
79 * -# InitPerturbedEnergyCalculation() performs initial calculation of the perturbed energy functional.
80 * -# RunStruct#OldActualLocalPsiNo is set to RunStruct#ActualLocalPsiNo, immediately followed by UpdateGramSchOldActualPsiNo()
81 * to bring info on all processes on par.
82 * -# UpdatePerturbedEnergyCalculation() re-calculates Gradient and GradientTypes#H1cGradient for RunStruct#ActualLocalPsiNo
83 * -# EnergyAllReduce() gathers various energy terms and sums up into Energy#TotalEnergy.
84 *
85 * And during the minimisation loop:
86 * -# FindPerturbedMinimum() performs the gradient conjugation, the line search and wave function update.
87 * -# UpdateActualPsiNo() steps on to the next wave function, orthonormalizing by GramSch() if necessary.
88 * -# UpdateEnergyArray() shifts TotalEnergy values to make space for new one.
89 * -# There is no density update as the energy function does not depend on the changing perturbed density but only on the fixed
90 * unperturbed one.
91 * -# UpdatePerturbedEnergyCalculation() re-calculates the perturbed energy of the changed wave function.
92 * -# EnergyAllReduce() gathers energy terms and sums up.
93 * -# CheckCPULIM() checks if external Stop signal has been given.
94 * -# CalculateMinimumStop() checks whether we have dropped below a certain minimum change during minimisation of total energy.
95 * -# finally step counters LatticeLevel#Step and SpeedStruct#Steps are increased.
96 *
97 * After the minimisation loop:
98 * -# SetGramSchExtraPsi() removes extra Psis from orthogonaliy check.
99 * -# ResetGramSchTagType() sets GramSchToDoType to NotUsedtoOrtho.
100 *
101 * And after all minimisation runs are done:
102 * -# UpdateActualPsiNo() steps back to PsiTypeTag#Occupied type.
103 *
104 * At the end we return to Occupied wave functions.
105 * \param *P at hand
106 * \param *Stop flag to determine if epsilon stop conditions have met
107 * \param *SuperStop flag to determinte whether external signal's required end of calculations
108 */
109void MinimisePerturbed (struct Problem *P, int *Stop, int *SuperStop) {
110 struct RunStruct *R = &P->R;
111 struct Lattice *Lat = &P->Lat;
112 struct Psis *Psi = &Lat->Psi;
113 int type, flag = 0;//,i;
114
115 for (type=Perturbed_P0;type<=Perturbed_RxP2;type++) { // go through each perturbation group separately //
116 *Stop=0; // reset stop flag
117 if(P->Call.out[LeaderOut]) fprintf(stderr,"(%i)Beginning perturbed minimisation of type %s ...\n", P->Par.me, R->MinimisationName[type]);
118 //OutputOrbitalPositions(P, Occupied);
119 R->PsiStep = R->MaxPsiStep; // reset in-Psi-minimisation-counter, so that we really advance to the next wave function
120 UpdateActualPsiNo(P, type); // step on to next perturbed one
121
122 if(P->Call.out[MinOut]) fprintf(stderr, "(%i) Re-initializing perturbed psi array for type %s ", P->Par.me, R->MinimisationName[type]);
123 if (P->Call.ReadSrcFiles && (flag = ReadSrcPsiDensity(P,type,1, R->LevS->LevelNo))) {// in flag store whether stored Psis are readible or not
124 SpeedMeasure(P, InitSimTime, StartTimeDo);
125 if(P->Call.out[MinOut]) fprintf(stderr,"from source file of recent calculation\n");
126 ReadSrcPsiDensity(P,type, 0, R->LevS->LevelNo);
127 ResetGramSchTagType(P, Psi, type, IsOrthogonal); // loaded values are orthonormal
128 SpeedMeasure(P, DensityTime, StartTimeDo);
129 //InitDensityCalculation(P);
130 SpeedMeasure(P, DensityTime, StopTimeDo);
131 R->OldActualLocalPsiNo = R->ActualLocalPsiNo; // needed otherwise called routines in function below crash
132 UpdateGramSchOldActualPsiNo(P,Psi);
133 InitPerturbedEnergyCalculation(P, 1); // go through all orbitals calculate each H^{(0)}-eigenvalue, recalc HGDensity, cause InitDensityCalc zero'd it
134 UpdatePerturbedEnergyCalculation(P); // H1cGradient and Gradient must be current ones
135 EnergyAllReduce(P); // gather energies for minimum search
136 SpeedMeasure(P, InitSimTime, StopTimeDo);
137 }
138 if ((P->Call.ReadSrcFiles != 1) || (!flag)) { // read and don't minimise only if SrcPsi were parsable!
139 SpeedMeasure(P, InitSimTime, StartTimeDo);
140 ResetGramSchTagType(P, Psi, type, NotOrthogonal); // perturbed now shall be orthonormalized
141 if ((P->Call.ReadSrcFiles != 2) || (!flag)) {
142 if (R->LevSNo == Lat->MaxLevel-1) { // is it the starting level? (see InitRunLevel())
143 if(P->Call.out[MinOut]) fprintf(stderr, "randomly.\n");
144 InitPsisValue(P, Psi->TypeStartIndex[type], Psi->TypeStartIndex[type+1]); // initialize perturbed array for this run
145 } else {
146 if(P->Call.out[MinOut]) fprintf(stderr, "from source file of last level.\n");
147 ReadSrcPerturbedPsis(P, type);
148 }
149 }
150 SpeedMeasure(P, InitGramSchTime, StartTimeDo);
151 GramSch(P, R->LevS, Psi, Orthogonalize);
152 SpeedMeasure(P, InitGramSchTime, StopTimeDo);
153 SpeedMeasure(P, InitDensityTime, StartTimeDo);
154 //InitDensityCalculation(P);
155 SpeedMeasure(P, InitDensityTime, StopTimeDo);
156 InitPerturbedEnergyCalculation(P, 1); // go through all orbitals calculate each H^{(0)}-eigenvalue, recalc HGDensity, cause InitDensityCalc zero'd it
157 R->OldActualLocalPsiNo = R->ActualLocalPsiNo; // needed otherwise called routines in function below crash
158 UpdateGramSchOldActualPsiNo(P,Psi);
159 UpdatePerturbedEnergyCalculation(P); // H1cGradient and Gradient must be current ones
160 EnergyAllReduce(P); // gather energies for minimum search
161 SpeedMeasure(P, InitSimTime, StopTimeDo);
162 R->LevS->Step++;
163 EnergyOutput(P,0);
164 while (*Stop != 1) {
165 //debug(P,"FindPerturbedMinimum");
166 FindPerturbedMinimum(P); // find minimum
167 //debug(P,"UpdateActualPsiNo");
168 UpdateActualPsiNo(P, type); // step on to next perturbed Psi
169 //debug(P,"UpdateEnergyArray");
170 UpdateEnergyArray(P); // shift energy values in their array by one
171 //debug(P,"UpdatePerturbedEnergyCalculation");
172 UpdatePerturbedEnergyCalculation(P); // re-calc energies (which is hopefully lower)
173 EnergyAllReduce(P); // gather from all processes and sum up to total energy
174 //ControlNativeDensity(P); // check total density (summed up PertMixed must be zero!)
175 //printf ("(%i,%i,%i)S(%i,%i,%i):\t %5d %10.5f\n",P->Par.my_color_comm_ST,P->Par.me_comm_ST, P->Par.me_comm_ST_PsiT, R->MinStep, R->ActualLocalPsiNo, R->PsiStep, (int)iter, s_multi->f);
176 if (*SuperStop != 1)
177 *SuperStop = CheckCPULIM(P);
178 *Stop = CalculateMinimumStop(P, *SuperStop);
179 P->Speed.Steps++; // step on
180 R->LevS->Step++;
181 }
182 // now release normalization condition and minimize wrt to norm
183 if(P->Call.out[MinOut]) fprintf(stderr,"(%i) Writing %s srcpsi to disk\n", P->Par.me, R->MinimisationName[type]);
184 OutputSrcPsiDensity(P, type);
185// if (!TestReadnWriteSrcDensity(P,type))
186// Error(SomeError,"TestReadnWriteSrcDensity failed!");
187 }
188
189 TestGramSch(P,R->LevS,Psi, type); // functions are orthonormal?
190 // calculate current density summands
191 //if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) Filling current density grid ...\n",P->Par.me);
192 SpeedMeasure(P, CurrDensTime, StartTimeDo);
193 if (*SuperStop != 1) {
194 if ((R->DoFullCurrent == 1) || ((R->DoFullCurrent == 2) && (CheckOrbitalOverlap(P) == 1))) { //test to check whether orbitals have mutual overlap and thus \\DeltaJ_{xc} must not be dropped
195 R->DoFullCurrent = 1; // set to 1 if it was 2 but Check...() yielded necessity
196 //debug(P,"Filling with Delta j ...");
197 //FillDeltaCurrentDensity(P);
198 }// else
199 //debug(P,"There is no overlap between orbitals.");
200 //debug(P,"Filling with j ...");
201 FillCurrentDensity(P);
202 }
203 SpeedMeasure(P, CurrDensTime, StopTimeDo);
204
205 SetGramSchExtraPsi(P,Psi,NotUsedToOrtho); // remove extra Psis from orthogonality check
206 ResetGramSchTagType(P, Psi, type, NotUsedToOrtho); // remove this group from the check for the next minimisation group as well!
207 }
208 UpdateActualPsiNo(P, Occupied); // step on back to an occupied one
209}
210
211/** Tests overlap matrix between each pair of orbitals for non-diagonal form.
212 * We simply check whether the overlap matrix Psis#lambda has off-diagonal entries greater MYEPSILON or not.
213 * \param *P Problem at hand
214 * \note The routine is meant as atest criteria if \f$\Delta J_[ij]\f$ contribution is necessary, as it is only non-zero if
215 * there is mutual overlap between the two orbitals.
216 */
217int CheckOrbitalOverlap(struct Problem *P)
218{
219 struct Lattice *Lat = &P->Lat;
220 struct Psis *Psi = &Lat->Psi;
221 int i,j;
222 int counter = 0;
223
224 // output matrix
225 if (P->Par.me == 0) fprintf(stderr, "(%i) S_ij =\n", P->Par.me);
226 for (i=0;i<Psi->NoOfPsis;i++) {
227 for (j=0;j<Psi->NoOfPsis;j++) {
228 if (fabs(Psi->lambda[i][j]) > MYEPSILON) counter++;
229 if (P->Par.me == 0) fprintf(stderr, "%e\t", Psi->lambda[i][j]); //Overlap[i][j]
230 }
231 if (P->Par.me == 0) fprintf(stderr, "\n");
232 }
233
234 fprintf(stderr, "(%i) CheckOverlap: %i overlaps found.\t", P->Par.me, counter);
235 if (counter > 0) return (1);
236 else return(0);
237}
238
239/** Initialization of perturbed energy.
240 * For each local wave function of the current minimisation type RunStruct#CurrentMin it is called:
241 * - CalculateNonLocalEnergyNoRT(): for the coefficient-dependent form factors
242 * - CalculatePerturbedEnergy(): for the perturbed energy, yet without gradient calculation
243 * - CalculateOverlap(): for the overlap between the perturbed wave functions of the current RunStruct#CurrentMin state.
244 *
245 * Afterwards for the two types AllPsiEnergyTypes#Perturbed1_0Energy and AllPsiEnergyTypes#Perturbed0_1Energy the
246 * energy contribution from each wave function is added up in Energy#AllLocalPsiEnergy.
247 * \param *P Problem at hand
248 * \param first state whether it is the first (1) or successive call (0), which avoids some initial calculations.
249 * \sa UpdatePerturbedEnergy()
250 * \note Afterwards EnergyAllReduce() must be called.
251 */
252void InitPerturbedEnergyCalculation(struct Problem *P, const int first)
253{
254 struct Lattice *Lat = &(P->Lat);
255 int p,i;
256 const enum PsiTypeTag state = P->R.CurrentMin;
257 for (p=Lat->Psi.TypeStartIndex[state]; p < Lat->Psi.TypeStartIndex[state+1]; p++) {
258 //if (p < 0 || p >= Lat->Psi.LocalNo) Error(SomeError,"InitPerturbedEnergyCalculation: p out of range");
259 //CalculateNonLocalEnergyNoRT(P, p); // recalculating non-local form factors which are coefficient dependent!
260 CalculatePsiEnergy(P,p,1);
261 CalculatePerturbedEnergy(P, p, 0, first);
262 CalculateOverlap(P, p, state);
263 }
264 for (i=0; i<= Perturbed0_1Energy; i++) {
265 Lat->E->AllLocalPsiEnergy[i] = 0.0;
266 for (p=0; p < Lat->Psi.LocalNo; p++)
267 if (P->Lat.Psi.LocalPsiStatus[p].PsiType == state)
268 Lat->E->AllLocalPsiEnergy[i] += Lat->E->PsiEnergy[i][p];
269 }
270}
271
272
273/** Updating of perturbed energy.
274 * For current and former (if not the same) local wave function RunStruct#ActualLocal, RunStruct#OldActualLocalPsiNo it is called:
275 * - CalculateNonLocalEnergyNoRT(): for the form factors
276 * - CalculatePerturbedEnergy(): for the perturbed energy, gradient only for RunStruct#ActualLocal
277 * - CalculatePerturbedOverlap(): for the overlap between the perturbed wave functions
278 *
279 * Afterwards for the two types AllPsiEnergyTypes#Perturbed1_0Energy and AllPsiEnergyTypes#Perturbed0_1Energy the
280 * energy contribution from each wave function is added up in Energy#AllLocalPsiEnergy.
281 * \param *P Problem at hand
282 * \sa CalculatePerturbedEnergy() called from here.
283 * \note Afterwards EnergyAllReduce() must be called.
284 */
285void UpdatePerturbedEnergyCalculation(struct Problem *P)
286{
287 struct Lattice *Lat = &(P->Lat);
288 struct Psis *Psi = &Lat->Psi;
289 struct RunStruct *R = &P->R;
290 const enum PsiTypeTag state = R->CurrentMin;
291 int p = R->ActualLocalPsiNo;
292 const int p_old = R->OldActualLocalPsiNo;
293 int i;
294
295 if (p != p_old) {
296 //if (p_old < 0 || p_old >= Lat->Psi.LocalNo) Error(SomeError,"UpdatePerturbedEnergyCalculation: p_old out of range");
297 //CalculateNonLocalEnergyNoRT(P, p_old);
298 CalculatePsiEnergy(P,p_old,0);
299 CalculatePerturbedEnergy(P, p_old, 0, 0);
300 CalculateOverlap(P, p_old, state);
301 }
302 //if (p < 0 || p >= Lat->Psi.LocalNo) Error(SomeError,"InitPerturbedEnergyCalculation: p out of range");
303 // recalculating non-local form factors which are coefficient dependent!
304 //CalculateNonLocalEnergyNoRT(P,p);
305 CalculatePsiEnergy(P,p,0);
306 CalculatePerturbedEnergy(P, p, 1, 0);
307 CalculateOverlap(P, p, state);
308
309 for (i=0; i<= Perturbed0_1Energy; i++) {
310 Lat->E->AllLocalPsiEnergy[i] = 0.0;
311 for (p=0; p < Psi->LocalNo; p++)
312 if (Psi->LocalPsiStatus[p].PsiType == state)
313 Lat->E->AllLocalPsiEnergy[i] += Lat->E->PsiEnergy[i][p];
314 }
315}
316
317/** Calculates gradient and evaluates second order perturbed energy functional for specific wave function.
318 * The in second order perturbed energy functional reads as follows.
319 * \f[
320 * E^{(2)} = \sum_{kl} \langle \varphi_k^{(1)} | H^{(0)} \delta_{kl} - \lambda_{kl} | \varphi_l^{(1)} \rangle
321 * + \underbrace{\langle \varphi_l^{(0)} | H^{(1)} | \varphi_l^{(1)} \rangle + \langle \varphi_l^{(1)} | H^{(1)} | \varphi_l^{(0)} \rangle}_{2 {\cal R} \langle \varphi_l^{(1)} | H^{(1)} | \varphi_l^{(0)} \rangle}
322 * \f]
323 * And the gradient
324 * \f[
325 * \widetilde{\varphi}_k^{(1)} = - \sum_l ({\cal H}^{(0)} \delta_{kl} - \lambda_{kl} | \varphi_l^{(1)} \rangle + {\cal H}^{(1)} | \varphi_k^{(0)} \rangle
326 * \f]
327 * First, the HGDensity is recalculated if \a first says so - see ApplyTotalHamiltonian().
328 *
329 * Next, we need the perturbation hamiltonian acting on both the respective occupied and current wave function,
330 * see perturbed.c for respective function calls.
331 *
332 * Finally, the scalar product between the wave function and Hc_Gradient yields the eigenvalue of the hamiltonian,
333 * which is summed up over all reciprocal grid vectors and stored in OnePsiElementAddData#Lambda. The Gradient is
334 * the inverse of Hc_Gradient and with the following summation over all perturbed wave functions (MPI exchange of
335 * non-local coefficients) the gradient is computed. Here we need Psis#lambda, which is computed in CalculateHamiltonian().
336 *
337 * Also \f${\cal H}^{(1)} | \varphi_l^{(0)} \rangle\f$ is stored in GradientTypes#H1cGradient.
338 * \param *P Problem at hand, contains RunStruct, Lattice, LatticeLevel RunStruct#LevS
339 * \param l offset of perturbed wave function within Psi#LocalPsiStatus (\f$\varphi_l^{(1)}\f$)
340 * \param DoGradient (1 = yes, 0 = no) whether gradient shall be calculated or not
341 * \param first recaculate HGDensity (1) or not (0)
342 * \note DensityTypes#ActualPsiDensity must be recent for gradient calculation!
343 * \sa CalculateGradientNoRT() - same procedure for evaluation of \f${\cal H}^{(0)}| \varphi_l^{(1)} \rangle\f$
344 * \note without the simplification of \f$2 {\cal R} \langle \varphi_l^{(1)} | H^{(1)} | \varphi_l^{(0)} \rangle\f$ the
345 * calculation would be impossible due to non-local nature of perturbed wave functions. The position operator would
346 * be impossible to apply in a sensible manner.
347 */
348void CalculatePerturbedEnergy(struct Problem *P, const int l, const int DoGradient, const int first)
349{
350 struct Lattice *Lat = &P->Lat;
351 struct Psis *Psi = &Lat->Psi;
352 struct Energy *E = Lat->E;
353 struct PseudoPot *PP = &P->PP;
354 struct RunStruct *R = &P->R;
355 struct LatticeLevel *LevS = R->LevS;
356 const int state = R->CurrentMin;
357 const int l_normal = Psi->TypeStartIndex[Occupied] + (l - Psi->TypeStartIndex[state]); // offset l to \varphi_l^{(0)}
358 const int ActNum = l - Psi->TypeStartIndex[state] + Psi->TypeStartIndex[1] * Psi->LocalPsiStatus[l].my_color_comm_ST_Psi;
359 int g, i, m, j;
360 double lambda, Lambda;
361 double RElambda10, RELambda10;
362 const fftw_complex *source = LevS->LPsi->LocalPsi[l];
363 fftw_complex *grad = P->Grad.GradientArray[ActualGradient];
364 fftw_complex *Hc_grad = P->Grad.GradientArray[HcGradient];
365 fftw_complex *H1c_grad = P->Grad.GradientArray[H1cGradient];
366 fftw_complex *TempPsi_0 = H1c_grad;
367 fftw_complex *varphi_1, *varphi_0;
368 struct OnePsiElement *OnePsiB, *LOnePsiB;
369 fftw_complex *LPsiDatB=NULL;
370 const int ElementSize = (sizeof(fftw_complex) / sizeof(double));
371 int RecvSource;
372 MPI_Status status;
373
374 // ============ Calculate H^(0) psi^(1) =============================
375 //if (Hc_grad != P->Grad.GradientArray[HcGradient]) Error(SomeError,"CalculatePerturbedEnergy: Hc_grad corrupted");
376 SetArrayToDouble0((double *)Hc_grad,2*R->InitLevS->MaxG);
377 ApplyTotalHamiltonian(P,source,Hc_grad, PP->fnl[l], 1, first);
378
379 // ============ ENERGY FUNCTIONAL Evaluation PART 1 ================
380 //if (l_normal < 0 || l_normal >= Psi->LocalNo) Error(SomeError,"CalculatePerturbedEnergy: l_normal out of range");
381 varphi_0 = LevS->LPsi->LocalPsi[l_normal];
382 //if (l < 0 || l >= Psi->LocalNo) Error(SomeError,"CalculatePerturbedEnergy: l out of range");
383 varphi_1 = LevS->LPsi->LocalPsi[l];
384 //if (TempPsi_0 != P->Grad.GradientArray[H1cGradient]) Error(SomeError,"CalculatePerturbedEnergy: TempPsi_0 corrupted");
385 SetArrayToDouble0((double *)TempPsi_0,2*R->InitLevS->MaxG);
386 switch (state) {
387 case Perturbed_P0:
388 CalculatePerturbationOperator_P(P,varphi_0,TempPsi_0,0); // \nabla_0 | \varphi_l^{(0)} \rangle
389 break;
390 case Perturbed_P1:
391 CalculatePerturbationOperator_P(P,varphi_0,TempPsi_0,1); // \nabla_1 | \varphi_l^{(0)} \rangle
392 break;
393 case Perturbed_P2:
394 CalculatePerturbationOperator_P(P,varphi_0,TempPsi_0,2); // \nabla_1 | \varphi_l^{(0)} \rangle
395 break;
396 case Perturbed_RxP0:
397 CalculatePerturbationOperator_RxP(P,varphi_0,TempPsi_0,l_normal,0); // r \times \nabla | \varphi_l^{(0)} \rangle
398 break;
399 case Perturbed_RxP1:
400 CalculatePerturbationOperator_RxP(P,varphi_0,TempPsi_0,l_normal,1); // r \times \nabla | \varphi_l^{(0)} \rangle
401 break;
402 case Perturbed_RxP2:
403 CalculatePerturbationOperator_RxP(P,varphi_0,TempPsi_0,l_normal,2); // r \times \nabla | \varphi_l^{(0)} \rangle
404 break;
405 default:
406 fprintf(stderr,"(%i) CalculatePerturbedEnergy called whilst not within perturbation run: CurrentMin = %i !\n",P->Par.me, R->CurrentMin);
407 break;
408 }
409
410 // ============ GRADIENT and EIGENVALUE Evaluation Part 1==============
411 lambda = 0.0;
412 if ((DoGradient) && (grad != NULL)) {
413 g = 0;
414 if (LevS->GArray[0].GSq == 0.0) {
415 lambda += Hc_grad[0].re*source[0].re;
416 //if (grad != P->Grad.GradientArray[ActualGradient]) Error(SomeError,"CalculatePerturbedEnergy: grad corrupted");
417 grad[0].re = -(Hc_grad[0].re + TempPsi_0[0].re);
418 grad[0].im = -(Hc_grad[0].im + TempPsi_0[0].im);
419 g++;
420 }
421 for (;g<LevS->MaxG;g++) {
422 lambda += 2.*(Hc_grad[g].re*source[g].re + Hc_grad[g].im*source[g].im);
423 //if (grad != P->Grad.GradientArray[ActualGradient] || g<0 || g>=LevS->MaxG) Error(SomeError,"CalculatePerturbedEnergy: grad corrupted");
424 grad[g].re = -(Hc_grad[g].re + TempPsi_0[g].re);
425 grad[g].im = -(Hc_grad[g].im + TempPsi_0[g].im);
426 }
427
428 m = -1;
429 for (j=0; j < Psi->MaxPsiOfType+P->Par.Max_me_comm_ST_PsiT; j++) { // go through all wave functions
430 OnePsiB = &Psi->AllPsiStatus[j]; // grab OnePsiB
431 if (OnePsiB->PsiType == state) { // drop all but the ones of current min state
432 m++; // increase m if it is type-specific wave function
433 if (OnePsiB->my_color_comm_ST_Psi == P->Par.my_color_comm_ST_Psi) // local?
434 LOnePsiB = &Psi->LocalPsiStatus[OnePsiB->MyLocalNo];
435 else
436 LOnePsiB = NULL;
437 if (LOnePsiB == NULL) { // if it's not local ... receive it from respective process into TempPsi
438 RecvSource = OnePsiB->my_color_comm_ST_Psi;
439 MPI_Recv( LevS->LPsi->TempPsi, LevS->MaxG*ElementSize, MPI_DOUBLE, RecvSource, PerturbedTag, P->Par.comm_ST_PsiT, &status );
440 LPsiDatB=LevS->LPsi->TempPsi;
441 } else { // .. otherwise send it to all other processes (Max_me... - 1)
442 for (i=0;i<P->Par.Max_me_comm_ST_PsiT;i++)
443 if (i != OnePsiB->my_color_comm_ST_Psi)
444 MPI_Send( LevS->LPsi->LocalPsi[OnePsiB->MyLocalNo], LevS->MaxG*ElementSize, MPI_DOUBLE, i, PerturbedTag, P->Par.comm_ST_PsiT);
445 LPsiDatB=LevS->LPsi->LocalPsi[OnePsiB->MyLocalNo];
446 } // LPsiDatB is now set to the coefficients of OnePsi either stored or MPI_Received
447
448 g = 0;
449 if (LevS->GArray[0].GSq == 0.0) { // perform the summation
450 //if (grad != P->Grad.GradientArray[ActualGradient]) Error(SomeError,"CalculatePerturbedEnergy: grad corrupted");
451 grad[0].re += Lat->Psi.lambda[ActNum][m]*LPsiDatB[0].re;
452 grad[0].im += Lat->Psi.lambda[ActNum][m]*LPsiDatB[0].im;
453 g++;
454 }
455 for (;g<LevS->MaxG;g++) {
456 //if (grad != P->Grad.GradientArray[ActualGradient] || g<0 || g>=LevS->MaxG) Error(SomeError,"CalculatePerturbedEnergy: grad corrupted");
457 grad[g].re += Lat->Psi.lambda[ActNum][m]*LPsiDatB[g].re;
458 grad[g].im += Lat->Psi.lambda[ActNum][m]*LPsiDatB[g].im;
459 }
460 }
461 }
462 } else {
463 lambda = GradSP(P,LevS,Hc_grad,source);
464 }
465 MPI_Allreduce ( &lambda, &Lambda, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
466 //fprintf(stderr,"(%i) Lambda[%i] = %lg\n",P->Par.me, l, Lambda);
467 //if (l < 0 || l >= Psi->LocalNo) Error(SomeError,"CalculatePerturbedEnergy: l out of range");
468 Lat->Psi.AddData[l].Lambda = Lambda;
469
470 // ============ ENERGY FUNCTIONAL Evaluation PART 2 ================
471 // varphi_1 jas negative symmetry, returning TempPsi_0 from CalculatePerturbedOperator also, thus real part of scalar product
472 // "-" due to purely imaginary wave function is on left hand side, thus becomes complex conjugated: i -> -i
473 // (-i goes into pert. op., "-" remains when on right hand side)
474 RElambda10 = GradSP(P,LevS,varphi_1,TempPsi_0) * sqrt(Psi->LocalPsiStatus[l].PsiFactor * Psi->LocalPsiStatus[l_normal].PsiFactor);
475 //RElambda01 = -GradSP(P,LevS,varphi_0,TempPsi_1) * sqrt(Psi->LocalPsiStatus[l].PsiFactor * Psi->LocalPsiStatus[l_normal].PsiFactor);
476
477 MPI_Allreduce ( &RElambda10, &RELambda10, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
478 //MPI_Allreduce ( &RElambda01, &RELambda01, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
479
480 //if (l < 0 || l >= Psi->LocalNo) Error(SomeError,"CalculatePerturbedEnergy: l out of range");
481 E->PsiEnergy[Perturbed1_0Energy][l] = RELambda10;
482 E->PsiEnergy[Perturbed0_1Energy][l] = RELambda10;
483// if (P->Par.me == 0) {
484// fprintf(stderr,"RE.Lambda10[%i-%i] = %lg\t RE.Lambda01[%i-%i] = %lg\n", l, l_normal, RELambda10, l_normal, l, RELambda01);
485// }
486 // GradImSP() is only applicable to a product of wave functions with uneven symmetry!
487 // Otherwise, due to the nature of symmetry, a sum over only half of the coefficients will in most cases not result in zero!
488}
489
490/** Applies \f$H^{(0)}\f$ to a given \a source.
491 * The DensityTypes#HGDensity is computed, the exchange potential added and the
492 * whole multiplied - coefficient by coefficient - with the current wave function, taken from its density coefficients,
493 * on the upper LatticeLevel (RunStruct#Lev0), which (DensityTypes#ActualPsiDensity) is updated beforehand.
494 * After an inverse fft (now G-dependent) the non-local potential is added and
495 * within the reciprocal basis set, the kinetic energy can be evaluated easily.
496 * \param *P Problem at hand
497 * \param *source pointer to source coefficient array, \f$| \varphi(G) \rangle\f$
498 * \param *dest pointer to dest coefficient array,\f$H^{(0)} | \varphi(G) \rangle\f$
499 * \param **fnl pointer to non-local form factor array
500 * \param PsiFactor occupation number of orbital
501 * \param first 1 - Re-calculate DensityTypes#HGDensity, 0 - don't
502 * \sa CalculateConDirHConDir() - same procedure
503 */
504void ApplyTotalHamiltonian(struct Problem *P, const fftw_complex *source, fftw_complex *dest, fftw_complex ***fnl, const double PsiFactor, const int first) {
505 struct Lattice *Lat = &P->Lat;
506 struct RunStruct *R = &P->R;
507 struct LatticeLevel *LevS = R->LevS;
508 struct LatticeLevel *Lev0 = R->Lev0;
509 struct Density *Dens0 = Lev0->Dens;
510 struct fft_plan_3d *plan = Lat->plan;
511 struct PseudoPot *PP = &P->PP;
512 struct Ions *I = &P->Ion;
513 fftw_complex *work = Dens0->DensityCArray[TempDensity];
514 fftw_real *HGcR = Dens0->DensityArray[HGcDensity];
515 fftw_complex *HGcRC = (fftw_complex*)HGcR;
516 fftw_complex *HGC = Dens0->DensityCArray[HGDensity];
517 fftw_real *HGCR = (fftw_real *)HGC;
518 fftw_complex *PsiC = Dens0->DensityCArray[ActualPsiDensity];
519 fftw_real *PsiCR = (fftw_real *)PsiC;
520 //const fftw_complex *dest_bak = dest;
521 int nx,ny,nz,iS,i0;
522 const int Nx = LevS->Plan0.plan->local_nx;
523 const int Ny = LevS->Plan0.plan->N[1];
524 const int Nz = LevS->Plan0.plan->N[2];
525 const int NUpx = LevS->NUp[0];
526 const int NUpy = LevS->NUp[1];
527 const int NUpz = LevS->NUp[2];
528 const double HGcRCFactor = 1./LevS->MaxN;
529 int g, Index, i, it;
530 fftw_complex vp,rp,rhog,TotalPsiDensity;
531 double Fac;
532
533 if (first) {
534 // recalculate HGDensity
535 //if (HGC != Dens0->DensityCArray[HGDensity]) Error(SomeError,"ApplyTotalHamiltonian: HGC corrupted");
536 SetArrayToDouble0((double *)HGC,2*Dens0->TotalSize);
537 g=0;
538 if (Lev0->GArray[0].GSq == 0.0) {
539 Index = Lev0->GArray[0].Index;
540 c_re(vp) = 0.0;
541 c_im(vp) = 0.0;
542 for (it = 0; it < I->Max_Types; it++) {
543 c_re(vp) += (c_re(I->I[it].SFactor[0])*PP->phi_ps_loc[it][0]);
544 c_im(vp) += (c_im(I->I[it].SFactor[0])*PP->phi_ps_loc[it][0]);
545 }
546 //if (HGC != Dens0->DensityCArray[HGDensity] || Index<0 || Index>=Dens0->LocalSizeC) Error(SomeError,"ApplyTotalHamiltonian: HGC corrupted");
547 c_re(HGC[Index]) = c_re(vp);
548 c_re(TotalPsiDensity) = c_re(Dens0->DensityCArray[TotalDensity][Index]);
549 c_im(TotalPsiDensity) = c_im(Dens0->DensityCArray[TotalDensity][Index]);
550
551 g++;
552 }
553 for (; g < Lev0->MaxG; g++) {
554 Index = Lev0->GArray[g].Index;
555 Fac = 4.*PI/(Lev0->GArray[g].GSq);
556 c_re(vp) = 0.0;
557 c_im(vp) = 0.0;
558 c_re(rp) = 0.0;
559 c_im(rp) = 0.0;
560 for (it = 0; it < I->Max_Types; it++) {
561 c_re(vp) += (c_re(I->I[it].SFactor[g])*PP->phi_ps_loc[it][g]);
562 c_im(vp) += (c_im(I->I[it].SFactor[g])*PP->phi_ps_loc[it][g]);
563 c_re(rp) += (c_re(I->I[it].SFactor[g])*PP->FacGauss[it][g]);
564 c_im(rp) += (c_im(I->I[it].SFactor[g])*PP->FacGauss[it][g]);
565 } // rp = n^{Gauss)(G)
566
567 // n^{tot} = n^0 + \lambda n^1 + ...
568 //if (isnan(c_re(Dens0->DensityCArray[TotalDensity][Index]))) { fprintf(stderr,"(%i) WARNING in CalculatePerturbedEnergy(): TotalDensity[%i] = NaN!\n", P->Par.me, Index); Error(SomeError, "NaN-Fehler!"); }
569 c_re(TotalPsiDensity) = c_re(Dens0->DensityCArray[TotalDensity][Index]);
570 c_im(TotalPsiDensity) = c_im(Dens0->DensityCArray[TotalDensity][Index]);
571
572 c_re(rhog) = c_re(TotalPsiDensity)*R->HGcFactor+c_re(rp);
573 c_im(rhog) = c_im(TotalPsiDensity)*R->HGcFactor+c_im(rp);
574 // rhog = n(G) + n^{Gauss}(G), rhoe = n(G)
575 //if (HGC != Dens0->DensityCArray[HGDensity] || Index<0 || Index>=Dens0->LocalSizeC) Error(SomeError,"ApplyTotalHamiltonian: HGC corrupted");
576 c_re(HGC[Index]) = c_re(vp)+Fac*c_re(rhog);
577 c_im(HGC[Index]) = c_im(vp)+Fac*c_im(rhog);
578 }
579 //
580 for (i=0; i<Lev0->MaxDoubleG; i++) {
581 //if (HGC != Dens0->DensityCArray[HGDensity] || Lev0->DoubleG[2*i+1]<0 || Lev0->DoubleG[2*i+1]>Dens0->LocalSizeC || Lev0->DoubleG[2*i]<0 || Lev0->DoubleG[2*i]>Dens0->LocalSizeC) Error(SomeError,"CalculatePerturbedEnergy: grad corrupted");
582 HGC[Lev0->DoubleG[2*i+1]].re = HGC[Lev0->DoubleG[2*i]].re;
583 HGC[Lev0->DoubleG[2*i+1]].im = -HGC[Lev0->DoubleG[2*i]].im;
584 }
585 }
586 // ============ GRADIENT and EIGENVALUE Evaluation Part 1==============
587 // \lambda_l^{(1)} = \langle \varphi_l^{(1)} | H^{(0)} | \varphi_l^{(1)} \rangle and gradient calculation
588 SpeedMeasure(P, LocTime, StartTimeDo);
589 // back-transform HGDensity: (G) -> (R)
590 //if (HGC != Dens0->DensityCArray[HGDensity]) Error(SomeError,"ApplyTotalHamiltonian: HGC corrupted");
591 if (first) fft_3d_complex_to_real(plan, Lev0->LevelNo, FFTNF1, HGC, work);
592 // evaluate exchange potential with this density, add up onto HGCR
593 //if (HGCR != (fftw_real *)Dens0->DensityCArray[HGDensity]) Error(SomeError,"ApplyTotalHamiltonian: HGCR corrupted");
594 if (first) CalculateXCPotentialNoRT(P, HGCR); // add V^{xc} on V^H + V^{ps}
595 // make sure that ActualPsiDensity is recent
596 CalculateOneDensityR(Lat, LevS, Dens0, source, Dens0->DensityArray[ActualDensity], R->FactorDensityR*PsiFactor, 1);
597 for (nx=0;nx<Nx;nx++)
598 for (ny=0;ny<Ny;ny++)
599 for (nz=0;nz<Nz;nz++) {
600 i0 = nz*NUpz+Nz*NUpz*(ny*NUpy+Ny*NUpy*nx*NUpx);
601 iS = nz+Nz*(ny+Ny*nx);
602 //if (HGcR != Dens0->DensityArray[HGcDensity] || iS<0 || iS>=LevS->Dens->LocalSizeR) Error(SomeError,"ApplyTotalHamiltonian: HGC corrupted");
603 HGcR[iS] = HGCR[i0]*PsiCR[i0]; /* Matrix Vector Mult */
604 }
605 // (R) -> (G)
606 //if (HGcRC != (fftw_complex *)Dens0->DensityArray[HGcDensity]) Error(SomeError,"ApplyTotalHamiltonian: HGcRC corrupted");
607 fft_3d_real_to_complex(plan, LevS->LevelNo, FFTNF1, HGcRC, work);
608 SpeedMeasure(P, LocTime, StopTimeDo);
609 /* NonLocalPP */
610 SpeedMeasure(P, NonLocTime, StartTimeDo);
611 //if (dest != dest_bak) Error(SomeError,"ApplyTotalHamiltonian: dest corrupted");
612 CalculateAddNLPot(P, dest, fnl, PsiFactor); // wave function hidden in form factors fnl, also resets Hc_grad beforehand
613 SpeedMeasure(P, NonLocTime, StopTimeDo);
614
615 /* create final vector */
616 for (g=0;g<LevS->MaxG;g++) {
617 Index = LevS->GArray[g].Index; /* FIXME - factoren */
618 //if (dest != dest_bak || g<0 || g>=LevS->MaxG) Error(SomeError,"ApplyTotalHamiltonian: dest corrupted");
619 dest[g].re += PsiFactor*(HGcRC[Index].re*HGcRCFactor + 0.5*LevS->GArray[g].GSq*source[g].re);
620 dest[g].im += PsiFactor*(HGcRC[Index].im*HGcRCFactor + 0.5*LevS->GArray[g].GSq*source[g].im);
621 }
622}
623
624#define stay_above 0.001 //!< value above which the coefficient of the wave function will always remain
625
626/** Finds the minimum of perturbed energy in regards of actual wave function.
627 * The following happens step by step:
628 * -# The Gradient is copied into GradientTypes#GraSchGradient (which is nothing but a pointer to
629 * one array in LPsiDat) and orthonormalized via GramSch() to all occupied wave functions
630 * except to the current perturbed one.
631 * -# Then comes pre-conditioning, analogous to CalculatePreConGrad().
632 * -# The Gradient is projected onto the current perturbed wave function and this is subtracted, i.e.
633 * vector is the conjugate gradient.
634 * -# Finally, Calculate1stPerturbedDerivative() and Calculate2ndPerturbedDerivative() are called and
635 * with these results and the current total energy, CalculateDeltaI() finds the parameter for the one-
636 * dimensional minimisation. The current wave function is set to newly found minimum and approximated
637 * total energy is printed.
638 *
639 * \param *P Problem at hand
640 * \sa CalculateNewWave() and functions therein
641 */
642void FindPerturbedMinimum(struct Problem *P)
643{
644 struct Lattice *Lat = &P->Lat;
645 struct RunStruct *R = &P->R;
646 struct Psis *Psi = &Lat->Psi;
647 struct PseudoPot *PP = &P->PP;
648 struct LatticeLevel *LevS = R->LevS;
649 struct LatticeLevel *Lev0 = R->Lev0;
650 struct Density *Dens = Lev0->Dens;
651 struct Energy *En = Lat->E;
652 struct FileData *F = &P->Files;
653 int g,p,i;
654 int step = R->PsiStep;
655 double *GammaDiv = &Lat->Psi.AddData[R->ActualLocalPsiNo].Gamma;
656 const int ElementSize = (sizeof(fftw_complex) / sizeof(double));
657 fftw_complex *source = LevS->LPsi->LocalPsi[R->ActualLocalPsiNo];
658 fftw_complex *grad = P->Grad.GradientArray[ActualGradient];
659 fftw_complex *GradOrtho = P->Grad.GradientArray[GraSchGradient];
660 fftw_complex *PCgrad = P->Grad.GradientArray[PreConGradient];
661 fftw_complex *PCOrtho = P->Grad.GradientArray[GraSchGradient];
662 fftw_complex *ConDir = P->Grad.GradientArray[ConDirGradient];
663 fftw_complex *ConDir_old = P->Grad.GradientArray[OldConDirGradient];
664 fftw_complex *Ortho = P->Grad.GradientArray[GraSchGradient];
665 const fftw_complex *Hc_grad = P->Grad.GradientArray[HcGradient];
666 const fftw_complex *H1c_grad = P->Grad.GradientArray[H1cGradient];
667 fftw_complex *HConDir = Dens->DensityCArray[ActualDensity];
668 const double PsiFactor = Lat->Psi.LocalPsiStatus[R->ActualLocalPsiNo].PsiFactor;
669 //double Lambda = Lat->Psi.AddData[R->ActualLocalPsiNo].Lambda;
670 double T;
671 double x, K; //, dK;
672 double dS[2], S[2], Gamma, GammaDivOld = *GammaDiv;
673 double LocalSP, PsiSP;
674 double dEdt0, ddEddt0, ConDirHConDir, ConDirConDir;//, sourceHsource;
675 double E0, E, delta;
676 //double E0, E, dE, ddE, delta, dcos, dsin;
677 //double EI, dEI, ddEI, deltaI, dcosI, dsinI;
678 //double HartreeddEddt0, XCddEddt0;
679 double d[4],D[4], Diff;
680 const int Num = Psi->NoOfPsis;
681
682 // ORTHOGONALIZED-GRADIENT
683 for (g=0;g<LevS->MaxG;g++) {
684 //if (GradOrtho != P->Grad.GradientArray[GraSchGradient] || g<0 || g>=LevS->MaxG) Error(SomeError,"FindPerturbedMinimum: GradOrtho corrupted");
685 GradOrtho[g].re = grad[g].re; //+Lambda*source[g].re;
686 GradOrtho[g].im = grad[g].im; //+Lambda*source[g].im;
687 }
688 // include the ExtraPsi (which is the GraSchGradient!)
689 SetGramSchExtraPsi(P, Psi, NotOrthogonal);
690 // exclude the minimised Psi
691 SetGramSchActualPsi(P, Psi, NotUsedToOrtho);
692 SpeedMeasure(P, GramSchTime, StartTimeDo);
693 // makes conjugate gradient orthogonal to all other orbits
694 //fprintf(stderr,"CalculateCGGradient: GramSch() for extra orbital\n");
695 GramSch(P, LevS, Psi, Orthogonalize);
696 SpeedMeasure(P, GramSchTime, StopTimeDo);
697 //if (grad != P->Grad.GradientArray[ActualGradient]) Error(SomeError,"FindPerturbedMinimum: grad corrupted");
698 memcpy(grad, GradOrtho, ElementSize*LevS->MaxG*sizeof(double));
699 //memcpy(PCOrtho, GradOrtho, ElementSize*LevS->MaxG*sizeof(double));
700
701 // PRE-CONDITION-GRADIENT
702 //if (fabs(T) < MYEPSILON) T = 1;
703 T = 0.;
704 for (i=0;i<Num;i++)
705 T += Psi->lambda[i][i];
706 for (g=0;g<LevS->MaxG;g++) {
707 x = .5*LevS->GArray[g].GSq;
708 // FIXME: Good way of accessing reciprocal Lev0 Density coefficients on LevS! (not so trivial)
709 //x += sqrt(Dens->DensityCArray[HGDensity][g].re*Dens->DensityCArray[HGDensity][g].re+Dens->DensityCArray[HGDensity][g].im*Dens->DensityCArray[HGDensity][g].im);
710 x -= T/(double)Num;
711 K = x/(x*x+stay_above*stay_above);
712 //if (PCOrtho != P->Grad.GradientArray[GraSchGradient] || g<0 || g>=LevS->MaxG) Error(SomeError,"FindPerturbedMinimum: PCOrtho corrupted");
713 c_re(PCOrtho[g]) = K*c_re(grad[g]);
714 c_im(PCOrtho[g]) = K*c_im(grad[g]);
715 }
716 SetGramSchExtraPsi(P, Psi, NotOrthogonal);
717 SpeedMeasure(P, GramSchTime, StartTimeDo);
718 // preconditioned direction is orthogonalized
719 //fprintf(stderr,"CalculatePreConGrad: GramSch() for extra orbital\n");
720 GramSch(P, LevS, Psi, Orthogonalize);
721 SpeedMeasure(P, GramSchTime, StopTimeDo);
722 //if (PCgrad != P->Grad.GradientArray[PreConGradient]) Error(SomeError,"FindPerturbedMinimum: PCgrad corrupted");
723 memcpy(PCgrad, PCOrtho, ElementSize*LevS->MaxG*sizeof(double));
724
725 //debug(P, "Before ConDir");
726 //fprintf(stderr,"|(%i)|^2 = %lg\t |PCgrad|^2 = %lg\t |PCgrad,(%i)| = %lg\n", R->ActualLocalPsiNo, GradSP(P,LevS,source,source),GradSP(P,LevS,PCgrad,PCgrad), R->ActualLocalPsiNo, GradSP(P,LevS,PCgrad,source));
727 // CONJUGATE-GRADIENT
728 LocalSP = GradSP(P, LevS, PCgrad, grad);
729 MPI_Allreduce ( &LocalSP, &PsiSP, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
730 *GammaDiv = dS[0] = PsiSP;
731 dS[1] = GammaDivOld;
732 S[0]=dS[0]; S[1]=dS[1];
733 /*MPI_Allreduce ( dS, S, 2, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_PsiT);*/
734 if (step) { // only in later steps is the scalar product used, but always condir stored in oldcondir and Ortho (working gradient)
735 if (fabs(S[1]) < MYEPSILON) fprintf(stderr,"CalculateConDir: S[1] = %lg\n",S[1]);
736 Gamma = S[0]/S[1];
737 if (fabs(S[1]) < MYEPSILON) {
738 if (fabs(S[0]) < MYEPSILON)
739 Gamma = 1.0;
740 else
741 Gamma = 0.0;
742 }
743 for (g=0; g < LevS->MaxG; g++) {
744 //if (ConDir != P->Grad.GradientArray[ConDirGradient] || g<0 || g>=LevS->MaxG) Error(SomeError,"FindPerturbedMinimum: ConDir corrupted");
745 c_re(ConDir[g]) = c_re(PCgrad[g]) + Gamma*c_re(ConDir_old[g]);
746 c_im(ConDir[g]) = c_im(PCgrad[g]) + Gamma*c_im(ConDir_old[g]);
747 //if (ConDir_old != P->Grad.GradientArray[OldConDirGradient] || g<0 || g>=LevS->MaxG) Error(SomeError,"FindPerturbedMinimum: ConDir_old corrupted");
748 c_re(ConDir_old[g]) = c_re(ConDir[g]);
749 c_im(ConDir_old[g]) = c_im(ConDir[g]);
750 //if (Ortho != P->Grad.GradientArray[GraSchGradient] || g<0 || g>=LevS->MaxG) Error(SomeError,"FindPerturbedMinimum: Ortho corrupted");
751 c_re(Ortho[g]) = c_re(ConDir[g]);
752 c_im(Ortho[g]) = c_im(ConDir[g]);
753 }
754 } else {
755 Gamma = 0.0;
756 for (g=0; g < LevS->MaxG; g++) {
757 //if (ConDir != P->Grad.GradientArray[ConDirGradient] || g<0 || g>=LevS->MaxG) Error(SomeError,"FindPerturbedMinimum: ConDir corrupted");
758 c_re(ConDir[g]) = c_re(PCgrad[g]);
759 c_im(ConDir[g]) = c_im(PCgrad[g]);
760 //if (ConDir_old != P->Grad.GradientArray[OldConDirGradient] || g<0 || g>=LevS->MaxG) Error(SomeError,"FindPerturbedMinimum: ConDir_old corrupted");
761 c_re(ConDir_old[g]) = c_re(ConDir[g]);
762 c_im(ConDir_old[g]) = c_im(ConDir[g]);
763 //if (Ortho != P->Grad.GradientArray[GraSchGradient] || g<0 || g>=LevS->MaxG) Error(SomeError,"FindPerturbedMinimum: Ortho corrupted");
764 c_re(Ortho[g]) = c_re(ConDir[g]);
765 c_im(Ortho[g]) = c_im(ConDir[g]);
766 }
767 }
768 // orthonormalize
769 SetGramSchExtraPsi(P, Psi, NotOrthogonal);
770 SpeedMeasure(P, GramSchTime, StartTimeDo);
771 //fprintf(stderr,"CalculateConDir: GramSch() for extra orbital\n");
772 GramSch(P, LevS, Psi, Orthogonalize);
773 SpeedMeasure(P, GramSchTime, StopTimeDo);
774 //if (ConDir != P->Grad.GradientArray[ConDirGradient]) Error(SomeError,"FindPerturbedMinimum: ConDir corrupted");
775 memcpy(ConDir, Ortho, ElementSize*LevS->MaxG*sizeof(double));
776 //debug(P, "Before LineSearch");
777 //fprintf(stderr,"|(%i)|^2 = %lg\t |ConDir|^2 = %lg\t |ConDir,(%i)| = %lg\n", R->ActualLocalPsiNo, GradSP(P,LevS,source,source),GradSP(P,LevS,ConDir,ConDir), R->ActualLocalPsiNo, GradSP(P,LevS,ConDir,source));
778 SetGramSchActualPsi(P, Psi, IsOrthogonal);
779
780 //fprintf(stderr,"(%i) Testing conjugate gradient for Orthogonality ...\n", P->Par.me);
781 //TestForOrth(P,LevS,ConDir);
782
783 // ONE-DIMENSIONAL LINE-SEARCH
784
785 // ========= dE / dt | 0 ============
786 p = Lat->Psi.TypeStartIndex[Occupied] + (R->ActualLocalPsiNo - Lat->Psi.TypeStartIndex[R->CurrentMin]);
787 //if (Hc_grad != P->Grad.GradientArray[HcGradient]) Error(SomeError,"FindPerturbedMinimum: Hc_grad corrupted");
788 //if (H1c_grad != P->Grad.GradientArray[H1cGradient]) Error(SomeError,"FindPerturbedMinimum: H1c_grad corrupted");
789 d[0] = Calculate1stPerturbedDerivative(P, LevS->LPsi->LocalPsi[p], source, ConDir, Hc_grad, H1c_grad);
790 //CalculateConDirHConDir(P, ConDir, PsiFactor, &d[1], &d[2], &d[3]);
791 //if (ConDir != P->Grad.GradientArray[ConDirGradient]) Error(SomeError,"FindPerturbedMinimum: ConDir corrupted");
792 CalculateCDfnl(P, ConDir, PP->CDfnl); // calculate needed non-local form factors
793 //if (HConDir != Dens->DensityCArray[ActualDensity]) Error(SomeError,"FindPerturbedMinimum: HConDir corrupted");
794 SetArrayToDouble0((double *)HConDir,Dens->TotalSize*2);
795 //if (ConDir != P->Grad.GradientArray[ConDirGradient]) Error(SomeError,"FindPerturbedMinimum: ConDir corrupted");
796 ApplyTotalHamiltonian(P,ConDir,HConDir, PP->CDfnl, PsiFactor, 0); // applies H^(0) with total perturbed density!
797 d[1] = GradSP(P,LevS,ConDir,HConDir);
798 d[2] = GradSP(P,LevS,ConDir,ConDir);
799 d[3] = 0.;
800
801 // gather results
802 MPI_Allreduce ( &d, &D, 4, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
803 // ========== ddE / ddt | 0 =========
804 dEdt0 = D[0];
805 for (i=MAXOLD-1; i > 0; i--)
806 En->dEdt0[i] = En->dEdt0[i-1];
807 En->dEdt0[0] = dEdt0;
808 ConDirHConDir = D[1];
809 ConDirConDir = D[2];
810 ddEddt0 = 0.0;
811 //if (ConDir != P->Grad.GradientArray[ConDirGradient]) Error(SomeError,"FindPerturbedMinimum: ConDir corrupted");
812 //if (H1c_grad != P->Grad.GradientArray[H1cGradient]) Error(SomeError,"FindPerturbedMinimum: H1c_grad corrupted");
813 ddEddt0 = Calculate2ndPerturbedDerivative(P, LevS->LPsi->LocalPsi[p], source, ConDir, Lat->Psi.AddData[R->ActualLocalPsiNo].Lambda * Psi->LocalPsiStatus[R->ActualLocalPsiNo].PsiFactor, ConDirHConDir, ConDirConDir);
814
815 for (i=MAXOLD-1; i > 0; i--)
816 En->ddEddt0[i] = En->ddEddt0[i-1];
817 En->ddEddt0[0] = ddEddt0;
818 E0 = En->TotalEnergy[0];
819 // delta
820 //if (isnan(E0)) { fprintf(stderr,"(%i) WARNING in CalculateLineSearch(): E0_%i[%i] = NaN!\n", P->Par.me, i, 0); Error(SomeError, "NaN-Fehler!"); }
821 //if (isnan(dEdt0)) { fprintf(stderr,"(%i) WARNING in CalculateLineSearch(): dEdt0_%i[%i] = NaN!\n", P->Par.me, i, 0); Error(SomeError, "NaN-Fehler!"); }
822 //if (isnan(ddEddt0)) { fprintf(stderr,"(%i) WARNING in CalculateLineSearch(): ddEddt0_%i[%i] = NaN!\n", P->Par.me, i, 0); Error(SomeError, "NaN-Fehler!"); }
823
824 ////deltaI = CalculateDeltaI(E0, dEdt0, ddEddt0,
825 //// &EI, &dEI, &ddEI, &dcosI, &dsinI);
826 ////delta = deltaI; E = EI; dE = dEI; ddE = ddEI; dcos = dcosI; dsin = dsinI;
827 if (ddEddt0 > 0) {
828 delta = - dEdt0/ddEddt0;
829 E = E0 + delta * dEdt0 + delta*delta/2. * ddEddt0;
830 } else {
831 delta = 0.;
832 E = E0;
833 fprintf(stderr,"(%i) Taylor approximation leads not to minimum!\n",P->Par.me);
834 }
835
836 // shift energy delta values
837 for (i=MAXOLD-1; i > 0; i--) {
838 En->delta[i] = En->delta[i-1];
839 En->ATE[i] = En->ATE[i-1];
840 }
841 // store new one
842 En->delta[0] = delta;
843 En->ATE[0] = E;
844 if (En->TotalEnergy[1] != 0.)
845 Diff = fabs(En->TotalEnergy[1] - E0)/(En->TotalEnergy[1] - E0)*fabs((E0 - En->ATE[1])/E0);
846 else
847 Diff = 0.;
848 R->Diffcount += pow(Diff,2);
849
850 // reinstate actual density (only needed for UpdateDensityCalculation) ...
851 //CalculateOneDensityR(Lat, LevS, Dens, source, Dens->DensityArray[ActualDensity], R->FactorDensityR*Psi->LocalPsiStatus[R->ActualLocalPsiNo].PsiFactor, 1);
852 // ... before changing actual local Psi
853 for (g = 0; g < LevS->MaxG; g++) { // Here all coefficients are updated for the new found wave function
854 //if (isnan(ConDir[g].re)) { fprintf(stderr,"WARNGING: CalculateLineSearch(): ConDir_%i(%i) = NaN!\n", R->ActualLocalPsiNo, g); Error(SomeError, "NaN-Fehler!"); }
855 //if (source != LevS->LPsi->LocalPsi[R->ActualLocalPsiNo] || g<0 || g>=LevS->MaxG) Error(SomeError,"FindPerturbedMinimum: source corrupted");
856 ////c_re(source[g]) = c_re(source[g])*dcos + c_re(ConDir[g])*dsin;
857 ////c_im(source[g]) = c_im(source[g])*dcos + c_im(ConDir[g])*dsin;
858 c_re(source[g]) = c_re(source[g]) + c_re(ConDir[g])*delta;
859 c_im(source[g]) = c_im(source[g]) + c_im(ConDir[g])*delta;
860 }
861 if (P->Call.out[StepLeaderOut]) {
862 fprintf(stderr, "(%i,%i,%i)S(%i,%i,%i):\tTE: %e\tATE: %e\t Diff: %e\t --- d: %e\tdEdt0: %e\tddEddt0: %e\n",P->Par.my_color_comm_ST,P->Par.me_comm_ST, P->Par.me_comm_ST_PsiT, R->MinStep, R->ActualLocalPsiNo, R->PsiStep, E0, E, Diff,delta, dEdt0, ddEddt0);
863 //fprintf(stderr, "(%i,%i,%i)S(%i,%i,%i):\tp0: %e p1: %e p2: %e \tATE: %e\t Diff: %e\t --- d: %e\tdEdt0: %e\tddEddt0: %e\n",P->Par.my_color_comm_ST,P->Par.me_comm_ST, P->Par.me_comm_ST_PsiT, R->MinStep, R->ActualLocalPsiNo, R->PsiStep, En->parts[0], En->parts[1], En->parts[2], E, Diff,delta, dEdt0, ddEddt0);
864 }
865 if (P->Par.me == 0) {
866 fprintf(F->MinimisationFile, "%i\t%i\t%i\t%e\t%e\t%e\t%e\t%e\n",R->MinStep, R->ActualLocalPsiNo, R->PsiStep, E0, E, delta, dEdt0, ddEddt0);
867 fflush(F->MinimisationFile);
868 }
869}
870
871/** Applies perturbation operator \f$\nabla_{index}\f$ to \a *source.
872 * As wave functions are stored in the reciprocal basis set, the application is straight-forward,
873 * for every G vector, the by \a index specified component is multiplied with the respective
874 * coefficient. Afterwards, 1/i is applied by flipping real and imaginary components (and an additional minus sign on the new imaginary term).
875 * \param *P Problem at hand
876 * \param *source complex coefficients of wave function \f$\varphi(G)\f$
877 * \param *dest returned complex coefficients of wave function \f$\widehat{p}_{index}|\varphi(G)\f$
878 * \param index_g vectorial index of operator to be applied
879 */
880void CalculatePerturbationOperator_P(struct Problem *P, const fftw_complex *source, fftw_complex *dest, const int index_g)
881{
882 struct RunStruct *R = &P->R;
883 struct LatticeLevel *LevS = R->LevS;
884 //const fftw_complex *dest_bak = dest;
885 int g = 0;
886 if (LevS->GArray[0].GSq == 0.0) {
887 //if (dest != dest_bak) Error(SomeError,"CalculatePerturbationOperator_P: dest corrupted");
888 dest[0].re = LevS->GArray[0].G[index_g]*source[0].im;
889 dest[0].im = -LevS->GArray[0].G[index_g]*source[0].re;
890 g++;
891 }
892 for (;g<LevS->MaxG;g++) {
893 //if (dest != dest_bak || g<0 || g>=LevS->MaxG) Error(SomeError,"CalculatePerturbationOperator_P: g out of range");
894 dest[g].re = LevS->GArray[g].G[index_g]*source[g].im;
895 dest[g].im = -LevS->GArray[g].G[index_g]*source[g].re;
896 }
897 // don't put dest[0].im = 0! Otherwise real parts of perturbed01/10 are not the same anymore!
898}
899
900/** Applies perturbation operator \f$\widehat{r}_{index}\f$ to \a *source.
901 * The \a *source wave function is blown up onto upper level LatticeLevel RunStruct#Lev0, fourier
902 * transformed. Afterwards, for each point on the real mesh the coefficient is multiplied times the real
903 * vector pointing within the cell to the mesh point, yet on LatticeLevel RunStruct#LevS. The new wave
904 * function is inverse fourier transformed and the resulting reciprocal coefficients stored in *dest.
905 * \param *P Problem at hand
906 * \param *source source coefficients
907 * \param *source2 second source coefficients, e.g. in the evaluation of a scalar product
908 * \param *dest destination coefficienta array, is overwrittten!
909 * \param index_r index of real vector.
910 * \param wavenr index of respective PsiTypeTag#Occupied(!) OnePsiElementAddData for the needed Wanner centre of the wave function.
911 */
912void CalculatePerturbationOperator_R(struct Problem *P, const fftw_complex *source, fftw_complex *dest, const fftw_complex *source2, const int index_r, const int wavenr)
913{
914 struct Lattice *Lat = &P->Lat;
915 struct RunStruct *R = &P->R;
916 struct LatticeLevel *Lev0 = R->Lev0;
917 struct LatticeLevel *LevS = R->LevS;
918 struct Density *Dens0 = Lev0->Dens;
919 struct fft_plan_3d *plan = Lat->plan;
920 fftw_complex *TempPsi = Dens0->DensityCArray[Temp2Density];
921 fftw_real *TempPsiR = (fftw_real *) TempPsi;
922 fftw_complex *workC = Dens0->DensityCArray[TempDensity];
923 fftw_complex *PsiC = Dens0->DensityCArray[ActualPsiDensity];
924 fftw_real *PsiCR = (fftw_real *) PsiC;
925 fftw_complex *tempdestRC = (fftw_complex *)Dens0->DensityArray[TempDensity];
926 fftw_complex *posfac, *destsnd, *destrcv;
927 double x[NDIM], X[NDIM], fac[NDIM], Wcentre[NDIM];
928 const int k_normal = Lat->Psi.TypeStartIndex[Occupied] + (wavenr - Lat->Psi.TypeStartIndex[R->CurrentMin]);
929 int n[NDIM], n0, g, Index, pos, iS, i0;
930 int N[NDIM], NUp[NDIM];
931 const int N0 = LevS->Plan0.plan->local_nx;
932 N[0] = LevS->Plan0.plan->N[0];
933 N[1] = LevS->Plan0.plan->N[1];
934 N[2] = LevS->Plan0.plan->N[2];
935 NUp[0] = LevS->NUp[0];
936 NUp[1] = LevS->NUp[1];
937 NUp[2] = LevS->NUp[2];
938 Wcentre[0] = Lat->Psi.AddData[k_normal].WannierCentre[0];
939 Wcentre[1] = Lat->Psi.AddData[k_normal].WannierCentre[1];
940 Wcentre[2] = Lat->Psi.AddData[k_normal].WannierCentre[2];
941 // init pointers and values
942 const int myPE = P->Par.me_comm_ST_Psi;
943 const double FFTFactor = 1./LevS->MaxN;
944 double vector;
945 //double result, Result;
946
947 // blow up source coefficients
948 LockDensityArray(Dens0,TempDensity,real); // tempdestRC
949 LockDensityArray(Dens0,Temp2Density,imag); // TempPsi
950 LockDensityArray(Dens0,ActualPsiDensity,imag); // PsiC
951 //if (tempdestRC != (fftw_complex *)Dens0->DensityArray[TempDensity]) Error(SomeError,"CalculatePerturbationOperator_R: tempdestRC corrupted");
952 SetArrayToDouble0((double *)tempdestRC ,Dens0->TotalSize*2);
953 //if (TempPsi != Dens0->DensityCArray[Temp2Density]) Error(SomeError,"CalculatePerturbationOperator_R: TempPsi corrupted");
954 SetArrayToDouble0((double *)TempPsi ,Dens0->TotalSize*2);
955 //if (PsiC != Dens0->DensityCArray[ActualPsiDensity]) Error(SomeError,"CalculatePerturbationOperator_R: PsiC corrupted");
956 SetArrayToDouble0((double *)PsiC,Dens0->TotalSize*2);
957 for (g=0; g<LevS->MaxG; g++) {
958 Index = LevS->GArray[g].Index;
959 posfac = &LevS->PosFactorUp[LevS->MaxNUp*g];
960 destrcv = &tempdestRC[LevS->MaxNUp*Index];
961 for (pos=0; pos < LevS->MaxNUp; pos++) {
962 //if (destrcv != &tempdestRC[LevS->MaxNUp*Index] || LevS->MaxNUp*Index+pos<0 || LevS->MaxNUp*Index+pos>=Dens0->LocalSizeC) Error(SomeError,"CalculatePerturbationOperator_R: destrcv corrupted");
963 destrcv [pos].re = (( source[g].re)*posfac[pos].re-(source[g].im)*posfac[pos].im);
964 destrcv [pos].im = (( source[g].re)*posfac[pos].im+(source[g].im)*posfac[pos].re);
965 }
966 }
967 for (g=0; g<LevS->MaxDoubleG; g++) {
968 destsnd = &tempdestRC [LevS->DoubleG[2*g]*LevS->MaxNUp];
969 destrcv = &tempdestRC [LevS->DoubleG[2*g+1]*LevS->MaxNUp];
970 for (pos=0; pos<LevS->MaxNUp; pos++) {
971 //if (destrcv != &tempdestRC [LevS->DoubleG[2*g+1]*LevS->MaxNUp] || LevS->DoubleG[2*g]*LevS->MaxNUp+pos<0 || LevS->DoubleG[2*g]*LevS->MaxNUp+pos>=Dens0->LocalSizeC|| LevS->DoubleG[2*g+1]*LevS->MaxNUp+pos<0 || LevS->DoubleG[2*g+1]*LevS->MaxNUp+pos>=Dens0->LocalSizeC) Error(SomeError,"CalculatePerturbationOperator_R: destrcv corrupted");
972 destrcv [pos].re = destsnd [pos].re;
973 destrcv [pos].im = -destsnd [pos].im;
974 }
975 }
976 // fourier transform blown up wave function
977 //if (tempdestRC != (fftw_complex *)Dens0->DensityArray[TempDensity]) Error(SomeError,"CalculatePerturbationOperator_R: tempdestRC corrupted");
978 //if (workC != Dens0->DensityCArray[TempDensity]) Error(SomeError,"CalculatePerturbationOperator_R: workC corrupted");
979 fft_3d_complex_to_real(plan,LevS->LevelNo, FFTNFUp, tempdestRC , workC);
980 //if (tempdestRC != (fftw_complex *)Dens0->DensityArray[TempDensity]) Error(SomeError,"CalculatePerturbationOperator_R: tempdestRC corrupted");
981 //if (TempPsiR != (fftw_real *)Dens0->DensityCArray[Temp2Density]) Error(SomeError,"CalculatePerturbationOperator_R: TempPsiR corrupted");
982 DensityRTransformPos(LevS,(fftw_real*)tempdestRC ,TempPsiR );
983 UnLockDensityArray(Dens0,TempDensity,real); // TempdestRC
984
985 //result = 0.;
986 // for every point on the real grid multiply with component of position vector
987 for (n0=0; n0<N0; n0++)
988 for (n[1]=0; n[1]<N[1]; n[1]++)
989 for (n[2]=0; n[2]<N[2]; n[2]++) {
990 n[0] = n0 + N0 * myPE;
991 fac[0] = (double)(n[0])/(double)((N[0]));
992 fac[1] = (double)(n[1])/(double)((N[1]));
993 fac[2] = (double)(n[2])/(double)((N[2]));
994 RMat33Vec3(x,Lat->RealBasis,fac);
995 iS = n[2] + N[2]*(n[1] + N[1]*n0); // mind splitting of x axis due to multiple processes
996 i0 = n[2]*NUp[2]+N[2]*NUp[2]*(n[1]*NUp[1]+N[1]*NUp[1]*n0*NUp[0]);
997 //PsiCR[iS] = ((double)n[0]/(double)N[0]*Lat->RealBasis[0] - fabs(Wcentre[0]))*TempPsiR[i0] - ((double)n[1]/(double)N[1]*Lat->RealBasis[4] - fabs(Wcentre[1]))*TempPsi2R[i0];
998 //fprintf(stderr,"(%i) R[%i] = (%lg,%lg,%lg)\n",P->Par.me, i0, x[0], x[1], x[2]);
999 //else fprintf(stderr,"(%i) WCentre[%i] = %e \n",P->Par.me, index_r, Wcentre[index_r]);
1000 MinImageConv(Lat,x, Wcentre, X);
1001 vector = sawtooth(Lat,X[index_r],index_r);
1002 //vector = 1.;//sin((double)(n[index_r])/(double)((N[index_r]))*2*PI);
1003 PsiCR[iS] = vector * TempPsiR[i0];
1004 //fprintf(stderr,"(%i) vector(%i/%i,%i/%i,%i/%i): %lg\tx[%i] = %e\tWcentre[%i] = %e\tTempPsiR[%i] = %e\tPsiCR[%i] = %e\n",P->Par.me, n[0], N[0], n[1], N[1], n[2], N[2], vector, index_r, x[index_r],index_r, Wcentre[index_r],i0,TempPsiR[i0],iS,PsiCR[iS]);
1005
1006 //truedist(Lat,x[cross(index_r,2)],Wcentre[cross(index_r,2)],cross(index_r,2)) * TempPsiR[i0];
1007 //tmp += truedist(Lat,x[index_r],WCentre[index_r],index_r) * RealPhiR[i0];
1008 //tmp += sawtooth(Lat,truedist(Lat,x[index_r],WCentre[index_r],index_r), index_r)*RealPhiR[i0];
1009 //(Fehler mit falschem Ort ist vor dieser Stelle!): ueber result = RealPhiR[i0] * (x[index_r]) * RealPhiR[i0]; gecheckt
1010 //result += TempPsiR[i0] * PsiCR[iS];
1011 }
1012 UnLockDensityArray(Dens0,Temp2Density,imag); // TempPsi
1013 //MPI_Allreduce( &result, &Result, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
1014 //if (P->Par.me == 0) fprintf(stderr,"(%i) PerturbationOpertator_R: %e\n",P->Par.me, Result/LevS->MaxN);
1015 // inverse fourier transform
1016 fft_3d_real_to_complex(plan,LevS->LevelNo, FFTNF1, PsiC, workC);
1017 //fft_3d_real_to_complex(plan,LevS->LevelNo, FFTNF1, Psi2C, workC);
1018
1019 // copy to destination array
1020 for (g=0; g<LevS->MaxG; g++) {
1021 Index = LevS->GArray[g].Index;
1022 dest[g].re = ( PsiC[Index].re)*FFTFactor;
1023 dest[g].im = ( PsiC[Index].im)*FFTFactor;
1024 }
1025 UnLockDensityArray(Dens0,ActualPsiDensity,imag); //PsiC
1026 //if (LevS->GArray[0].GSq == 0)
1027 // dest[0].im = 0; // imaginary of G=0 is zero
1028}
1029/*
1030{
1031 struct RunStruct *R = &P->R;
1032 struct LatticeLevel *Lev0 = R->Lev0;
1033 struct LatticeLevel *LevS = R->LevS;
1034 struct Lattice *Lat = &P->Lat;
1035 struct fft_plan_3d *plan = Lat->plan;
1036 struct Density *Dens0 = Lev0->Dens;
1037 fftw_complex *tempdestRC = Dens0->DensityCArray[TempDensity];
1038 fftw_real *tempdestR = (fftw_real *) tempdestRC;
1039 fftw_complex *work = Dens0->DensityCArray[Temp2Density];
1040 fftw_complex *PsiC = (fftw_complex *) Dens0->DensityCArray[ActualPsiDensity];;
1041 fftw_real *PsiCR = (fftw_real *) PsiC;
1042 fftw_real *RealPhiR = (fftw_real *) Dens0->DensityArray[Temp2Density];
1043 fftw_complex *posfac, *destsnd, *destrcv;
1044 double x[NDIM], fac[NDIM], WCentre[NDIM];
1045 int n[NDIM], N0, n0, g, Index, pos, iS, i0;
1046
1047 // init pointers and values
1048 int myPE = P->Par.me_comm_ST_Psi;
1049 double FFTFactor = 1./LevS->MaxN;
1050 int N[NDIM], NUp[NDIM];
1051 N[0] = LevS->Plan0.plan->N[0];
1052 N[1] = LevS->Plan0.plan->N[1];
1053 N[2] = LevS->Plan0.plan->N[2];
1054 NUp[0] = LevS->NUp[0];
1055 NUp[1] = LevS->NUp[1];
1056 NUp[2] = LevS->NUp[2];
1057 N0 = LevS->Plan0.plan->local_nx;
1058 wavenr = Lat->Psi.TypeStartIndex[Occupied] + (wavenr - Lat->Psi.TypeStartIndex[R->CurrentMin]);
1059 Wcentre[0] = Lat->Psi.AddData[wavenr].WannierCentre[0];
1060 Wcentre[1] = Lat->Psi.AddData[wavenr].WannierCentre[1];
1061 Wcentre[2] = Lat->Psi.AddData[wavenr].WannierCentre[2];
1062
1063 // blow up source coefficients
1064 SetArrayToDouble0((double *)tempdestRC,Dens0->TotalSize*2);
1065 SetArrayToDouble0((double *)RealPhiR,Dens0->TotalSize*2);
1066 SetArrayToDouble0((double *)PsiC,Dens0->TotalSize*2);
1067 for (g=0; g<LevS->MaxG; g++) {
1068 Index = LevS->GArray[g].Index;
1069 posfac = &LevS->PosFactorUp[LevS->MaxNUp*g];
1070 destrcv = &tempdestRC[LevS->MaxNUp*Index];
1071 for (pos=0; pos<LevS->MaxNUp; pos++) {
1072 destrcv[pos].re = (( source[g].re)*posfac[pos].re-( source[g].im)*posfac[pos].im);
1073 destrcv[pos].im = (( source[g].re)*posfac[pos].im+( source[g].im)*posfac[pos].re);
1074 }
1075 }
1076 for (g=0; g<LevS->MaxDoubleG; g++) {
1077 destsnd = &tempdestRC[LevS->DoubleG[2*g]*LevS->MaxNUp];
1078 destrcv = &tempdestRC[LevS->DoubleG[2*g+1]*LevS->MaxNUp];
1079 for (pos=0; pos<LevS->MaxNUp; pos++) {
1080 destrcv[pos].re = destsnd[pos].re;
1081 destrcv[pos].im = -destsnd[pos].im;
1082 }
1083 }
1084
1085 // fourier transform blown up wave function
1086 fft_3d_complex_to_real(plan,LevS->LevelNo, FFTNFUp, tempdestRC, work);
1087 DensityRTransformPos(LevS,tempdestR,RealPhiR);
1088
1089 //fft_Psi(P,source,RealPhiR,0,0);
1090
1091 // for every point on the real grid multiply with component of position vector
1092 for (n0=0; n0<N0; n0++)
1093 for (n[1]=0; n[1]<N[1]; n[1]++)
1094 for (n[2]=0; n[2]<N[2]; n[2]++) {
1095 n[0] = n0 + N0 * myPE;
1096 fac[0] = (double)(n[0])/(double)((N[0]));
1097 fac[1] = (double)(n[1])/(double)((N[1]));
1098 fac[2] = (double)(n[2])/(double)((N[2]));
1099 RMat33Vec3(x,Lat->RealBasis,fac);
1100 iS = n[2] + N[2]*(n[1] + N[1]*n0); // mind splitting of x axis due to multiple processes
1101 i0 = n[2]*NUp[2]+N[2]*NUp[2]*(n[1]*NUp[1]+N[1]*NUp[1]*n0*NUp[0]);
1102 //PsiCR[iS] = (x[index_r]) * RealPhiR[i0]; //- WCentre[index_r]
1103 PsiCR[iS] = truedist(Lat,x[index_r],WCentre[index_r],index_r) * RealPhiR[i0];
1104 //PsiCR[iS] = truedist(Lat,x[index_r],0.,index_r) * RealPhiR[i0];
1105 //PsiCR[iS] = sawtooth(Lat,truedist(Lat,x[index_r],WCentre[index_r],index_r), index_r)*RealPhiR[i0];
1106 //(Fehler mit falschem Ort ist vor dieser Stelle!): ueber result = RealPhiR[i0] * (x[index_r]) * RealPhiR[i0]; gecheckt
1107 }
1108
1109 // inverse fourier transform
1110 fft_3d_real_to_complex(plan,LevS->LevelNo, FFTNF1, PsiC, work);
1111
1112 // copy to destination array
1113 for (g=0; g<LevS->MaxG; g++) {
1114 Index = LevS->GArray[g].Index;
1115 dest[g].re = ( PsiC[Index].re)*FFTFactor;
1116 dest[g].im = ( PsiC[Index].im)*FFTFactor;
1117 if (LevS->GArray[g].GSq == 0)
1118 dest[g].im = 0; // imaginary of G=0 is zero
1119 }
1120}*/
1121
1122/** Prints the positions of all unperturbed orbitals to screen.
1123 * \param *P Problem at hand
1124 * \param type PsiTypeTag specifying group of orbitals
1125 * \sa CalculatePerturbationOperator_R()
1126 */
1127void OutputOrbitalPositions(struct Problem *P, const enum PsiTypeTag type)
1128{
1129 struct Lattice *Lat = &P->Lat;
1130 struct Psis *Psi = &Lat->Psi;
1131 struct RunStruct *R = &P->R;
1132 struct LatticeLevel *LevS = R->LevS;
1133 fftw_complex *temp = LevS->LPsi->TempPsi;
1134 fftw_complex *source;
1135 int wavenr, index;
1136 double result[NDIM], Result[NDIM];
1137 //double imsult[NDIM], Imsult[NDIM];
1138 double norm[NDIM], Norm[NDIM];
1139 //double imnorm[NDIM], imNorm[NDIM];
1140 double Wcentre[NDIM];
1141
1142 // for every unperturbed wave function
1143 for (wavenr=Psi->TypeStartIndex[type]; wavenr<Psi->TypeStartIndex[type+1]; wavenr++) {
1144 source = LevS->LPsi->LocalPsi[wavenr];
1145 Wcentre[0] = Psi->AddData[wavenr].WannierCentre[0];
1146 Wcentre[1] = Psi->AddData[wavenr].WannierCentre[1];
1147 Wcentre[2] = Psi->AddData[wavenr].WannierCentre[2];
1148 for (index=0; index<NDIM; index++) {
1149 SetArrayToDouble0((double *)temp,2*R->InitLevS->MaxG);
1150 // apply position operator
1151 CalculatePerturbationOperator_R(P,source,temp,source,index, wavenr + Psi->TypeStartIndex[R->CurrentMin]);
1152 // take scalar product
1153 result[index] = GradSP(P,LevS,source,temp);
1154 //imsult[index] = GradImSP(P,LevS,source,temp);
1155 norm[index] = GradSP(P,LevS,source,source);
1156 //imnorm[index] = GradImSP(P,LevS,source,source);
1157 MPI_Allreduce( result, Result, NDIM, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
1158 //MPI_Allreduce( imsult, Imsult, NDIM, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
1159 MPI_Allreduce( norm, Norm, NDIM, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
1160 //MPI_Allreduce( imnorm, imNorm, NDIM, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
1161 }
1162 // print output to stderr
1163 if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) Position of Orbital %i: (%e,%e,%e)\n",P->Par.me, wavenr, Result[0]/Norm[0]+Wcentre[0], Result[1]/Norm[1]+Wcentre[1], Result[2]/Norm[2]+Wcentre[2]);
1164 //fprintf(stderr,"(%i) Position of Orbital %i wrt Wannier: (%e,%e,%e)\n",P->Par.me, wavenr, Result[0]/Norm[0], Result[1]/Norm[1], Result[2]/Norm[2]);
1165 //fprintf(stderr,"(%i) with Norm: (%e,%e,%e) + i (%e,%e,%e)\n",P->Par.me, Norm[0], Norm[1], Norm[2], imNorm[0], imNorm[1], imNorm[2]);
1166 //if (P->Par.me == 0) fprintf(stderr,"(%i) Position of Orbital %i: (%e,%e,%e)\n",P->Par.me, wavenr, Result[0]/Norm[0], Result[1]/Norm[1], Result[2]/Norm[2]);
1167 }
1168}
1169
1170#define borderstart 0.9
1171
1172/** Applies perturbation operator \f$(\widehat{r} \times \nabla)_{index}\f$ to \a *source.
1173 * The source is fourier-transformed by transforming it to a density (on the next higher level RunStruct#Lev0)
1174 * and at the same time multiply it with the respective component of the reciprocal G vector - the momentum. This
1175 * is done by callinf fft_Psi(). Thus we get \f$\nabla_k | \varphi (R) \rangle\f$.
1176 *
1177 * Next, we apply the two of three components of the position operator r, which ones stated by cross(), while going
1178 * in a loop through every point of the grid. In order to do this sensibly, truedist() is used to map the coordinates
1179 * onto -L/2...L/2, by subtracting the OneElementPsiAddData#WannierCentre R and wrapping. Also, due to the breaking up
1180 * of the x axis into equally sized chunks for each coefficient sharing process, we need to step only over local
1181 * x-axis grid points, however shift them to the global position when being used as position. In the end, we get
1182 * \f$\epsilon_{index,j,k} (\widehat{r}-R)_j \nabla_k | \varphi (R) \rangle\f$.
1183 *
1184 * One last fft brings the wave function back to reciprocal basis and it is copied to \a *dest.
1185 * \param *P Problem at hand
1186 * \param *source complex coefficients of wave function \f$\varphi(G)\f$
1187 * \param *dest returned complex coefficients of wave function \f$(\widehat{r} \times \widehat{p})_{index}|\varphi(G)\rangle\f$
1188 * \param phi0nr number within LocalPsi of the unperturbed pendant of the given perturbed wavefunction \a *source.
1189 * \param index_rxp index desired of the vector product
1190 * \sa CalculateConDirHConDir() - the procedure of fft and inverse fft is very similar.
1191 */
1192void CalculatePerturbationOperator_RxP(struct Problem *P, const fftw_complex *source, fftw_complex *dest, const int phi0nr, const int index_rxp)
1193
1194{
1195 struct Lattice *Lat = &P->Lat;
1196 struct RunStruct *R = &P->R;
1197 struct LatticeLevel *Lev0 = R->Lev0;
1198 struct LatticeLevel *LevS = R->LevS;
1199 struct Density *Dens0 = Lev0->Dens;
1200 struct fft_plan_3d *plan = Lat->plan;
1201 fftw_complex *TempPsi = Dens0->DensityCArray[Temp2Density];
1202 fftw_real *TempPsiR = (fftw_real *) TempPsi;
1203 fftw_complex *TempPsi2 = (fftw_complex *)Dens0->DensityArray[Temp2Density];
1204 fftw_real *TempPsi2R = (fftw_real *) TempPsi2;
1205 fftw_complex *workC = Dens0->DensityCArray[TempDensity];
1206 fftw_complex *PsiC = Dens0->DensityCArray[ActualPsiDensity];
1207 fftw_real *PsiCR = (fftw_real *) PsiC;
1208 double x[NDIM], X[NDIM], fac[NDIM], *Wcentre;
1209 int n[NDIM], n0, g, Index, iS, i0; //pos,
1210 const int *N, *NUp;
1211 const int N0 = LevS->Plan0.plan->local_nx;
1212 N = LevS->Plan0.plan->N;
1213 NUp = LevS->NUp;
1214 Wcentre = Lat->Psi.AddData[phi0nr].WannierCentre;
1215 // init pointers and values
1216 const int myPE = P->Par.me_comm_ST_Psi;
1217 const double FFTFactor = 1./LevS->MaxN; //
1218// double max[NDIM], max_psi[NDIM];
1219// double max_n[NDIM];
1220 int index[4];
1221// double smooth, wall[NDIM];
1222// for (g=0;g<NDIM;g++) {
1223// max[g] = 0.;
1224// max_psi[g] = 0.;
1225// max_n[g] = -1.;
1226// }
1227
1228 //fprintf(stderr,"(%i) Wannier[%i] (%2.13e, %2.13e, %2.13e)\n", P->Par.me, phi0nr, 10.-Wcentre[0], 10.-Wcentre[1], 10.-Wcentre[2]);
1229 for (g=0;g<4;g++)
1230 index[g] = cross(index_rxp,g);
1231
1232 // blow up source coefficients
1233 LockDensityArray(Dens0,Temp2Density,imag); // TempPsi
1234 LockDensityArray(Dens0,Temp2Density,real); // TempPsi2
1235 LockDensityArray(Dens0,ActualPsiDensity,imag); // PsiC
1236
1237 fft_Psi(P,source,TempPsiR ,index[1],7);
1238 fft_Psi(P,source,TempPsi2R,index[3],7);
1239
1240 //result = 0.;
1241 // for every point on the real grid multiply with component of position vector
1242 for (n0=0; n0<N0; n0++)
1243 for (n[1]=0; n[1]<N[1]; n[1]++)
1244 for (n[2]=0; n[2]<N[2]; n[2]++) {
1245 n[0] = n0 + N0 * myPE;
1246 fac[0] = (double)(n[0])/(double)((N[0]));
1247 fac[1] = (double)(n[1])/(double)((N[1]));
1248 fac[2] = (double)(n[2])/(double)((N[2]));
1249 RMat33Vec3(x,Lat->RealBasis,fac);
1250// fac[0] = (fac[0] > .9) ? fac[0]-0.9 : 0.;
1251// fac[1] = (fac[1] > .9) ? fac[1]-0.9 : 0.;
1252// fac[2] = (fac[2] > .9) ? fac[2]-0.9 : 0.;
1253// RMat33Vec3(wall,Lat->RealBasis,fac);
1254// smooth = exp(wall[0]*wall[0]+wall[1]*wall[1]+wall[2]*wall[2]); // smoothing near the borders of the virtual cell
1255 iS = n[2] + N[2]*(n[1] + N[1]*n0); // mind splitting of x axis due to multiple processes
1256 i0 = n[2]*NUp[2]+N[2]*NUp[2]*(n[1]*NUp[1]+N[1]*NUp[1]*n0*NUp[0]);
1257
1258// if (fabs(truedist(Lat,x[index[1]],Wcentre[index[1]],index[1])) >= borderstart * sqrt(Lat->RealBasisSQ[index[1]])/2.)
1259// if (max[index[1]] < sawtooth(Lat,truedist(Lat,x[index[1]],Wcentre[index[1]],index[1]),index[1]) * TempPsiR [i0]) {
1260// max[index[1]] = sawtooth(Lat,truedist(Lat,x[index[1]],Wcentre[index[1]],index[1]),index[1]) * TempPsiR [i0];
1261// max_psi[index[1]] = TempPsiR [i0];
1262// max_n[index[1]] = truedist(Lat,x[index[1]],Wcentre[index[1]],index[1]);
1263// }
1264//
1265// if (fabs(truedist(Lat,x[index[3]],Wcentre[index[3]],index[3])) >= borderstart * sqrt(Lat->RealBasisSQ[index[3]])/2.)
1266// if (max[index[3]] < sawtooth(Lat,truedist(Lat,x[index[3]],Wcentre[index[3]],index[3]),index[3]) * TempPsiR [i0]) {
1267// max[index[3]] = sawtooth(Lat,truedist(Lat,x[index[3]],Wcentre[index[3]],index[3]),index[3]) * TempPsiR [i0];
1268// max_psi[index[3]] = TempPsiR [i0];
1269// max_n[index[3]] = truedist(Lat,x[index[3]],Wcentre[index[3]],index[3]);
1270// }
1271
1272 MinImageConv(Lat, x, Wcentre, X);
1273 PsiCR[iS] = //vector * TempPsiR[i0];
1274 sawtooth(Lat,X[index[0]],index[0]) * TempPsiR [i0]
1275 -sawtooth(Lat,X[index[2]],index[2]) * TempPsi2R[i0];
1276// ShiftGaugeOrigin(P,X,index[0]) * TempPsiR [i0]
1277// -ShiftGaugeOrigin(P,X,index[2]) * TempPsi2R[i0];
1278// PsiCR[iS] = (x[index[0]] - Wcentre[index[0]]) * TempPsiR [i0] - (x[index[2]] - Wcentre[index[2]]) * TempPsi2R[i0];
1279 }
1280 //if (P->Par.me == 0) fprintf(stderr,"(%i) PerturbationOpertator_R(xP): %e\n",P->Par.me, Result/LevS->MaxN);
1281 UnLockDensityArray(Dens0,Temp2Density,imag); // TempPsi
1282 UnLockDensityArray(Dens0,Temp2Density,real); // TempPsi2
1283
1284// // print maximum values
1285// fprintf (stderr,"(%i) RxP: Maximum values = (",P->Par.me);
1286// for (g=0;g<NDIM;g++)
1287// fprintf(stderr,"%lg\t", max[g]);
1288// fprintf(stderr,"\b)\t(");
1289// for (g=0;g<NDIM;g++)
1290// fprintf(stderr,"%lg\t", max_psi[g]);
1291// fprintf(stderr,"\b)\t");
1292// fprintf (stderr,"at (");
1293// for (g=0;g<NDIM;g++)
1294// fprintf(stderr,"%lg\t", max_n[g]);
1295// fprintf(stderr,"\b)\n");
1296
1297 // inverse fourier transform
1298 //if (PsiC != Dens0->DensityCArray[ActualPsiDensity]) Error(SomeError,"CalculatePerturbationOperator_RxP: PsiC corrupted");
1299 fft_3d_real_to_complex(plan,LevS->LevelNo, FFTNF1, PsiC, workC);
1300
1301 // copy to destination array
1302 SetArrayToDouble0((double *)dest, 2*R->InitLevS->MaxG);
1303 for (g=0; g<LevS->MaxG; g++) {
1304 Index = LevS->GArray[g].Index;
1305 dest[g].re += ( PsiC[Index].re)*FFTFactor; // factor confirmed, see grad.c:CalculateConDirHConDir()
1306 dest[g].im += ( PsiC[Index].im)*FFTFactor;
1307 //fprintf(stderr,"(%i) PsiC[(%lg,%lg,%lg)] = %lg +i %lg\n", P->Par.me, LevS->GArray[g].G[0], LevS->GArray[g].G[1], LevS->GArray[g].G[2], dest[g].re, dest[g].im);
1308 }
1309 UnLockDensityArray(Dens0,ActualPsiDensity,imag); // PsiC
1310 //if (LevS->GArray[0].GSq == 0.)
1311 //dest[0].im = 0.; // don't do this, see ..._P()
1312}
1313
1314/** Applies perturbation operator \f$-(\nabla \times \widehat{r})_{index}\f$ to \a *source.
1315 * Is analogous to CalculatePerturbationOperator_RxP(), only the order is reversed, first position operator, then
1316 * momentum operator
1317 * \param *P Problem at hand
1318 * \param *source complex coefficients of wave function \f$\varphi(G)\f$
1319 * \param *dest returned complex coefficients of wave function \f$(\widehat{r} \times \widehat{p})_{index}|\varphi(G)\rangle\f$
1320 * \param phi0nr number within LocalPsi of the unperturbed pendant of the given perturbed wavefunction \a *source.
1321 * \param index_pxr index of position operator
1322 * \note Only third component is important due to initial rotiation of cell such that B field is aligned with z axis.
1323 * \sa CalculateConDirHConDir() - the procedure of fft and inverse fft is very similar.
1324 * \bug routine is not tested (but should work), as it offers no advantage over CalculatePerturbationOperator_RxP()
1325 */
1326void CalculatePerturbationOperator_PxR(struct Problem *P, const fftw_complex *source, fftw_complex *dest, const int phi0nr, const int index_pxr)
1327
1328{
1329 struct Lattice *Lat = &P->Lat;
1330 struct RunStruct *R = &P->R;
1331 struct LatticeLevel *Lev0 = R->Lev0;
1332 struct LatticeLevel *LevS = R->LevS;
1333 struct Density *Dens0 = Lev0->Dens;
1334 struct fft_plan_3d *plan = Lat->plan;
1335 fftw_complex *TempPsi = Dens0->DensityCArray[Temp2Density];
1336 fftw_real *TempPsiR = (fftw_real *) TempPsi;
1337 fftw_complex *workC = Dens0->DensityCArray[TempDensity];
1338 fftw_complex *PsiC = Dens0->DensityCArray[ActualPsiDensity];
1339 fftw_real *PsiCR = (fftw_real *) PsiC;
1340 fftw_complex *Psi2C = Dens0->DensityCArray[ActualDensity];
1341 fftw_real *Psi2CR = (fftw_real *) Psi2C;
1342 fftw_complex *tempdestRC = (fftw_complex *)Dens0->DensityArray[Temp2Density];
1343 fftw_complex *posfac, *destsnd, *destrcv;
1344 double x[NDIM], X[NDIM], fac[NDIM], Wcentre[NDIM];
1345 int n[NDIM], n0, g, Index, pos, iS, i0;
1346 int N[NDIM], NUp[NDIM];
1347 const int N0 = LevS->Plan0.plan->local_nx;
1348 N[0] = LevS->Plan0.plan->N[0];
1349 N[1] = LevS->Plan0.plan->N[1];
1350 N[2] = LevS->Plan0.plan->N[2];
1351 NUp[0] = LevS->NUp[0];
1352 NUp[1] = LevS->NUp[1];
1353 NUp[2] = LevS->NUp[2];
1354 Wcentre[0] = Lat->Psi.AddData[phi0nr].WannierCentre[0];
1355 Wcentre[1] = Lat->Psi.AddData[phi0nr].WannierCentre[1];
1356 Wcentre[2] = Lat->Psi.AddData[phi0nr].WannierCentre[2];
1357 // init pointers and values
1358 const int myPE = P->Par.me_comm_ST_Psi;
1359 const double FFTFactor = 1./LevS->MaxN;
1360
1361 // blow up source coefficients
1362 SetArrayToDouble0((double *)tempdestRC ,Dens0->TotalSize*2);
1363 SetArrayToDouble0((double *)TempPsi ,Dens0->TotalSize*2);
1364 SetArrayToDouble0((double *)PsiC,Dens0->TotalSize*2);
1365 SetArrayToDouble0((double *)Psi2C,Dens0->TotalSize*2);
1366 for (g=0; g<LevS->MaxG; g++) {
1367 Index = LevS->GArray[g].Index;
1368 posfac = &LevS->PosFactorUp[LevS->MaxNUp*g];
1369 destrcv = &tempdestRC[LevS->MaxNUp*Index];
1370 for (pos=0; pos < LevS->MaxNUp; pos++) {
1371 destrcv [pos].re = (( source[g].re)*posfac[pos].re-( source[g].im)*posfac[pos].im);
1372 destrcv [pos].im = (( source[g].re)*posfac[pos].im+( source[g].im)*posfac[pos].re);
1373 }
1374 }
1375 for (g=0; g<LevS->MaxDoubleG; g++) {
1376 destsnd = &tempdestRC [LevS->DoubleG[2*g]*LevS->MaxNUp];
1377 destrcv = &tempdestRC [LevS->DoubleG[2*g+1]*LevS->MaxNUp];
1378 for (pos=0; pos<LevS->MaxNUp; pos++) {
1379 destrcv [pos].re = destsnd [pos].re;
1380 destrcv [pos].im = -destsnd [pos].im;
1381 }
1382 }
1383 // fourier transform blown up wave function
1384 fft_3d_complex_to_real(plan,LevS->LevelNo, FFTNFUp, tempdestRC , workC);
1385 DensityRTransformPos(LevS,(fftw_real*)tempdestRC ,TempPsiR );
1386
1387 //fft_Psi(P,source,TempPsiR ,cross(index_pxr,1),7);
1388 //fft_Psi(P,source,TempPsi2R,cross(index_pxr,3),7);
1389
1390 //result = 0.;
1391 // for every point on the real grid multiply with component of position vector
1392 for (n0=0; n0<N0; n0++)
1393 for (n[1]=0; n[1]<N[1]; n[1]++)
1394 for (n[2]=0; n[2]<N[2]; n[2]++) {
1395 n[0] = n0 + N0 * myPE;
1396 fac[0] = (double)(n[0])/(double)((N[0]));
1397 fac[1] = (double)(n[1])/(double)((N[1]));
1398 fac[2] = (double)(n[2])/(double)((N[2]));
1399 RMat33Vec3(x,Lat->RealBasis,fac);
1400 iS = n[2] + N[2]*(n[1] + N[1]*n0); // mind splitting of x axis due to multiple processes
1401 i0 = n[2]*NUp[2]+N[2]*NUp[2]*(n[1]*NUp[1]+N[1]*NUp[1]*n0*NUp[0]);
1402// PsiCR[iS] = sawtooth(Lat,X[cross(index_pxr,1)],cross(index_pxr,1)) * TempPsiR[i0];
1403// Psi2CR[iS] = sawtooth(Lat,X[cross(index_pxr,3)],cross(index_pxr,3)) * TempPsiR[i0];
1404 MinImageConv(Lat,x,Wcentre,X);
1405 PsiCR[iS] = ShiftGaugeOrigin(P,X,cross(index_pxr,1)) * TempPsiR[i0];
1406 Psi2CR[iS] = ShiftGaugeOrigin(P,X,cross(index_pxr,3)) * TempPsiR[i0];
1407 }
1408
1409 // inverse fourier transform
1410 fft_3d_real_to_complex(plan,LevS->LevelNo, FFTNF1, PsiC, workC);
1411 fft_3d_real_to_complex(plan,LevS->LevelNo, FFTNF1, Psi2C, workC);
1412
1413 // copy to destination array
1414 for (g=0; g<LevS->MaxG; g++) {
1415 Index = LevS->GArray[g].Index;
1416 dest[g].re = -LevS->GArray[g].G[cross(index_pxr,0)]*( PsiC[Index].im)*FFTFactor;
1417 dest[g].im = -LevS->GArray[g].G[cross(index_pxr,0)]*(-PsiC[Index].re)*FFTFactor;
1418 dest[g].re -= -LevS->GArray[g].G[cross(index_pxr,2)]*( Psi2C[Index].im)*FFTFactor;
1419 dest[g].im -= -LevS->GArray[g].G[cross(index_pxr,2)]*(-Psi2C[Index].re)*FFTFactor;
1420 }
1421 if (LevS->GArray[0].GSq == 0.)
1422 dest[0].im = 0.; // don't do this, see ..._P()
1423}
1424
1425/** Evaluates first derivative of perturbed energy functional with respect to minimisation parameter \f$\Theta\f$.
1426 * \f[
1427 * \frac{\delta {\cal E}^{(2)}} {\delta \Theta} =
1428 * 2 {\cal R} \langle \widetilde{\varphi}_i^{(1)} | {\cal H}^{(0)} | \varphi_i^{(1)} \rangle
1429 * - \sum_l \lambda_{il} \langle \widetilde{\varphi}_i^{(1)} | \varphi_l^{(1)} \rangle
1430 * - \sum_k \lambda_{ki} \langle \varphi_k^{(1)} | \widetilde{\varphi}_i^{(1)} \rangle
1431 * + 2 {\cal R} \langle \widetilde{\varphi}_i^{(1)} | {\cal H}^{(1)} | \varphi_i^{(0)} \rangle
1432 * \f]
1433 *
1434 * The summation over all Psis has again to be done with an MPI exchange of non-local coefficients, as the conjugate
1435 * directions are not the same in situations where PePGamma > 1 (Psis split up among processes = multiple minimisation)
1436 * \param *P Problem at hand
1437 * \param source0 unperturbed wave function \f$\varphi_l^{(0)}\f$
1438 * \param source perturbed wave function \f$\varphi_l^{(1)} (G)\f$
1439 * \param ConDir normalized conjugate direction \f$\widetilde{\varphi}_l^{(1)} (G)\f$
1440 * \param Hc_grad complex coefficients of \f$H^{(0)} | \varphi_l^{(1)} (G) \rangle\f$, see GradientArray#HcGradient
1441 * \param H1c_grad complex coefficients of \f$H^{(1)} | \varphi_l^{(0)} (G) \rangle\f$, see GradientArray#H1cGradient
1442 * \sa CalculateLineSearch() - used there, \sa CalculateConDirHConDir() - same principles
1443 * \warning The MPI_Allreduce for the scalar product in the end has not been done and must not have been done for given
1444 * parameters yet!
1445 */
1446double Calculate1stPerturbedDerivative(struct Problem *P, const fftw_complex *source0, const fftw_complex *source, const fftw_complex *ConDir, const fftw_complex *Hc_grad, const fftw_complex *H1c_grad)
1447{
1448 struct RunStruct *R = &P->R;
1449 struct Psis *Psi = &P->Lat.Psi;
1450 struct LatticeLevel *LevS = R->LevS;
1451 double result = 0., E0 = 0., Elambda = 0., E1 = 0.;//, E2 = 0.;
1452 int i,m,j;
1453 const int state = R->CurrentMin;
1454 //const int l_normal = R->ActualLocalPsiNo - Psi->TypeStartIndex[state] + Psi->TypeStartIndex[Occupied];
1455 const int ActNum = R->ActualLocalPsiNo - Psi->TypeStartIndex[state] + Psi->TypeStartIndex[1] * Psi->LocalPsiStatus[R->ActualLocalPsiNo].my_color_comm_ST_Psi;
1456 //int l = R->ActualLocalPsiNo;
1457 //int l_normal = Psi->TypeStartIndex[Occupied] + (l - Psi->TypeStartIndex[state]); // offset l to \varphi_l^{(0)}
1458 struct OnePsiElement *OnePsiB, *LOnePsiB;
1459 //fftw_complex *HConGrad = LevS->LPsi->TempPsi;
1460 fftw_complex *LPsiDatB=NULL;
1461 const int ElementSize = (sizeof(fftw_complex) / sizeof(double));
1462 int RecvSource;
1463 MPI_Status status;
1464
1465 //CalculateCDfnl(P,ConDir,PP->CDfnl);
1466 //ApplyTotalHamiltonian(P,ConDir,HConDir, PP->CDfnl, 1, 0);
1467 //E0 = (GradSP(P, LevS, ConDir, Hc_grad) + GradSP(P, LevS, source, HConDir)) * Psi->LocalPsiStatus[R->ActualLocalPsiNo].PsiFactor;
1468 E0 = 2.*GradSP(P, LevS, ConDir, Hc_grad) * Psi->LocalPsiStatus[R->ActualLocalPsiNo].PsiFactor;
1469 result = E0;
1470 //fprintf(stderr,"(%i) 1st: E0 = \t\t%lg\n", P->Par.me, E0);
1471
1472 m = -1;
1473 for (j=0; j < Psi->MaxPsiOfType+P->Par.Max_me_comm_ST_PsiT; j++) { // go through all wave functions
1474 OnePsiB = &Psi->AllPsiStatus[j]; // grab OnePsiB
1475 if (OnePsiB->PsiType == state) { // drop all but the ones of current min state
1476 m++; // increase m if it is type-specific wave function
1477 if (OnePsiB->my_color_comm_ST_Psi == P->Par.my_color_comm_ST_Psi) // local?
1478 LOnePsiB = &Psi->LocalPsiStatus[OnePsiB->MyLocalNo];
1479 else
1480 LOnePsiB = NULL;
1481 if (LOnePsiB == NULL) { // if it's not local ... receive it from respective process into TempPsi
1482 RecvSource = OnePsiB->my_color_comm_ST_Psi;
1483 MPI_Recv( LevS->LPsi->TempPsi, LevS->MaxG*ElementSize, MPI_DOUBLE, RecvSource, PerturbedTag, P->Par.comm_ST_PsiT, &status );
1484 LPsiDatB=LevS->LPsi->TempPsi;
1485 } else { // .. otherwise send it to all other processes (Max_me... - 1)
1486 for (i=0;i<P->Par.Max_me_comm_ST_PsiT;i++)
1487 if (i != OnePsiB->my_color_comm_ST_Psi)
1488 MPI_Send( LevS->LPsi->LocalPsi[OnePsiB->MyLocalNo], LevS->MaxG*ElementSize, MPI_DOUBLE, i, PerturbedTag, P->Par.comm_ST_PsiT);
1489 LPsiDatB=LevS->LPsi->LocalPsi[OnePsiB->MyLocalNo];
1490 } // LPsiDatB is now set to the coefficients of OnePsi either stored or MPI_Received
1491
1492 Elambda -= 2.*Psi->lambda[ActNum][m]*GradSP(P, LevS, ConDir, LPsiDatB) * OnePsiB->PsiFactor; // lambda is symmetric
1493 }
1494 }
1495 result += Elambda;
1496 //fprintf(stderr,"(%i) 1st: Elambda = \t%lg\n", P->Par.me, Elambda);
1497
1498 E1 = 2.*GradSP(P,LevS,ConDir,H1c_grad) * sqrt(Psi->AllPsiStatus[ActNum].PsiFactor*Psi->LocalPsiStatus[R->ActualLocalPsiNo].PsiFactor);
1499 result += E1;
1500 //fprintf(stderr,"(%i) 1st: E1 = \t\t%lg\n", P->Par.me, E1);
1501
1502 return result;
1503}
1504
1505
1506/** Evaluates second derivative of perturbed energy functional with respect to minimisation parameter \f$\Theta\f$.
1507 * \f[
1508 * \frac{\delta^2 {\cal E}^{(2)}} {\delta \Theta^2} =
1509 * 2 \bigl( \langle \widetilde{\varphi}_l^{(1)} | {\cal H}^{(0)} | \widetilde{\varphi}_l^{(1)} \rangle
1510 * - \langle \varphi_l^{(1)} | {\cal H}^{(0)} | \varphi_l^{(1)} \rangle \bigr )
1511 * + 2 \sum_{i,i \neq l } \lambda_{il} \langle \varphi_i^{(1)} | \varphi_l^{(1)} \rangle
1512 * - 2 {\cal R} \langle \varphi_l^{(1)} | {\cal H}^{(1)} | \varphi_l^{(0)} \rangle
1513 * \f]
1514 *
1515 * The energy eigenvalues of \a ConDir and \a source must be supplied, they can be calculated via CalculateConDirHConDir() and/or
1516 * by the due to CalculatePerturbedEnergy() already present OnePsiElementAddData#Lambda eigenvalue. The summation over the
1517 * unperturbed lambda within the scalar product of perturbed wave functions is evaluated with Psis#lambda and Psis#Overlap.
1518 * Afterwards, the ConDir density is calculated and also the i-th perturbed density to first degree. With these in a sum over
1519 * all real mesh points the exchange-correlation first and second derivatives and also the Hartree potential ones can be calculated
1520 * and summed up.
1521 * \param *P Problem at hand
1522 * \param source0 unperturbed wave function \f$\varphi_l^{(0)}\f$
1523 * \param source wave function \f$\varphi_l^{(1)}\f$
1524 * \param ConDir conjugated direction \f$\widetilde{\varphi}_l^{(1)}\f$
1525 * \param sourceHsource eigenvalue of wave function \f$\langle \varphi_l^{(1)} | H^{(0)} | \varphi_l^{(1)}\rangle\f$
1526 * \param ConDirHConDir perturbed eigenvalue of conjugate direction \f$\langle \widetilde{\varphi}_l^{(1)} | H^{(0)} | \widetilde{\varphi}_l^{(1)}\rangle\f$
1527 * \param ConDirConDir norm of conjugate direction \f$\langle \widetilde{\varphi}_l^{(1)} | \widetilde{\varphi}_l^{(1)}\rangle\f$
1528 * \warning No MPI_AllReduce() takes place, parameters have to be reduced already.
1529 */
1530double Calculate2ndPerturbedDerivative(struct Problem *P, const fftw_complex *source0,const fftw_complex *source, const fftw_complex *ConDir,const double sourceHsource, const double ConDirHConDir, const double ConDirConDir)
1531{
1532 struct RunStruct *R = &P->R;
1533 struct Psis *Psi = &P->Lat.Psi;
1534 //struct Lattice *Lat = &P->Lat;
1535 //struct Energy *E = Lat->E;
1536 double result = 0.;
1537 double Con0 = 0., Elambda = 0.;//, E0 = 0., E1 = 0.;
1538 //int i;
1539 const int state = R->CurrentMin;
1540 //const int l_normal = R->ActualLocalPsiNo - Psi->TypeStartIndex[state] + Psi->TypeStartIndex[Occupied];
1541 const int ActNum = R->ActualLocalPsiNo - Psi->TypeStartIndex[state] + Psi->TypeStartIndex[1] * Psi->LocalPsiStatus[R->ActualLocalPsiNo].my_color_comm_ST_Psi;
1542
1543 Con0 = 2.*ConDirHConDir;
1544 result += Con0;
1545 ////E0 = -2.*sourceHsource;
1546 ////result += E0;
1547 ////E1 = -E->PsiEnergy[Perturbed1_0Energy][R->ActualLocalPsiNo] - E->PsiEnergy[Perturbed0_1Energy][R->ActualLocalPsiNo];
1548 ////result += E1;
1549 //fprintf(stderr,"(%i) 2nd: E1 = \t%lg\n", P->Par.me, E1);
1550
1551 ////for (i=0;i<Lat->Psi.NoOfPsis;i++) {
1552 //// if (i != ActNum) Elambda += Psi->lambda[i][ActNum]*Psi->Overlap[i][ActNum]+ Psi->lambda[ActNum][i]*Psi->Overlap[ActNum][i]; // overlap contains PsiFactor
1553 ////}
1554 ////Elambda = Psi->lambda[ActNum][ActNum]*Psi->Overlap[ActNum][ActNum];
1555 Elambda = 2.*Psi->lambda[ActNum][ActNum]*ConDirConDir;
1556 result -= Elambda;
1557
1558 //fprintf(stderr,"(%i) 2ndPerturbedDerivative: Result = Con0 + E0 + E1 + Elambda + dEdt0_XC + ddEddt0_XC + dEdt0_H + ddEddt0_H = %lg + %lg + %lg + %lg + %lg + %lg + %lg + %lg = %lg\n", P->Par.me, Con0, E0, E1, Elambda, VolumeFactorR*dEdt0_XC, VolumeFactorR*ddEddt0_XC, dEdt0_H, ddEddt0_H, result);
1559
1560 return (result);
1561}
1562
1563/** Returns index of specific component in 3x3 cross product.
1564 * \param i vector product component index, ranging from 0..NDIM
1565 * \param j index specifies which one of the four vectors in x*y - y*x, ranging from 0..3 (0,1 positive sign, 2,3 negative sign)
1566 * \return Component 0..2 of vector to be taken to evaluate a vector product
1567 * \sa crossed() - is the same but vice versa, return value must be specified, \a i is returned.
1568 */
1569inline int cross(int i, int j)
1570{
1571 const int matrix[NDIM*4] = {1,2,2,1,2,0,0,2,0,1,1,0};
1572 if (i>=0 && i<NDIM && j>=0 && j<4)
1573 return (matrix[i*4+j]);
1574 else {
1575 Error(SomeError,"cross: i or j out of range!");
1576 return (0);
1577 }
1578}
1579
1580/** Returns index of resulting vector component in 3x3 cross product.
1581 * In the column specified by the \a j index \a i is looked for and the found row index returned.
1582 * \param i vector component index, ranging from 0..NDIM
1583 * \param j index specifies which one of the four vectors in x*y - y*x, ranging from 0..3 (0,1 positive sign, 2,3 negative sign)
1584 * \return Component 0..2 of resulting vector
1585 * \sa cross() - is the same but vice versa, return value must be specified, \a i is returned.
1586 */
1587inline int crossed(int i, int j)
1588{
1589 const int matrix[NDIM*4] = {1,2,2,1,2,0,0,2,0,1,1,0};
1590 int k;
1591 if (i>=0 && i<NDIM && j>=0 && j<4) {
1592 for (k=0;k<NDIM;k++)
1593 if (matrix[4*k+j] == i) return(k);
1594 Error(SomeError,"crossed: given component not found!");
1595 return(-1);
1596 } else {
1597 Error(SomeError,"crossed: i or j out of range!");
1598 return (-1);
1599 }
1600}
1601
1602#define Nsin 16 //!< should be dependent on MaxG/MaxN per axis!
1603
1604/** Returns sawtooth shaped profile for position operator within cell.
1605 * This is a mapping from -L/2...L/2 (L = length of unit cell derived from Lattice#RealBasisSQ) to -L/2 to L/2 with a smooth transition:
1606 * \f[
1607 * f(x): x \rightarrow \left \{
1608 * \begin{array}{l}
1609 * -\frac{L}{2} \cdot \sin \left ( \frac{x}{0,05\cdot L} \cdot \frac{\pi}{2} \right ), 0<x<0,05\cdot L \\
1610 * (x - 0,05\cdot L) \cdot \frac{10}{9} - \frac{L}{2}, 0,05\cdot L \leq x<0,95\cdot L \\
1611 * \frac{L}{2} \cdot \cos \left ( \frac{x-0,95\cdot L}{0,05\cdot L} \cdot \frac{\pi}{2} \right), 0,95\cdot L<x<L
1612 * \end{array} \right \}
1613 * \f]
1614 * \param *Lat pointer to Lattice structure for Lattice#RealBasisSQ
1615 * \param L parameter x
1616 * \param index component index for Lattice#RealBasisSQ
1617 */
1618inline double sawtooth(struct Lattice *Lat, double L, const int index)
1619{
1620 double axis = sqrt(Lat->RealBasisSQ[index]);
1621 double sawstart = Lat->SawtoothStart;
1622 double sawend = 1. - sawstart;
1623 double sawfactor = (sawstart+sawend)/(sawend-sawstart);
1624 //return(L);
1625
1626 //fprintf(stderr, "sawstart: %e\tsawend: %e\tsawfactor: %e\tL: %e\n", sawstart, sawend, sawfactor, L);
1627 // transform and return (sawtooth profile checked, 04.08.06)
1628 L += axis/2.; // transform to 0 ... L
1629 if (L < (sawstart*axis)) return (-axis/(2*sawfactor)*sin(L/(sawstart*axis)*PI/2.)); // first smooth transition from 0 ... -L/2
1630 if (L > (sawend*axis)) return ( axis/(2*sawfactor)*cos((L-sawend*axis)/(sawstart*axis)*PI/2.)); // second smooth transition from +L/2 ... 0
1631 //fprintf(stderr,"L %e\t sawstart %e\t sawend %e\t sawfactor %e\t axis%e\n", L, sawstart, sawend, sawfactor, axis);
1632 //return ((L - sawstart*axis) - axis/(2*sawfactor)); // area in between scale to -L/2 ... +L/2
1633 return (L - axis/2); // area in between return as it was
1634}
1635
1636/** Shifts the origin of the gauge according to the CSDGT method.
1637 * \f[
1638 * d(r) = r - \sum_{I_s,I_a} (r-R_{I_s,I_a}) exp{(-\alpha_{I_s,I_a}(r-R_{I_s,I_a})^4)}
1639 * \f]
1640 * This trafo is necessary as the current otherweise (CSGT) sensitively depends on the current around
1641 * the core region inadequately/only moderately well approximated by a plane-wave-pseudo-potential-method.
1642 * \param *P Problem at hand, containing Lattice and Ions
1643 * \param r coordinate vector
1644 * \param index index of the basis vector
1645 * \return \f$d(r)\f$
1646 * \note Continuous Set of Damped Gauge Transformations according to Keith and Bader
1647 */
1648inline double ShiftGaugeOrigin(struct Problem *P, double r[NDIM], const int index)
1649{
1650 struct Ions *I = &P->Ion;
1651 struct Lattice *Lat = &P->Lat;
1652 double x[NDIM], tmp;
1653 int is,ia, i;
1654
1655 // loop over all ions to calculate the sum
1656 for(i=0;i<NDIM;i++)
1657 x[i] = r[i];
1658 for (is=0; is < I->Max_Types; is++)
1659 for (ia=0; ia < I->I[is].Max_IonsOfType; ia++)
1660 for(i=0;i<NDIM;i++) {
1661 tmp = (r[i] - I->I[is].R[NDIM*ia]);
1662 x[i] -= tmp*exp(- I->I[is].alpha[ia] * tpow(tmp,4));
1663 }
1664
1665 return(sawtooth(Lat,x[index],index)); // still use sawtooth due to the numerical instability around the border region of the cell
1666}
1667
1668/** Print sawtooth() for each node along one axis.
1669 * \param *P Problem at hand, containing RunStruct, Lattice and LatticeLevel RunStruct#LevS
1670 * \param index index of axis
1671 */
1672void TestSawtooth(struct Problem *P, const int index)
1673{
1674 struct RunStruct *R = &P->R;
1675 struct LatticeLevel *LevS = R->LevS;
1676 struct Lattice *Lat =&P->Lat;
1677 double x[NDIM];
1678 double n[NDIM];
1679 int N[NDIM];
1680 N[0] = LevS->Plan0.plan->N[0];
1681 N[1] = LevS->Plan0.plan->N[1];
1682 N[2] = LevS->Plan0.plan->N[2];
1683
1684 n[0] = n[1] = n[2] = 0.;
1685 for (n[index]=0;n[index]<N[index];n[index]++) {
1686 n[index] = (double)n[index]/(double)N[index] * sqrt(Lat->RealBasisSQ[index]);
1687 //fprintf(stderr,"(%i) x %e\t Axis/2 %e\n",P->Par.me, x, sqrt(Lat->RealBasisSQ[index])/2. );
1688 MinImageConv(Lat, n, Lat->RealBasisCenter, x);
1689 fprintf(stderr,"%e\t%e\n", n[index], sawtooth(Lat,n[index],index));
1690 }
1691}
1692
1693/** Secures minimum image convention between two given points \a R[] and \a r[] within periodic boundary.
1694 * Each distance component within a periodic boundary must always be between -L/2 ... L/2
1695 * \param *Lat pointer to Lattice structure
1696 * \param R[] first vector, NDIM, each must be between 0...L
1697 * \param r[] second vector, NDIM, each must be between 0...L
1698 * \param result[] return vector
1699 */
1700inline void MinImageConv(struct Lattice *Lat, const double R[NDIM], const double r[NDIM], double *result)
1701{
1702 //double axis = Lat->RealBasisQ[index];
1703 double x[NDIM], X[NDIM], Result[NDIM];
1704 int i;
1705
1706 for(i=0;i<NDIM;i++)
1707 result[i] = x[i] = x[i] = 0.;
1708 //fprintf(stderr, "R = (%lg, %lg, %lg), r = (%lg, %lg, %lg)\n", R[0], R[1], R[2], r[0], r[1], r[2]);
1709 RMat33Vec3(X, Lat->ReciBasis, R); // transform both to [0,1]^3
1710 RMat33Vec3(x, Lat->ReciBasis, r);
1711 //fprintf(stderr, "X = (%lg, %lg, %lg), x = (%lg, %lg, %lg)\n", X[0], X[1], X[2], x[0], x[1], x[2]);
1712 for(i=0;i<NDIM;i++) {
1713// if (fabs(X[i]) > 1.)
1714// fprintf(stderr,"X[%i] > 1. : %lg!\n", i, X[i]);
1715// if (fabs(x[i]) > 1.)
1716// fprintf(stderr,"x[%i] > 1. : %lg!\n", i, x[i]);
1717 if (fabs(Result[i] = X[i] - x[i] + 2.*PI) < PI) { }
1718 else if (fabs(Result[i] = X[i] - x[i]) <= PI) { }
1719 else if (fabs(Result[i] = X[i] - x[i] - 2.*PI) < PI) { }
1720 else Error(SomeError, "MinImageConv: None of the three cases applied!");
1721 }
1722 for(i=0;i<NDIM;i++) // ReciBasis is not true inverse, but times 2.*PI
1723 Result[i] /= 2.*PI;
1724 RMat33Vec3(result, Lat->RealBasis, Result);
1725}
1726
1727/** Linear interpolation for coordinate \a R that lies between grid nodes of \a *grid.
1728 * \param *P Problem at hand
1729 * \param *Lat Lattice structure for grid axis
1730 * \param *Lev LatticeLevel structure for grid axis node counts
1731 * \param R[] coordinate vector
1732 * \param *grid grid with fixed nodes
1733 * \return linearly interpolated value of \a *grid for position \a R[NDIM]
1734 */
1735double LinearInterpolationBetweenGrid(struct Problem *P, struct Lattice *Lat, struct LatticeLevel *Lev, double R[NDIM], fftw_real *grid)
1736{
1737 double x[2][NDIM];
1738 const int myPE = P->Par.me_comm_ST_Psi;
1739 int N[NDIM];
1740 const int N0 = Lev->Plan0.plan->local_nx;
1741 N[0] = Lev->Plan0.plan->N[0];
1742 N[1] = Lev->Plan0.plan->N[1];
1743 N[2] = Lev->Plan0.plan->N[2];
1744 int g;
1745 double n[NDIM];
1746 int k[2][NDIM];
1747 double sigma;
1748
1749 RMat33Vec3(n, Lat->ReciBasis, &R[0]); // transform real coordinates to [0,1]^3 vector
1750 for (g=0;g<NDIM;g++) {
1751 // k[i] are right and left nearest neighbour node to true position
1752 k[0][g] = floor(n[g]/(2.*PI)*(double)N[g]); // n[2] is floor grid
1753 k[1][g] = ceil(n[g]/(2.*PI)*(double)N[g]); // n[1] is ceil grid
1754 // x[i] give weights of left and right neighbours (the nearer the true point is to one, the closer its weight to 1)
1755 x[0][g] = (k[1][g] - n[g]/(2.*PI)*(double)N[g]);
1756 x[1][g] = 1. - x[0][g];
1757 //fprintf(stderr,"(%i) n = %lg, n_floor[%i] = %i\tn_ceil[%i] = %i --- x_floor[%i] = %e\tx_ceil[%i] = %e\n",P->Par.me, n[g], g,k[0][g], g,k[1][g], g,x[0][g], g,x[1][g]);
1758 }
1759 sigma = 0.;
1760 for (g=0;g<2;g++) { // interpolate linearly between adjacent grid points per axis
1761 if ((k[g][0] >= N0*myPE) && (k[g][0] < N0*(myPE+1))) {
1762 //fprintf(stderr,"(%i) grid[%i]: sigma = %e\n", P->Par.me, k[g][2]+N[2]*(k[g][1]+N[1]*(k[g][0]-N0*myPE)), sigma);
1763 sigma += (x[g][0]*x[0][1]*x[0][2])*grid[k[0][2]+N[2]*(k[0][1]+N[1]*(k[g][0]-N0*myPE))]*mu0; // if it's local and factor from inverse fft
1764 //fprintf(stderr,"(%i) grid[%i]: sigma += %e * %e \n", P->Par.me, k[g][2]+N[2]*(k[g][1]+N[1]*(k[g][0]-N0*myPE)), (x[g][0]*x[0][1]*x[0][2]), grid[k[0][2]+N[2]*(k[0][1]+N[1]*(k[g][0]-N0*myPE))]*mu0);
1765 sigma += (x[g][0]*x[0][1]*x[1][2])*grid[k[1][2]+N[2]*(k[0][1]+N[1]*(k[g][0]-N0*myPE))]*mu0; // if it's local and factor from inverse fft
1766 //fprintf(stderr,"(%i) grid[%i]: sigma += %e * %e \n", P->Par.me, k[g][2]+N[2]*(k[g][1]+N[1]*(k[g][0]-N0*myPE)), (x[g][0]*x[0][1]*x[1][2]), grid[k[1][2]+N[2]*(k[0][1]+N[1]*(k[g][0]-N0*myPE))]*mu0);
1767 sigma += (x[g][0]*x[1][1]*x[0][2])*grid[k[0][2]+N[2]*(k[1][1]+N[1]*(k[g][0]-N0*myPE))]*mu0; // if it's local and factor from inverse fft
1768 //fprintf(stderr,"(%i) grid[%i]: sigma += %e * %e \n", P->Par.me, k[g][2]+N[2]*(k[g][1]+N[1]*(k[g][0]-N0*myPE)), (x[g][0]*x[1][1]*x[0][2]), grid[k[0][2]+N[2]*(k[1][1]+N[1]*(k[g][0]-N0*myPE))]*mu0);
1769 sigma += (x[g][0]*x[1][1]*x[1][2])*grid[k[1][2]+N[2]*(k[1][1]+N[1]*(k[g][0]-N0*myPE))]*mu0; // if it's local and factor from inverse fft
1770 //fprintf(stderr,"(%i) grid[%i]: sigma += %e * %e \n", P->Par.me, k[g][2]+N[2]*(k[g][1]+N[1]*(k[g][0]-N0*myPE)), (x[g][0]*x[1][1]*x[1][2]), grid[k[1][2]+N[2]*(k[1][1]+N[1]*(k[g][0]-N0*myPE))]*mu0);
1771 }
1772 }
1773 return sigma;
1774}
1775
1776/** Linear Interpolation from all eight corners of the box that singles down to a point on the lower level.
1777 * \param *P Problem at hand
1778 * \param *Lev LatticeLevel structure for node numbers
1779 * \param upperNode Node around which to interpolate
1780 * \param *upperGrid array of grid points
1781 * \return summed up and then averaged octant around \a upperNode
1782 */
1783double LinearPullDownFromUpperLevel(struct Problem *P, struct LatticeLevel *Lev, int upperNode, fftw_real *upperGrid)
1784{
1785 const int N0 = Lev->Plan0.plan->local_nx;
1786 const int N1 = Lev->Plan0.plan->N[1];
1787 const int N2 = Lev->Plan0.plan->N[2];
1788 double lowerGrid = 0.;
1789 int nr=1;
1790 lowerGrid += upperGrid[upperNode];
1791 if (upperNode % N0 != N0-1) {
1792 lowerGrid += upperGrid[upperNode+1];
1793 nr++;
1794 if (upperNode % N1 != N1-1) {
1795 lowerGrid += upperGrid[upperNode + 0 + N2*(1 + N1*1)];
1796 nr++;
1797 if (upperNode % N2 != N2-1) {
1798 lowerGrid += upperGrid[upperNode + 1 + N2*(1 + N1*1)];
1799 nr++;
1800 }
1801 }
1802 if (upperNode % N2 != N2-1) {
1803 lowerGrid += upperGrid[upperNode + 1 + N2*(0 + N1*1)];
1804 nr++;
1805 }
1806 }
1807 if (upperNode % N1 != N1-1) {
1808 lowerGrid += upperGrid[upperNode + 0 + N2*(1 + N1*0)];
1809 nr++;
1810 if (upperNode % N2 != N2-1) {
1811 lowerGrid += upperGrid[upperNode + 1 + N2*(1 + N1*0)];
1812 nr++;
1813 }
1814 }
1815 if (upperNode % N2 != N2-1) {
1816 lowerGrid += upperGrid[upperNode + 1 + N2*(0 + N1*0)];
1817 nr++;
1818 }
1819 return (lowerGrid/(double)nr);
1820}
1821
1822/** Evaluates the 1-stern in order to evaluate the first derivative on the grid.
1823 * \param *P Problem at hand
1824 * \param *Lev Level to interpret the \a *density on
1825 * \param *density array with gridded values
1826 * \param *n 3 vector with indices on the grid
1827 * \param axis axis along which is derived
1828 * \param myPE number of processes who share the density
1829 * \return [+1/2 -1/2] of \a *n
1830 */
1831double FirstDiscreteDerivative(struct Problem *P, struct LatticeLevel *Lev, fftw_real *density, int *n, int axis, int myPE)
1832{
1833 int *N = Lev->Plan0.plan->N; // maximum nodes per axis
1834 const int N0 = Lev->Plan0.plan->local_nx; // special local number due to parallel split up
1835 double ret[NDIM], Ret[NDIM]; // return value local/global
1836 int i;
1837
1838 for (i=0;i<NDIM;i++) {
1839 ret[i] = Ret[i] = 0.;
1840 }
1841 if (((n[0]+1)%N[0] >= N0*myPE) && ((n[0]+1)%N[0] < N0*(myPE+1))) // next cell belongs to this process
1842 ret[0] += 1./2. * (density[n[2]+N[2]*(n[1]+N[1]*(n[0]+1-N0*myPE))]);
1843 if (((n[0]-1)%N[0] >= N0*myPE) && ((n[0]-1)%N[0] < N0*(myPE+1))) // previous cell belongs to this process
1844 ret[0] -= 1./2. * (density[n[2]+N[2]*(n[1]+N[1]*(n[0]-1-N0*myPE))]);
1845 if ((n[0] >= N0*myPE) && (n[0] < N0*(myPE+1))) {
1846 ret[1] += 1./2. * (density[n[2]+N[2]*((n[1]+1)%N[1] + N[1]*(n[0]%N0))]);
1847 ret[1] -= 1./2. * (density[n[2]+N[2]*((n[1]-1)%N[1] + N[1]*(n[0]%N0))]);
1848 }
1849 if ((n[0] >= N0*myPE) && (n[0] < N0*(myPE+1))) {
1850 ret[2] += 1./2. * (density[(n[2]+1)%N[2] + N[2]*(n[1]+N[1]*(n[0]%N0))]);
1851 ret[2] -= 1./2. * (density[(n[2]-1)%N[2] + N[2]*(n[1]+N[1]*(n[0]%N0))]);
1852 }
1853
1854 if (MPI_Allreduce(ret, Ret, 3, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi) != MPI_SUCCESS)
1855 Error(SomeError, "FirstDiscreteDerivative: MPI_Allreduce failure!");
1856
1857 for (i=0;i<NDIM;i++) // transform from node count to [0,1]^3
1858 Ret[i] *= N[i];
1859 RMat33Vec3(ret, P->Lat.ReciBasis, Ret); // this actually divides it by mesh length in real coordinates
1860 //fprintf(stderr, "(%i) sum at (%i,%i,%i) : %lg\n",P->Par.me, n[0],n[1],n[2], ret[axis]);
1861 return ret[axis]; ///(P->Lat.RealBasisQ[axis]/N[axis]);
1862}
1863
1864/** Fouriertransforms given \a source.
1865 * By the use of the symmetry parameter an additional imaginary unit and/or the momentum operator can
1866 * be applied at the same time.
1867 * \param *P Problem at hand
1868 * \param *Psi source array of reciprocal coefficients
1869 * \param *PsiR destination array, becoming filled with real coefficients
1870 * \param index_g component of G vector (only needed for symmetry=4..7)
1871 * \param symmetry 0 - do nothing, 1 - factor by "-1", 2 - factor by "i", 3 - factor by "1/i = -i", from 4 to 7 the same
1872 * but additionally with momentum operator
1873 */
1874void fft_Psi(struct Problem *P, const fftw_complex *Psi, fftw_real *PsiR, const int index_g, const int symmetry)
1875{
1876 struct Lattice *Lat = &P->Lat;
1877 struct RunStruct *R = &P->R;
1878 struct LatticeLevel *Lev0 = R->Lev0;
1879 struct LatticeLevel *LevS = R->LevS;
1880 struct Density *Dens0 = Lev0->Dens;
1881 struct fft_plan_3d *plan = Lat->plan;
1882 fftw_complex *tempdestRC = (fftw_complex *)Dens0->DensityArray[TempDensity];
1883 fftw_complex *work = Dens0->DensityCArray[TempDensity];
1884 fftw_complex *posfac, *destpos, *destRCS, *destRCD;
1885 int i, Index, pos;
1886
1887 LockDensityArray(Dens0,TempDensity,imag); // tempdestRC
1888 SetArrayToDouble0((double *)tempdestRC, Dens0->TotalSize*2);
1889 SetArrayToDouble0((double *)PsiR, Dens0->TotalSize*2);
1890 switch (symmetry) {
1891 case 0:
1892 for (i=0;i<LevS->MaxG;i++) { // incoming is positive, outgoing is positive
1893 Index = LevS->GArray[i].Index;
1894 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
1895 destpos = &tempdestRC[LevS->MaxNUp*Index];
1896 for (pos=0; pos < LevS->MaxNUp; pos++) {
1897 //if (destpos != &tempdestRC[LevS->MaxNUp*Index] || LevS->MaxNUp*Index+pos<0 || LevS->MaxNUp*Index+pos>=Dens0->TotalSize) Error(SomeError,"fft_Psi: destpos corrupted");
1898 destpos[pos].re = (Psi[i].re)*posfac[pos].re-(Psi[i].im)*posfac[pos].im;
1899 destpos[pos].im = (Psi[i].re)*posfac[pos].im+(Psi[i].im)*posfac[pos].re;
1900 }
1901 }
1902 break;
1903 case 1:
1904 for (i=0;i<LevS->MaxG;i++) { // incoming is positive, outgoing is - positive
1905 Index = LevS->GArray[i].Index;
1906 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
1907 destpos = &tempdestRC[LevS->MaxNUp*Index];
1908 for (pos=0; pos < LevS->MaxNUp; pos++) {
1909 //if (destpos != &tempdestRC[LevS->MaxNUp*Index] || LevS->MaxNUp*Index+pos<0 || LevS->MaxNUp*Index+pos>=Dens0->TotalSize) Error(SomeError,"fft_Psi: destpos corrupted");
1910 destpos[pos].re = -((Psi[i].re)*posfac[pos].re-(Psi[i].im)*posfac[pos].im);
1911 destpos[pos].im = -((Psi[i].re)*posfac[pos].im+(Psi[i].im)*posfac[pos].re);
1912 }
1913 }
1914 break;
1915 case 2:
1916 for (i=0;i<LevS->MaxG;i++) { // incoming is positive, outgoing is negative
1917 Index = LevS->GArray[i].Index;
1918 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
1919 destpos = &tempdestRC[LevS->MaxNUp*Index];
1920 for (pos=0; pos < LevS->MaxNUp; pos++) {
1921 //if (destpos != &tempdestRC[LevS->MaxNUp*Index] || LevS->MaxNUp*Index+pos<0 || LevS->MaxNUp*Index+pos>=Dens0->TotalSize) Error(SomeError,"fft_Psi: destpos corrupted");
1922 destpos[pos].re = (-Psi[i].im)*posfac[pos].re-(Psi[i].re)*posfac[pos].im;
1923 destpos[pos].im = (-Psi[i].im)*posfac[pos].im+(Psi[i].re)*posfac[pos].re;
1924 }
1925 }
1926 break;
1927 case 3:
1928 for (i=0;i<LevS->MaxG;i++) { // incoming is negative, outgoing is positive
1929 Index = LevS->GArray[i].Index;
1930 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
1931 destpos = &tempdestRC[LevS->MaxNUp*Index];
1932 for (pos=0; pos < LevS->MaxNUp; pos++) {
1933 //if (destpos != &tempdestRC[LevS->MaxNUp*Index] || LevS->MaxNUp*Index+pos<0 || LevS->MaxNUp*Index+pos>=Dens0->TotalSize) Error(SomeError,"fft_Psi: destpos corrupted");
1934 destpos[pos].re = (Psi[i].im)*posfac[pos].re-(-Psi[i].re)*posfac[pos].im;
1935 destpos[pos].im = (Psi[i].im)*posfac[pos].im+(-Psi[i].re)*posfac[pos].re;
1936 }
1937 }
1938 break;
1939 case 4:
1940 for (i=0;i<LevS->MaxG;i++) { // incoming is positive, outgoing is positive
1941 Index = LevS->GArray[i].Index;
1942 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
1943 destpos = &tempdestRC[LevS->MaxNUp*Index];
1944 for (pos=0; pos < LevS->MaxNUp; pos++) {
1945 //if (destpos != &tempdestRC[LevS->MaxNUp*Index] || LevS->MaxNUp*Index+pos<0 || LevS->MaxNUp*Index+pos>=Dens0->TotalSize) Error(SomeError,"fft_Psi: destpos corrupted");
1946 destpos[pos].re = LevS->GArray[i].G[index_g]*((Psi[i].re)*posfac[pos].re-(Psi[i].im)*posfac[pos].im);
1947 destpos[pos].im = LevS->GArray[i].G[index_g]*((Psi[i].re)*posfac[pos].im+(Psi[i].im)*posfac[pos].re);
1948 }
1949 }
1950 break;
1951 case 5:
1952 for (i=0;i<LevS->MaxG;i++) { // incoming is positive, outgoing is - positive
1953 Index = LevS->GArray[i].Index;
1954 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
1955 destpos = &tempdestRC[LevS->MaxNUp*Index];
1956 for (pos=0; pos < LevS->MaxNUp; pos++) {
1957 //if (destpos != &tempdestRC[LevS->MaxNUp*Index] || LevS->MaxNUp*Index+pos<0 || LevS->MaxNUp*Index+pos>=Dens0->TotalSize) Error(SomeError,"fft_Psi: destpos corrupted");
1958 destpos[pos].re = -LevS->GArray[i].G[index_g]*((Psi[i].re)*posfac[pos].re-(Psi[i].im)*posfac[pos].im);
1959 destpos[pos].im = -LevS->GArray[i].G[index_g]*((Psi[i].re)*posfac[pos].im+(Psi[i].im)*posfac[pos].re);
1960 }
1961 }
1962 break;
1963 case 6:
1964 for (i=0;i<LevS->MaxG;i++) { // incoming is positive, outgoing is negative
1965 Index = LevS->GArray[i].Index;
1966 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
1967 destpos = &tempdestRC[LevS->MaxNUp*Index];
1968 for (pos=0; pos < LevS->MaxNUp; pos++) {
1969 //if (destpos != &tempdestRC[LevS->MaxNUp*Index] || LevS->MaxNUp*Index+pos<0 || LevS->MaxNUp*Index+pos>=Dens0->TotalSize) Error(SomeError,"fft_Psi: destpos corrupted");
1970 destpos[pos].re = LevS->GArray[i].G[index_g]*((-Psi[i].im)*posfac[pos].re-(Psi[i].re)*posfac[pos].im);
1971 destpos[pos].im = LevS->GArray[i].G[index_g]*((-Psi[i].im)*posfac[pos].im+(Psi[i].re)*posfac[pos].re);
1972 }
1973 }
1974 break;
1975 case 7:
1976 for (i=0;i<LevS->MaxG;i++) { // incoming is negative, outgoing is positive
1977 Index = LevS->GArray[i].Index;
1978 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
1979 destpos = &tempdestRC[LevS->MaxNUp*Index];
1980 for (pos=0; pos < LevS->MaxNUp; pos++) {
1981 //if (destpos != &tempdestRC[LevS->MaxNUp*Index] || LevS->MaxNUp*Index+pos<0 || LevS->MaxNUp*Index+pos>=Dens0->TotalSize) Error(SomeError,"fft_Psi: destpos corrupted");
1982 destpos[pos].re = LevS->GArray[i].G[index_g]*((Psi[i].im)*posfac[pos].re-(-Psi[i].re)*posfac[pos].im);
1983 destpos[pos].im = LevS->GArray[i].G[index_g]*((Psi[i].im)*posfac[pos].im+(-Psi[i].re)*posfac[pos].re);
1984 }
1985 }
1986 break;
1987 }
1988 for (i=0; i<LevS->MaxDoubleG; i++) {
1989 destRCS = &tempdestRC[LevS->DoubleG[2*i]*LevS->MaxNUp];
1990 destRCD = &tempdestRC[LevS->DoubleG[2*i+1]*LevS->MaxNUp];
1991 for (pos=0; pos < LevS->MaxNUp; pos++) {
1992 //if (destRCD != &tempdestRC[LevS->DoubleG[2*i+1]*LevS->MaxNUp] || LevS->DoubleG[2*i+1]*LevS->MaxNUp+pos<0 || LevS->DoubleG[2*i+1]*LevS->MaxNUp+pos>=Dens0->TotalSize) Error(SomeError,"fft_Psi: destRCD corrupted");
1993 destRCD[pos].re = destRCS[pos].re;
1994 destRCD[pos].im = -destRCS[pos].im;
1995 }
1996 }
1997 fft_3d_complex_to_real(plan, LevS->LevelNo, FFTNFUp, tempdestRC, work);
1998 DensityRTransformPos(LevS,(fftw_real*)tempdestRC, PsiR);
1999 UnLockDensityArray(Dens0,TempDensity,imag); // tempdestRC
2000}
2001
2002/** Locks all NDIM_NDIM current density arrays
2003 * \param Dens0 Density structure to be locked (in the current parts)
2004 */
2005void AllocCurrentDensity(struct Density *Dens0) {
2006 // real
2007 LockDensityArray(Dens0,CurrentDensity0,real); // CurrentDensity[B_index]
2008 LockDensityArray(Dens0,CurrentDensity1,real); // CurrentDensity[B_index]
2009 LockDensityArray(Dens0,CurrentDensity2,real); // CurrentDensity[B_index]
2010 LockDensityArray(Dens0,CurrentDensity3,real); // CurrentDensity[B_index]
2011 LockDensityArray(Dens0,CurrentDensity4,real); // CurrentDensity[B_index]
2012 LockDensityArray(Dens0,CurrentDensity5,real); // CurrentDensity[B_index]
2013 LockDensityArray(Dens0,CurrentDensity6,real); // CurrentDensity[B_index]
2014 LockDensityArray(Dens0,CurrentDensity7,real); // CurrentDensity[B_index]
2015 LockDensityArray(Dens0,CurrentDensity8,real); // CurrentDensity[B_index]
2016 // imaginary
2017 LockDensityArray(Dens0,CurrentDensity0,imag); // CurrentDensity[B_index]
2018 LockDensityArray(Dens0,CurrentDensity1,imag); // CurrentDensity[B_index]
2019 LockDensityArray(Dens0,CurrentDensity2,imag); // CurrentDensity[B_index]
2020 LockDensityArray(Dens0,CurrentDensity3,imag); // CurrentDensity[B_index]
2021 LockDensityArray(Dens0,CurrentDensity4,imag); // CurrentDensity[B_index]
2022 LockDensityArray(Dens0,CurrentDensity5,imag); // CurrentDensity[B_index]
2023 LockDensityArray(Dens0,CurrentDensity6,imag); // CurrentDensity[B_index]
2024 LockDensityArray(Dens0,CurrentDensity7,imag); // CurrentDensity[B_index]
2025 LockDensityArray(Dens0,CurrentDensity8,imag); // CurrentDensity[B_index]
2026}
2027
2028/** Reset and unlocks all NDIM_NDIM current density arrays
2029 * \param Dens0 Density structure to be unlocked/resetted (in the current parts)
2030 */
2031void DisAllocCurrentDensity(struct Density *Dens0) {
2032 //int i;
2033 // real
2034// for(i=0;i<NDIM*NDIM;i++)
2035// SetArrayToDouble0((double *)Dens0->DensityArray[i], Dens0->TotalSize*2);
2036 UnLockDensityArray(Dens0,CurrentDensity0,real); // CurrentDensity[B_index]
2037 UnLockDensityArray(Dens0,CurrentDensity1,real); // CurrentDensity[B_index]
2038 UnLockDensityArray(Dens0,CurrentDensity2,real); // CurrentDensity[B_index]
2039 UnLockDensityArray(Dens0,CurrentDensity3,real); // CurrentDensity[B_index]
2040 UnLockDensityArray(Dens0,CurrentDensity4,real); // CurrentDensity[B_index]
2041 UnLockDensityArray(Dens0,CurrentDensity5,real); // CurrentDensity[B_index]
2042 UnLockDensityArray(Dens0,CurrentDensity6,real); // CurrentDensity[B_index]
2043 UnLockDensityArray(Dens0,CurrentDensity7,real); // CurrentDensity[B_index]
2044 UnLockDensityArray(Dens0,CurrentDensity8,real); // CurrentDensity[B_index]
2045 // imaginary
2046// for(i=0;i<NDIM*NDIM;i++)
2047// SetArrayToDouble0((double *)Dens0->DensityCArray[i], Dens0->TotalSize*2);
2048 UnLockDensityArray(Dens0,CurrentDensity0,imag); // CurrentDensity[B_index]
2049 UnLockDensityArray(Dens0,CurrentDensity1,imag); // CurrentDensity[B_index]
2050 UnLockDensityArray(Dens0,CurrentDensity2,imag); // CurrentDensity[B_index]
2051 UnLockDensityArray(Dens0,CurrentDensity3,imag); // CurrentDensity[B_index]
2052 UnLockDensityArray(Dens0,CurrentDensity4,imag); // CurrentDensity[B_index]
2053 UnLockDensityArray(Dens0,CurrentDensity5,imag); // CurrentDensity[B_index]
2054 UnLockDensityArray(Dens0,CurrentDensity6,imag); // CurrentDensity[B_index]
2055 UnLockDensityArray(Dens0,CurrentDensity7,imag); // CurrentDensity[B_index]
2056 UnLockDensityArray(Dens0,CurrentDensity8,imag); // CurrentDensity[B_index]
2057}
2058
2059// these defines safe-guard same symmetry for same kind of wave function
2060#define Psi0symmetry 0 // //0 //0 //0 // regard psi0 as real
2061#define Psi1symmetry 0 // //3 //0 //0 // regard psi0 as real
2062#define Psip0symmetry 6 //6 //6 //6 //6 // momentum times "i" due to operation on left hand
2063#define Psip1symmetry 7 //7 //4 //6 //7 // momentum times "-i" as usual (right hand)
2064
2065/** Evaluates the 3x3 current density arrays.
2066 * The formula we want to evaluate is as follows
2067 * \f[
2068 * j_k(r) = \langle \psi_k^{(0)} | \Bigl ( p|r'\rangle\langle r' | + | r' \rangle \langle r' | p \Bigr )
2069 \Bigl [ | \psi_k^{(r\times p )} \rangle - r' \times | \psi_k^{(p)} \rangle \Bigr ] \cdot B.
2070 * \f]
2071 * Most of the DensityTypes-arrays are locked for temporary use. Pointers are set to their
2072 * start address and afterwards the current density arrays locked and reset'ed. Then for every
2073 * unperturbed wave function we do:
2074 * -# FFT unperturbed p-perturbed and rxp-perturbed wave function
2075 * -# FFT wave function with applied momentum operator for all three indices
2076 * -# For each index of the momentum operator:
2077 * -# FFT p-perturbed wave function
2078 * -# For every index of the external field:
2079 * -# FFT rxp-perturbed wave function
2080 * -# Evaluate current density for these momentum index and external field indices
2081 *
2082 * Afterwards the temporary densities are unlocked and the density ones gathered from all Psi-
2083 * sharing processes.
2084 *
2085 * \param *P Problem at hand, containing Lattice and RunStruct
2086 */
2087void FillCurrentDensity(struct Problem *P)
2088{
2089 struct Lattice *Lat = &P->Lat;
2090 struct RunStruct *R = &P->R;
2091 struct Psis *Psi = &Lat->Psi;
2092 struct LatticeLevel *LevS = R->LevS;
2093 struct LatticeLevel *Lev0 = R->Lev0;
2094 struct Density *Dens0 = Lev0->Dens;
2095 fftw_complex *Psi0;
2096 fftw_real *Psi0R, *Psip0R;
2097 fftw_real *CurrentDensity[NDIM*NDIM];
2098 fftw_real *Psi1R;
2099 fftw_real *Psip1R;
2100 fftw_real *tempArray; // intendedly the same
2101 double r_bar[NDIM], x[NDIM], X[NDIM], fac[NDIM];
2102 double Current;//, current;
2103 const double UnitsFactor = 1.; ///LevS->MaxN; // 1/N (from ff-backtransform)
2104 int i, index, B_index;
2105 int k, j, i0;
2106 int n[NDIM], n0;
2107 int *N;
2108 N = Lev0->Plan0.plan->N;
2109 const int N0 = Lev0->Plan0.plan->local_nx;
2110 //int ActNum;
2111 const int myPE = P->Par.me_comm_ST_Psi;
2112 const int type = R->CurrentMin;
2113 MPI_Status status;
2114 int cross_lookup_1[4], cross_lookup_3[4], l_1 = 0, l_3 = 0;
2115 double Factor;//, factor;
2116
2117 //fprintf(stderr,"(%i) FactoR %e\n", P->Par.me, R->FactorDensityR);
2118
2119 // Init values and pointers
2120 if (P->Call.out[PsiOut]) {
2121 fprintf(stderr,"(%i) LockArray: ", P->Par.me);
2122 for(i=0;i<MaxDensityTypes;i++)
2123 fprintf(stderr,"(%i,%i) ",Dens0->LockArray[i],Dens0->LockCArray[i]);
2124 fprintf(stderr,"\n");
2125 }
2126 LockDensityArray(Dens0,Temp2Density,real); // Psi1R
2127 LockDensityArray(Dens0,Temp2Density,imag); // Psip1R and tempArray
2128 LockDensityArray(Dens0,GapDensity,real); // Psi0R
2129 LockDensityArray(Dens0,GapLocalDensity,real); // Psip0R
2130
2131 Psi0R = (fftw_real *)Dens0->DensityArray[GapDensity];
2132 Psip0R = (fftw_real *)Dens0->DensityArray[GapLocalDensity];
2133 Psi1R = (fftw_real *)Dens0->DensityArray[Temp2Density];
2134 tempArray = Psip1R = (fftw_real *)Dens0->DensityCArray[Temp2Density];
2135 SetArrayToDouble0((double *)Psi0R,Dens0->TotalSize*2);
2136 SetArrayToDouble0((double *)Psip0R,Dens0->TotalSize*2);
2137 SetArrayToDouble0((double *)Psi1R,Dens0->TotalSize*2);
2138 SetArrayToDouble0((double *)Psip1R,Dens0->TotalSize*2);
2139
2140 if (P->Call.out[PsiOut]) {
2141 fprintf(stderr,"(%i) LockArray: ", P->Par.me);
2142 for(i=0;i<MaxDensityTypes;i++)
2143 fprintf(stderr,"(%i,%i) ",Dens0->LockArray[i],Dens0->LockCArray[i]);
2144 fprintf(stderr,"\n");
2145 }
2146
2147 // don't put the following stuff into a for loop, they might not be continuous! (preprocessor values: CurrentDensity...)
2148 CurrentDensity[0] = (fftw_real *) Dens0->DensityArray[CurrentDensity0];
2149 CurrentDensity[1] = (fftw_real *) Dens0->DensityArray[CurrentDensity1];
2150 CurrentDensity[2] = (fftw_real *) Dens0->DensityArray[CurrentDensity2];
2151 CurrentDensity[3] = (fftw_real *) Dens0->DensityArray[CurrentDensity3];
2152 CurrentDensity[4] = (fftw_real *) Dens0->DensityArray[CurrentDensity4];
2153 CurrentDensity[5] = (fftw_real *) Dens0->DensityArray[CurrentDensity5];
2154 CurrentDensity[6] = (fftw_real *) Dens0->DensityArray[CurrentDensity6];
2155 CurrentDensity[7] = (fftw_real *) Dens0->DensityArray[CurrentDensity7];
2156 CurrentDensity[8] = (fftw_real *) Dens0->DensityArray[CurrentDensity8];
2157
2158 // initialize the array if it is the first of all six perturbation run
2159 if ((R->DoFullCurrent == 0) && (R->CurrentMin == Perturbed_P0)) { // reset if FillDelta...() hasn't done it before
2160 debug(P,"resetting CurrentDensity...");
2161 for (B_index=0; B_index<NDIM*NDIM; B_index++) // initialize current density array
2162 SetArrayToDouble0((double *)CurrentDensity[B_index],Dens0->TotalSize*2); // DensityArray is fftw_real, no 2*LocalSizeR here!
2163 }
2164
2165 switch(type) { // set j (which is linked to the index from derivation wrt to B^{ext})
2166 case Perturbed_P0:
2167 case Perturbed_P1:
2168 case Perturbed_P2:
2169 j = type - Perturbed_P0;
2170 l_1 = crossed(j,1);
2171 l_3 = crossed(j,3);
2172 for(k=0;k<4;k++) {
2173 cross_lookup_1[k] = cross(l_1,k);
2174 cross_lookup_3[k] = cross(l_3,k);
2175 }
2176 break;
2177 case Perturbed_RxP0:
2178 case Perturbed_RxP1:
2179 case Perturbed_RxP2:
2180 j = type - Perturbed_RxP0;
2181 break;
2182 default:
2183 j = 0;
2184 Error(SomeError,"FillCurrentDensity() called while not in perturbed minimisation!");
2185 break;
2186 }
2187
2188 int wished = -1;
2189 FILE *file = fopen(P->Call.MainParameterFile,"r");
2190 if (!ParseForParameter(0,file,"Orbital",0,1,1,int_type,&wished, 1, optional)) {
2191 if (P->Call.out[ReadOut]) fprintf(stderr,"Desired Orbital missing, using: All!\n");
2192 wished = -1;
2193 } else if (wished != -1) {
2194 if (P->Call.out[ReadOut]) fprintf(stderr,"Desired Orbital is: %i.\n", wished);
2195 } else {
2196 if (P->Call.out[ReadOut]) fprintf(stderr,"Desired Orbital is: All.\n");
2197 }
2198 fclose(file);
2199
2200 // Commence grid filling
2201 for (k=Psi->TypeStartIndex[Occupied];k<Psi->TypeStartIndex[Occupied+1];k++) // every local wave functions adds up its part of the current
2202 if ((k + P->Par.me_comm_ST_PsiT*(Psi->TypeStartIndex[UnOccupied]-Psi->TypeStartIndex[Occupied]) == wished) || (wished == -1)) { // compare with global number
2203 if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i)Calculating Current Density Summand of type %s for Psi (%i/%i) ... \n", P->Par.me, R->MinimisationName[type], Psi->LocalPsiStatus[k].MyGlobalNo, k);
2204 //ActNum = k - Psi->TypeStartIndex[Occupied] + Psi->TypeStartIndex[1] * Psi->LocalPsiStatus[k].my_color_comm_ST_Psi; // global number of unperturbed Psi
2205 Psi0 = LevS->LPsi->LocalPsi[k]; // Local unperturbed psi
2206
2207 // now some preemptive ffts for the whole grid
2208 if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) Bringing |Psi0> one level up and fftransforming\n", P->Par.me);
2209 fft_Psi(P, Psi0, Psi0R, 0, Psi0symmetry); //0 // 0 //0
2210
2211 if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) Bringing |Psi1> one level up and fftransforming\n", P->Par.me);
2212 fft_Psi(P, LevS->LPsi->LocalPsi[Psi->TypeStartIndex[type]+k], Psi1R, 0, Psi1symmetry); //3 //0 //0
2213
2214 for (index=0;index<NDIM;index++) { // for all NDIM components of momentum operator
2215
2216 if ((P->Call.out[StepLeaderOut]) && (!index)) fprintf(stderr,"(%i) Bringing p|Psi0> one level up and fftransforming\n", P->Par.me);
2217 fft_Psi(P, Psi0, Psip0R, index, Psip0symmetry); //6 //6 //6
2218
2219 if ((P->Call.out[StepLeaderOut]) && (!index)) fprintf(stderr,"(%i) Bringing p|Psi1> one level up and fftransforming\n", P->Par.me);
2220 fft_Psi(P, LevS->LPsi->LocalPsi[Psi->TypeStartIndex[type]+k], Psip1R, index, Psip1symmetry); //4 //6 //7
2221
2222 // then for every point on the grid in real space ...
2223
2224 //if (Psi1R != (fftw_real *)Dens0->DensityArray[Temp2Density] || i0<0 || i0>=Dens0->LocalSizeR) Error(SomeError,"fft_Psi: Psi1R corrupted");
2225 //Psi1R[i0] = (Psi1_rxp_R[j])[i0] - (r_bar[cross(j,0)] * (Psi1_p_R[cross(j,1)])[i0] - r_bar[cross(j,2)] * (Psi1_p_R[cross(j,3)])[i0]); //
2226 //if (Psip1R != (fftw_real *)Dens0->DensityCArray[Temp2Density] || i0<0 || i0>=Dens0->LocalSizeR) Error(SomeError,"fft_Psi: Psip1R corrupted");
2227 //Psip1R[i0] = Psi1_rxp_pR[i0] - (r_bar[cross(j,0)] * (Psi1_p_pR[cross(j,1)])[i0] - r_bar[cross(j,2)] * (Psi1_p_pR[cross(j,3)])[i0]); //
2228
2229 switch(type) {
2230 case Perturbed_P0:
2231 case Perturbed_P1:
2232 case Perturbed_P2:
2233/* // evaluate factor to compensate r x normalized phi(r) against normalized phi(rxp)
2234 factor = 0.;
2235 for (n0=0;n0<N0;n0++) // only local points on x axis
2236 for (n[1]=0;n[1]<N[1];n[1]++)
2237 for (n[2]=0;n[2]<N[2];n[2]++) {
2238 i0 = n[2]+N[2]*(n[1]+N[1]*n0);
2239 n[0]=n0 + N0*myPE; // global relative coordinate: due to partitoning of x-axis in PEPGamma>1 case
2240 fac[0] = (double)n[0]/(double)N[0];
2241 fac[1] = (double)n[1]/(double)N[1];
2242 fac[2] = (double)n[2]/(double)N[2];
2243 RMat33Vec3(x, Lat->RealBasis, fac); // relative coordinate times basis matrix gives absolute ones
2244 MinImageConv(Lat, x, Psi->AddData[k].WannierCentre, X)
2245 for (i=0;i<NDIM;i++) // build gauge-translated r_bar evaluation point
2246 r_bar[i] = sawtooth(Lat,X,i);
2247// ShiftGaugeOrigin(P,X,i);
2248 //truedist(Lat, x[i], Psi->AddData[k].WannierCentre[i], i);
2249 factor += Psi1R[i0] * (r_bar[cross_lookup_1[0]] * Psi1R[i0]);
2250 }
2251 MPI_Allreduce (&factor, &Factor, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
2252 Factor *= R->FactorDensityR; // discrete integration constant
2253 fprintf(stderr,"(%i) normalization factor of Phi^(RxP%i)_{%i} is %lg\n", P->Par.me, type, k, Factor);
2254 Factor = 1./sqrt(fabs(Factor)); //Factor/fabs(Factor) */
2255 Factor = 1.;
2256 for (n0=0;n0<N0;n0++) // only local points on x axis
2257 for (n[1]=0;n[1]<N[1];n[1]++)
2258 for (n[2]=0;n[2]<N[2];n[2]++) {
2259 i0 = n[2]+N[2]*(n[1]+N[1]*n0);
2260 n[0]=n0 + N0*myPE; // global relative coordinate: due to partitoning of x-axis in PEPGamma>1 case
2261 fac[0] = (double)n[0]/(double)N[0];
2262 fac[1] = (double)n[1]/(double)N[1];
2263 fac[2] = (double)n[2]/(double)N[2];
2264 RMat33Vec3(x, Lat->RealBasis, fac); // relative coordinate times basis matrix gives absolute ones
2265 MinImageConv(Lat, x, Psi->AddData[k].WannierCentre, X);
2266 for (i=0;i<NDIM;i++) // build gauge-translated r_bar evaluation point
2267 r_bar[i] = sawtooth(Lat,X[i],i);
2268// ShiftGaugeOrigin(P,X,i);
2269 //X[i];
2270 Current = Psip0R[i0] * (r_bar[cross_lookup_1[0]] * Psi1R[i0]);
2271 Current += (Psi0R[i0] * r_bar[cross_lookup_1[0]] * Psip1R[i0]);
2272 Current *= .5 * UnitsFactor * Psi->LocalPsiStatus[k].PsiFactor * R->FactorDensityR; // factor confirmed, see CalculateOneDensityR() and InitDensityCalculation()
2273 ////if (CurrentDensity[index+j*NDIM] != (fftw_real *) Dens0->DensityArray[CurrentDensity0 + index+j*NDIM] || i0<0 || i0>=Dens0->LocalSizeR || (index+j*NDIM)<0 || (index+j*NDIM)>=NDIM*NDIM) Error(SomeError,"FillCurrentDensity: CurrentDensity[] corrupted");
2274 CurrentDensity[index+l_1*NDIM][i0] -= Current; // note: sign of cross product resides in Current itself (here: plus)
2275 Current = - Psip0R[i0] * (r_bar[cross_lookup_3[2]] * Psi1R[i0]);
2276 Current += - (Psi0R[i0] * r_bar[cross_lookup_3[2]] * Psip1R[i0]);
2277 Current *= .5 * UnitsFactor * Psi->LocalPsiStatus[k].PsiFactor * R->FactorDensityR; // factor confirmed, see CalculateOneDensityR() and InitDensityCalculation()
2278 ////if (CurrentDensity[index+j*NDIM] != (fftw_real *) Dens0->DensityArray[CurrentDensity0 + index+j*NDIM] || i0<0 || i0>=Dens0->LocalSizeR || (index+j*NDIM)<0 || (index+j*NDIM)>=NDIM*NDIM) Error(SomeError,"FillCurrentDensity: CurrentDensity[] corrupted");
2279 CurrentDensity[index+l_3*NDIM][i0] -= Current; // note: sign of cross product resides in Current itself (here: minus)
2280 }
2281 break;
2282 case Perturbed_RxP0:
2283 case Perturbed_RxP1:
2284 case Perturbed_RxP2:
2285 for (n0=0;n0<N0;n0++) // only local points on x axis
2286 for (n[1]=0;n[1]<N[1];n[1]++)
2287 for (n[2]=0;n[2]<N[2];n[2]++) {
2288 i0 = n[2]+N[2]*(n[1]+N[1]*n0);
2289 Current = (Psip0R[i0] * Psi1R[i0] + Psi0R[i0] * Psip1R[i0]);
2290 Current *= .5 * UnitsFactor * Psi->LocalPsiStatus[k].PsiFactor * R->FactorDensityR; // factor confirmed, see CalculateOneDensityR() and InitDensityCalculation()
2291 ////if (CurrentDensity[index+j*NDIM] != (fftw_real *) Dens0->DensityArray[CurrentDensity0 + index+j*NDIM] || i0<0 || i0>=Dens0->LocalSizeR || (index+j*NDIM)<0 || (index+j*NDIM)>=NDIM*NDIM) Error(SomeError,"FillCurrentDensity: CurrentDensity[] corrupted");
2292 CurrentDensity[index+j*NDIM][i0] += Current;
2293 }
2294 break;
2295 default:
2296 break;
2297 }
2298 }
2299 //OutputCurrentDensity(P);
2300 }
2301
2302 //debug(P,"Unlocking arrays");
2303 //debug(P,"GapDensity");
2304 UnLockDensityArray(Dens0,GapDensity,real); // Psi0R
2305 //debug(P,"GapLocalDensity");
2306 UnLockDensityArray(Dens0,GapLocalDensity,real); // Psip0R
2307 //debug(P,"Temp2Density");
2308 UnLockDensityArray(Dens0,Temp2Density,real); // Psi1R
2309
2310// if (P->Call.out[StepLeaderOut])
2311// fprintf(stderr,"\n\n");
2312
2313 //debug(P,"MPI operation");
2314 // and in the end gather partial densities from other processes
2315 if (type == Perturbed_RxP2) // exchange all (due to shared wave functions) only after last pertubation run
2316 for (index=0;index<NDIM*NDIM;index++) {
2317 //if (tempArray != (fftw_real *)Dens0->DensityCArray[Temp2Density]) Error(SomeError,"FillCurrentDensity: tempArray corrupted");
2318 //debug(P,"tempArray to zero");
2319 SetArrayToDouble0((double *)tempArray, Dens0->TotalSize*2);
2320 ////if (CurrentDensity[index] != (fftw_real *) Dens0->DensityArray[CurrentDensity0 + index]) Error(SomeError,"FillCurrentDensity: CurrentDensity[] corrupted");
2321 //debug(P,"CurrentDensity exchange");
2322 MPI_Allreduce( CurrentDensity[index], tempArray, Dens0->LocalSizeR, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_PsiT); // gather results from all wave functions ...
2323 switch(Psi->PsiST) { // ... and also from SpinUp/Downs
2324 default:
2325 //debug(P,"CurrentDensity = tempArray");
2326 for (i=0;i<Dens0->LocalSizeR;i++) {
2327 ////if (CurrentDensity[index] != (fftw_real *) Dens0->DensityArray[CurrentDensity0 + index] || i<0 || i>=Dens0->LocalSizeR) Error(SomeError,"FillCurrentDensity: CurrentDensity[] corrupted");
2328 CurrentDensity[index][i] = tempArray[i];
2329 }
2330 break;
2331 case SpinUp:
2332 //debug(P,"CurrentDensity exchange spinup");
2333 MPI_Sendrecv(tempArray, Dens0->LocalSizeR, MPI_DOUBLE, P->Par.me_comm_ST, CurrentTag1,
2334 CurrentDensity[index], Dens0->LocalSizeR, MPI_DOUBLE, P->Par.me_comm_ST, CurrentTag2, P->Par.comm_STInter, &status );
2335 //debug(P,"CurrentDensity += tempArray");
2336 for (i=0;i<Dens0->LocalSizeR;i++) {
2337 ////if (CurrentDensity[index] != (fftw_real *) Dens0->DensityArray[CurrentDensity0 + index] || i<0 || i>=Dens0->LocalSizeR) Error(SomeError,"FillCurrentDensity: CurrentDensity[] corrupted");
2338 CurrentDensity[index][i] += tempArray[i];
2339 }
2340 break;
2341 case SpinDown:
2342 //debug(P,"CurrentDensity exchange spindown");
2343 MPI_Sendrecv(tempArray, Dens0->LocalSizeR, MPI_DOUBLE, P->Par.me_comm_ST, CurrentTag2,
2344 CurrentDensity[index], Dens0->LocalSizeR, MPI_DOUBLE, P->Par.me_comm_ST, CurrentTag1, P->Par.comm_STInter, &status );
2345 //debug(P,"CurrentDensity += tempArray");
2346 for (i=0;i<Dens0->LocalSizeR;i++) {
2347 ////if (CurrentDensity[index] != (fftw_real *) Dens0->DensityArray[CurrentDensity0 + index] || i<0 || i>=Dens0->LocalSizeR) Error(SomeError,"FillCurrentDensity: CurrentDensity[] corrupted");
2348 CurrentDensity[index][i] += tempArray[i];
2349 }
2350 break;
2351 }
2352 }
2353 //debug(P,"Temp2Density");
2354 UnLockDensityArray(Dens0,Temp2Density,imag); // Psip1R and tempArray
2355 //debug(P,"CurrentDensity end");
2356}
2357
2358/** Structure holding Problem at hand and two indices, defining the greens function to be inverted.
2359 */
2360struct params
2361{
2362 struct Problem *P;
2363 int *k;
2364 int *l;
2365 int *iter;
2366 fftw_complex *x_l;
2367};
2368
2369/** Wrapper function to solve G_kl x = b for x.
2370 * \param *x above x
2371 * \param *param additional parameters, here Problem at hand
2372 * \return evaluated to be minimized functional \f$\frac{1}{2}x \cdot Ax - xb\f$ at \a x on return
2373 */
2374static double DeltaCurrent_f(const gsl_vector * x, void * param)
2375{
2376 struct Problem *P = ((struct params *)param)->P;
2377 struct RunStruct *R = &P->R;
2378 struct LatticeLevel *LevS = R->LevS;
2379 struct Psis *Psi = &P->Lat.Psi;
2380 struct PseudoPot *PP = &P->PP;
2381 const double PsiFactor = Psi->AllPsiStatus[*((struct params *)param)->k].PsiFactor;
2382 double result = 0.;
2383 fftw_complex *TempPsi = LevS->LPsi->TempPsi;
2384 fftw_complex *TempPsi2 = LevS->LPsi->TempPsi2;
2385 int u;
2386
2387 //fprintf(stderr,"Evaluating f(%i,%i) for %i-th time\n", *((struct params *)param)->k, *((struct params *)param)->l, *((struct params *)param)->iter);
2388
2389 // extract gsl_vector
2390 for (u=0;u<LevS->MaxG;u++) {
2391 TempPsi[u].re = gsl_vector_get(x, 2*u);
2392 TempPsi[u].im = gsl_vector_get(x, 2*u+1);
2393 }
2394 // generate fnl
2395 CalculateCDfnl(P, TempPsi, PP->CDfnl); // calculate needed non-local form factors
2396 // Apply Hamiltonian to x
2397 ApplyTotalHamiltonian(P,TempPsi,TempPsi2, PP->CDfnl,PsiFactor,0);
2398 // take scalar product to get eigen value
2399 result = .5 * PsiFactor * (((*((struct params *)param)->k == *((struct params *)param)->l ? GradSP(P,LevS,TempPsi,TempPsi2) : 0.) - Psi->lambda[*((struct params *)param)->k][*((struct params *)param)->l])) - GradSP(P,LevS,TempPsi,LevS->LPsi->LocalPsi[*((struct params *)param)->l]);
2400 return result;
2401}
2402
2403/** Wrapper function to solve G_kl x = b for x.
2404 * \param *x above x
2405 * \param *param additional parameters, here Problem at hand
2406 * \param *g gradient vector on return
2407 * \return error code
2408 */
2409static void DeltaCurrent_df(const gsl_vector * x, void * param, gsl_vector * g)
2410{
2411 struct Problem *P = ((struct params *)param)->P;
2412 struct RunStruct *R = &P->R;
2413 struct LatticeLevel *LevS = R->LevS;
2414 struct Psis *Psi = &P->Lat.Psi;
2415 struct PseudoPot *PP = &P->PP;
2416 const double PsiFactor = Psi->AllPsiStatus[*((struct params *)param)->k].PsiFactor;
2417 fftw_complex *TempPsi = LevS->LPsi->TempPsi;
2418 fftw_complex *TempPsi2 = LevS->LPsi->TempPsi2;
2419 fftw_complex *x_l = ((struct params *)param)->x_l;
2420 int u;
2421
2422 //fprintf(stderr,"Evaluating df(%i,%i) for %i-th time\n", *((struct params *)param)->k, *((struct params *)param)->l, *((struct params *)param)->iter);
2423
2424 // extract gsl_vector
2425 for (u=0;u<LevS->MaxG;u++) {
2426 TempPsi[u].re = gsl_vector_get(x, 2*u);
2427 TempPsi[u].im = gsl_vector_get(x, 2*u+1);
2428 }
2429 // generate fnl
2430 CalculateCDfnl(P, TempPsi, PP->CDfnl); // calculate needed non-local form factors
2431 // Apply Hamiltonian to x
2432 ApplyTotalHamiltonian(P,TempPsi,TempPsi2, PP->CDfnl,PsiFactor,0);
2433 // put into returning vector
2434 for (u=0;u<LevS->MaxG;u++) {
2435 gsl_vector_set(g, 2*u, TempPsi2[u].re - x_l[u].re);
2436 gsl_vector_set(g, 2*u+1, TempPsi2[u].im - x_l[u].im);
2437 }
2438}
2439
2440/** Wrapper function to solve G_kl x = b for x.
2441 * \param *x above x
2442 * \param *param additional parameters, here Problem at hand
2443 * \param *f evaluated to be minimized functional \f$\frac{1}{2}x \cdot Ax - xb\f$ at \a x on return
2444 * \param *g gradient vector on return
2445 * \return error code
2446 */
2447static void DeltaCurrent_fdf(const gsl_vector * x, void * param, double * f, gsl_vector * g)
2448{
2449 struct Problem *P = ((struct params *)param)->P;
2450 struct RunStruct *R = &P->R;
2451 struct LatticeLevel *LevS = R->LevS;
2452 struct Psis *Psi = &P->Lat.Psi;
2453 struct PseudoPot *PP = &P->PP;
2454 const double PsiFactor = Psi->AllPsiStatus[*((struct params *)param)->k].PsiFactor;
2455 fftw_complex *TempPsi = LevS->LPsi->TempPsi;
2456 fftw_complex *TempPsi2 = LevS->LPsi->TempPsi2;
2457 fftw_complex *x_l = ((struct params *)param)->x_l;
2458 int u;
2459
2460 //fprintf(stderr,"Evaluating fdf(%i,%i) for %i-th time\n", *((struct params *)param)->k, *((struct params *)param)->l, *((struct params *)param)->iter);
2461
2462 // extract gsl_vector
2463 for (u=0;u<LevS->MaxG;u++) {
2464 TempPsi[u].re = gsl_vector_get(x, 2*u);
2465 TempPsi[u].im = gsl_vector_get(x, 2*u+1);
2466 }
2467 // generate fnl
2468 CalculateCDfnl(P, TempPsi, PP->CDfnl); // calculate needed non-local form factors
2469 // Apply Hamiltonian to x
2470 ApplyTotalHamiltonian(P,TempPsi,TempPsi2, PP->CDfnl,PsiFactor,0);
2471 // put into returning vector
2472 for (u=0;u<LevS->MaxG;u++) {
2473 gsl_vector_set(g, 2*u, TempPsi[u].re - x_l[u].re);
2474 gsl_vector_set(g, 2*u+1, TempPsi[u].im - x_l[u].im);
2475 }
2476
2477 *f = .5 * PsiFactor * (((*((struct params *)param)->k == *((struct params *)param)->l ? GradSP(P,LevS,TempPsi,TempPsi2) : 0.) - Psi->lambda[*((struct params *)param)->k][*((struct params *)param)->l])) - GradSP(P,LevS,TempPsi,LevS->LPsi->LocalPsi[*((struct params *)param)->l]);
2478}
2479
2480/** Evaluates the \f$\Delta j_k(r')\f$ component of the current density.
2481 * \f[
2482 * \Delta j_k(r') = \frac{e}{m} \sum_l \langle \varphi^{(0)}_k | \left ( p |r'\rangle \langle r'| + | r'\rangle\langle r'|p \right ) {\cal G}_{kl} (d_k - d_l) \times p | \varphi^{(1)}_l \rangle \cdot B
2483 * \f]
2484 * \param *P Problem at hand
2485 * \note result has not yet been MPI_Allreduced for ParallelSimulationData#comm_ST_inter or ParallelSimulationData#comm_ST_PsiT groups!
2486 * \warning the routine is checked but does not yet produce sensible results.
2487 */
2488void FillDeltaCurrentDensity(struct Problem *P)
2489{
2490 struct Lattice *Lat = &P->Lat;
2491 struct RunStruct *R = &P->R;
2492 struct Psis *Psi = &Lat->Psi;
2493 struct LatticeLevel *Lev0 = R->Lev0;
2494 struct LatticeLevel *LevS = R->LevS;
2495 struct Density *Dens0 = Lev0->Dens;
2496 int i,j,s;
2497 int k,l,u, in, dex, index,i0;
2498 //const int Num = Psi->NoOfPsis;
2499 int RecvSource;
2500 MPI_Status status;
2501 struct OnePsiElement *OnePsiB, *LOnePsiB, *OnePsiA, *LOnePsiA;
2502 const int ElementSize = (sizeof(fftw_complex) / sizeof(double));
2503 int n[NDIM], n0;
2504 int N[NDIM];
2505 N[0] = Lev0->Plan0.plan->N[0];
2506 N[1] = Lev0->Plan0.plan->N[1];
2507 N[2] = Lev0->Plan0.plan->N[2];
2508 const int N0 = Lev0->Plan0.plan->local_nx;
2509 fftw_complex *LPsiDatB;
2510 fftw_complex *Psi0, *Psi1;
2511 fftw_real *Psi0R, *Psip0R;
2512 fftw_real *Psi1R, *Psip1R;
2513 fftw_complex *x_l = LevS->LPsi->TempPsi;//, **x_l_bak;
2514 fftw_real *CurrentDensity[NDIM*NDIM];
2515 int mem_avail, MEM_avail;
2516 double Current;
2517 double X[NDIM];
2518 const double UnitsFactor = 1.;
2519 int cross_lookup[4];
2520 struct params param;
2521 double factor; // temporary factor in Psi1 pre-evaluation
2522
2523 LockDensityArray(Dens0,GapDensity,real); // Psi0R
2524 LockDensityArray(Dens0,GapLocalDensity,real); // Psip0R
2525 LockDensityArray(Dens0,Temp2Density,imag); // Psi1
2526 LockDensityArray(Dens0,GapUpDensity,real); // Psi1R
2527 LockDensityArray(Dens0,GapDownDensity,real); // Psip1R
2528
2529 CurrentDensity[0] = (fftw_real *) Dens0->DensityArray[CurrentDensity0];
2530 CurrentDensity[1] = (fftw_real *) Dens0->DensityArray[CurrentDensity1];
2531 CurrentDensity[2] = (fftw_real *) Dens0->DensityArray[CurrentDensity2];
2532 CurrentDensity[3] = (fftw_real *) Dens0->DensityArray[CurrentDensity3];
2533 CurrentDensity[4] = (fftw_real *) Dens0->DensityArray[CurrentDensity4];
2534 CurrentDensity[5] = (fftw_real *) Dens0->DensityArray[CurrentDensity5];
2535 CurrentDensity[6] = (fftw_real *) Dens0->DensityArray[CurrentDensity6];
2536 CurrentDensity[7] = (fftw_real *) Dens0->DensityArray[CurrentDensity7];
2537 CurrentDensity[8] = (fftw_real *) Dens0->DensityArray[CurrentDensity8];
2538
2539 Psi0R = (fftw_real *)Dens0->DensityArray[GapDensity];
2540 Psip0R = (fftw_real *)Dens0->DensityArray[GapLocalDensity];
2541 Psi1 = (fftw_complex *) Dens0->DensityCArray[Temp2Density];
2542 Psi1R = (fftw_real *)Dens0->DensityArray[GapUpDensity];
2543 Psip1R = (fftw_real *)Dens0->DensityArray[GapDownDensity];
2544
2545// if (R->CurrentMin == Perturbed_P0)
2546// for (B_index=0; B_index<NDIM*NDIM; B_index++) { // initialize current density array
2547// debug(P,"resetting CurrentDensity...");
2548// SetArrayToDouble0((double *)CurrentDensity[B_index],Dens0->TotalSize*2); // DensityArray is fftw_real, no 2*LocalSizeR here!
2549// }
2550 //if (Psi1 != (fftw_complex *) Dens0->DensityCArray[Temp2Density]) Error(SomeError,"FillDeltaCurrentDensity: Psi1 corrupted");
2551 SetArrayToDouble0((double *)Psi1,2*Dens0->TotalSize);
2552
2553// gsl_vector *x = gsl_vector_alloc(Num);
2554// gsl_matrix *G = gsl_matrix_alloc(Num,Num);
2555// gsl_permutation *p = gsl_permutation_alloc(Num);
2556 //int signum;
2557 // begin of GSL linearer CG solver stuff
2558 int iter, Status;
2559
2560 const gsl_multimin_fdfminimizer_type *T;
2561 gsl_multimin_fdfminimizer *minset;
2562
2563 /* Position of the minimum (1,2). */
2564 //double par[2] = { 1.0, 2.0 };
2565
2566 gsl_vector *x;
2567 gsl_multimin_function_fdf my_func;
2568
2569 param.P = P;
2570 param.k = &k;
2571 param.l = &l;
2572 param.iter = &iter;
2573 param.x_l = x_l;
2574
2575 my_func.f = &DeltaCurrent_f;
2576 my_func.df = &DeltaCurrent_df;
2577 my_func.fdf = &DeltaCurrent_fdf;
2578 my_func.n = 2*LevS->MaxG;
2579 my_func.params = (void *)&param;
2580
2581 T = gsl_multimin_fdfminimizer_conjugate_pr;
2582 minset = gsl_multimin_fdfminimizer_alloc (T, 2*LevS->MaxG);
2583 x = gsl_vector_alloc (2*LevS->MaxG);
2584 // end of GSL CG stuff
2585
2586
2587// // construct G_kl = - (H^{(0)} \delta_{kl} -\langle \varphi^{(0)}_k |H^{(0)}| \varphi^{(0)}_l|rangle)^{-1} = A^{-1}
2588// for (k=0;k<Num;k++)
2589// for (l=0;l<Num;l++)
2590// gsl_matrix_set(G, k, l, k == l ? 0. : Psi->lambda[k][l]);
2591// // and decompose G_kl = L U
2592
2593 mem_avail = MEM_avail = 0;
2594// x_l_bak = x_l = (fftw_complex **) Malloc(sizeof(fftw_complex *)*Num,"FillDeltaCurrentDensity: *x_l");
2595// for (i=0;i<Num;i++) {
2596// x_l[i] = NULL;
2597// x_l[i] = (fftw_complex *) malloc(sizeof(fftw_complex)*LevS->MaxG);
2598// if (x_l[i] == NULL) {
2599// mem_avail = 1; // there was not enough memory for this node
2600// fprintf(stderr,"(%i) FillDeltaCurrentDensity: x_l[%i] ... insufficient memory.\n",P->Par.me,i);
2601// }
2602// }
2603// MPI_Allreduce(&mem_avail,&MEM_avail,1,MPI_INT,MPI_SUM,P->Par.comm_ST); // sum results from all processes
2604
2605 if (MEM_avail != 0) { // means at least node couldn't allocate sufficient memory, skipping...
2606 fprintf(stderr,"(%i) FillDeltaCurrentDensity: x_l[], not enough memory: %i! Skipping FillDeltaCurrentDensity evaluation.", P->Par.me, MEM_avail);
2607 } else {
2608 // sum over k and calculate \Delta j_k(r')
2609 k=-1;
2610 for (i=0; i < Psi->MaxPsiOfType+P->Par.Max_me_comm_ST_PsiT; i++) { // go through all wave functions
2611 //fprintf(stderr,"(%i) GlobalNo: %d\tLocalNo: %d\n", P->Par.me,Psi->AllPsiStatus[i].MyGlobalNo,Psi->AllPsiStatus[i].MyLocalNo);
2612 OnePsiA = &Psi->AllPsiStatus[i]; // grab OnePsiA
2613 if (OnePsiA->PsiType == Occupied) { // drop the extra and perturbed ones
2614 k++;
2615 if (OnePsiA->my_color_comm_ST_Psi == P->Par.my_color_comm_ST_Psi) // local?
2616 LOnePsiA = &Psi->LocalPsiStatus[OnePsiA->MyLocalNo];
2617 else
2618 LOnePsiA = NULL;
2619 if (LOnePsiA != NULL) {
2620 Psi0=LevS->LPsi->LocalPsi[OnePsiA->MyLocalNo];
2621
2622 if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) Bringing |Psi0> one level up and fftransforming\n", P->Par.me);
2623 //if (Psi0R != (fftw_real *)Dens0->DensityArray[GapDensity]) Error(SomeError,"FillDeltaCurrentDensity: Psi0R corrupted");
2624 fft_Psi(P,Psi0,Psi0R, 0, Psi0symmetry); //0 // 0 //0
2625
2626 for (in=0;in<NDIM;in++) { // in is the index from derivation wrt to B^{ext}
2627 l = -1;
2628 for (j=0; j < Psi->MaxPsiOfType+P->Par.Max_me_comm_ST_PsiT; j++) { // go through all wave functions
2629 OnePsiB = &Psi->AllPsiStatus[j]; // grab OnePsiA
2630 if (OnePsiB->PsiType == Occupied)
2631 l++;
2632 if ((OnePsiB != OnePsiA) && (OnePsiB->PsiType == Occupied)) { // drop the same and the extra ones
2633 if (OnePsiB->my_color_comm_ST_Psi == P->Par.my_color_comm_ST_Psi) // local?
2634 LOnePsiB = &Psi->LocalPsiStatus[OnePsiB->MyLocalNo];
2635 else
2636 LOnePsiB = NULL;
2637 if (LOnePsiB == NULL) { // if it's not local ... receive x from respective process
2638 RecvSource = OnePsiB->my_color_comm_ST_Psi;
2639 MPI_Recv( x_l, LevS->MaxG*ElementSize, MPI_DOUBLE, RecvSource, HamiltonianTag, P->Par.comm_ST_PsiT, &status );
2640 } else { // .. otherwise setup wave function as x ...
2641 // Evaluate cross product: \epsilon_{ijm} (d_k - d_l)_j p_m | \varphi^{(0)} \rangle = b_i ... and
2642 LPsiDatB=LevS->LPsi->LocalPsi[OnePsiB->MyLocalNo];
2643 //LPsiDatx=LevS->LPsi->LocalPsi[OnePsiB->MyLocalNo+Psi->TypeStartIndex[Perturbed_P0]];
2644 //CalculatePerturbationOperator_P(P,LPsiDatB,LPsiDatB_p0,cross(in,1),0);
2645 //CalculatePerturbationOperator_P(P,LPsiDatB,LPsiDatB_p1,cross(in,3),0);
2646 for (dex=0;dex<4;dex++)
2647 cross_lookup[dex] = cross(in,dex);
2648 MinImageConv(Lat,Psi->AddData[LOnePsiA->MyLocalNo].WannierCentre, Psi->AddData[LOnePsiB->MyLocalNo].WannierCentre,X);
2649 for(s=0;s<LevS->MaxG;s++) {
2650 //if (x_l != x_l_bak || s<0 || s>LevS->MaxG) Error(SomeError,"FillDeltaCurrentDensity: x_l[] corrupted");
2651 factor = (X[cross_lookup[0]] * LevS->GArray[s].G[cross_lookup[1]] - X[cross_lookup[2]] * LevS->GArray[s].G[cross_lookup[3]]);
2652 x_l[s].re = factor * (-LPsiDatB[s].im); // switched due to factorization with "-i G"
2653 x_l[s].im = factor * (LPsiDatB[s].re);
2654 }
2655 // ... and send it to all other processes (Max_me... - 1)
2656 for (u=0;u<P->Par.Max_me_comm_ST_PsiT;u++)
2657 if (u != OnePsiB->my_color_comm_ST_Psi)
2658 MPI_Send( x_l, LevS->MaxG*ElementSize, MPI_DOUBLE, u, HamiltonianTag, P->Par.comm_ST_PsiT);
2659 } // x_l row is now filled (either by receiving result or evaluating it on its own)
2660 // Solve Ax = b by minimizing 1/2 xAx -xb (gradient is residual Ax - b) with conjugate gradient polak-ribiere
2661
2662 debug(P,"fill starting point x with values from b");
2663 /* Starting point, x = b */
2664 for (u=0;u<LevS->MaxG;u++) {
2665 gsl_vector_set (x, 2*u, x_l[u].re);
2666 gsl_vector_set (x, 2*u+1, x_l[u].im);
2667 }
2668
2669 gsl_multimin_fdfminimizer_set (minset, &my_func, x, 0.01, 1e-4);
2670
2671 fprintf(stderr,"(%i) Start solving for (%i,%i) and index %i\n",P->Par.me, k,l,in);
2672 // start solving
2673 iter = 0;
2674 do
2675 {
2676 iter++;
2677 Status = gsl_multimin_fdfminimizer_iterate (minset);
2678
2679 if (Status)
2680 break;
2681
2682 Status = gsl_multimin_test_gradient (minset->gradient, 1e-3);
2683
2684 if (Status == GSL_SUCCESS)
2685 fprintf (stderr,"(%i) Minimum found after %i iterations.\n", P->Par.me, iter);
2686
2687 } while (Status == GSL_CONTINUE && iter < 100);
2688
2689 debug(P,"Put solution into Psi1");
2690 // ... and what do we do now? Put solution into Psi1!
2691 for(s=0;s<LevS->MaxG;s++) {
2692 //if (Psi1 != (fftw_complex *) Dens0->DensityCArray[Temp2Density] || s<0 || s>LevS->MaxG) Error(SomeError,"FillDeltaCurrentDensity: Psi1 corrupted");
2693 Psi1[s].re = gsl_vector_get (minset->x, 2*s);
2694 Psi1[s].im = gsl_vector_get (minset->x, 2*s+1);
2695 }
2696
2697 // // Solve A^{-1} b_i = x
2698 // for(s=0;s<LevS->MaxG;s++) {
2699 // // REAL PART
2700 // // retrieve column from gathered matrix
2701 // for(u=0;u<Num;u++)
2702 // gsl_vector_set(x,u,x_l[u][s].re);
2703 //
2704 // // solve: sum_l A_{kl}^(-1) b_l (s) = x_k (s)
2705 // gsl_linalg_LU_svx (G, p, x);
2706 //
2707 // // put solution back into x_l[s]
2708 // for(u=0;u<Num;u++) {
2709 // //if (x_l != x_l_bak || s<0 || s>=LevS->MaxG) Error(SomeError,"FillDeltaCurrentDensity: x_l[] corrupted");
2710 // x_l[u][s].re = gsl_vector_get(x,u);
2711 // }
2712 //
2713 // // IMAGINARY PART
2714 // // retrieve column from gathered matrix
2715 // for(u=0;u<Num;u++)
2716 // gsl_vector_set(x,u,x_l[u][s].im);
2717 //
2718 // // solve: sum_l A_{kl}^(-1) b_l (s) = x_k (s)
2719 // gsl_linalg_LU_svx (G, p, x);
2720 //
2721 // // put solution back into x_l[s]
2722 // for(u=0;u<Num;u++) {
2723 // //if (x_l != x_l_bak || s<0 || s>=LevS->MaxG) Error(SomeError,"FillDeltaCurrentDensity: x_l[] corrupted");
2724 // x_l[u][s].im = gsl_vector_get(x,u);
2725 // }
2726 // } // now we have in x_l a vector similar to "Psi1" which we use to evaluate the current density
2727 //
2728 // // evaluate \Delta J_k ... mind the minus sign from G_kl!
2729 // // fill Psi1
2730 // for(s=0;s<LevS->MaxG;s++) {
2731 // //if (Psi1 != (fftw_complex *) Dens0->DensityCArray[Temp2Density] || s<0 || s>LevS->MaxG) Error(SomeError,"FillDeltaCurrentDensity: Psi1 corrupted");
2732 // Psi1[s].re = x_l[k][s].re;
2733 // Psi1[s].im = x_l[k][s].im;
2734 // }
2735
2736 if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) Bringing |Psi1> one level up and fftransforming\n", P->Par.me);
2737 //if (Psi1R != (fftw_real *)Dens0->DensityArray[GapUpDensity]) Error(SomeError,"FillDeltaCurrentDensity: Psi1R corrupted");
2738 fft_Psi(P,Psi1,Psi1R, 0, Psi1symmetry); //2 // 0 //0
2739
2740 for (index=0;index<NDIM;index++) { // for all NDIM components of momentum operator
2741
2742 if ((P->Call.out[StepLeaderOut]) && (!index)) fprintf(stderr,"(%i) Bringing p|Psi0> one level up and fftransforming\n", P->Par.me);
2743 //if (Psip0R != (fftw_real *)Dens0->DensityArray[GapLocalDensity]) Error(SomeError,"FillDeltaCurrentDensity: Psip0R corrupted");
2744 fft_Psi(P,Psi0,Psip0R, index, Psip0symmetry); //6 //6 //6
2745
2746 if ((P->Call.out[StepLeaderOut]) && (!index)) fprintf(stderr,"(%i) Bringing p|Psi1> one level up and fftransforming\n", P->Par.me);
2747 //if (Psip1R != (fftw_real *)Dens0->DensityArray[GapDownDensity]) Error(SomeError,"FillDeltaCurrentDensity: Psip1R corrupted");
2748 fft_Psi(P,Psi1,Psip1R, index, Psip1symmetry); //4 //6 //6
2749
2750 // then for every point on the grid in real space ...
2751 for (n0=0;n0<N0;n0++) // only local points on x axis
2752 for (n[1]=0;n[1]<N[1];n[1]++)
2753 for (n[2]=0;n[2]<N[2];n[2]++) {
2754 i0 = n[2]+N[2]*(n[1]+N[1]*n0);
2755 // and take the product
2756 Current = (Psip0R[i0] * Psi1R[i0] + Psi0R[i0] * Psip1R[i0]);
2757 Current *= 0.5 * UnitsFactor * Psi->AllPsiStatus[OnePsiA->MyGlobalNo].PsiFactor * R->FactorDensityR;
2758 ////if (CurrentDensity[index+in*NDIM] != (fftw_real *) Dens0->DensityArray[CurrentDensity0 + index+in*NDIM]) Error(SomeError,"FillCurrentDensity: CurrentDensity[] corrupted");
2759 //if (i0<0 || i0>=Dens0->LocalSizeR) Error(SomeError,"FillDeltaCurrentDensity: i0 out of range");
2760 //if ((index+in*NDIM)<0 || (index+in*NDIM)>=NDIM*NDIM) Error(SomeError,"FillDeltaCurrentDensity: index out of range");
2761 CurrentDensity[index+in*NDIM][i0] += Current; // minus sign is from G_kl
2762 }
2763 }
2764 }
2765 }
2766 }
2767 }
2768 }
2769 }
2770 }
2771 UnLockDensityArray(Dens0,GapDensity,real); // Psi0R
2772 UnLockDensityArray(Dens0,GapLocalDensity,real); // Psip0R
2773 UnLockDensityArray(Dens0,Temp2Density,imag); // Psi1
2774 UnLockDensityArray(Dens0,GapUpDensity,real); // Psi1R
2775 UnLockDensityArray(Dens0,GapDownDensity,real); // Psip1R
2776// for (i=0;i<Num;i++)
2777// if (x_l[i] != NULL) Free(x_l[i], "FillDeltaCurrentDensity: x_l[i]");
2778// Free(x_l, "FillDeltaCurrentDensity: x_l");
2779 gsl_multimin_fdfminimizer_free (minset);
2780 gsl_vector_free (x);
2781// gsl_matrix_free(G);
2782// gsl_permutation_free(p);
2783// gsl_vector_free(x);
2784}
2785
2786
2787/** Evaluates the overlap integral between \a state wave functions.
2788 * \f[
2789 * S_{kl} = \langle \varphi_k^{(1)} | \varphi_l^{(1)} \rangle
2790 * \f]
2791 * The scalar product is calculated via GradSP(), MPI_Allreduced among comm_ST_Psi and the result
2792 * stored in Psis#Overlap. The rows have to be MPI exchanged, as otherwise processes will add
2793 * to the TotalEnergy overlaps calculated with old wave functions - they have been minimised after
2794 * the product with exchanged coefficients was taken.
2795 * \param *P Problem at hand
2796 * \param l local number of perturbed wave function.
2797 * \param state PsiTypeTag minimisation state of wave functions to be overlapped
2798 */
2799void CalculateOverlap(struct Problem *P, const int l, const enum PsiTypeTag state)
2800{
2801 struct RunStruct *R = &P->R;
2802 struct Lattice *Lat = &(P->Lat);
2803 struct Psis *Psi = &Lat->Psi;
2804 struct LatticeLevel *LevS = R->LevS;
2805 struct OnePsiElement *OnePsiB, *LOnePsiB;
2806 fftw_complex *LPsiDatB=NULL, *LPsiDatA=NULL;
2807 const int ElementSize = (sizeof(fftw_complex) / sizeof(double));
2808 int RecvSource;
2809 MPI_Status status;
2810 int i,j,m,p;
2811 //const int l_normal = l - Psi->TypeStartIndex[state] + Psi->TypeStartIndex[Occupied];
2812 const int ActNum = l - Psi->TypeStartIndex[state] + Psi->TypeStartIndex[1] * Psi->LocalPsiStatus[l].my_color_comm_ST_Psi;
2813 double *sendbuf, *recvbuf;
2814 double tmp,TMP;
2815 const int gsize = P->Par.Max_me_comm_ST_PsiT; //number of processes in PsiT
2816 int p_num; // number of wave functions (for overlap)
2817
2818 // update overlap table after wave function has changed
2819 LPsiDatA = LevS->LPsi->LocalPsi[l];
2820 m = -1; // to access U matrix element (0..Num-1)
2821 for (j=0; j < Psi->MaxPsiOfType+P->Par.Max_me_comm_ST_PsiT; j++) { // go through all wave functions
2822 OnePsiB = &Psi->AllPsiStatus[j]; // grab OnePsiB
2823 if (OnePsiB->PsiType == state) { // drop all but the ones of current min state
2824 m++; // increase m if it is non-extra wave function
2825 if (OnePsiB->my_color_comm_ST_Psi == P->Par.my_color_comm_ST_Psi) // local?
2826 LOnePsiB = &Psi->LocalPsiStatus[OnePsiB->MyLocalNo];
2827 else
2828 LOnePsiB = NULL;
2829 if (LOnePsiB == NULL) { // if it's not local ... receive it from respective process into TempPsi
2830 RecvSource = OnePsiB->my_color_comm_ST_Psi;
2831 MPI_Recv( LevS->LPsi->TempPsi, LevS->MaxG*ElementSize, MPI_DOUBLE, RecvSource, OverlapTag, P->Par.comm_ST_PsiT, &status );
2832 LPsiDatB=LevS->LPsi->TempPsi;
2833 } else { // .. otherwise send it to all other processes (Max_me... - 1)
2834 for (p=0;p<P->Par.Max_me_comm_ST_PsiT;p++)
2835 if (p != OnePsiB->my_color_comm_ST_Psi)
2836 MPI_Send( LevS->LPsi->LocalPsi[OnePsiB->MyLocalNo], LevS->MaxG*ElementSize, MPI_DOUBLE, p, OverlapTag, P->Par.comm_ST_PsiT);
2837 LPsiDatB=LevS->LPsi->LocalPsi[OnePsiB->MyLocalNo];
2838 } // LPsiDatB is now set to the coefficients of OnePsi either stored or MPI_Received
2839
2840 tmp = GradSP(P, LevS, LPsiDatA, LPsiDatB) * sqrt(Psi->LocalPsiStatus[l].PsiFactor * OnePsiB->PsiFactor);
2841 MPI_Allreduce ( &tmp, &TMP, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi);
2842 //fprintf(stderr,"(%i) Setting Overlap [%i][%i] = %lg\n",P->Par.me, ActNum,m,TMP);
2843 Psi->Overlap[ActNum][m] = TMP; //= Psi->Overlap[m][ActNum]
2844 }
2845 }
2846
2847 // exchange newly calculated rows among PsiT
2848 p_num = (m+1) + 1; // number of Psis: one more due to ActNum
2849 sendbuf = (double *) Malloc(p_num * sizeof(double), "CalculateOverlap: sendbuf");
2850 sendbuf[0] = ActNum; // first entry is the global row number
2851 for (i=1;i<p_num;i++)
2852 sendbuf[i] = Psi->Overlap[ActNum][i-1]; // then follow up each entry of overlap row
2853 recvbuf = (double *) Malloc(gsize * p_num * sizeof(double), "CalculateOverlap: recvbuf");
2854 MPI_Allgather(sendbuf, p_num, MPI_DOUBLE, recvbuf, p_num, MPI_DOUBLE, P->Par.comm_ST_PsiT);
2855 Free(sendbuf, "CalculateOverlap: sendbuf");
2856 for (i=0;i<gsize;i++) {// extract results from other processes out of receiving buffer
2857 m = recvbuf[i*p_num]; // m is ActNum of the process whose results we've just received
2858 //fprintf(stderr,"(%i) Received row %i from process %i\n", P->Par.me, m, i);
2859 for (j=1;j<p_num;j++)
2860 Psi->Overlap[m][j-1] = Psi->Overlap[j-1][m] = recvbuf[i*p_num+j]; // put each entry into correspondent Overlap row
2861 }
2862 Free(recvbuf, "CalculateOverlap: recvbuf");
2863}
2864
2865
2866/** Calculates magnetic susceptibility from known current density.
2867 * The bulk susceptibility tensor can be expressed as a function of the current density.
2868 * \f[
2869 * \chi_{ij} = \frac{\mu_0}{2\Omega} \frac{\delta}{\delta B_i^{ext}} \int_\Omega d^3 r \left (r \times j(r) \right )_j
2870 * \f]
2871 * Thus the integral over real space and subsequent MPI_Allreduce() over results from ParallelSimulationData#comm_ST_Psi is
2872 * straightforward. Tensor is diagonalized afterwards and split into its various sub-tensors of lower rank (e.g., isometric
2873 * value is tensor of rank 0) which are printed to screen and the tensorial elements to file '....chi.csv'
2874 * \param *P Problem at hand
2875 */
2876void CalculateMagneticSusceptibility(struct Problem *P)
2877{
2878 struct RunStruct *R = &P->R;
2879 struct Lattice *Lat = &P->Lat;
2880 struct LatticeLevel *Lev0 = R->Lev0;
2881 struct Density *Dens0 = R->Lev0->Dens;
2882 struct Ions *I = &P->Ion;
2883 fftw_real *CurrentDensity[NDIM*NDIM];
2884 int in, dex, i, i0, n0;
2885 int n[NDIM];
2886 const int N0 = Lev0->Plan0.plan->local_nx;
2887 int N[NDIM];
2888 N[0] = Lev0->Plan0.plan->N[0];
2889 N[1] = Lev0->Plan0.plan->N[1];
2890 N[2] = Lev0->Plan0.plan->N[2];
2891 double chi[NDIM*NDIM],Chi[NDIM*NDIM], x[NDIM], X[NDIM], fac[NDIM];
2892 const double discrete_factor = Lat->Volume/Lev0->MaxN;
2893 const int myPE = P->Par.me_comm_ST_Psi;
2894 double eta, delta_chi, S, A, iso;
2895 int cross_lookup[4];
2896
2897 if(P->Call.out[NormalOut]) fprintf(stderr,"(%i)Calculating Magnetic Susceptibility \n", P->Par.me);
2898
2899 // set pointers onto current density
2900 CurrentDensity[0] = (fftw_real *) Dens0->DensityArray[CurrentDensity0];
2901 CurrentDensity[1] = (fftw_real *) Dens0->DensityArray[CurrentDensity1];
2902 CurrentDensity[2] = (fftw_real *) Dens0->DensityArray[CurrentDensity2];
2903 CurrentDensity[3] = (fftw_real *) Dens0->DensityArray[CurrentDensity3];
2904 CurrentDensity[4] = (fftw_real *) Dens0->DensityArray[CurrentDensity4];
2905 CurrentDensity[5] = (fftw_real *) Dens0->DensityArray[CurrentDensity5];
2906 CurrentDensity[6] = (fftw_real *) Dens0->DensityArray[CurrentDensity6];
2907 CurrentDensity[7] = (fftw_real *) Dens0->DensityArray[CurrentDensity7];
2908 CurrentDensity[8] = (fftw_real *) Dens0->DensityArray[CurrentDensity8];
2909 //for(i=0;i<NDIM;i++) {
2910// field[i] = Dens0->DensityArray[TempDensity+i];
2911 //LockDensityArray(Dens0,TempDensity+i,real);
2912// SetArrayToDouble0((double *)field[i],Dens0->TotalSize*2);
2913 //}
2914 gsl_matrix_complex *H = gsl_matrix_complex_calloc(NDIM,NDIM);
2915
2916
2917 if (P->Call.out[ValueOut]) fprintf(stderr,"(%i) magnetic susceptibility tensor \\Chi_ij = \n",P->Par.me);
2918 for (in=0; in<NDIM; in++) { // index i of integrand vector component
2919 for(dex=0;dex<4;dex++) // initialise cross lookup
2920 cross_lookup[dex] = cross(in,dex);
2921 for (dex=0; dex<NDIM; dex++) { // index j of derivation wrt B field
2922 chi[in+dex*NDIM] = 0.;
2923 // do the integration over real space
2924 for(n0=0;n0<N0;n0++)
2925 for(n[1]=0;n[1]<N[1];n[1]++)
2926 for(n[2]=0;n[2]<N[2];n[2]++) {
2927 n[0]=n0 + N0*myPE; // global relative coordinate: due to partitoning of x-axis in PEPGamma>1 case
2928 fac[0] = (double)(n[0])/(double)N[0];
2929 fac[1] = (double)(n[1])/(double)N[1];
2930 fac[2] = (double)(n[2])/(double)N[2];
2931 RMat33Vec3(x, Lat->RealBasis, fac);
2932 i0 = n[2]+N[2]*(n[1]+N[1]*(n0)); // the index of current density must match LocalSizeR!
2933 MinImageConv(Lat,x, Lat->RealBasisCenter, X);
2934 chi[in+dex*NDIM] += X[cross_lookup[0]] * CurrentDensity[dex*NDIM+cross_lookup[1]][i0]; // x[cross(in,0)], Lat->RealBasisCenter[cross_lookup[0]]
2935 chi[in+dex*NDIM] -= X[cross_lookup[2]] * CurrentDensity[dex*NDIM+cross_lookup[3]][i0]; // x[cross(in,2)], Lat->RealBasisCenter[cross_lookup[2]]
2936// if (in == dex) field[in][i0] =
2937// truedist(Lat,x[cross_lookup[0]], sqrt(Lat->RealBasisSQ[c[0]])/2.,cross_lookup[0]) * CurrentDensity[dex*NDIM+cross_lookup[1]][i0]
2938// - truedist(Lat,x[cross_lookup[2]], sqrt(Lat->RealBasisSQ[c[2]])/2.,cross_lookup[2]) * CurrentDensity[dex*NDIM+cross_lookup[3]][i0];
2939 //fprintf(stderr,"(%i) temporary susceptiblity \\chi[%i][%i] += %e * %e = r[%i] * CurrDens[%i][%i] = %e\n",P->Par.me,in,dex,(double)n[cross_lookup[0]]/(double)N[cross_lookup[0]]*(sqrt(Lat->RealBasisSQ[cross_lookup[0]])),CurrentDensity[dex*NDIM+cross_lookup[1]][i0],cross_lookup[0],dex*NDIM+cross_lookup[1],i0,chi[in*NDIM+dex]);
2940 }
2941 chi[in+dex*NDIM] *= mu0*discrete_factor/(2.*Lat->Volume); // integral factor
2942 chi[in+dex*NDIM] *= (-1625.); // empirical gauge factor ... sigh
2943 MPI_Allreduce ( &chi[in+dex*NDIM], &Chi[in+dex*NDIM], 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi); // sum "LocalSize to TotalSize"
2944 I->I[0].chi[in+dex*NDIM] = Chi[in+dex*NDIM];
2945 Chi[in+dex*NDIM] *= Lat->Volume*loschmidt_constant; // factor for _molar_ susceptibility
2946 if (P->Call.out[ValueOut]) {
2947 fprintf(stderr,"%e\t", Chi[in+dex*NDIM]);
2948 if (dex == NDIM-1) fprintf(stderr,"\n");
2949 }
2950 }
2951 }
2952 // store symmetrized matrix
2953 for (in=0;in<NDIM;in++)
2954 for (dex=0;dex<NDIM;dex++)
2955 gsl_matrix_complex_set(H,in,dex,gsl_complex_rect((Chi[in+dex*NDIM]+Chi[dex+in*NDIM])/2.,0));
2956 // output tensor to file
2957 if (P->Par.me == 0) {
2958 FILE *ChiFile;
2959 char suffixchi[255];
2960 time_t seconds;
2961
2962 time(&seconds); // get current time
2963 sprintf(&suffixchi[0], ".chi.L%i.csv", Lev0->LevelNo);
2964 OpenFile(P, &ChiFile, suffixchi, "a", P->Call.out[ReadOut]);
2965 fprintf(ChiFile,"# magnetic susceptibility tensor chi[01,02,03,10,11,12,20,21,22], seed %i, config %s, run on %s", R->Seed, P->Files.default_path, ctime(&seconds));
2966 fprintf(ChiFile,"%lg\t", Lev0->ECut/4.); // ECut is in Rydberg
2967 for (in=0;in<NDIM*NDIM;in++)
2968 fprintf(ChiFile,"%e\t", Chi[in]);
2969 fprintf(ChiFile,"\n");
2970 fclose(ChiFile);
2971 }
2972 // diagonalize chi
2973 gsl_vector *eval = gsl_vector_alloc(NDIM);
2974 gsl_eigen_herm_workspace *w = gsl_eigen_herm_alloc(NDIM);
2975 gsl_eigen_herm(H, eval, w);
2976 gsl_eigen_herm_free(w);
2977 gsl_sort_vector(eval); // sort eigenvalues
2978 // print eigenvalues
2979 iso = 0;
2980 for (i=0;i<NDIM;i++) {
2981 I->I[0].chi_PAS[i] = gsl_vector_get(eval,i);
2982 iso += Chi[i+i*NDIM]/3.;
2983 }
2984 eta = (gsl_vector_get(eval,1)-gsl_vector_get(eval,0))/(gsl_vector_get(eval,2)-iso);
2985 delta_chi = gsl_vector_get(eval,2) - 0.5*(gsl_vector_get(eval,0)+gsl_vector_get(eval,1));
2986 S = (delta_chi*delta_chi)*(1+1./3.*eta*eta);
2987 A = 0.;
2988 for (i=0;i<NDIM;i++) {
2989 in = cross(i,0);
2990 dex = cross(i,1);
2991 A += pow(-1,i)*pow(0.5*(Chi[in+dex*NDIM]-Chi[dex+in*NDIM]),2);
2992 }
2993 if (P->Call.out[ValueOut]) {
2994 fprintf(stderr,"(%i) converted to Principal Axis System\n==================\nDiagonal entries:", P->Par.me);
2995 for (i=0;i<NDIM;i++)
2996 fprintf(stderr,"\t%lg",gsl_vector_get(eval,i));
2997 fprintf(stderr,"\nsusceptib. : %e\n", iso);
2998 fprintf(stderr,"anisotropy : %e\n", delta_chi);
2999 fprintf(stderr,"asymmetry : %e\n", eta);
3000 fprintf(stderr,"S : %e\n", S);
3001 fprintf(stderr,"A : %e\n", A);
3002 fprintf(stderr,"==================\n");
3003 }
3004 // output PAS tensor to file
3005 if (P->Par.me == 0) {
3006 FILE *ChiFile;
3007 char suffixchi[255];
3008 time_t seconds;
3009
3010 time(&seconds); // get current time
3011 sprintf(&suffixchi[0], ".chi_PAS.csv");
3012 if (Lev0->LevelNo == Lat->MaxLevel-2) {
3013 OpenFile(P, &ChiFile, suffixchi, "w", P->Call.out[ReadOut]);
3014 fprintf(ChiFile,"# magnetic susceptibility tensor chi[00,11,22] Principal Axis System, seed %i, config %s, run on %s", R->Seed, P->Files.default_path, ctime(&seconds));
3015 fprintf(ChiFile,"# Ecut\tChi_XX\t\tChi_YY\t\tChi_ZZ\tShielding\tanisotropy\tasymmetry\tS\t\tA\n");
3016 } else
3017 OpenFile(P, &ChiFile, suffixchi, "a", P->Call.out[ReadOut]);
3018 fprintf(ChiFile,"%lg\t", Lev0->ECut/4.); // ECut is in Rydberg
3019 for (i=0;i<NDIM;i++)
3020 fprintf(ChiFile,"%e\t", gsl_vector_get(eval,i));
3021 fprintf(ChiFile,"%lg\t%lg\t%lg\t%lg\t%lg\t\n", iso, delta_chi, eta, S, A);
3022 fclose(ChiFile);
3023 }
3024 //for(i=0;i<NDIM;i++)
3025 //UnLockDensityArray(Dens0,TempDensity+i,real);
3026 gsl_vector_free(eval);
3027 gsl_matrix_complex_free(H);
3028}
3029
3030/** Fouriertransforms all nine current density components and calculates shielding tensor.
3031 * \f[
3032 * \sigma_{ij} = \left ( \frac{G}{|G|^2} \times J_i(G) \right )_j
3033 * \f]
3034 * The CurrentDensity has to be fouriertransformed to reciprocal subspace in order to be useful, and the final
3035 * product \f$\sigma_{ij}(G)\f$ has to be back-transformed to real space. However, the shielding is the only evaluated
3036 * at the grid points and not where the real ion position is. The shieldings there are interpolated between the eight
3037 * adjacent grid points by a simple linear weighting. Afterwards follows the same analaysis and printout of the rank-2-tensor
3038 * as in the case of CalculateMagneticShielding().
3039 * \param *P Problem at hand
3040 * \note Lots of arrays are used temporarily during the routine for the fft'ed Current density tensor.
3041 * \note MagneticSusceptibility is needed for G=0-component and thus has to be computed beforehand
3042 */
3043void CalculateChemicalShieldingByReciprocalCurrentDensity(struct Problem *P)
3044{
3045 struct RunStruct *R = &P->R;
3046 struct Lattice *Lat = &P->Lat;
3047 struct LatticeLevel *Lev0 = R->Lev0;
3048 struct FileData *F = &P->Files;
3049 struct Ions *I = &P->Ion;
3050 struct Density *Dens0 = Lev0->Dens;
3051 struct OneGData *GArray = Lev0->GArray;
3052 struct fft_plan_3d *plan = Lat->plan;
3053 fftw_real *CurrentDensity[NDIM*NDIM];
3054 fftw_complex *CurrentDensityC[NDIM*NDIM];
3055 fftw_complex *work = (fftw_complex *)Dens0->DensityCArray[TempDensity];
3056 //fftw_complex *sigma_imag = (fftw_complex *)Dens0->DensityCArray[Temp2Density];
3057 //fftw_real *sigma_real = (fftw_real *)sigma_imag;
3058 fftw_complex *sigma_imag[NDIM_NDIM];
3059 fftw_real *sigma_real[NDIM_NDIM];
3060 double sigma,Sigma;
3061 int it, ion, in, dex, g, Index, i;
3062 int *N = Lev0->Plan0.plan->N;
3063 //const double FFTfactor = 1.;///Lev0->MaxN;
3064 double eta, delta_sigma, S, A, iso;
3065 int cross_lookup[4]; // cross lookup table
3066 const double factorDC = R->FactorDensityC;
3067 gsl_matrix_complex *H = gsl_matrix_complex_calloc(NDIM,NDIM);
3068 FILE *SigmaFile;
3069 char suffixsigma[255];
3070
3071 time_t seconds;
3072 time(&seconds); // get current time
3073
3074 if(P->Call.out[NormalOut]) fprintf(stderr,"(%i)Calculating Chemical Shielding\n", P->Par.me);
3075
3076 // inverse Fourier transform current densities
3077 CurrentDensityC[0] = (fftw_complex *) Dens0->DensityCArray[CurrentDensity0];
3078 CurrentDensityC[1] = (fftw_complex *) Dens0->DensityCArray[CurrentDensity1];
3079 CurrentDensityC[2] = (fftw_complex *) Dens0->DensityCArray[CurrentDensity2];
3080 CurrentDensityC[3] = (fftw_complex *) Dens0->DensityCArray[CurrentDensity3];
3081 CurrentDensityC[4] = (fftw_complex *) Dens0->DensityCArray[CurrentDensity4];
3082 CurrentDensityC[5] = (fftw_complex *) Dens0->DensityCArray[CurrentDensity5];
3083 CurrentDensityC[6] = (fftw_complex *) Dens0->DensityCArray[CurrentDensity6];
3084 CurrentDensityC[7] = (fftw_complex *) Dens0->DensityCArray[CurrentDensity7];
3085 CurrentDensityC[8] = (fftw_complex *) Dens0->DensityCArray[CurrentDensity8];
3086 // don't put the following stuff into a for loop, they are not continuous! (preprocessor values CurrentDensity.)
3087 CurrentDensity[0] = (fftw_real *) Dens0->DensityArray[CurrentDensity0];
3088 CurrentDensity[1] = (fftw_real *) Dens0->DensityArray[CurrentDensity1];
3089 CurrentDensity[2] = (fftw_real *) Dens0->DensityArray[CurrentDensity2];
3090 CurrentDensity[3] = (fftw_real *) Dens0->DensityArray[CurrentDensity3];
3091 CurrentDensity[4] = (fftw_real *) Dens0->DensityArray[CurrentDensity4];
3092 CurrentDensity[5] = (fftw_real *) Dens0->DensityArray[CurrentDensity5];
3093 CurrentDensity[6] = (fftw_real *) Dens0->DensityArray[CurrentDensity6];
3094 CurrentDensity[7] = (fftw_real *) Dens0->DensityArray[CurrentDensity7];
3095 CurrentDensity[8] = (fftw_real *) Dens0->DensityArray[CurrentDensity8];
3096
3097 // inverse Fourier transform current densities
3098 if (P->Call.out[LeaderOut]) fprintf(stderr,"(%i) Transforming and checking J_{ij} (G=0) = 0 for each i,j ... \n",P->Par.me);
3099 for (in=0;in<NDIM*NDIM;in++) {
3100 CalculateOneDensityC(Lat, R->LevS, Dens0, CurrentDensity[in], CurrentDensityC[in], factorDC);
3101 TestReciprocalCurrent(P, CurrentDensityC[in], GArray, in);
3102 }
3103
3104 // linking pointers to the arrays
3105 for (in=0;in<NDIM*NDIM;in++) {
3106 LockDensityArray(Dens0,in,real); // Psi1R
3107 sigma_imag[in] = (fftw_complex *) Dens0->DensityArray[in];
3108 sigma_real[in] = (fftw_real *) sigma_imag[in];
3109 }
3110
3111 LockDensityArray(Dens0,TempDensity,imag); // work
3112 LockDensityArray(Dens0,Temp2Density,imag); // tempdestRC and field
3113 // go through reciprocal nodes and calculate shielding tensor sigma
3114 for (in=0; in<NDIM; in++) {// index i of vector component in integrand
3115 for(dex=0;dex<4;dex++) // initialise cross lookup
3116 cross_lookup[dex] = cross(in,dex);
3117 for (dex=0; dex<NDIM; dex++) { // index j of B component derivation in current density tensor
3118 //if (tempdestRC != (fftw_complex *)Dens0->DensityCArray[Temp2Density]) Error(SomeError,"CalculateChemicalShieldingByReciprocalCurrentDensity: tempdestRC corrupted");
3119 SetArrayToDouble0((double *)sigma_imag[in+dex*NDIM],Dens0->TotalSize*2);
3120 for (g=0; g < Lev0->MaxG; g++)
3121 if (GArray[g].GSq > MYEPSILON) { // skip due to divisor
3122 Index = GArray[g].Index; // re = im, im = -re due to "i" in formula
3123 //if (tempdestRC != (fftw_complex *)Dens0->DensityCArray[Temp2Density] || Index<0 || Index>=Dens0->LocalSizeC) Error(SomeError,"CalculateChemicalShieldingByReciprocalCurrentDensity: tempdestRC corrupted");
3124 sigma_imag[in+dex*NDIM][Index].re = GArray[g].G[cross_lookup[0]] * (-CurrentDensityC[dex*NDIM+cross_lookup[1]][Index].im)/GArray[g].GSq;//*FFTfactor;
3125 sigma_imag[in+dex*NDIM][Index].re -= GArray[g].G[cross_lookup[2]] * (-CurrentDensityC[dex*NDIM+cross_lookup[3]][Index].im)/GArray[g].GSq;//*FFTfactor;
3126 sigma_imag[in+dex*NDIM][Index].im = GArray[g].G[cross_lookup[0]] * ( CurrentDensityC[dex*NDIM+cross_lookup[1]][Index].re)/GArray[g].GSq;//*FFTfactor;
3127 sigma_imag[in+dex*NDIM][Index].im -= GArray[g].G[cross_lookup[2]] * ( CurrentDensityC[dex*NDIM+cross_lookup[3]][Index].re)/GArray[g].GSq;//*FFTfactor;
3128 } else { // divergent G=0-component stems from magnetic susceptibility
3129 sigma_imag[in+dex*NDIM][GArray[g].Index].re = 2./3.*I->I[0].chi[in+dex*NDIM];//-4.*M_PI*(0.5*I->I[0].chi[0+0*NDIM]+0.5*I->I[0].chi[1+1*NDIM]+2./3.*I->I[0].chi[2+2*NDIM]);
3130 }
3131 for (g=0; g<Lev0->MaxDoubleG; g++) { // apply symmetry
3132 //if (tempdestRC != (fftw_complex *)Dens0->DensityCArray[Temp2Density] || Lev0->DoubleG[2*g+1]<0 || Lev0->DoubleG[2*g+1]>=Dens0->LocalSizeC) Error(SomeError,"CalculateChemicalShieldingByReciprocalCurrentDensity: tempdestRC corrupted");
3133 sigma_imag[in+dex*NDIM][Lev0->DoubleG[2*g+1]].re = sigma_imag[in+dex*NDIM][Lev0->DoubleG[2*g]].re;
3134 sigma_imag[in+dex*NDIM][Lev0->DoubleG[2*g+1]].im = -sigma_imag[in+dex*NDIM][Lev0->DoubleG[2*g]].im;
3135 }
3136 // fourier transformation of sigma
3137 //if (tempdestRC != (fftw_complex *)Dens0->DensityCArray[Temp2Density]) Error(SomeError,"CalculateChemicalShieldingByReciprocalCurrentDensity: tempdestRC corrupted");
3138 fft_3d_complex_to_real(plan, Lev0->LevelNo, FFTNF1, sigma_imag[in+dex*NDIM], work);
3139
3140 for (it=0; it < I->Max_Types; it++) { // integration over all types
3141 for (ion=0; ion < I->I[it].Max_IonsOfType; ion++) { // and each ion of type
3142 // read transformed sigma at core position and MPI_Allreduce
3143 sigma = -LinearInterpolationBetweenGrid(P, Lat, Lev0, &I->I[it].R[NDIM*ion], sigma_real[in+dex*NDIM]) * R->FactorDensityR; // factor from inverse fft
3144
3145 MPI_Allreduce ( &sigma, &Sigma, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi); // sum local to total
3146 I->I[it].sigma_rezi[ion][in+dex*NDIM] = Sigma;
3147 }
3148 }
3149 // fabs() all sigma values, as we need them as a positive density: OutputVis plots them in logarithmic scale and
3150 // thus cannot deal with negative values!
3151 for (i=0; i< Dens0->LocalSizeR; i++)
3152 sigma_real[in+dex*NDIM][i] = fabs(sigma_real[in+dex*NDIM][i]);
3153 }
3154 }
3155 UnLockDensityArray(Dens0,TempDensity,imag); // work
3156 UnLockDensityArray(Dens0,Temp2Density,imag); // tempdestRC and field
3157
3158 // output tensor to file
3159 if (P->Par.me == 0) {
3160 sprintf(&suffixsigma[0], ".sigma_chi_rezi.L%i.csv", Lev0->LevelNo);
3161 OpenFile(P, &SigmaFile, suffixsigma, "a", P->Call.out[ReadOut]);
3162 fprintf(SigmaFile,"# chemical shielding tensor sigma_rezi[01,02,03,10,11,12,20,21,22], seed %i, config %s, run on %s", R->Seed, P->Files.default_path, ctime(&seconds));
3163 fprintf(SigmaFile,"%lg\t", Lev0->ECut/4.);
3164 for (in=0;in<NDIM;in++)
3165 for (dex=0;dex<NDIM;dex++)
3166 fprintf(SigmaFile,"%e\t", GSL_REAL(gsl_matrix_complex_get(H,in,dex)));
3167 fprintf(SigmaFile,"\n");
3168 fclose(SigmaFile);
3169 }
3170
3171 gsl_vector *eval = gsl_vector_alloc(NDIM);
3172 gsl_eigen_herm_workspace *w = gsl_eigen_herm_alloc(NDIM);
3173
3174 for (it=0; it < I->Max_Types; it++) { // integration over all types
3175 for (ion=0; ion < I->I[it].Max_IonsOfType; ion++) { // and each ion of type
3176 if (P->Call.out[ValueOut]) fprintf(stderr,"(%i) Shielding Tensor for Ion %i of element %s \\sigma_ij = \n",P->Par.me, ion, I->I[it].Name);
3177 for (in=0; in<NDIM; in++) { // index i of vector component in integrand
3178 for (dex=0; dex<NDIM; dex++) {// index j of B component derivation in current density tensor
3179 gsl_matrix_complex_set(H,in,dex,gsl_complex_rect((I->I[it].sigma_rezi[ion][in+dex*NDIM]+I->I[it].sigma_rezi[ion][dex+in*NDIM])/2.,0));
3180 if (P->Call.out[ValueOut]) fprintf(stderr,"%e\t", I->I[it].sigma_rezi[ion][in+dex*NDIM]);
3181 }
3182 if (P->Call.out[ValueOut]) fprintf(stderr,"\n");
3183 }
3184 // output tensor to file
3185 if (P->Par.me == 0) {
3186 sprintf(&suffixsigma[0], ".sigma_i%i_%s_rezi.L%i.csv", ion, I->I[it].Symbol, Lev0->LevelNo);
3187 OpenFile(P, &SigmaFile, suffixsigma, "a", P->Call.out[ReadOut]);
3188 fprintf(SigmaFile,"# chemical shielding tensor sigma_rezi[01,02,03,10,11,12,20,21,22], seed %i, config %s, run on %s", R->Seed, P->Files.default_path, ctime(&seconds));
3189 fprintf(SigmaFile,"%lg\t", Lev0->ECut/4.); // ECut is in Rydberg
3190 for (in=0;in<NDIM;in++)
3191 for (dex=0;dex<NDIM;dex++)
3192 fprintf(SigmaFile,"%e\t", I->I[it].sigma_rezi[ion][in+dex*NDIM]);
3193 fprintf(SigmaFile,"\n");
3194 fclose(SigmaFile);
3195 }
3196 // diagonalize sigma
3197 gsl_eigen_herm(H, eval, w);
3198 gsl_sort_vector(eval); // sort eigenvalues
3199// print eigenvalues
3200// if (P->Call.out[ValueOut]) {
3201// fprintf(stderr,"(%i) diagonal shielding for Ion %i of element %s:", P->Par.me, ion, I->I[it].Name);
3202// for (in=0;in<NDIM;in++)
3203// fprintf(stderr,"\t%lg",gsl_vector_get(eval,in));
3204// fprintf(stderr,"\n\n");
3205// }
3206 iso = 0.;
3207 for (i=0;i<NDIM;i++) {
3208 I->I[it].sigma_rezi_PAS[ion][i] = gsl_vector_get(eval,i);
3209 iso += I->I[it].sigma_rezi[ion][i+i*NDIM]/3.;
3210 }
3211 eta = (gsl_vector_get(eval,1)-gsl_vector_get(eval,0))/(gsl_vector_get(eval,2)-iso);
3212 delta_sigma = gsl_vector_get(eval,2) - 0.5*(gsl_vector_get(eval,0)+gsl_vector_get(eval,1));
3213 S = (delta_sigma*delta_sigma)*(1+1./3.*eta*eta);
3214 A = 0.;
3215 for (i=0;i<NDIM;i++) {
3216 in = cross(i,0);
3217 dex = cross(i,1);
3218 A += pow(-1,i)*pow(0.5*(I->I[it].sigma_rezi[ion][in+dex*NDIM]-I->I[it].sigma_rezi[ion][dex+in*NDIM]),2);
3219 }
3220 if (P->Call.out[ValueOut]) {
3221 fprintf(stderr,"(%i) converted to Principal Axis System\n==================\nDiagonal entries:", P->Par.me);
3222 for (i=0;i<NDIM;i++)
3223 fprintf(stderr,"\t%lg",gsl_vector_get(eval,i));
3224 fprintf(stderr,"\nshielding : %e\n", iso);
3225 fprintf(stderr,"anisotropy : %e\n", delta_sigma);
3226 fprintf(stderr,"asymmetry : %e\n", eta);
3227 fprintf(stderr,"S : %e\n", S);
3228 fprintf(stderr,"A : %e\n", A);
3229 fprintf(stderr,"==================\n");
3230 }
3231 if (P->Par.me == 0) {
3232 FILE *SigmaFile;
3233 char suffixsigma[255];
3234 sprintf(&suffixsigma[0], ".sigma_i%i_%s_PAS.csv", ion, I->I[it].Symbol);
3235 if (Lev0->LevelNo == Lat->MaxLevel-2) {
3236 OpenFile(P, &SigmaFile, suffixsigma, "w", P->Call.out[ReadOut]);
3237 fprintf(SigmaFile,"# chemical shielding tensor sigma[00,11,22] Principal Axis System, seed %i, config %s, run on %s", R->Seed, P->Files.default_path, ctime(&seconds));
3238 fprintf(SigmaFile,"# Ecut\tSigma_XX\tSigma_YY\tSigma_ZZ\tShielding\tanisotropy\tasymmetry\tS\t\tA\n");
3239 } else
3240 OpenFile(P, &SigmaFile, suffixsigma, "a", P->Call.out[ReadOut]);
3241 fprintf(SigmaFile,"%lg\t", Lev0->ECut/4.); // ECut is in Rydberg
3242 for (i=0;i<NDIM;i++)
3243 fprintf(SigmaFile,"%lg\t", gsl_vector_get(eval,i));
3244 fprintf(SigmaFile,"%lg\t%lg\t%lg\t%lg\t%lg\t\n", iso, delta_sigma, eta, S, A);
3245 fclose(SigmaFile);
3246 }
3247 }
3248 }
3249
3250 // Output of magnetic field densities for each direction
3251 for (i=0;i<NDIM*NDIM;i++)
3252 OutputVis(P, sigma_real[i]);
3253 // Diagonalizing the tensor "field" B_ij [r]
3254 fprintf(stderr,"(%i) Diagonalizing B_ij [r] ... \n", P->Par.me);
3255 for (i=0; i< Dens0->LocalSizeR; i++) {
3256 for (in=0; in<NDIM; in++) // index i of vector component in integrand
3257 for (dex=0; dex<NDIM; dex++) { // index j of B component derivation in current density tensor
3258 //fprintf(stderr,"(%i) Setting B_(%i,%i)[%i] ... \n", P->Par.me, in,dex,i);
3259 gsl_matrix_complex_set(H,in,dex,gsl_complex_rect((sigma_real[in+dex*NDIM][i]+sigma_real[dex+in*NDIM][i])/2.,0));
3260 }
3261 gsl_eigen_herm(H, eval, w);
3262 gsl_sort_vector(eval); // sort eigenvalues
3263 for (in=0;in<NDIM;in++)
3264 sigma_real[in][i] = gsl_vector_get(eval,in);
3265 }
3266
3267 if (Lev0->LevelNo == 0) {
3268 if (!P->Par.me && P->Call.out[NormalOut]) fprintf(stderr,"(%i)Output of NICS map ...\n", P->Par.me);
3269 // Output of magnetic field densities for each direction
3270 //for (i=0;i<NDIM*NDIM;i++)
3271 // OutputVis(P, sigma_real[i]);
3272 // Diagonalizing the tensor "field" B_ij [r]
3273 if (P->Call.out[ValueOut]) fprintf(stderr,"(%i) Diagonalizing B_ij [r] ... \n", P->Par.me);
3274 for (i=0; i< Dens0->LocalSizeR; i++) {
3275 for (in=0; in<NDIM; in++) // index i of vector component in integrand
3276 for (dex=0; dex<NDIM; dex++) { // index j of B component derivation in current density tensor
3277 //fprintf(stderr,"(%i) Setting B_(%i,%i)[%i] ... \n", P->Par.me, in,dex,i);
3278 gsl_matrix_complex_set(H,in,dex,gsl_complex_rect((sigma_real[in+dex*NDIM][i]+sigma_real[dex+in*NDIM][i])/2.,0.));
3279 }
3280 gsl_eigen_herm(H, eval, w);
3281 gsl_sort_vector(eval); // sort eigenvalues
3282 for (in=0;in<NDIM;in++)
3283 sigma_real[in][i] = gsl_vector_get(eval,in);
3284 }
3285 }
3286
3287 // now absolute the B values (as density scales them by log) and output
3288 if (F->DoOutNICS) {
3289 for (i=0; i< Dens0->LocalSizeR; i++)
3290 for (in=0;in<NDIM;in++)
3291 sigma_real[in][i] = fabs(sigma_real[in][i]);
3292 // Output of diagonalized magnetic field densities for each direction
3293 for (i=0;i<NDIM;i++)
3294 OutputVis(P, sigma_real[i]);
3295 }
3296 for (i=0;i<NDIM*NDIM;i++)
3297 UnLockDensityArray(Dens0,i,real); // sigma_imag/real free
3298
3299 gsl_eigen_herm_free(w);
3300 gsl_vector_free(eval);
3301 gsl_matrix_complex_free(H);
3302}
3303
3304
3305/** Calculates the chemical shielding tensor at the positions of the nuclei.
3306 * The chemical shielding tensor at position R is defined as the proportionality factor between the induced and
3307 * the externally applied field.
3308 * \f[
3309 * \sigma_{ij} (R) = \frac{\delta B_j^{ind} (R)}{\delta B_i^{ext}}
3310 * = \frac{\mu_0}{4 \pi} \int d^3 r' \left ( \frac{r'-r}{| r' - r |^3} \times J_i (r') \right )_j
3311 * \f]
3312 * One after another for each nuclear position is the tensor evaluated and the result printed
3313 * to screen. Tensor is diagonalized afterwards.
3314 * \param *P Problem at hand
3315 * \sa CalculateMagneticSusceptibility() - similar calculation, yet without translation to ion centers.
3316 * \warning This routine is out-dated due to being numerically unstable because of the singularity which is not
3317 * considered carefully, recommendend replacement is CalculateChemicalShieldingByReciprocalCurrentDensity().
3318 */
3319void CalculateChemicalShielding(struct Problem *P)
3320{
3321 struct RunStruct *R = &P->R;
3322 struct Lattice *Lat = &P->Lat;
3323 struct LatticeLevel *Lev0 = R->Lev0;
3324 struct Density *Dens0 = R->Lev0->Dens;
3325 struct Ions *I = &P->Ion;
3326 double sigma[NDIM*NDIM],Sigma[NDIM*NDIM];
3327 fftw_real *CurrentDensity[NDIM*NDIM];
3328 int it, ion, in, dex, i0, n[NDIM], n0, i;//, *NUp;
3329 double r[NDIM], fac[NDIM], X[NDIM];
3330 const double discrete_factor = Lat->Volume/Lev0->MaxN;
3331 double eta, delta_sigma, S, A, iso;
3332 const int myPE = P->Par.me_comm_ST_Psi;
3333 int N[NDIM];
3334 N[0] = Lev0->Plan0.plan->N[0];
3335 N[1] = Lev0->Plan0.plan->N[1];
3336 N[2] = Lev0->Plan0.plan->N[2];
3337 const int N0 = Lev0->Plan0.plan->local_nx;
3338 FILE *SigmaFile;
3339 char suffixsigma[255];
3340 time_t seconds;
3341 time(&seconds); // get current time
3342
3343 // set pointers onto current density
3344 CurrentDensity[0] = (fftw_real *) Dens0->DensityArray[CurrentDensity0];
3345 CurrentDensity[1] = (fftw_real *) Dens0->DensityArray[CurrentDensity1];
3346 CurrentDensity[2] = (fftw_real *) Dens0->DensityArray[CurrentDensity2];
3347 CurrentDensity[3] = (fftw_real *) Dens0->DensityArray[CurrentDensity3];
3348 CurrentDensity[4] = (fftw_real *) Dens0->DensityArray[CurrentDensity4];
3349 CurrentDensity[5] = (fftw_real *) Dens0->DensityArray[CurrentDensity5];
3350 CurrentDensity[6] = (fftw_real *) Dens0->DensityArray[CurrentDensity6];
3351 CurrentDensity[7] = (fftw_real *) Dens0->DensityArray[CurrentDensity7];
3352 CurrentDensity[8] = (fftw_real *) Dens0->DensityArray[CurrentDensity8];
3353 gsl_matrix_complex *H = gsl_matrix_complex_calloc(NDIM,NDIM);
3354
3355 for (it=0; it < I->Max_Types; it++) { // integration over all types
3356 for (ion=0; ion < I->I[it].Max_IonsOfType; ion++) { // and each ion of type
3357 if (P->Call.out[ValueOut]) fprintf(stderr,"(%i) Shielding Tensor for Ion %i of element %s \\sigma_ij = \n",P->Par.me, ion, I->I[it].Name);
3358 for (in=0; in<NDIM; in++) {// index i of vector component in integrand
3359 for (dex=0; dex<NDIM; dex++) { // index j of B component derivation in current density tensor
3360 sigma[in+dex*NDIM] = 0.;
3361
3362 for(n0=0;n0<N0;n0++) // do the integration over real space
3363 for(n[1]=0;n[1]<N[1];n[1]++)
3364 for(n[2]=0;n[2]<N[2];n[2]++) {
3365 n[0]=n0 + N0*myPE; // global relative coordinate: due to partitoning of x-axis in PEPGamma>1 case
3366 fac[0] = (double)n[0]/(double)N[0];
3367 fac[1] = (double)n[1]/(double)N[1];
3368 fac[2] = (double)n[2]/(double)N[2];
3369 RMat33Vec3(r, Lat->RealBasis, fac);
3370 MinImageConv(Lat,r, &(I->I[it].R[NDIM*ion]),X);
3371 i0 = n[2]+N[2]*(n[1]+N[1]*(n0)); // the index of current density must match LocalSizeR!
3372 //z = MinImageConv(Lat,r, I->I[it].R[NDIM*ion],in); // "in" always is missing third component in cross product
3373 sigma[in+dex*NDIM] += (X[cross(in,0)] * CurrentDensity[dex*NDIM+cross(in,1)][i0] - X[cross(in,2)] * CurrentDensity[dex*NDIM+cross(in,3)][i0]);
3374 //if (it == 0 && ion == 0) fprintf(stderr,"(%i) moment[%i][%i] += (%e * %e - %e * %e) = %e\n", P->Par.me, in, dex, x,CurrentDensity[dex*NDIM+cross(in,1)][i0],y,CurrentDensity[dex*NDIM+cross(in,3)][i0],moment[in+dex*NDIM]);
3375 }
3376 sigma[in+dex*NDIM] *= -mu0*discrete_factor/(4.*PI); // due to summation instead of integration
3377 MPI_Allreduce ( &sigma[in+dex*NDIM], &Sigma[in+dex*NDIM], 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi); // sum "LocalSize to TotalSize"
3378 I->I[it].sigma[ion][in+dex*NDIM] = Sigma[in+dex*NDIM];
3379 if (P->Call.out[ValueOut]) fprintf(stderr," %e", Sigma[in+dex*NDIM]);
3380 }
3381 if (P->Call.out[ValueOut]) fprintf(stderr,"\n");
3382 }
3383 // store symmetrized matrix
3384 for (in=0;in<NDIM;in++)
3385 for (dex=0;dex<NDIM;dex++)
3386 gsl_matrix_complex_set(H,in,dex,gsl_complex_rect((Sigma[in+dex*NDIM]+Sigma[dex+in*NDIM])/2.,0));
3387 // output tensor to file
3388 if (P->Par.me == 0) {
3389 sprintf(&suffixsigma[0], ".sigma_i%i_%s.L%i.csv", ion, I->I[it].Symbol, Lev0->LevelNo);
3390 OpenFile(P, &SigmaFile, suffixsigma, "a", P->Call.out[ReadOut]);
3391 fprintf(SigmaFile,"# chemical shielding tensor sigma[01,02,03,10,11,12,20,21,22], seed %i, config %s, run on %s", R->Seed, P->Files.default_path, ctime(&seconds));
3392 fprintf(SigmaFile,"%lg\t", P->Lat.ECut/(Lat->LevelSizes[0]*Lat->LevelSizes[0]));
3393 for (in=0;in<NDIM*NDIM;in++)
3394 fprintf(SigmaFile,"%e\t", Sigma[in]);
3395 fprintf(SigmaFile,"\n");
3396 fclose(SigmaFile);
3397 }
3398 // diagonalize sigma
3399 gsl_vector *eval = gsl_vector_alloc(NDIM);
3400 gsl_eigen_herm_workspace *w = gsl_eigen_herm_alloc(NDIM);
3401 gsl_eigen_herm(H, eval, w);
3402 gsl_eigen_herm_free(w);
3403 gsl_sort_vector(eval); // sort eigenvalues
3404 // print eigenvalues
3405// if (P->Call.out[ValueOut]) {
3406// fprintf(stderr,"(%i) diagonal shielding for Ion %i of element %s:", P->Par.me, ion, I->I[it].Name);
3407// for (in=0;in<NDIM;in++)
3408// fprintf(stderr,"\t%lg",gsl_vector_get(eval,in));
3409// fprintf(stderr,"\n\n");
3410// }
3411 // print eigenvalues
3412 iso = 0;
3413 for (i=0;i<NDIM;i++) {
3414 I->I[it].sigma[ion][i] = gsl_vector_get(eval,i);
3415 iso += Sigma[i+i*NDIM]/3.;
3416 }
3417 eta = (gsl_vector_get(eval,1)-gsl_vector_get(eval,0))/(gsl_vector_get(eval,2)-iso);
3418 delta_sigma = gsl_vector_get(eval,2) - 0.5*(gsl_vector_get(eval,0)+gsl_vector_get(eval,1));
3419 S = (delta_sigma*delta_sigma)*(1+1./3.*eta*eta);
3420 A = 0.;
3421 for (i=0;i<NDIM;i++) {
3422 in = cross(i,0);
3423 dex = cross(i,1);
3424 A += pow(-1,i)*pow(0.5*(Sigma[in+dex*NDIM]-Sigma[dex+in*NDIM]),2);
3425 }
3426 if (P->Call.out[ValueOut]) {
3427 fprintf(stderr,"(%i) converted to Principal Axis System\n==================\nDiagonal entries:", P->Par.me);
3428 for (i=0;i<NDIM;i++)
3429 fprintf(stderr,"\t%lg",gsl_vector_get(eval,i));
3430 fprintf(stderr,"\nshielding : %e\n", iso);
3431 fprintf(stderr,"anisotropy : %e\n", delta_sigma);
3432 fprintf(stderr,"asymmetry : %e\n", eta);
3433 fprintf(stderr,"S : %e\n", S);
3434 fprintf(stderr,"A : %e\n", A);
3435 fprintf(stderr,"==================\n");
3436
3437 }
3438 gsl_vector_free(eval);
3439 }
3440 }
3441
3442 gsl_matrix_complex_free(H);
3443}
3444
3445/** Test if G=0-component of reciprocal current is 0.
3446 * In most cases we do not reach a numerical sensible zero as in MYEPSILON and remain satisfied as long
3447 * as the integrated current density is very small (e.g. compared to single entries in the current density array)
3448 * \param *P Problem at hand
3449 * \param *CurrentC pointer to reciprocal current density
3450 * \param *GArray pointer to array with G vectors
3451 * \param in index of current component
3452 * \sa TestCurrent() these two tests are equivalent and follow by fourier transformation
3453 */
3454void TestReciprocalCurrent(struct Problem *P, const fftw_complex *CurrentC, struct OneGData *GArray, int in)
3455{
3456 double tmp;
3457 tmp = sqrt(CurrentC[0].re*CurrentC[0].re+CurrentC[0].im*CurrentC[0].im);
3458 if ((P->Call.out[LeaderOut]) && (GArray[0].GSq < MYEPSILON)) {
3459 if (in % NDIM == 0) fprintf(stderr,"(%i) ",P->Par.me);
3460 if (tmp > MYEPSILON) {
3461 fprintf(stderr,"J_{%i,%i} = |%e + i%e| < %e ? (%e)\t", in / NDIM, in%NDIM, CurrentC[0].re, CurrentC[0].im, MYEPSILON, tmp - MYEPSILON);
3462 } else {
3463 fprintf(stderr,"J_{%i,%i} ok\t", in / NDIM, in%NDIM);
3464 }
3465 if (in % NDIM == (NDIM-1)) fprintf(stderr,"\n");
3466 }
3467}
3468
3469/** Test if integrated current over cell is 0.
3470 * In most cases we do not reach a numerical sensible zero as in MYEPSILON and remain satisfied as long
3471 * as the integrated current density is very small (e.g. compared to single entries in the current density array)
3472 * \param *P Problem at hand
3473 * \param index index of current component
3474 * \sa CalculateNativeIntDens() for integration of one current tensor component
3475 */
3476 void TestCurrent(struct Problem *P, const int index)
3477{
3478 struct RunStruct *R = &P->R;
3479 struct LatticeLevel *Lev0 = R->Lev0;
3480 struct Density *Dens0 = Lev0->Dens;
3481 fftw_real *CurrentDensity[NDIM*NDIM];
3482 int in;
3483 double result[NDIM*NDIM], res = 0.;
3484
3485 // set pointers onto current density array and get number of grid points in each direction
3486 CurrentDensity[0] = (fftw_real *) Dens0->DensityArray[CurrentDensity0];
3487 CurrentDensity[1] = (fftw_real *) Dens0->DensityArray[CurrentDensity1];
3488 CurrentDensity[2] = (fftw_real *) Dens0->DensityArray[CurrentDensity2];
3489 CurrentDensity[3] = (fftw_real *) Dens0->DensityArray[CurrentDensity3];
3490 CurrentDensity[4] = (fftw_real *) Dens0->DensityArray[CurrentDensity4];
3491 CurrentDensity[5] = (fftw_real *) Dens0->DensityArray[CurrentDensity5];
3492 CurrentDensity[6] = (fftw_real *) Dens0->DensityArray[CurrentDensity6];
3493 CurrentDensity[7] = (fftw_real *) Dens0->DensityArray[CurrentDensity7];
3494 CurrentDensity[8] = (fftw_real *) Dens0->DensityArray[CurrentDensity8];
3495 for(in=0;in<NDIM;in++) {
3496 result[in] = CalculateNativeIntDens(P,Lev0,CurrentDensity[in + NDIM*index],R->FactorDensityR);
3497 res += pow(result[in],2.);
3498 }
3499 res = sqrt(res);
3500 // if greater than 0, complain about it
3501 if ((res > MYEPSILON) && (P->Call.out[LeaderOut]))
3502 fprintf(stderr, "(%i) \\int_\\Omega d^3 r j_%i(r) = (%e,%e,%e), %e > %e!\n",P->Par.me, index, result[0], result[1], result[2], res, MYEPSILON);
3503}
3504
3505/** Testing whether re<->im switches (due to symmetry) confuses fft.
3506 * \param *P Problem at hand
3507 * \param l local wave function number
3508 */
3509void test_fft_symmetry(struct Problem *P, const int l)
3510{
3511 struct Lattice *Lat = &P->Lat;
3512 struct RunStruct *R = &P->R;
3513 struct LatticeLevel *LevS = R->LevS;
3514 struct LatticeLevel *Lev0 = R->Lev0;
3515 struct Density *Dens0 = Lev0->Dens;
3516 struct fft_plan_3d *plan = Lat->plan;
3517 fftw_complex *tempdestRC = (fftw_complex *)Dens0->DensityCArray[Temp2Density];
3518 fftw_complex *work = Dens0->DensityCArray[TempDensity];
3519 fftw_complex *workC = (fftw_complex *)Dens0->DensityArray[TempDensity];
3520 fftw_complex *posfac, *destpos, *destRCS, *destRCD;
3521 fftw_complex *PsiC = Dens0->DensityCArray[ActualPsiDensity];
3522 fftw_real *PsiCR = (fftw_real *) PsiC;
3523 fftw_complex *Psi0 = LevS->LPsi->LocalPsi[l];
3524 fftw_complex *dest = LevS->LPsi->TempPsi;
3525 fftw_real *Psi0R = (fftw_real *)Dens0->DensityArray[Temp2Density];
3526 int i,Index, pos, i0, iS,g; //, NoOfPsis = Psi->TypeStartIndex[UnOccupied] - Psi->TypeStartIndex[Occupied];
3527 int n[NDIM], n0;
3528 const int N0 = LevS->Plan0.plan->local_nx; // we don't want to build global density, but local
3529 int N[NDIM], NUp[NDIM];
3530 N[0] = LevS->Plan0.plan->N[0];
3531 N[1] = LevS->Plan0.plan->N[1];
3532 N[2] = LevS->Plan0.plan->N[2];
3533 NUp[0] = LevS->NUp[0];
3534 NUp[1] = LevS->NUp[1];
3535 NUp[2] = LevS->NUp[2];
3536 //const int k_normal = Lat->Psi.TypeStartIndex[Occupied] + (l - Lat->Psi.TypeStartIndex[R->CurrentMin]);
3537 //const double *Wcentre = Lat->Psi.AddData[k_normal].WannierCentre;
3538 //double x[NDIM], fac[NDIM];
3539 double result1=0., result2=0., result3=0., result4=0.;
3540 double Result1=0., Result2=0., Result3=0., Result4=0.;
3541 const double HGcRCFactor = 1./LevS->MaxN; // factor for inverse fft
3542
3543
3544 // fft to real space
3545 SetArrayToDouble0((double *)tempdestRC, Dens0->TotalSize*2);
3546 SetArrayToDouble0((double *)PsiC, Dens0->TotalSize*2);
3547 for (i=0;i<LevS->MaxG;i++) { // incoming is positive, outgoing is positive
3548 Index = LevS->GArray[i].Index;
3549 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
3550 destpos = &tempdestRC[LevS->MaxNUp*Index];
3551 for (pos=0; pos < LevS->MaxNUp; pos++) {
3552 destpos[pos].re = (Psi0[i].re)*posfac[pos].re-(Psi0[i].im)*posfac[pos].im;
3553 destpos[pos].im = (Psi0[i].re)*posfac[pos].im+(Psi0[i].im)*posfac[pos].re;
3554 //destpos[pos].re = (Psi0[i].im)*posfac[pos].re-(-Psi0[i].re)*posfac[pos].im;
3555 //destpos[pos].im = (Psi0[i].im)*posfac[pos].im+(-Psi0[i].re)*posfac[pos].re;
3556 }
3557 }
3558 for (i=0; i<LevS->MaxDoubleG; i++) {
3559 destRCS = &tempdestRC[LevS->DoubleG[2*i]*LevS->MaxNUp];
3560 destRCD = &tempdestRC[LevS->DoubleG[2*i+1]*LevS->MaxNUp];
3561 for (pos=0; pos < LevS->MaxNUp; pos++) {
3562 destRCD[pos].re = destRCS[pos].re;
3563 destRCD[pos].im = -destRCS[pos].im;
3564 }
3565 }
3566 fft_3d_complex_to_real(plan, LevS->LevelNo, FFTNFUp, tempdestRC, work);
3567 DensityRTransformPos(LevS,(fftw_real*)tempdestRC, Psi0R);
3568
3569 // apply position operator and do first result
3570 for (n0=0;n0<N0;n0++) // only local points on x axis
3571 for (n[1]=0;n[1]<N[1];n[1]++)
3572 for (n[2]=0;n[2]<N[2];n[2]++) {
3573 n[0]=n0 + LevS->Plan0.plan->start_nx; // global relative coordinate: due to partitoning of x-axis in PEPGamma>1 case
3574 i0 = n[2]*NUp[2]+N[2]*NUp[2]*(n[1]*NUp[1]+N[1]*NUp[1]*n0*NUp[0]);
3575 iS = n[2]+N[2]*(n[1]+N[1]*n0);
3576 //x[0] += 1; // shifting expectation value of x coordinate from 0 to 1
3577 PsiCR[iS] = Psi0R[i0]; // truedist(Lat, x[0], Wcentre[0],0) *
3578 result1 += PsiCR[iS] * Psi0R[i0];
3579 }
3580 result1 /= LevS->MaxN; // factor due to discrete integration
3581 MPI_Allreduce ( &result1, &Result1, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi); // sum "LocalSize to TotalSize"
3582 if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) 1st result: %e\n",P->Par.me, Result1);
3583
3584 // fft to reciprocal space and do second result
3585 fft_3d_real_to_complex(plan, LevS->LevelNo, FFTNF1, PsiC, workC);
3586 SetArrayToDouble0((double *)dest, 2*R->InitLevS->MaxG);
3587 for (g=0; g < LevS->MaxG; g++) {
3588 Index = LevS->GArray[g].Index;
3589 dest[g].re = (Psi0[Index].re)*HGcRCFactor;
3590 dest[g].im = (Psi0[Index].im)*HGcRCFactor;
3591 }
3592 result2 = GradSP(P,LevS,Psi0,dest);
3593 MPI_Allreduce ( &result2, &Result2, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi); // sum "LocalSize to TotalSize"
3594 if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) 2nd result: %e\n",P->Par.me, Result2);
3595
3596 // fft again to real space, this time change symmetry
3597 SetArrayToDouble0((double *)tempdestRC, Dens0->TotalSize*2);
3598 SetArrayToDouble0((double *)PsiC, Dens0->TotalSize*2);
3599 for (i=0;i<LevS->MaxG;i++) { // incoming is positive, outgoing is positive
3600 Index = LevS->GArray[i].Index;
3601 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
3602 destpos = &tempdestRC[LevS->MaxNUp*Index];
3603 for (pos=0; pos < LevS->MaxNUp; pos++) {
3604 destpos[pos].re = (Psi0[i].im)*posfac[pos].re-(-Psi0[i].re)*posfac[pos].im;
3605 destpos[pos].im = (Psi0[i].im)*posfac[pos].im+(-Psi0[i].re)*posfac[pos].re;
3606 }
3607 }
3608 for (i=0; i<LevS->MaxDoubleG; i++) {
3609 destRCS = &tempdestRC[LevS->DoubleG[2*i]*LevS->MaxNUp];
3610 destRCD = &tempdestRC[LevS->DoubleG[2*i+1]*LevS->MaxNUp];
3611 for (pos=0; pos < LevS->MaxNUp; pos++) {
3612 destRCD[pos].re = destRCS[pos].re;
3613 destRCD[pos].im = -destRCS[pos].im;
3614 }
3615 }
3616 fft_3d_complex_to_real(plan, LevS->LevelNo, FFTNFUp, tempdestRC, work);
3617 DensityRTransformPos(LevS,(fftw_real*)tempdestRC, Psi0R);
3618
3619 // bring down from Lev0 to LevS
3620 for (n0=0;n0<N0;n0++) // only local points on x axis
3621 for (n[1]=0;n[1]<N[1];n[1]++)
3622 for (n[2]=0;n[2]<N[2];n[2]++) {
3623 i0 = n[2]*NUp[2]+N[2]*NUp[2]*(n[1]*NUp[1]+N[1]*NUp[1]*n0*NUp[0]);
3624 iS = n[2]+N[2]*(n[1]+N[1]*n0);
3625 PsiCR[iS] = Psi0R[i0]; // truedist(Lat, x[0], Wcentre[0],0) *
3626 result3 += PsiCR[iS] * Psi0R[i0];
3627 }
3628 result3 /= LevS->MaxN; // factor due to discrete integration
3629 MPI_Allreduce ( &result3, &Result3, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi); // sum "LocalSize to TotalSize"
3630 if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) 3rd result: %e\n",P->Par.me, Result3);
3631
3632 // fft back to reciprocal space, change symmetry back and do third result
3633 fft_3d_real_to_complex(plan, LevS->LevelNo, FFTNF1, PsiC, workC);
3634 SetArrayToDouble0((double *)dest, 2*R->InitLevS->MaxG);
3635 for (g=0; g < LevS->MaxG; g++) {
3636 Index = LevS->GArray[g].Index;
3637 dest[g].re = (-PsiC[Index].im)*HGcRCFactor;
3638 dest[g].im = ( PsiC[Index].re)*HGcRCFactor;
3639 }
3640 result4 = GradSP(P,LevS,Psi0,dest);
3641 MPI_Allreduce ( &result4, &Result4, 1, MPI_DOUBLE, MPI_SUM, P->Par.comm_ST_Psi); // sum "LocalSize to TotalSize"
3642 if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) 4th result: %e\n",P->Par.me, Result4);
3643}
3644
3645
3646/** Test function to check RxP application.
3647 * Checks applied solution to an analytic for a specific and simple wave function -
3648 * where just one coefficient is unequal to zero.
3649 * \param *P Problem at hand
3650 exp(I b G) - I exp(I b G) b G - exp(I a G) + I exp(I a G) a G
3651 -------------------------------------------------------------
3652 2
3653 G
3654 */
3655void test_rxp(struct Problem *P)
3656{
3657 struct RunStruct *R = &P->R;
3658 struct Lattice *Lat = &P->Lat;
3659 //struct LatticeLevel *Lev0 = R->Lev0;
3660 struct LatticeLevel *LevS = R->LevS;
3661 struct OneGData *GA = LevS->GArray;
3662 //struct Density *Dens0 = Lev0->Dens;
3663 fftw_complex *Psi0 = LevS->LPsi->TempPsi;
3664 fftw_complex *Psi2 = P->Grad.GradientArray[GraSchGradient];
3665 fftw_complex *Psi3 = LevS->LPsi->TempPsi2;
3666 int g, g_bar, i, j, k, k_normal = 0;
3667 double tmp, a,b, G;
3668 //const double *Wcentre = Lat->Psi.AddData[k_normal].WannierCentre;
3669 const double discrete_factor = 1.;//Lat->Volume/LevS->MaxN;
3670 fftw_complex integral;
3671
3672 // reset coefficients
3673 debug (P,"Creating RxP test function.");
3674 SetArrayToDouble0((double *)Psi0,2*R->InitLevS->MaxG);
3675 SetArrayToDouble0((double *)Psi2,2*R->InitLevS->MaxG);
3676
3677 // pick one which becomes non-zero
3678 g = 3;
3679
3680 //for (g=0;g<LevS->MaxG;g++) {
3681 Psi0[g].re = 1.;
3682 Psi0[g].im = 0.;
3683 //}
3684 fprintf(stderr,"(%i) G[%i] = (%e,%e,%e) \n",P->Par.me, g, GA[g].G[0], GA[g].G[1], GA[g].G[2]);
3685 i = 0;
3686
3687 // calculate analytic result
3688 debug (P,"Calculating analytic solution.");
3689 for (g_bar=0;g_bar<LevS->MaxG;g_bar++) {
3690 for (g=0;g<LevS->MaxG;g++) {
3691 if (GA[g].G[i] == GA[g_bar].G[i]) {
3692 j = cross(i,0);
3693 k = cross(i,1);
3694 if (GA[g].G[k] == GA[g_bar].G[k]) {
3695 //b = truedist(Lat, sqrt(Lat->RealBasisSQ[j]), Wcentre[j], j);
3696 b = sqrt(Lat->RealBasisSQ[j]);
3697 //a = truedist(Lat, 0., Wcentre[j], j);
3698 a = 0.;
3699 G = 1; //GA[g].G[k];
3700 if (GA[g].G[j] == GA[g_bar].G[j]) {
3701 Psi2[g_bar].re += G*Psi0[g].re * (.5 * b * b - .5 * a * a) * discrete_factor;
3702 Psi2[g_bar].im += G*Psi0[g].im * (.5 * b * b - .5 * a * a) * discrete_factor;
3703 //if ((G != 0) && ((fabs(Psi0[g].re) > MYEPSILON) || (fabs(Psi0[g].im) > MYEPSILON)))
3704 //fprintf(stderr,"(%i) Psi[%i].re += %e +i %e\n",P->Par.me, g_bar, G*Psi0[g].re * (.5 * b * b - .5 * a * a) * discrete_factor, G*Psi0[g].im * (.5 * b * b - .5 * a * a) * discrete_factor);
3705 } else {
3706 tmp = GA[g].G[j]-GA[g_bar].G[j];
3707 integral.re = (cos(tmp*b)+sin(tmp*b)*b*tmp - cos(tmp*a)-sin(tmp*a)*a*tmp) / (tmp * tmp);
3708 integral.im = (sin(tmp*b)-cos(tmp*b)*b*tmp - sin(tmp*a)+cos(tmp*a)*a*tmp) / (tmp * tmp);
3709 Psi2[g_bar].re += G*(Psi0[g].re*integral.re - Psi0[g].im*integral.im) * discrete_factor;
3710 Psi2[g_bar].im += G*(Psi0[g].re*integral.im + Psi0[g].im*integral.re) * discrete_factor;
3711 //if ((G != 0) && ((fabs(Psi0[g].re) > MYEPSILON) || (fabs(Psi0[g].im) > MYEPSILON)))
3712 //fprintf(stderr,"(%i) Psi[%i].re += %e\tPsi[%i].im += %e \n",P->Par.me, g_bar, G*(Psi0[g].re*integral.re - Psi0[g].im*integral.im) * discrete_factor, g_bar, G*(Psi0[g].re*integral.im + Psi0[g].im*integral.re) * discrete_factor);
3713 }
3714 }
3715 j = cross(i,2);
3716 k = cross(i,3);
3717 if (GA[g].G[k] == GA[g_bar].G[k]) {
3718 //b = truedist(Lat, sqrt(Lat->RealBasisSQ[j]), Wcentre[j], j);
3719 b = sqrt(Lat->RealBasisSQ[j]);
3720 //a = truedist(Lat, 0., Wcentre[j], j);
3721 a = 0.;
3722 G = 1; //GA[g].G[k];
3723 if (GA[g].G[j] == GA[g_bar].G[j]) {
3724 Psi2[g_bar].re += G*Psi0[g].re * (.5 * b * b - .5 * a * a) * discrete_factor;
3725 Psi2[g_bar].im += G*Psi0[g].im * (.5 * b * b - .5 * a * a) * discrete_factor;
3726 //if ((G != 0) && ((fabs(Psi0[g].re) > MYEPSILON) || (fabs(Psi0[g].im) > MYEPSILON)))
3727 //fprintf(stderr,"(%i) Psi[%i].re += %e +i %e\n",P->Par.me, g_bar, G*Psi0[g].re * (.5 * b * b - .5 * a * a) * discrete_factor, G*Psi0[g].im * (.5 * b * b - .5 * a * a) * discrete_factor);
3728 } else {
3729 tmp = GA[g].G[j]-GA[g_bar].G[j];
3730 integral.re = (cos(tmp*b)+sin(tmp*b)*b*tmp - cos(tmp*a)-sin(tmp*a)*a*tmp) / (tmp * tmp);
3731 integral.im = (sin(tmp*b)-cos(tmp*b)*b*tmp - sin(tmp*a)+cos(tmp*a)*a*tmp) / (tmp * tmp);
3732 Psi2[g_bar].re += G*(Psi0[g].re*integral.re - Psi0[g].im*integral.im) * discrete_factor;
3733 Psi2[g_bar].im += G*(Psi0[g].re*integral.im + Psi0[g].im*integral.re) * discrete_factor;
3734 //if ((G != 0) && ((fabs(Psi0[g].re) > MYEPSILON) || (fabs(Psi0[g].im) > MYEPSILON)))
3735 //fprintf(stderr,"(%i) Psi[%i].re += %e\tPsi[%i].im += %e \n",P->Par.me, g_bar, G*(Psi0[g].re*integral.re - Psi0[g].im*integral.im) * discrete_factor, g_bar, G*(Psi0[g].re*integral.im + Psi0[g].im*integral.re) * discrete_factor);
3736 }
3737 }
3738 }
3739 }
3740 }
3741
3742 // apply rxp
3743 debug (P,"Applying RxP to test function.");
3744 CalculatePerturbationOperator_RxP(P,Psi0,Psi3,k_normal,i);
3745
3746 // compare both coefficient arrays
3747 debug(P,"Beginning comparison of analytic and Rxp applied solution.");
3748 for (g=0;g<LevS->MaxG;g++) {
3749 if ((fabs(Psi3[g].re-Psi2[g].re) >= MYEPSILON) || (fabs(Psi3[g].im-Psi2[g].im) >= MYEPSILON))
3750 fprintf(stderr,"(%i) Psi3[%i] = %e +i %e != Psi2[%i] = %e +i %e\n",P->Par.me, g, Psi3[g].re, Psi3[g].im, g, Psi2[g].re, Psi2[g].im);
3751 //else
3752 //fprintf(stderr,"(%i) Psi1[%i] == Psi2[%i] = %e +i %e\n",P->Par.me, g, g, Psi1[g].re, Psi1[g].im);
3753 }
3754 fprintf(stderr,"(%i) <0|1> = <0|r|0> == %e +i %e\n",P->Par.me, GradSP(P,LevS,Psi0,Psi3), GradImSP(P,LevS,Psi0,Psi3));
3755 fprintf(stderr,"(%i) <1|1> = |r|ᅵ == %e +i %e\n",P->Par.me, GradSP(P,LevS,Psi3,Psi3), GradImSP(P,LevS,Psi3,Psi3));
3756 fprintf(stderr,"(%i) <0|0> = %e +i %e\n",P->Par.me, GradSP(P,LevS,Psi0,Psi0), GradImSP(P,LevS,Psi0,Psi0));
3757 fprintf(stderr,"(%i) <0|2> = %e +i %e\n",P->Par.me, GradSP(P,LevS,Psi0,Psi2), GradImSP(P,LevS,Psi0,Psi2));
3758}
3759
3760
3761/** Output of a (X,Y,DX,DY) 2d-vector plot.
3762 * For a printable representation of the induced current two-dimensional vector plots are useful, as three-dimensional
3763 * isospheres are sometimes mis-leading or do not represent the desired flow direction. The routine simply extracts a
3764 * two-dimensional cut orthogonal to one of the lattice axis at a certain node.
3765 * \param *P Problem at hand
3766 * \param B_index direction of B field
3767 * \param n_orth grid node in B_index direction of the plane (the order in which the remaining two coordinate axis
3768 * appear is the same as in a cross product, which is used to determine orthogonality)
3769 */
3770void PlotVectorPlane(struct Problem *P, int B_index, int n_orth)
3771{
3772 struct RunStruct *R = &P->R;
3773 struct LatticeLevel *Lev0 = R->Lev0;
3774 struct Density *Dens0 = Lev0->Dens;
3775 char filename[255];
3776 char *suchpointer;
3777 FILE *PlotFile = NULL;
3778 const int myPE = P->Par.me_comm_ST;
3779 time_t seconds;
3780 fftw_real *CurrentDensity[NDIM*NDIM];
3781 CurrentDensity[0] = (fftw_real *) Dens0->DensityArray[CurrentDensity0];
3782 CurrentDensity[1] = (fftw_real *) Dens0->DensityArray[CurrentDensity1];
3783 CurrentDensity[2] = (fftw_real *) Dens0->DensityArray[CurrentDensity2];
3784 CurrentDensity[3] = (fftw_real *) Dens0->DensityArray[CurrentDensity3];
3785 CurrentDensity[4] = (fftw_real *) Dens0->DensityArray[CurrentDensity4];
3786 CurrentDensity[5] = (fftw_real *) Dens0->DensityArray[CurrentDensity5];
3787 CurrentDensity[6] = (fftw_real *) Dens0->DensityArray[CurrentDensity6];
3788 CurrentDensity[7] = (fftw_real *) Dens0->DensityArray[CurrentDensity7];
3789 CurrentDensity[8] = (fftw_real *) Dens0->DensityArray[CurrentDensity8];
3790 time(&seconds); // get current time
3791
3792 if (!myPE) { // only process 0 writes to file
3793 // open file
3794 sprintf(&filename[0], ".current.L%i.csv", Lev0->LevelNo);
3795 OpenFile(P, &PlotFile, filename, "w", P->Call.out[ReadOut]);
3796 strcpy(filename, ctime(&seconds));
3797 suchpointer = strchr(filename, '\n');
3798 if (suchpointer != NULL)
3799 *suchpointer = '\0';
3800 if (PlotFile != NULL) {
3801 fprintf(PlotFile,"# current vector plot of plane perpendicular to direction e_%i at node %i, seed %i, config %s, run on %s, #cpus %i", B_index, n_orth, R->Seed, P->Files.default_path, filename, P->Par.Max_me_comm_ST_Psi);
3802 fprintf(PlotFile,"\n");
3803 } else { Error(SomeError, "PlotVectorPlane: Opening Plot File"); }
3804 }
3805
3806 // plot density
3807 if (!P->Par.me_comm_ST_PsiT) // only first wave function group as current density of all psis was gathered
3808 PlotRealDensity(P, Lev0, PlotFile, B_index, n_orth, CurrentDensity[B_index*NDIM+cross(B_index,0)], CurrentDensity[B_index*NDIM+cross(B_index,1)]);
3809
3810 if (PlotFile != NULL) {
3811 // close file
3812 fclose(PlotFile);
3813 }
3814}
3815
3816
3817/** Reads psi coefficients of \a type from file and transforms to new level.
3818 * \param *P Problem at hand
3819 * \param type PsiTypeTag of which minimisation group to load from file
3820 * \sa ReadSrcPsiDensity() - reading the coefficients, ChangePsiAndDensToLevUp() - transformation to upper level
3821 */
3822void ReadSrcPerturbedPsis(struct Problem *P, enum PsiTypeTag type)
3823{
3824 struct RunStruct *R = &P->R;
3825 struct Lattice *Lat = &P->Lat;
3826 struct LatticeLevel *Lev0 = &P->Lat.Lev[R->Lev0No+1]; // one level higher than current (ChangeLevUp already occurred)
3827 struct LatticeLevel *LevS = &P->Lat.Lev[R->LevSNo+1];
3828 struct Density *Dens = Lev0->Dens;
3829 struct Psis *Psi = &Lat->Psi;
3830 struct fft_plan_3d *plan = Lat->plan;
3831 fftw_complex *work = (fftw_complex *)Dens->DensityCArray[TempDensity];
3832 fftw_complex *tempdestRC = (fftw_complex *)Dens->DensityArray[TempDensity];
3833 fftw_complex *posfac, *destpos, *destRCS, *destRCD;
3834 fftw_complex *source, *source0;
3835 int Index,i,pos;
3836 double factorC = 1./Lev0->MaxN;
3837 int p,g;
3838
3839 // ================= read coefficients from file to LocalPsi ============
3840 ReadSrcPsiDensity(P, type, 0, R->LevSNo+1);
3841
3842 // ================= transform to upper level ===========================
3843 // for all local Psis do the usual transformation (completing coefficients for all grid vectors, fft, permutation)
3844 LockDensityArray(Dens, TempDensity, real);
3845 LockDensityArray(Dens, TempDensity, imag);
3846 for (p=Psi->LocalNo-1; p >= 0; p--)
3847 if (Psi->LocalPsiStatus[p].PsiType == type) { // only for the desired type
3848 source = LevS->LPsi->LocalPsi[p];
3849 source0 = Lev0->LPsi->LocalPsi[p];
3850 //fprintf(stderr,"(%i) ReadSrcPerturbedPsis: LevSNo %i\t Lev0No %i\tp %i\t source %p\t source0 %p\n", P->Par.me, LevS->LevelNo, Lev0->LevelNo, p, source, source0);
3851 SetArrayToDouble0((double *)tempdestRC, Dens->TotalSize*2);
3852 for (i=0;i<LevS->MaxG;i++) {
3853 Index = LevS->GArray[i].Index;
3854 posfac = &LevS->PosFactorUp[LevS->MaxNUp*i];
3855 destpos = &tempdestRC[LevS->MaxNUp*Index];
3856 //if (isnan(source[i].re)) { fprintf(stderr,"(%i) WARNING in ReadSrcPerturbedPsis(): source_%i[%i] = NaN!\n", P->Par.me, p, i); Error(SomeError, "NaN-Fehler!"); }
3857 for (pos=0; pos < LevS->MaxNUp; pos++) {
3858 destpos[pos].re = source[i].re*posfac[pos].re-source[i].im*posfac[pos].im;
3859 destpos[pos].im = source[i].re*posfac[pos].im+source[i].im*posfac[pos].re;
3860 }
3861 }
3862 for (i=0; i<LevS->MaxDoubleG; i++) {
3863 destRCS = &tempdestRC[LevS->DoubleG[2*i]*LevS->MaxNUp];
3864 destRCD = &tempdestRC[LevS->DoubleG[2*i+1]*LevS->MaxNUp];
3865 for (pos=0; pos < LevS->MaxNUp; pos++) {
3866 destRCD[pos].re = destRCS[pos].re;
3867 destRCD[pos].im = -destRCS[pos].im;
3868 }
3869 }
3870 fft_3d_complex_to_real(plan, LevS->LevelNo, FFTNFUp, tempdestRC, work);
3871 DensityRTransformPos(LevS,(fftw_real*)tempdestRC,(fftw_real *)Dens->DensityCArray[ActualPsiDensity]);
3872 // now we have density in the upper level, fft back to complex and store it as wave function coefficients
3873 fft_3d_real_to_complex(plan, Lev0->LevelNo, FFTNF1, Dens->DensityCArray[ActualPsiDensity], work);
3874 for (g=0; g < Lev0->MaxG; g++) {
3875 Index = Lev0->GArray[g].Index;
3876 source0[g].re = Dens->DensityCArray[ActualPsiDensity][Index].re*factorC;
3877 source0[g].im = Dens->DensityCArray[ActualPsiDensity][Index].im*factorC;
3878 //if (isnan(source0[g].re)) { fprintf(stderr,"(%i) WARNING in ReadSrcPerturbedPsis(): source0_%i[%i] = NaN!\n", P->Par.me, p, g); Error(SomeError, "NaN-Fehler!"); }
3879 }
3880 if (Lev0->GArray[0].GSq == 0.0)
3881 source0[g].im = 0.0;
3882 }
3883 UnLockDensityArray(Dens, TempDensity, real);
3884 UnLockDensityArray(Dens, TempDensity, imag);
3885 // finished.
3886}
3887
3888/** evaluates perturbed energy functional
3889 * \param norm norm of current Psi in functional
3890 * \param *params void-pointer to parameter array
3891 * \return evaluated functional at f(x) with \a norm
3892 */
3893double perturbed_function (double norm, void *params) {
3894 struct Problem *P = (struct Problem *)params;
3895 int i, n = P->R.LevS->MaxG;
3896 double old_norm = GramSchGetNorm2(P,P->R.LevS,P->R.LevS->LPsi->LocalPsi[P->R.ActualLocalPsiNo]);
3897 fftw_complex *currentPsi = P->R.LevS->LPsi->LocalPsi[P->R.ActualLocalPsiNo];
3898 fprintf(stderr,"(%i) perturbed_function: setting norm to %lg ...", P->Par.me, norm);
3899 // set desired norm for current Psi
3900 for (i=0; i< n; i++) {
3901 currentPsi[i].re *= norm/old_norm; // real part
3902 currentPsi[i].im *= norm/old_norm; // imaginary part
3903 }
3904 P->R.PsiStep = 0; // make it not advance to next Psi
3905
3906 //debug(P,"UpdateActualPsiNo");
3907 UpdateActualPsiNo(P, P->R.CurrentMin); // orthogonalize
3908 //debug(P,"UpdateEnergyArray");
3909 UpdateEnergyArray(P); // shift energy values in their array by one
3910 //debug(P,"UpdatePerturbedEnergyCalculation");
3911 UpdatePerturbedEnergyCalculation(P); // re-calc energies (which is hopefully lower)
3912 EnergyAllReduce(P); // gather from all processes and sum up to total energy
3913/*
3914 for (i=0; i< n; i++) {
3915 currentPsi[i].re /= norm/old_norm; // real part
3916 currentPsi[i].im /= norm/old_norm; // imaginary part
3917 }*/
3918
3919 fprintf(stderr,"%lg\n", P->Lat.E->TotalEnergy[0]);
3920 return P->Lat.E->TotalEnergy[0]; // and return evaluated functional
3921}
3922
3923/** evaluates perturbed energy functional.
3924 * \param *x current position in functional
3925 * \param *params void-pointer to parameter array
3926 * \return evaluated functional at f(x)
3927 */
3928double perturbed_f (const gsl_vector *x, void *params) {
3929 struct Problem *P = (struct Problem *)params;
3930 int i, n = P->R.LevS->MaxG*2;
3931 fftw_complex *currentPsi = P->R.LevS->LPsi->LocalPsi[P->R.ActualLocalPsiNo];
3932 //int diff = 0;
3933 //debug(P,"f");
3934 // put x into current Psi
3935 for (i=0; i< n; i+=2) {
3936 //if ((currentPsi[i/2].re != gsl_vector_get (x, i)) || (currentPsi[i/2].im != gsl_vector_get (x, i+1))) diff++;
3937 currentPsi[i/2].re = gsl_vector_get (x, i); // real part
3938 currentPsi[i/2].im = gsl_vector_get (x, i+1); // imaginary part
3939 }
3940 //if (diff) fprintf(stderr,"(%i) %i differences between old and new currentPsi.\n", P->Par.me, diff);
3941 P->R.PsiStep = 0; // make it not advance to next Psi
3942
3943 //debug(P,"UpdateActualPsiNo");
3944 UpdateActualPsiNo(P, P->R.CurrentMin); // orthogonalize
3945 //debug(P,"UpdateEnergyArray");
3946 UpdateEnergyArray(P); // shift energy values in their array by one
3947 //debug(P,"UpdatePerturbedEnergyCalculation");
3948 UpdatePerturbedEnergyCalculation(P); // re-calc energies (which is hopefully lower)
3949 EnergyAllReduce(P); // gather from all processes and sum up to total energy
3950
3951 return P->Lat.E->TotalEnergy[0]; // and return evaluated functional
3952}
3953
3954/** evaluates perturbed energy gradient.
3955 * \param *x current position in functional
3956 * \param *params void-pointer to parameter array
3957 * \param *g array for gradient vector on return
3958 */
3959void perturbed_df (const gsl_vector *x, void *params, gsl_vector *g) {
3960 struct Problem *P = (struct Problem *)params;
3961 int i, n = P->R.LevS->MaxG*2;
3962 fftw_complex *currentPsi = P->R.LevS->LPsi->LocalPsi[P->R.ActualLocalPsiNo];
3963 fftw_complex *gradient = P->Grad.GradientArray[ActualGradient];
3964 //int diff = 0;
3965 //debug(P,"df");
3966 // put x into current Psi
3967 for (i=0; i< n; i+=2) {
3968 //if ((currentPsi[i/2].re != gsl_vector_get (x, i)) || (currentPsi[i/2].im != gsl_vector_get (x, i+1))) diff++;
3969 currentPsi[i/2].re = gsl_vector_get (x, i); // real part
3970 currentPsi[i/2].im = gsl_vector_get (x, i+1); // imaginary part
3971 }
3972 //if (diff) fprintf(stderr,"(%i) %i differences between old and new currentPsi.\n", P->Par.me, diff);
3973 P->R.PsiStep = 0; // make it not advance to next Psi
3974
3975 //debug(P,"UpdateActualPsiNo");
3976 UpdateActualPsiNo(P, P->R.CurrentMin); // orthogonalize
3977 //debug(P,"UpdateEnergyArray");
3978 UpdateEnergyArray(P); // shift energy values in their array by one
3979 //debug(P,"UpdatePerturbedEnergyCalculation");
3980 UpdatePerturbedEnergyCalculation(P); // re-calc energies (which is hopefully lower)
3981 EnergyAllReduce(P); // gather from all processes and sum up to total energy
3982
3983 // checkout gradient
3984 //diff = 0;
3985 for (i=0; i< n; i+=2) {
3986 //if ((-gradient[i/2].re != gsl_vector_get (g, i)) || (-gradient[i/2].im != gsl_vector_get (g, i+1))) diff++;
3987 gsl_vector_set (g, i, -gradient[i/2].re); // real part
3988 gsl_vector_set (g, i+1, -gradient[i/2].im); // imaginary part
3989 }
3990 //if (diff) fprintf(stderr,"(%i) %i differences between old and new gradient.\n", P->Par.me, diff);
3991}
3992
3993/** evaluates perturbed energy functional and gradient.
3994 * \param *x current position in functional
3995 * \param *params void-pointer to parameter array
3996 * \param *f pointer to energy function value on return
3997 * \param *g array for gradient vector on return
3998 */
3999void perturbed_fdf (const gsl_vector *x, void *params, double *f, gsl_vector *g) {
4000 struct Problem *P = (struct Problem *)params;
4001 int i, n = P->R.LevS->MaxG*2;
4002 fftw_complex *currentPsi = P->R.LevS->LPsi->LocalPsi[P->R.ActualLocalPsiNo];
4003 fftw_complex *gradient = P->Grad.GradientArray[ActualGradient];
4004 //int diff = 0;
4005 //debug(P,"fdf");
4006 // put x into current Psi
4007 for (i=0; i< n; i+=2) {
4008 //if ((currentPsi[i/2].re != gsl_vector_get (x, i)) || (currentPsi[i/2].im != gsl_vector_get (x, i+1))) diff++;
4009 currentPsi[i/2].re = gsl_vector_get (x, i); // real part
4010 currentPsi[i/2].im = gsl_vector_get (x, i+1); // imaginary part
4011 }
4012 //if (diff) fprintf(stderr,"(%i) %i differences between old and new currentPsi.\n", P->Par.me, diff);
4013 P->R.PsiStep = 0; // make it not advance to next Psi
4014
4015 //debug(P,"UpdateActualPsiNo");
4016 UpdateActualPsiNo(P, P->R.CurrentMin); // orthogonalize
4017 //debug(P,"UpdateEnergyArray");
4018 UpdateEnergyArray(P); // shift energy values in their array by one
4019 //debug(P,"UpdatePerturbedEnergyCalculation");
4020 UpdatePerturbedEnergyCalculation(P); // re-calc energies (which is hopefully lower)
4021 EnergyAllReduce(P); // gather from all processes and sum up to total energy
4022
4023 // checkout gradient
4024 //diff = 0;
4025 for (i=0; i< n; i+=2) {
4026 //if ((-gradient[i/2].re != gsl_vector_get (g, i)) || (-gradient[i/2].im != gsl_vector_get (g, i+1))) diff++;
4027 gsl_vector_set (g, i, -gradient[i/2].re); // real part
4028 gsl_vector_set (g, i+1, -gradient[i/2].im); // imaginary part
4029 }
4030 //if (diff) fprintf(stderr,"(%i) %i differences between old and new gradient.\n", P->Par.me, diff);
4031
4032 *f = P->Lat.E->TotalEnergy[0]; // and return evaluated functional
4033}
4034
4035/* MinimisePerturbed with all the brent minimisation approach
4036void MinimisePerturbed (struct Problem *P, int *Stop, int *SuperStop) {
4037 struct RunStruct *R = &P->R;
4038 struct Lattice *Lat = &P->Lat;
4039 struct Psis *Psi = &Lat->Psi;
4040 int type;
4041 //int i;
4042
4043 // stuff for GSL minimization
4044 //size_t iter;
4045 //int status, Status
4046 int n = R->LevS->MaxG*2;
4047 const gsl_multimin_fdfminimizer_type *T_multi;
4048 const gsl_min_fminimizer_type *T;
4049 gsl_multimin_fdfminimizer *s_multi;
4050 gsl_min_fminimizer *s;
4051 gsl_vector *x;//, *ss;
4052 gsl_multimin_function_fdf my_func;
4053 gsl_function F;
4054 //fftw_complex *currentPsi;
4055 //double a,b,m, f_m, f_a, f_b;
4056 //double old_norm;
4057
4058 my_func.f = &perturbed_f;
4059 my_func.df = &perturbed_df;
4060 my_func.fdf = &perturbed_fdf;
4061 my_func.n = n;
4062 my_func.params = P;
4063 F.function = &perturbed_function;
4064 F.params = P;
4065
4066 x = gsl_vector_alloc (n);
4067 //ss = gsl_vector_alloc (Psi->NoOfPsis);
4068 T_multi = gsl_multimin_fdfminimizer_vector_bfgs;
4069 s_multi = gsl_multimin_fdfminimizer_alloc (T_multi, n);
4070 T = gsl_min_fminimizer_brent;
4071 s = gsl_min_fminimizer_alloc (T);
4072
4073 for (type=Perturbed_P0;type<=Perturbed_RxP2;type++) { // go through each perturbation group separately //
4074 *Stop=0; // reset stop flag
4075 fprintf(stderr,"(%i)Beginning perturbed minimisation of type %s ...\n", P->Par.me, R->MinimisationName[type]);
4076 //OutputOrbitalPositions(P, Occupied);
4077 R->PsiStep = R->MaxPsiStep; // reset in-Psi-minimisation-counter, so that we really advance to the next wave function
4078 UpdateActualPsiNo(P, type); // step on to next perturbed one
4079 fprintf(stderr, "(%i) Re-initializing perturbed psi array for type %s ", P->Par.me, R->MinimisationName[type]);
4080 if (P->Call.ReadSrcFiles && ReadSrcPsiDensity(P,type,1, R->LevSNo)) {
4081 SpeedMeasure(P, InitSimTime, StartTimeDo);
4082 fprintf(stderr,"from source file of recent calculation\n");
4083 ReadSrcPsiDensity(P,type, 0, R->LevSNo);
4084 ResetGramSchTagType(P, Psi, type, IsOrthogonal); // loaded values are orthonormal
4085 SpeedMeasure(P, DensityTime, StartTimeDo);
4086 //InitDensityCalculation(P);
4087 SpeedMeasure(P, DensityTime, StopTimeDo);
4088 R->OldActualLocalPsiNo = R->ActualLocalPsiNo; // needed otherwise called routines in function below crash
4089 UpdateGramSchOldActualPsiNo(P,Psi);
4090 InitPerturbedEnergyCalculation(P, 1); // go through all orbitals calculate each H^{(0)}-eigenvalue, recalc HGDensity, cause InitDensityCalc zero'd it
4091 UpdatePerturbedEnergyCalculation(P); // H1cGradient and Gradient must be current ones
4092 EnergyAllReduce(P); // gather energies for minimum search
4093 SpeedMeasure(P, InitSimTime, StopTimeDo);
4094 }
4095 if (P->Call.ReadSrcFiles != 1) {
4096 SpeedMeasure(P, InitSimTime, StartTimeDo);
4097 ResetGramSchTagType(P, Psi, type, NotOrthogonal); // perturbed now shall be orthonormalized
4098 if (P->Call.ReadSrcFiles != 2) {
4099 if (R->LevSNo == Lat->MaxLevel-1) { // is it the starting level? (see InitRunLevel())
4100 fprintf(stderr, "randomly.\n");
4101 InitPsisValue(P, Psi->TypeStartIndex[type], Psi->TypeStartIndex[type+1]); // initialize perturbed array for this run
4102 } else {
4103 fprintf(stderr, "from source file of last level.\n");
4104 ReadSrcPerturbedPsis(P, type);
4105 }
4106 }
4107 SpeedMeasure(P, InitGramSchTime, StartTimeDo);
4108 GramSch(P, R->LevS, Psi, Orthogonalize);
4109 SpeedMeasure(P, InitGramSchTime, StopTimeDo);
4110 SpeedMeasure(P, InitDensityTime, StartTimeDo);
4111 //InitDensityCalculation(P);
4112 SpeedMeasure(P, InitDensityTime, StopTimeDo);
4113 InitPerturbedEnergyCalculation(P, 1); // go through all orbitals calculate each H^{(0)}-eigenvalue, recalc HGDensity, cause InitDensityCalc zero'd it
4114 R->OldActualLocalPsiNo = R->ActualLocalPsiNo; // needed otherwise called routines in function below crash
4115 UpdateGramSchOldActualPsiNo(P,Psi);
4116 UpdatePerturbedEnergyCalculation(P); // H1cGradient and Gradient must be current ones
4117 EnergyAllReduce(P); // gather energies for minimum search
4118 SpeedMeasure(P, InitSimTime, StopTimeDo);
4119 R->LevS->Step++;
4120 EnergyOutput(P,0);
4121 while (*Stop != 1) {
4122 // copy current Psi into starting vector
4123 currentPsi = R->LevS->LPsi->LocalPsi[R->ActualLocalPsiNo];
4124 for (i=0; i< n; i+=2) {
4125 gsl_vector_set (x, i, currentPsi[i/2].re); // real part
4126 gsl_vector_set (x, i+1, currentPsi[i/2].im); // imaginary part
4127 }
4128 gsl_multimin_fdfminimizer_set (s_multi, &my_func, x, 0.01, 1e-2);
4129 iter = 0;
4130 status = 0;
4131 do { // look for minimum along current local psi
4132 iter++;
4133 status = gsl_multimin_fdfminimizer_iterate (s_multi);
4134 MPI_Allreduce(&status, &Status, 1, MPI_INT, MPI_MAX, P->Par.comm_ST_Psi);
4135 if (Status)
4136 break;
4137 status = gsl_multimin_test_gradient (s_multi->gradient, 1e-2);
4138 MPI_Allreduce(&status, &Status, 1, MPI_INT, MPI_MAX, P->Par.comm_ST_Psi);
4139 //if (Status == GSL_SUCCESS)
4140 //printf ("Minimum found at:\n");
4141 if (P->Par.me == 0) fprintf (stderr,"(%i,%i,%i)S(%i,%i,%i):\t %5d %10.5f\n",P->Par.my_color_comm_ST,P->Par.me_comm_ST, P->Par.me_comm_ST_PsiT, R->MinStep, R->ActualLocalPsiNo, R->PsiStep, (int)iter, s_multi->f);
4142 //TestGramSch(P,R->LevS,Psi, type); // functions are orthonormal?
4143 } while (Status == GSL_CONTINUE && iter < 3);
4144 // now minimize norm of currentPsi (one-dim)
4145 if (0) {
4146 iter = 0;
4147 status = 0;
4148 m = 1.;
4149 a = MYEPSILON;
4150 b = 100.;
4151 f_a = perturbed_function (a, P);
4152 f_b = perturbed_function (b, P);
4153 f_m = perturbed_function (m, P);
4154 //if ((f_m < f_a) && (f_m < f_b)) {
4155 gsl_min_fminimizer_set (s, &F, m, a, b);
4156 do { // look for minimum along current local psi
4157 iter++;
4158 status = gsl_min_fminimizer_iterate (s);
4159 m = gsl_min_fminimizer_x_minimum (s);
4160 a = gsl_min_fminimizer_x_lower (s);
4161 b = gsl_min_fminimizer_x_upper (s);
4162 status = gsl_min_test_interval (a, b, 0.001, 0.0);
4163 if (status == GSL_SUCCESS)
4164 printf ("Minimum found at:\n");
4165 printf ("%5d [%.7f, %.7f] %.7f %.7f\n",
4166 (int) iter, a, b,
4167 m, b - a);
4168 } while (status == GSL_CONTINUE && iter < 100);
4169 old_norm = GramSchGetNorm2(P,P->R.LevS,P->R.LevS->LPsi->LocalPsi[P->R.ActualLocalPsiNo]);
4170 for (i=0; i< n; i++) {
4171 currentPsi[i].re *= m/old_norm; // real part
4172 currentPsi[i].im *= m/old_norm; // imaginary part
4173 }
4174 } else debug(P,"Norm not minimizable!");
4175 //P->R.PsiStep = P->R.MaxPsiStep; // make it advance to next Psi
4176 FindPerturbedMinimum(P);
4177 //debug(P,"UpdateActualPsiNo");
4178 UpdateActualPsiNo(P, type); // step on to next perturbed Psi
4179 //debug(P,"UpdateEnergyArray");
4180 UpdateEnergyArray(P); // shift energy values in their array by one
4181 //debug(P,"UpdatePerturbedEnergyCalculation");
4182 UpdatePerturbedEnergyCalculation(P); // re-calc energies (which is hopefully lower)
4183 EnergyAllReduce(P); // gather from all processes and sum up to total energy
4184 //ControlNativeDensity(P); // check total density (summed up PertMixed must be zero!)
4185 //printf ("(%i,%i,%i)S(%i,%i,%i):\t %5d %10.5f\n",P->Par.my_color_comm_ST,P->Par.me_comm_ST, P->Par.me_comm_ST_PsiT, R->MinStep, R->ActualLocalPsiNo, R->PsiStep, (int)iter, s_multi->f);
4186 if (*SuperStop != 1)
4187 *SuperStop = CheckCPULIM(P);
4188 *Stop = CalculateMinimumStop(P, *SuperStop);
4189 P->Speed.Steps++; // step on
4190 R->LevS->Step++;
4191 }
4192 // now release normalization condition and minimize wrt to norm
4193 *Stop = 0;
4194 while (*Stop != 1) {
4195 currentPsi = R->LevS->LPsi->LocalPsi[R->ActualLocalPsiNo];
4196 iter = 0;
4197 status = 0;
4198 m = 1.;
4199 a = 0.001;
4200 b = 10.;
4201 f_a = perturbed_function (a, P);
4202 f_b = perturbed_function (b, P);
4203 f_m = perturbed_function (m, P);
4204 if ((f_m < f_a) && (f_m < f_b)) {
4205 gsl_min_fminimizer_set (s, &F, m, a, b);
4206 do { // look for minimum along current local psi
4207 iter++;
4208 status = gsl_min_fminimizer_iterate (s);
4209 m = gsl_min_fminimizer_x_minimum (s);
4210 a = gsl_min_fminimizer_x_lower (s);
4211 b = gsl_min_fminimizer_x_upper (s);
4212 status = gsl_min_test_interval (a, b, 0.001, 0.0);
4213 if (status == GSL_SUCCESS)
4214 printf ("Minimum found at:\n");
4215 printf ("%5d [%.7f, %.7f] %.7f %.7f\n",
4216 (int) iter, a, b,
4217 m, b - a);
4218 } while (status == GSL_CONTINUE && iter < 100);
4219 old_norm = GramSchGetNorm2(P,P->R.LevS,P->R.LevS->LPsi->LocalPsi[P->R.ActualLocalPsiNo]);
4220 for (i=0; i< n; i++) {
4221 currentPsi[i].re *= m/old_norm; // real part
4222 currentPsi[i].im *= m/old_norm; // imaginary part
4223 }
4224 }
4225 P->R.PsiStep = P->R.MaxPsiStep; // make it advance to next Psi
4226 //debug(P,"UpdateActualPsiNo");
4227 UpdateActualPsiNo(P, type); // step on to next perturbed Psi
4228 if (*SuperStop != 1)
4229 *SuperStop = CheckCPULIM(P);
4230 *Stop = CalculateMinimumStop(P, *SuperStop);
4231 P->Speed.Steps++; // step on
4232 R->LevS->Step++;
4233 }
4234 if(P->Call.out[NormalOut]) fprintf(stderr,"(%i) Write %s srcpsi to disk\n", P->Par.me, R->MinimisationName[type]);
4235 OutputSrcPsiDensity(P, type);
4236// if (!TestReadnWriteSrcDensity(P,type))
4237// Error(SomeError,"TestReadnWriteSrcDensity failed!");
4238 }
4239
4240 TestGramSch(P,R->LevS,Psi, type); // functions are orthonormal?
4241 // calculate current density summands
4242 //if (P->Call.out[StepLeaderOut]) fprintf(stderr,"(%i) Filling current density grid ...\n",P->Par.me);
4243 SpeedMeasure(P, CurrDensTime, StartTimeDo);
4244 if (*SuperStop != 1) {
4245 if ((R->DoFullCurrent == 1) || ((R->DoFullCurrent == 2) && (CheckOrbitalOverlap(P) == 1))) { //test to check whether orbitals have mutual overlap and thus \\DeltaJ_{xc} must not be dropped
4246 R->DoFullCurrent = 1; // set to 1 if it was 2 but Check...() yielded necessity
4247 //debug(P,"Filling with Delta j ...");
4248 //FillDeltaCurrentDensity(P);
4249 }// else
4250 //debug(P,"There is no overlap between orbitals.");
4251 //debug(P,"Filling with j ...");
4252 FillCurrentDensity(P);
4253 }
4254 SpeedMeasure(P, CurrDensTime, StopTimeDo);
4255
4256 SetGramSchExtraPsi(P,Psi,NotUsedToOrtho); // remove extra Psis from orthogonality check
4257 ResetGramSchTagType(P, Psi, type, NotUsedToOrtho); // remove this group from the check for the next minimisation group as well!
4258 }
4259 UpdateActualPsiNo(P, Occupied); // step on back to an occupied one
4260
4261 gsl_multimin_fdfminimizer_free (s_multi);
4262 gsl_min_fminimizer_free (s);
4263 gsl_vector_free (x);
4264 //gsl_vector_free (ss);
4265}
4266*/
Note: See TracBrowser for help on using the repository browser.