source: pcp/src/perturbed.c@ 807e8a

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

libblas needed for libgsl is now found via AC_SEARCH_LIB and list blas cblas gslblas gslcblas

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