source: pcp/src/output.c@ e24bbb

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

PlotRealDensity(): nodes now calculated via ReciBasis-matrix multiplication

We introcude cartesian coordinate in the config file which is then transformed to integer cell (node) coordinate.

  • Property mode set to 100644
File size: 84.6 KB
Line 
1/** \file output.c
2 * Output of forces and energies, Visuals (density and ions for OpenDX).
3 *
4 * Herein all the functions concerning the output of data are gathered:
5 * Initialization of the FileData structure InitOutVisArray() and the files themselves
6 * InitOutputFiles(),
7 * Saving of a calculated state OutputVisSrcFiles() - 1. Psis OutputSrcPsiDensity(), 2. Ions
8 * OutSrcIons() - or retrieving Psis ReadSrcPsiDensity() and Ions ReadSrcIons(),
9 * Preparation (in case of RiemannTensor use) CalculateOutVisDensityPos(), OutVisPosRTransformPosNFRto0(),
10 * Output of visual data (for OpenDx explorer) OutputVis() - 1. OpenDX files CreateDensityOutputGeneral(),
11 * 2. electronic densitiy data OutVisDensity() (uses MPI sending density coefficients OutputOutVisDensity()
12 * and receiving and writing CombineOutVisDensity()), 3. ion data OutVisIons() -
13 * Closing of files CloseOutputFiles().
14 *
15 * There are some more routines: OutputCurrentDensity() outputs the current density for each magnetic field
16 * direction, OutputVisAllOrbital() outputs all orbitals of a certain minimsation type and
17 * TestReadnWriteSrcDensity() checks whether a certain minimisation group can be written and read again
18 * correctly.
19 *
20 * There are some helpers that open files with certain filenames, making extensive usage of all
21 * the suffixes defined in here: OpenFile(), OpenFileNo(), OpenFileNo2(), OpenFileNoNo() and
22 * OpenFileNoPost().
23 *
24 *
25 Project: ParallelCarParrinello
26 \author Jan Hamaekers, Frederik Heber
27 \date 2000, 2006
28
29 File: output.c
30 $Id: output.c,v 1.51.2.2 2007-04-21 12:55:50 foo Exp $
31*/
32#include<stdlib.h>
33#include<stdio.h>
34#include<string.h>
35#include<math.h>
36//#include<sys/time.h>
37#include <time.h>
38#include<unistd.h>
39#include"data.h"
40#include"density.h"
41#include"errors.h"
42#include"gramsch.h"
43#include"helpers.h"
44#include "init.h"
45#include"myfft.h"
46#include"mymath.h"
47#include"output.h"
48#include"pdbformat.h"
49#include"perturbed.h"
50
51
52/* Konvention: Rueckgabe 0 einer Funktion, bedeutet keinen Fehler (entsprechend exitcode 0) */
53/* Oeffnet Datei P->mainname+"..." mit what*/
54
55/** Opens a file with FileData::mainname+suffix.
56 * Both the path under which the main programme resides and the path to the config file are
57 * tried subsequently.
58 * \param *P Problem at hand
59 * \param **file file pointer array
60 * \param *suffix suffix after mainname
61 * \param *what access parameter for the file: r, w, rw, r+, w+ ...
62 * \param verbose 1 - print status, 0 - don't print anything
63 * \return 0 - could not open file, 1 - file is open
64 */
65int OpenFile(struct Problem *P, FILE** file, const char* suffix, const char* what, int verbose)
66{
67 char* name; /* Zu erzeugender Dateiname */
68 name = (char*)
69 Malloc(strlen(P->Files.default_path) + strlen(P->Files.mainname) + strlen(suffix) + 1,"OpenFile");
70 sprintf(name, "%s%s%s", P->Files.default_path, P->Files.mainname, suffix);
71 *file = fopen(name, what);
72 if (*file == NULL) {
73 fprintf(stderr,"(%i) Normal access failed: name %s, what %s\n", P->Par.me, name, what);
74 /* hier default file ausprobieren, falls nur gelesen werden soll! */
75 if(*what == 'r') {
76 name = (char*)
77 Realloc(name,strlen(P->Files.default_path) + strlen(suffix)+1,"OpenFile");
78 sprintf(name, "%s%s", P->Files.default_path,suffix);
79 *file = fopen(name,what);
80 if (*file != NULL) {
81 if (verbose) fprintf(stderr,"(%i) Default file is open: %s\n",P->Par.me,name);
82 Free(name, "OpenFile: name");
83 return(1);
84 } else {
85 fprintf(stderr,"\n(%i)Error: Cannot open neither normal nor default file for reading: %s\n",P->Par.me,name);
86 Free(name, "OpenFile: name");
87 return(0);
88 }
89 } else {
90 fprintf(stderr,"\n(%i)Error: Cannot open normal file for writing: %s\n",P->Par.me,name);
91 Free(name, "OpenFile: name");
92 return(0);
93 }
94 } else {
95 if(verbose) fprintf(stderr,"(%i) File is open: %s\n",P->Par.me,name);
96 Free(name, "OpenFile: name");
97 return(1);
98 }
99}
100
101/** Opens a file with FileData::mainname+suffix+"."+No (2 digits).
102 * Both the path under which the main programme resides and the path to the config file are
103 * tried subsequently.
104 * \param *P Problem at hand
105 * \param **file file pointer array
106 * \param *suffix suffix after mainname
107 * \param No the number with up to two digits
108 * \param *what access parameter for the file: r, w, rw, r+, w+ ...
109 * \param verbose 1 - print status, 0 - don't print anything
110 * \return 0 - could not open file, 1 - file is open
111 */
112int OpenFileNo2(struct Problem *P, FILE** file, const char* suffix, int No, const char* what, int verbose)
113{
114 char* name; /* Zu erzeugender Dateiname */
115 name = (char*)
116 Malloc(strlen(P->Files.default_path) + strlen(P->Files.mainname) + strlen(suffix) + 4,"OpenFile");
117 sprintf(name, "%s%s%s.%02i", P->Files.default_path, P->Files.mainname, suffix, No);
118 *file = fopen(name, what);
119 if (*file == NULL) {
120 /* falls nur gelesen wird, auch default_path ausprobieren */
121 if (*what == 'r') {
122 name = (char*)
123 Realloc(name,strlen(P->Files.default_path) + strlen(suffix) + 4,"OpenFileNo2");
124 sprintf(name, "%s%s.%02i", P->Files.default_path, suffix, No);
125 *file = fopen(name, what);
126 if (*file != NULL) {
127 if(verbose) fprintf(stderr,"(%i) Default file is open: %s\n", P->Par.me, name);
128 Free(name, "OpenFileNo2: name");
129 return(1);
130 }
131 }
132 fprintf(stderr,"\n(%i)Error: Cannot open file: %s\n", P->Par.me, name);
133 Free(name, "OpenFileNo2: name");
134 return(0);
135 } else {
136 if(verbose) fprintf(stderr,"(%i) File is open: %s\n",P->Par.me, name);
137 Free(name, "OpenFileNo2: name");
138 return(1);
139 }
140}
141
142/* Oeffnet Datei P->Files.mainname+"...".Nr(4stellig) mit what*/
143/** Opens a file with FileData::mainname+suffix+"."+No (4 digits).
144 * Both the path under which the main programme resides and the path to the config file are
145 * tried subsequently.
146 * \param *P Problem at hand
147 * \param **file file pointer array
148 * \param *suffix suffix after mainname
149 * \param No the number with up to four digits
150 * \param *what access parameter for the file: r, w, rw, r+, w+ ...
151 * \param verbose 1 - print status, 0 - don't print anything
152 * \return 0 - could not open file, 1 - file is open
153 */
154int OpenFileNo(struct Problem *P, FILE** file, const char* suffix, int No, const char* what, int verbose)
155{
156 char* name; /* Zu erzeugender Dateiname */
157 name = (char*)
158 Malloc(strlen(P->Files.default_path) + strlen(P->Files.mainname) + strlen(suffix) + 6,"OpenFileNo");
159 sprintf(name, "%s%s%s.%04i", P->Files.default_path, P->Files.mainname, suffix, No);
160 *file = fopen(name, what);
161 if (*file == NULL) {
162 /* falls nur gelesen wird, auch default_path ausprobieren */
163 if (*what == 'r') {
164 name = (char*)
165 Realloc(name,strlen(P->Files.default_path) + strlen(suffix) + 6,"OpenFileNo");
166 sprintf(name, "%s%s.%04i", P->Files.default_path, suffix, No);
167 *file = fopen(name, what);
168 if (*file != NULL) {
169 if(verbose) fprintf(stderr,"(%i) Default file is open: %s\n", P->Par.me, name);
170 Free(name, "OpenFileNo: name");
171 return(1);
172 }
173 }
174 fprintf(stderr,"\n(%i)Error: Cannot open file: %s\n", P->Par.me, name);
175 Free(name, "OpenFileNo: name");
176 return(0);
177 } else {
178 if(verbose) fprintf(stderr,"(%i) File is open: %s\n", P->Par.me, name);
179 Free(name, "OpenFileNo: name");
180 return(1);
181 }
182}
183
184/* die nachfolgende Routine wird von seq und par benutzt!!! */
185/* Oeffnet Datei P->Files.mainname+"...".No.postfix mit what*/
186
187/** Opens a file with FileData::mainname+suffix+"."+No(4 digits)+postfix.
188 * Only the path under which the main programme resides is tried.
189 * \param *P Problem at hand
190 * \param **file file pointer array
191 * \param *suffix suffix after mainname
192 * \param No the number with up to four digits
193 * \param *postfix post-suffix at the end of filename
194 * \param *what access parameter for the file: r, w, rw, r+, w+ ...
195 * \param verbose 1 - print status, 0 - don't print anything
196 * \return 0 - could not open file, 1 - file is open
197 */
198int OpenFileNoPost(struct Problem *P, FILE** file, const char* suffix, int No, const char* postfix, const char* what, int verbose)
199{
200 char* name; /* Zu erzeugender Dateiname */
201 name = (char*)
202 Malloc(strlen(P->Files.default_path) + strlen(P->Files.mainname) + strlen(postfix) + strlen(suffix) + 6,"OpenFileNoPost");
203 sprintf(name, "%s%s%s.%04i%s", P->Files.default_path, P->Files.mainname, suffix, No, postfix);
204 *file = fopen(name, what);
205 if (*file == NULL) {
206 fprintf(stderr,"\n(%i)Error: Cannot open file: %s\n", P->Par.me, name);
207 Free(name, "OpenFileNoPost: name");
208 return(0);
209 } else {
210 if(verbose) fprintf(stderr,"(%i) File is open: %s\n", P->Par.me, name);
211 Free(name, "OpenFileNoPost: name");
212 return(1);
213 }
214}
215
216/** Opens a file with FileData::mainname+suffix+"."+No1(4 digits)+"."+No2(4 digits).
217 * Only the path under which the main programme resides is tried.
218 * \param *P Problem at hand
219 * \param **file file pointer array
220 * \param *suffix suffix after mainname
221 * \param No1 first number with up to four digits
222 * \param No2 second number with up to four digits
223 * \param *what access parameter for the file: r, w, rw, r+, w+ ...
224 * \param verbose 1 - print status, 0 - don't print anything
225 * \return 0 - could not open file, 1 - file is open
226 */
227int OpenFileNoNo(struct Problem *P, FILE** file, const char* suffix, int No1, int No2, const char* what, int verbose)
228{ /* zuerst suffix; No1: lfd, No2: procId */
229 char *name;
230 name = (char*)
231 Malloc(strlen(P->Files.default_path) + strlen(P->Files.mainname) + strlen(suffix) + 5 + 6 + 1,"OpenFileNoNo");
232 sprintf(name,"%s%s%s.%04i.%04i", P->Files.default_path, P->Files.mainname, suffix, No1, No2);
233 *file = fopen(name, what);
234 if(*file == NULL) {
235 fprintf(stderr,"\n(%i)Error: Cannot open file: %s\n", P->Par.me, name);
236 Free(name, "OpenFileNoNo: name");
237 return(0);
238 }
239 if(verbose) fprintf(stderr,"(%i) File is open: %s\n", P->Par.me, name);
240 Free(name, "OpenFileNoNo: name");
241 return(1);
242}
243
244/** Transformation of wave function of Riemann level to zeroth before output as Vis.
245 * \param *RT RiemannTensor structure, contains RiemannTensor::LatticeLevel
246 * \param *source source wave function array
247 * \param *dest destination wave function array
248 * \param NF dimensional factor (NDIM, generally 3)
249 */
250static void OutVisPosRTransformPosNFRto0(const struct RiemannTensor *RT, fftw_real *source, fftw_real *dest, const int NF)
251{
252 struct LatticeLevel *Lev = RT->LevR;
253 int es = Lev->NUp0[2]*NF;
254 unsigned int cpyes = sizeof(fftw_real)*es;
255 int nx=Lev->Plan0.plan->local_nx,ny=Lev->N[1],nz=Lev->N[2],Nx=Lev->NUp0[0],Ny=Lev->NUp0[1];
256 int lx,ly,z,Lx,Ly;
257 for(lx=0; lx < nx; lx++)
258 for(Lx=0; Lx < Nx; Lx++)
259 for(ly=0; ly < ny; ly++)
260 for(Ly=0; Ly < Ny; Ly++)
261 for(z=0; z < nz; z++) {
262 memcpy( &dest[es*(z+nz*(Ly+Ny*(ly+ny*(Lx+Nx*lx))))],
263 &source[es*(Ly+Ny*(Lx+Nx*(z+nz*(ly+ny*lx))))],
264 cpyes);
265 }
266}
267
268
269/** prints Norm of each wave function to screen.
270 * \param *out output destination (eg. stdout)
271 * \param *P Problem at hand
272 */
273void OutputNorm (FILE *out, struct Problem *P) {
274 struct Lattice *Lat = &P->Lat;
275 struct Psis *Psi = &Lat->Psi;
276 struct LatticeLevel *Lev = P->R.LevS;
277 struct OnePsiElement *OnePsi, *LOnePsi;
278 int i;
279
280 for (i=0;i<Psi->MaxPsiOfType+P->Par.Max_me_comm_ST_PsiT;i++) {
281 OnePsi =&Psi->AllPsiStatus[i];
282 if (OnePsi->my_color_comm_ST_Psi == P->Par.my_color_comm_ST_Psi) { // local one
283 LOnePsi = &Psi->LocalPsiStatus[OnePsi->MyLocalNo];
284 fprintf(out,"(%i) Norm of Psi %i: %e\n", P->Par.me, OnePsi->MyGlobalNo, GramSchGetNorm2(P,Lev,Lev->LPsi->LocalPsi[OnePsi->MyLocalNo]));
285 }
286 }
287}
288
289/** Output of current Psis state of source level RunStruct::LevS's LevelPsi::LocalPsi to file \ref suffixsrcpsidat.
290 * In case of process 0, the doc file is written in a parsable way with minimisation type RunStruct#CurrentMin, level number
291 * LatticeLevel#LevelNo, and number of grid nodes LatticeLevel#N.
292 * The two (three) different Psis::SpinType's are discerned and in case of SpinUpDown separate data files opened.
293 *
294 * Then for every global wave function of the desired type each coefficient of the reciprocal grid is copied into
295 * Density::DensityCArray[TempDensity], fouriertransformed, copied into Density::DensityArray[TempDensity].
296 *
297 * As the file output is only handled by process 0, all other coefficient shares are sent within the Psi group to which
298 * the current global wave function belongs to process 0 of the Psi group and from there on to process 0 of all via
299 * MPI.
300 * \param *P Problem at hand
301 * \param type Current minimisation state
302 * \note This serves as a backup file, when the process is terminated and one would like to restart it at the
303 * current calculation lateron, see ReadSrcPsiDensity(). Note also that it is not necessary to specify the
304 * same number of processes on later restart, any number may be used under the condition that the number of
305 * grid nodes match and that there 2 sharing wave functions in case of SpinUpDown.
306 * \sa ReadSrcPsiDensity() - same for Reading the coefficients, TestReadnWriteSrcDensity() - checks both routines against
307 * each other
308 */
309void OutputSrcPsiDensity(struct Problem *P, enum PsiTypeTag type)
310{
311 int i,j,k, Index, zahl, owner;
312 struct Lattice *Lat = &P->Lat;
313 //struct RunStruct *R = &P->R;
314 struct fft_plan_3d *plan = Lat->plan;
315 struct LatticeLevel *LevS = P->R.LevS;
316 struct Psis *Psi = &Lat->Psi;
317 fftw_complex *work = (fftw_complex *)LevS->Dens->DensityArray[TempDensity];
318 double *destpsiR = (double *)LevS->Dens->DensityArray[TempDensity];
319 fftw_real *srcpsiR = (fftw_real *)LevS->Dens->DensityCArray[TempDensity];
320 fftw_complex *srcpsiC = (fftw_complex *)LevS->Dens->DensityCArray[TempDensity];
321 FILE *SrcPsiData, *SrcPsiDoc;
322 char suffixdat[255], suffixdoc[255];
323 MPI_Status status;
324 struct OnePsiElement *OnePsiA, *LOnePsiA;
325 fftw_complex *LPsiDatA;
326 int Num = 0, colorNo = 0;
327 int Sent = 0, sent = 0;
328
329 SpeedMeasure(P,ReadnWriteTime,StartTimeDo);
330 sprintf(suffixdat, ".%.254s.L%i", P->R.MinimisationName[type], LevS->LevelNo);
331 strncpy (suffixdoc, suffixdat, 255);
332 // for the various spin cases, output the doc-file if it's process 0
333 if (!(P->Par.me_comm_ST)) {
334 switch (Lat->Psi.PsiST) {
335 case SpinDouble:
336 colorNo = 0;
337 strncat (suffixdat, suffixsrcpsidat, 255-strlen(suffixdat));
338 strncat (suffixdoc, suffixsrcpsidoc, 255-strlen(suffixdoc));
339 Num = Lat->Psi.GlobalNo[PsiMaxNoDouble];
340 break;
341 case SpinUp:
342 colorNo = 0;
343 strncat (suffixdat, suffixsrcpsiupdat, 255-strlen(suffixdat));
344 strncat (suffixdoc, suffixsrcpsiupdoc, 255-strlen(suffixdoc));
345 Num = Lat->Psi.GlobalNo[PsiMaxNoUp];
346 break;
347 case SpinDown:
348 colorNo = 1;
349 strncat (suffixdat, suffixsrcpsidowndat, 255-strlen(suffixdat));
350 strncat (suffixdoc, suffixsrcpsidowndoc, 255-strlen(suffixdoc));
351 Num = Lat->Psi.GlobalNo[PsiMaxNoDown];
352 break;
353 }
354 if (!OpenFileNo(P, &SrcPsiData, suffixdat, colorNo, "wb",P->Call.out[ReadOut])) // open SourcePsiData as write binary
355 fprintf(stderr,"(%i) Error opening file with suffix %s for writing!\n",P->Par.me, suffixdat);
356 if (!(P->Par.my_color_comm_ST_Psi)) { // if we are process 0
357 if (!OpenFile(P, &SrcPsiDoc, suffixdoc, "w",P->Call.out[ReadOut])) // open the (text) doc file
358 fprintf(stderr,"(%i) Error opening file with suffix %s for writing!\n",P->Par.me, suffixdoc);
359 fprintf(SrcPsiDoc, "Mintype\t%i\n", (int)type);
360 fprintf(SrcPsiDoc, "LevelNo\t%i\n", LevS->LevelNo);
361 fprintf(SrcPsiDoc, "GridNodes\t%i\t%i\t%i\n", LevS->N[0], LevS->N[1], LevS->N[2]);
362 fprintf(SrcPsiDoc, "PsiNo\t%i\t%i\n", Num, P->Lat.Psi.GlobalNo[PsiMaxAdd]);
363 fprintf(SrcPsiDoc, "Epsilon\t%lg\t%lg\n", P->R.RelEpsTotalEnergy, P->R.RelEpsKineticEnergy);
364 for (i = 0; i < P->Par.Max_my_color_comm_ST_Psi; i++) {
365 fprintf(SrcPsiDoc, "\t%i", Lat->Psi.RealAllLocalNo[i]); // print number of local Psis
366 }
367 fprintf(SrcPsiDoc, "\n");
368 fclose(SrcPsiDoc);
369 }
370 }
371
372 // send/receive around and write share of coefficient array of each wave function
373 MPI_Allreduce(&sent, &Sent, 1, MPI_INT, MPI_SUM, P->Par.comm_ST); // catch all at the starter line
374 if (P->Call.out[PsiOut]) fprintf(stderr,"(%i) me (%i/%i) \t Psi (%i/%i)\t PsiT (%i/%i)\n", P->Par.me, P->Par.me_comm_ST, P->Par.Max_me_comm_ST, P->Par.me_comm_ST_Psi, P->Par.Max_me_comm_ST_Psi, P->Par.me_comm_ST_PsiT, P->Par.Max_me_comm_ST_PsiT);
375 k = -1; // k is global PsiNo counter for the desired group
376 for (j=0; j < Psi->MaxPsiOfType+P->Par.Max_me_comm_ST_PsiT; j++) { // go through all wave functions (plus the extra one for each process)
377 OnePsiA = &Psi->AllPsiStatus[j]; // grab OnePsiA
378 if (OnePsiA->PsiType == type) { // only take desired minimisation group
379 k++;
380 owner = 0; // notes down in process 0 of each psi group the owner of the next coefficient share
381 //fprintf(stderr,"(%i) ST_Psi: OnePsiA %i\tP->Par.me %i\n", P->Par.me,OnePsiA->my_color_comm_ST_Psi,P->Par.my_color_comm_ST_Psi);
382 if (OnePsiA->my_color_comm_ST_Psi == P->Par.my_color_comm_ST_Psi) { // Belongs to my Psi group?
383 LOnePsiA = &Psi->LocalPsiStatus[OnePsiA->MyLocalNo];
384 LPsiDatA = LevS->LPsi->LocalPsi[OnePsiA->MyLocalNo];
385 SetArrayToDouble0((double *)srcpsiR, LevS->Dens->TotalSize*2); // zero DensityCArray[TempDensity]
386 for (i=0;i<LevS->MaxG;i++) { // for each every unique G grid vector
387 Index = LevS->GArray[i].Index;
388 srcpsiC[Index].re = LPsiDatA[i].re; // copy real value
389 srcpsiC[Index].im = LPsiDatA[i].im; // copy imaginary value
390 }
391 for (i=0; i<LevS->MaxDoubleG; i++) { // also for every doubly appearing G vector (symmetry savings)
392 srcpsiC[LevS->DoubleG[2*i+1]].re = srcpsiC[LevS->DoubleG[2*i]].re;
393 srcpsiC[LevS->DoubleG[2*i+1]].im = -srcpsiC[LevS->DoubleG[2*i]].im;
394 }
395 // do an fft transform from complex to real on these srcPsiR
396 fft_3d_complex_to_real(plan, LevS->LevelNo, FFTNF1, srcpsiC, work);
397
398 for (i=0; i < LevS->Dens->LocalSizeR; i++)
399 destpsiR[i] = (double)srcpsiR[i];
400 } else
401 LOnePsiA = NULL;
402
403 if (P->Par.me_comm_ST == 0) { // if we are process 0 of all, only we may access the file
404 for (i=0; i<P->Par.Max_me_comm_ST_Psi;i++) { // for each share of the coefficient in the PsiGroup
405 if (LOnePsiA == NULL) { // if it's not local, receive coefficients from correct PsiGroup (process 0 within that group)
406 if (MPI_Recv( destpsiR, LevS->Dens->LocalSizeR, MPI_DOUBLE, OnePsiA->my_color_comm_ST_Psi, OutputSrcPsiTag, P->Par.comm_ST_PsiT, &status ) != MPI_SUCCESS)
407 Error(SomeError, "OutputSrcPsiDensity: MPI_Recv of loaded coefficients failed!");
408 MPI_Get_count(&status, MPI_DOUBLE, &zahl);
409 if (zahl != LevS->Dens->LocalSizeR) // check number of elements
410 fprintf(stderr,"(%i)OutputSrcPsiDensity: MPI_Recv of loaded coefficients of GlobalNo %i, owner %i failed: Too few coefficients - %i instead of %i!\n", P->Par.me, k, i, zahl, LevS->Dens->LocalSizeR);
411 //else
412 //fprintf(stderr,"(%i)OutputSrcPsiDensity: MPI_Recv of loaded coefficients of GlobalNo %i, owner %i succeeded!\n", P->Par.me, k, i);
413 } else { // if it's local ...
414 if (i != 0) { // but share of array not for us, receive from owner process within Psi group
415 if (MPI_Recv( destpsiR, LevS->Dens->LocalSizeR, MPI_DOUBLE, i, OutputSrcPsiTag, P->Par.comm_ST_Psi, &status ) != MPI_SUCCESS)
416 Error(SomeError, "OutputSrcPsiDensity: MPI_Recv of loaded coefficients failed!");
417 MPI_Get_count(&status, MPI_DOUBLE, &zahl);
418 if (zahl != LevS->Dens->LocalSizeR) // check number of elements
419 fprintf(stderr,"(%i)OutputSrcPsiDensity: MPI_Recv of loaded coefficients of GlobalNo %i, owner %i failed: Too few coefficients - %i instead of %i!\n", P->Par.me, k, i, zahl, LevS->Dens->LocalSizeR);
420 //else
421 //fprintf(stderr,"(%i)OutputSrcPsiDensity: MPI_Recv of loaded coefficients of GlobalNo %i, owner %i succeeded!\n", P->Par.me, k, i);
422 } // otherwise it was our share already
423 }
424 // store the final share on disc
425 if ((zahl = fwrite(destpsiR, sizeof(double), (size_t)(LevS->Dens->LocalSizeR), SrcPsiData)) != (size_t)(LevS->Dens->LocalSizeR)) {
426 fclose(SrcPsiData);
427 //if (P->Par.me == 0)
428 fprintf(stderr, "(%i)OutputSrcPsiDensity: only %i bytes written instead of expected %i\n", P->Par.me, zahl, LevS->Dens->LocalSizeR);
429 Error(SomeError,"OutputSrcPsiDensity: fwrite Error");
430 }
431 }
432 } else { // if we are not process 0 of all, we are but a deliverer
433 if (LOnePsiA != NULL) { // send if it's local
434 if (P->Par.me_comm_ST_Psi == 0) { // if we are process 0 in the group, send final share to our process 0
435 for (owner = 0; owner < P->Par.Max_me_comm_ST_Psi; owner++) { // for all processes of our Psi group
436 if (owner != 0) { // still not our share of coefficients, receive from owner in our Psi group (increasing owner)
437 if (MPI_Recv( destpsiR, LevS->Dens->LocalSizeR, MPI_DOUBLE, owner, OutputSrcPsiTag, P->Par.comm_ST_Psi, &status ) != MPI_SUCCESS)
438 Error(SomeError, "OutputSrcPsiDensity: MPI_Recv of loaded coefficients failed!");
439 MPI_Get_count(&status, MPI_DOUBLE, &zahl);
440 if (zahl != LevS->Dens->LocalSizeR) // check number of elements
441 fprintf(stderr,"(%i)OutputSrcPsiDensity: MPI_Recv of loaded coefficients of GlobalNo %i, owner %i failed: Too few coefficients - %i instead of %i!\n", P->Par.me, k, owner, zahl, LevS->Dens->LocalSizeR);
442 //else
443 //fprintf(stderr,"(%i)OutputSrcPsiDensity: MPI_Recv of loaded coefficients of GlobalNo %i, owner %i succeeded!\n", P->Par.me, k, owner);
444 } else sent++; // only count sent if it was our share
445
446 if (MPI_Send(destpsiR, LevS->Dens->LocalSizeR, MPI_DOUBLE, 0, OutputSrcPsiTag, P->Par.comm_ST_PsiT) != MPI_SUCCESS)
447 Error(SomeError, "OutputSrcPsiDensity: MPI_Send of loaded coefficients failed!");
448 //else
449 //fprintf(stderr,"(%i)OutputSrcPsiDensity: MPI_Send to process %i in PsiT group of loaded coefficients GlobalNo %i succeeded!\n", P->Par.me, 0, k);
450 }
451 } else {
452 sent++;
453 if (MPI_Send(destpsiR, LevS->Dens->LocalSizeR, MPI_DOUBLE, 0, OutputSrcPsiTag, P->Par.comm_ST_Psi) != MPI_SUCCESS)
454 Error(SomeError, "OutputSrcPsiDensity: MPI_Send of loaded coefficients failed!");
455 //else
456 //fprintf(stderr,"(%i)OutputSrcPsiDensity: MPI_Send to process %i in Psi group of loaded coefficients GlobalNo %i succeeded!\n", P->Par.me, 0, k);
457 }
458 }
459 // otherwise we don't have anything to do with this
460 }
461 }
462 }
463 MPI_Allreduce(&sent, &Sent, 1, MPI_INT, MPI_SUM, P->Par.comm_ST); // catch all again at finish
464 if (P->Call.out[PsiOut]) fprintf(stderr,"(%i) Out of %i shares %i had to be sent in total, %i from this process alone.\n", P->Par.me, P->Par.Max_me_comm_ST_Psi*Psi->NoOfPsis, Sent, sent);
465 if (!(P->Par.me_comm_ST))
466 fclose(SrcPsiData);
467 SpeedMeasure(P,ReadnWriteTime,StopTimeDo);
468}
469
470/** Tests whether writing and successant reading of coefficient array is working correctly.
471 * The local wave function array is written to a disc (\sa OutputSrcPsiDensity()), the by \a wavenr
472 * specified coefficient array copied to OldPsiDat and afterwards read again via ReadSrcPsiDensity().
473 * Comparison per process of each local coefficient shows incorrect read or writes.
474 * \param *P Problem at hand
475 * \param type minimisation type array to test for read and write
476 * \return 1 - successful, 0 - test failed
477 */
478int TestReadnWriteSrcDensity(struct Problem *P, enum PsiTypeTag type)
479{
480 struct RunStruct *R = &P->R;
481 struct LatticeLevel *LevS = R->LevS;
482 struct Lattice *Lat = &P->Lat;
483 struct Psis *Psi = &Lat->Psi;
484 int i,k;
485 fftw_complex *destpsiC, *srcpsiC;
486
487 //fprintf(stderr,"(%i)TestReadnWriteSrcDensity\n",P->Par.me);
488 // write whole array of type to disc
489 OutputSrcPsiDensity(P,type);
490 debug(P,"array written");
491
492 // copy specified array to OldPsiDat
493 for (k=0; k < Lat->Psi.LocalNo; k++) // for every local wave function of type, copy coefficients
494 if (Psi->LocalPsiStatus[k].PsiType == type) { // ... yet only for given type
495 srcpsiC = LevS->LPsi->LocalPsi[k];
496 destpsiC = LevS->LPsi->OldLocalPsi[k - Psi->TypeStartIndex[type]];
497 for (i=0;i<LevS->MaxG;i++) { // for each every unique G grid vector
498 destpsiC[i].re = srcpsiC[i].re; // copy real value
499 destpsiC[i].im = srcpsiC[i].im; // copy imaginary value
500 }
501 }
502 debug(P,"array copied");
503
504 // read whole array again
505 if (!ReadSrcPsiDensity(P,type,0,R->LevSNo))
506 return 0;
507 debug(P,"array read");
508
509 // compare with copied array
510 for (k=0; k < Lat->Psi.LocalNo; k++) // for every local wave function of type, compare coefficients
511 if (Psi->LocalPsiStatus[k].PsiType == type) { // ... yet only for given type
512 srcpsiC = LevS->LPsi->LocalPsi[k];
513 destpsiC = LevS->LPsi->OldLocalPsi[k - Psi->TypeStartIndex[type]];
514 for (i=0;i<LevS->MaxG;i++) // for each every unique G grid vector
515 if ((fabs(destpsiC[i].re - srcpsiC[i].re) >= MYEPSILON) ||(fabs(destpsiC[i].im - srcpsiC[i].im) >= MYEPSILON)) {
516 fprintf(stderr,"(%i)TestReadnWriteSrcDensity: First difference at index %i - %lg+i%lg against loaded %lg+i%lg\n",P->Par.me, i, srcpsiC[i].re, srcpsiC[i].im,destpsiC[i].re,destpsiC[i].im);
517 return 0;
518 }
519 }
520 debug(P,"array compared");
521 fprintf(stderr,"(%i)TestReadnWriteSrcDensity: OK!\n",P->Par.me);
522 return 1;
523}
524
525
526/** Read Psis state from an earlier run.
527 * The doc file is opened, mesh sizes LatticeLevel::N[], global number of Psis read and checked against the known
528 * values in the inital level RunStruct::LevS.
529 * Note, only process 0 handles the files, all read coefficients are transfered to their respective owners via MPI
530 * afterwards. Here, process 0 of a certain Psi group is used as a transferer for the coefficients of the other processes
531 * in this Psi group. He receives them all from process 0 and sends them onward accordingly. The complete set of
532 * coefficients on the real grid for one wave function in the Psi group are transformed into complex wave function
533 * coefficients by the usual fft procedure (see ChangeToLevUp()).
534 *
535 * \param *P Problem at hand
536 * \param type minimisation type to read
537 * \param test whether to just test for presence of files (1) or really read them (0)
538 * \param LevSNo level number to be read
539 * \note This is the counterpart to OutputSrcPsiDensity().
540 * \return 1 - success, 0 - failure
541 * \note It is not necessary to specify the same number of processes on later restart, any number may be used under
542 * the condition that the number of grid nodes match and that there at least 2 processes sharing wave functions
543 * in case of SpinUpDown.
544 * \sa OutputSrcPsiDensity() - same for writing the coefficients, TestReadnWriteSrcDensity() - checks both routines against
545 * each other
546 */
547int ReadSrcPsiDensity(struct Problem *P, enum PsiTypeTag type, int test, int LevSNo)
548{
549 int i, j, k, Index, owner;
550 struct RunStruct *R = &P->R;
551 struct Lattice *Lat = &P->Lat;
552 struct fft_plan_3d *plan = Lat->plan;
553 struct LatticeLevel *LevS = R->LevS; // keep open for LevelNo read from file
554 struct Psis *Psi = &Lat->Psi;
555 //struct Energy *E = Lat->E;
556 fftw_complex *work;
557 double *destpsiR;
558 fftw_real *srcpsiR;
559 fftw_complex *srcpsiC;
560 FILE *SrcPsiData, *SrcPsiDoc;
561 int N[NDIM], GlobalNo[2];
562 int LevelNo, readnr=0;
563 int zahl, signal = test ? 1 : 2; // 0 - ok, 1 - test failed, 2 - throw Error
564 char suffixdat[255], suffixdoc[255];
565 int read_type, Num = 0, colorNo = 0;
566 char spin[20];
567 double Eps[2];
568 MPI_Status status;
569 struct OnePsiElement *OnePsiA, *LOnePsiA;
570 int Recv=0, recv=0;
571
572 SpeedMeasure(P,ReadnWriteTime,StartTimeDo);
573 sprintf(suffixdat, ".%.254s.L%i", P->R.MinimisationName[type], LevSNo);
574 strncpy (suffixdoc, suffixdat, 255);
575 // Depending on Psis::SpinType the source psi doc file is opened and header written
576 switch (Lat->Psi.PsiST) {
577 case SpinDouble:
578 colorNo = 0;
579 strncat (suffixdat, suffixsrcpsidat, 255-strlen(suffixdat));
580 strncat (suffixdoc, suffixsrcpsidoc, 255-strlen(suffixdoc));
581 strncpy (spin, "GlobalNoSpinDouble", 20);
582 Num = Lat->Psi.GlobalNo[PsiMaxNoDouble];
583 break;
584 case SpinUp:
585 colorNo = 0;
586 strncat (suffixdat, suffixsrcpsiupdat, 255-strlen(suffixdat));
587 strncat (suffixdoc, suffixsrcpsiupdoc, 255-strlen(suffixdoc));
588 strncpy (spin, "GlobalNoSpinUp", 20);
589 Num = Lat->Psi.GlobalNo[PsiMaxNoUp];
590 break;
591 case SpinDown:
592 colorNo = 1;
593 strncat (suffixdat, suffixsrcpsidowndat, 255-strlen(suffixdat));
594 strncat (suffixdoc, suffixsrcpsidowndoc, 255-strlen(suffixdoc));
595 strncpy (spin, "GlobalNoSpinDown", 20);
596 Num = Lat->Psi.GlobalNo[PsiMaxNoDown];
597 break;
598 }
599 // open doc file ...
600 if (!(P->Par.me_comm_ST)) {
601 if (!OpenFile(P, &SrcPsiDoc, suffixdoc, "r", test ? 0 : P->Call.out[ReadOut])) { // open doc file
602 debug(P,"ReadSrcPsiDensity: doc file pointer NULL\n");
603 if (test) {
604 if (MPI_Bcast(&signal,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
605 Error(SomeError,"ReadSrcPsiDensity: Bcast of signal failed\n");
606 return 0;
607 }
608 if (MPI_Bcast(&signal,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
609 Error(SomeError,"ReadSrcPsiDensity: Bcast of signal failed\n");
610 Error(SomeError,"ReadSrcPsiDensity: cannot open doc file!");
611 }
612 // ... and parse critical ...
613 readnr += ParseForParameter(0,SrcPsiDoc,"Mintype",0,1,1,int_type,(int *)&read_type, 1, test ? optional : critical);
614 readnr += ParseForParameter(0,SrcPsiDoc,"LevelNo",0,1,1,int_type,&LevelNo,1, test ? optional : critical);
615 readnr += 3*ParseForParameter(0,SrcPsiDoc,"GridNodes",0,3,1,row_int,&N[0], 1, test ? optional : critical);
616 readnr += 2*ParseForParameter(0,SrcPsiDoc,"PsiNo",0,2,1,row_int,&GlobalNo[0], 1, test ? optional : critical);
617 // and optional items ...
618 if (ParseForParameter(0,SrcPsiDoc,"Epsilon",0,2,1,row_double,&Eps[0],1,optional))
619 if ((P->Call.ReadSrcFiles == 1) && ((Eps[1] < R->RelEpsKineticEnergy) || (Eps[0] < R->RelEpsTotalEnergy))) {
620 //fprintf(stderr,"(%i) Eps %lg %lg\tRelEps %lg %lg\n", P->Par.me, Eps[0], Eps[1], R->RelEpsTotalEnergy, R->RelEpsKineticEnergy);
621 fprintf(stderr,"(%i) NOTE: Doing minimization after source file parsing due to smaller specified epsilon stop conditions.\n",P->Par.me);
622 P->Call.ReadSrcFiles = 2; // do minimisation even after loading
623 }
624 if (readnr != 7) { // check number of items read
625 debug(P, "ReadSrcPsiDensity: too few doc items in file\n");
626 fclose(SrcPsiDoc);
627 if (test) {
628 if (MPI_Bcast(&signal,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
629 Error(SomeError,"ReadSrcPsiDensity: Bcast of signal failed\n");
630 return 0;
631 }
632 fprintf(stderr,"ReadSrcPsiDensity: Only %i items read!\n",readnr);
633 if (MPI_Bcast(&signal,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
634 Error(SomeError,"ReadSrcPsiDensity: Bcast of signal failed\n");
635 Error(SomeError, "ReadSrcPsiDensity: read error");
636 }
637 // check if levels match
638 if (LevSNo != LevelNo) {
639 debug(P,"ReadSrcPsiDensity: mismatching levels\n");
640 fclose(SrcPsiDoc);
641 if (test) {
642 if (MPI_Bcast(&signal,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
643 Error(SomeError,"ReadSrcPsiDensity: Bcast of signal failed\n");
644 return 0;
645 }
646 if (MPI_Bcast(&signal,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
647 Error(SomeError,"ReadSrcPsiDensity: Bcast of signal failed\n");
648 Error(SomeError,"ReadSrcPsiDensity: Mismatching levels!");
649 } else {
650 LevS = &P->Lat.Lev[LevelNo];
651 }
652
653 // check if systems in memory and file match
654 if ((read_type != R->CurrentMin) || (N[0] != LevS->N[0]) || (N[1] != LevS->N[1]) || (N[2] != LevS->N[2]) || (GlobalNo[0] != Num) || (GlobalNo[1] != Lat->Psi.GlobalNo[PsiMaxAdd])) { // check read system
655 debug(P,"ReadSrcPsiDensity: srcpsi file does not fit to system\n");
656 fclose(SrcPsiDoc);
657 if (test) {
658 fprintf(stderr,"(%i) Min\t N(x,y,z)\tPsiNo+AddNo\n file: %s\t %i %i %i\t %i + %i\nsystem: %s\t %d %d %d\t %d + %d\n",P->Par.me, R->MinimisationName[read_type], N[0], N[1], N[2], GlobalNo[0], GlobalNo[1], R->MinimisationName[R->CurrentMin], LevS->N[0] , LevS->N[1], LevS->N[2], Lat->Psi.GlobalNo[PsiMaxNoDouble], Lat->Psi.GlobalNo[PsiMaxAdd]);
659 if (MPI_Bcast(&signal,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
660 Error(SomeError,"ReadSrcPsiDensity: Bcast of signal failed\n");
661 return 0;
662 }
663 fprintf(stderr,"ReadSrcPsiDensity: Type %i != CurrentMin %i || N[0] %i != %i || N[1] %i != %i || N[2] %i != %i || %s %i + %i != %i + %i\n", read_type, R->CurrentMin, N[0], LevS->N[0], N[1], LevS->N[1], N[2], LevS->N[2], spin, GlobalNo[0], GlobalNo[1], Num, P->Lat.Psi.GlobalNo[PsiMaxAdd]);
664 if (MPI_Bcast(&signal,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
665 Error(SomeError,"ReadSrcPsiDensity: Bcast of signal failed\n");
666 Error(SomeError,"ReadSrcPsiDensity: srcpsi file does not fit to system");
667 }
668 signal = 0; // everything went alright, signal ok
669 if (MPI_Bcast(&signal,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
670 Error(SomeError,"ReadSrcPsiDensity: Bcast of signal failed\n");
671 } else { // others wait for signal from root process
672 if (MPI_Bcast(&signal,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
673 Error(SomeError,"ReadSrcPsiDensity: Bcast of signal failed\n");
674 if (signal == 1)
675 return 0;
676 else if (signal == 2)
677 Error(SomeError, "ReadSrcPsiDensity: Something went utterly wrong, see root process");
678 else if (P->Call.out[PsiOut])
679 fprintf(stderr,"(%i) ReadSrcPsiDensity: Everything went alright so far\n", P->Par.me);
680 }
681 if (MPI_Bcast(&LevelNo,1,MPI_INT,0,P->Par.comm_ST) != MPI_SUCCESS)
682 Error(SomeError,"ReadSrcPsiDensity: Bcast of LevelNo failed\n");
683 LevS = &P->Lat.Lev[LevelNo];
684 //if (!test) fprintf(stderr,"(%i) LevelSNo %i\n", P->Par.me, LevS->LevelNo);
685
686 if (!test) {
687 // set some pointers for work to follow
688 work = (fftw_complex *)LevS->Dens->DensityArray[TempDensity];
689 destpsiR = (double *)LevS->Dens->DensityArray[TempDensity];
690 srcpsiR = (fftw_real *)LevS->Dens->DensityCArray[TempDensity];
691 srcpsiC = (fftw_complex *)LevS->Dens->DensityCArray[TempDensity];
692
693 // read share of coefficient array for each wave function and send/receive around
694 owner = 0;
695 MPI_Allreduce (&recv, &Recv, 1, MPI_INT, MPI_SUM, P->Par.comm_ST);
696 //fprintf(stderr,"(%i) me (%i/%i) \t Psi (%i/%i)\t PsiT (%i/%i)\n", P->Par.me, P->Par.me_comm_ST, P->Par.Max_me_comm_ST, P->Par.me_comm_ST_Psi, P->Par.Max_me_comm_ST_Psi, P->Par.me_comm_ST_PsiT, P->Par.Max_me_comm_ST_PsiT);
697 k = -1; // k is global PsiNo counter for the desired group
698 for (j=0; j < Psi->MaxPsiOfType+P->Par.Max_me_comm_ST_PsiT; j++) { // go through all wave functions (plus the extra one for each process)
699 OnePsiA = &Psi->AllPsiStatus[j]; // grab OnePsiA
700 if (OnePsiA->PsiType == type) { // only take desired minimisation group
701 k++;
702 //fprintf(stderr,"(%i) ST_Psi: OnePsiA %i\tP->Par.my_color_comm_ST_Psi %i\n", P->Par.me,OnePsiA->my_color_comm_ST_Psi,P->Par.my_color_comm_ST_Psi);
703 if (OnePsiA->my_color_comm_ST_Psi == P->Par.my_color_comm_ST_Psi) // Belongs to my Psi group?
704 LOnePsiA = &Psi->LocalPsiStatus[OnePsiA->MyLocalNo];
705 else
706 LOnePsiA = NULL;
707
708 if (P->Par.me_comm_ST == 0) { // if we are process 0 of all, we may access file
709 if (!OpenFileNo(P, &SrcPsiData, suffixdat, colorNo, "r", test ? 0 : P->Call.out[ReadOut])) {
710 Error(SomeError,"ReadSrcPsiDensity: cannot open data file");
711 }
712 for (i=P->Par.Max_me_comm_ST_Psi-1; i>=0;i--) { // load coefficients
713 fseek( SrcPsiData, (long)((k*N[0]*N[1]*N[2]+i*((long)LevS->Dens->LocalSizeR))*sizeof(double)), SEEK_SET); // seek to beginning of this process' coefficients
714 // readin
715 if ((zahl = fread(destpsiR, sizeof(double), (size_t)(LevS->Dens->LocalSizeR), SrcPsiData)) != (size_t)LevS->Dens->LocalSizeR) {
716 fclose(SrcPsiData);
717 fprintf(stderr, "(%i)ReadSrcPsiDensity: only %i bytes read instead of expected %i\n", P->Par.me, zahl, LevS->Dens->LocalSizeR);
718 Error(SomeError,"ReadSrcPsiDensity: fread Error");
719 }
720 if (LOnePsiA == NULL) { // if it's not local, send away coefficients to correct PsiGroup (process 0 within that group)
721 if (MPI_Send(destpsiR, LevS->Dens->LocalSizeR, MPI_DOUBLE, OnePsiA->my_color_comm_ST_Psi, ReadSrcPsiTag, P->Par.comm_ST_PsiT) != MPI_SUCCESS)
722 Error(SomeError, "ReadSrcPsiDensity: MPI_Send of loaded coefficients failed!");
723 //else
724 //fprintf(stderr,"(%i)ReadSrcPsiDensity: MPI_Send to process %i of loaded coefficients GlobalNo %i, owner %i succeeded!\n", P->Par.me, OnePsiA->my_color_comm_ST_Psi, k, i);
725 } else { // if it's local ...
726 if (i != 0) { // but share of array not for us, send to owner process within Psi group
727 if (MPI_Send(destpsiR, LevS->Dens->LocalSizeR, MPI_DOUBLE, i, ReadSrcPsiTag, P->Par.comm_ST_Psi) != MPI_SUCCESS)
728 Error(SomeError, "ReadSrcPsiDensity: MPI_Send within Psi group of loaded coefficients failed!");
729 //else
730 //fprintf(stderr,"(%i)ReadSrcPsiDensity: MPI_Send to process %i within Psi group of loaded coefficients GlobalNo %i succeeded!\n", P->Par.me, i, k);
731 } // otherwise it was our share already
732 }
733 }
734 } else {
735 if (LOnePsiA != NULL) { // receive
736 if (P->Par.me_comm_ST_Psi == 0) { // if we are process 0 in the group, receive share from process 0 of all
737 for (owner = P->Par.Max_me_comm_ST_Psi -1; owner >=0; owner--) { // for all processes of our Psi group
738 if (MPI_Recv(destpsiR, LevS->Dens->LocalSizeR, MPI_DOUBLE, 0, ReadSrcPsiTag, P->Par.comm_ST_PsiT, &status) != MPI_SUCCESS)
739 Error(SomeError, "ReadSrcPsiDensity: MPI_Recv of loaded coefficients failed!");
740 MPI_Get_count(&status, MPI_DOUBLE, &zahl);
741 if (zahl != LevS->Dens->LocalSizeR) // check number of elements
742 fprintf(stderr,"(%i)ReadSrcPsiDensity: MPI_Recv from process 0 of loaded coefficients of GlobalNo %i, owner %i failed: Too few coefficients - %i instead of %i!\n", P->Par.me, k, owner, zahl, LevS->Dens->LocalSizeR);
743 //else
744 //fprintf(stderr,"(%i)ReadSrcPsiDensity: MPI_Recv from process 0 of loaded coefficients of GlobalNo %i, owner %i succeeded!\n", P->Par.me, k, owner);
745
746 if (owner != 0) { // not our share of coefficients, send to owner in our Psi group
747 if (MPI_Send(destpsiR, LevS->Dens->LocalSizeR, MPI_DOUBLE, owner, ReadSrcPsiTag, P->Par.comm_ST_Psi) != MPI_SUCCESS)
748 Error(SomeError, "ReadSrcPsiDensity: MPI_Send within Psi group of loaded coefficients failed!");
749 //else
750 //fprintf(stderr,"(%i)ReadSrcPsiDensity: MPI_Send to process %i within Psi group of loaded coefficients GlobalNo %i succeeded!\n", P->Par.me, owner, k);
751 } else recv++;
752 }
753 // otherwise it's our share!
754 } else { // our share within Psi Group not belonging to process 0 of all
755 recv++;
756 if (MPI_Recv(destpsiR, LevS->Dens->LocalSizeR, MPI_DOUBLE, 0, ReadSrcPsiTag, P->Par.comm_ST_Psi, &status) != MPI_SUCCESS)
757 Error(SomeError, "ReadSrcPsiDensity: MPI_Recv of loaded coefficients failed!");
758 MPI_Get_count(&status, MPI_DOUBLE, &zahl);
759 if (zahl != LevS->Dens->LocalSizeR) // check number of elements
760 fprintf(stderr,"(%i)ReadSrcPsiDensity: MPI_Recv of loaded coefficients of GlobalNo %i, owner %i failed: Too few coefficients - %i instead of %i!\n", P->Par.me, k, P->Par.me_comm_ST_Psi, zahl, LevS->Dens->LocalSizeR);
761 //else
762 //fprintf(stderr,"(%i)ReadSrcPsiDensity: MPI_Recv of loaded coefficients of GlobalNo %i, owner %i succeeded!\n", P->Par.me, k, P->Par.me_comm_ST_Psi);
763 }
764 }
765 // otherwise we don't have anything to do with this
766 }
767
768 if (LOnePsiA != NULL) {
769 SetArrayToDouble0((double *)srcpsiR, LevS->Dens->TotalSize*2);
770 for (i=0; i < LevS->Dens->LocalSizeR; i++) // copy dest to src
771 srcpsiR[i] = (fftw_real)destpsiR[i];
772
773 fft_3d_real_to_complex(plan, LevS->LevelNo, FFTNF1, srcpsiC, work); // fft transform
774 //if (P->Call.out[PsiOut])
775 //fprintf(stderr,"(%i) LevSNo %i\t LocalPsi %p\n", P->Par.me, LevS->LevelNo, LevS->LPsi->LocalPsi[LOnePsiA->MyLocalNo]);
776 for (i=0;i<LevS->MaxG;i++) { // and copy into wave functions coefficients
777 Index = LevS->GArray[i].Index;
778 LevS->LPsi->LocalPsi[LOnePsiA->MyLocalNo][i].re = srcpsiC[Index].re/LevS->MaxN;
779 LevS->LPsi->LocalPsi[LOnePsiA->MyLocalNo][i].im = srcpsiC[Index].im/LevS->MaxN;
780 }
781 }
782 if ((P->Par.me_comm_ST == 0) && (SrcPsiData != NULL)) fclose(SrcPsiData);
783 }
784 }
785 MPI_Allreduce (&recv, &Recv, 1, MPI_INT, MPI_SUM, P->Par.comm_ST);
786 if (P->Call.out[PsiOut]) fprintf(stderr,"(%i) Out of %i shares %i had to be received in total, %i from this process alone.\n", P->Par.me, P->Par.Max_me_comm_ST_Psi*Psi->NoOfPsis, Recv, recv);
787 SpeedMeasure(P,ReadnWriteTime,StopTimeDo);
788 }
789 return 1; // everything went well till the end
790}
791
792/** Creates the density \ref suffixdensdoc and \ref suffixdensdx files for OpenDx.
793 * Opens \ref suffixdensdoc, fills (pos&data file name, byte order, max mesh points, matrix alignment, steps)
794 * and closes it.
795 * Opens \ref suffixdensdx, then for every FileData::OutVisStep a describing structure for DX is written and
796 * the file closed again.
797 * \param *P Problem at hand
798 * \param me my process number in communicator Psi (0 - do nothing, else - do)
799 */
800static void CreateDensityOutputGeneral(struct Problem *P, const int me)
801{
802 FILE *DensityDoc, *DensityDx;
803 char *posname, *datname;
804 struct LatticeLevel *Lev = &P->Lat.Lev[STANDARTLEVEL];
805 unsigned int i, MaxPoints, N[NDIM];
806 double *RB = P->Lat.RealBasis;
807 if (me) return;
808 N[0] = Lev->N[0]*Lev->NUp[0];
809 N[1] = Lev->N[1]*Lev->NUp[1];
810 N[2] = Lev->N[2]*Lev->NUp[2];
811 MaxPoints = (N[0]+1)*(N[1]+1)*(N[2]+1);
812 posname = (char*)
813 Malloc(strlen(P->Files.mainname) + strlen(suffixdenspos) + 1,"OpenFile");
814 sprintf(posname, "%s%s", P->Files.mainname, suffixdenspos);
815 datname = (char*)
816 Malloc(strlen(P->Files.mainname) + strlen(suffixdensdat) + 1,"OpenFile");
817 sprintf(datname, "%s%s", P->Files.mainname, suffixdensdat);
818 // write doc file
819 OpenFile(P, &DensityDoc, suffixdensdoc, "w",P->Call.out[ReadOut]);
820 fprintf(DensityDoc,"DensityPositions file = %s.####\n", posname);
821 fprintf(DensityDoc,"DensityData file = %s.####\n", datname);
822 fprintf(DensityDoc,"format = ieee float (Bytes %lu) %s\n",(unsigned long) sizeof(float),msb);
823 fprintf(DensityDoc,"points = %i\n", MaxPoints);
824 fprintf(DensityDoc,"majority = row\n");
825 fprintf(DensityDoc,"TimeSeries = %i\n",P->Files.OutVisStep+1);
826 fclose(DensityDoc);
827 // write DX file
828 OpenFile(P, &DensityDx, suffixdensdx, "w",P->Call.out[ReadOut]);
829 for (i=0; i < (unsigned int)P->Files.OutVisStep+1; i++) { // for every OutVis step
830 if (i==0) {
831 fprintf(DensityDx,"object \"gridcon\" class gridconnections counts %i %i %i\n\n",(N[0]+1),(N[1]+1),(N[2]+1));
832 if (P->Files.OutputPosType[i] != active)
833 fprintf(DensityDx, "object \"posdens\" class gridpositions counts %i %i %i\norigin 0.0 0.0 0.0\ndelta %f %f %f\ndelta %f %f %f\ndelta %f %f %f\n\n",
834 (N[0]+1),(N[1]+1),(N[2]+1),
835 (float)(RB[0]/N[0]),(float)(RB[1]/N[0]),(float)RB[2]/N[0],
836 (float)(RB[3]/N[1]),(float)(RB[4]/N[1]),(float)RB[5]/N[1],
837 (float)(RB[6]/N[2]),(float)(RB[7]/N[2]),(float)RB[8]/N[2]);
838 }
839 if (P->Files.OutputPosType[i] == active) {
840 fprintf(DensityDx,
841 "object \"pos.%04u\" class array type float rank 1 shape 3 items %i %s binary\n",i,MaxPoints,msb);
842 fprintf(DensityDx,"data file %s.%04u,0\n",posname,i);
843 }
844 fprintf(DensityDx,"attribute \"dep\" string \"positions\"\n");
845 fprintf(DensityDx,"# %lu - %lu Bytes\n\n",MaxPoints*i*(unsigned long)sizeof(float)*NDIM,MaxPoints*(i+1)*(unsigned long)sizeof(float)*NDIM-1);
846
847 fprintf(DensityDx,"object \"dat.%04u\" class array type float rank 0 items %i %s binary\n",i,MaxPoints,msb);
848 fprintf(DensityDx,"data file %s.%04u,0\n",datname,i);
849 fprintf(DensityDx,"attribute \"dep\" string \"positions\"\n");
850 fprintf(DensityDx,"# %lu - %lu Bytes\n\n",MaxPoints*i*(unsigned long)sizeof(float),MaxPoints*(i+1)*(unsigned long)sizeof(float)-1);
851
852 fprintf(DensityDx,"object \"obj.%04u\" class field\n",i);
853 if (P->Files.OutputPosType[i] == active)
854 fprintf(DensityDx,"component \"positions\" \"pos.%04i\"\n",i);
855 if (P->Files.OutputPosType[i] != active)
856 fprintf(DensityDx,"component \"positions\" \"posdens\"\n");
857 fprintf(DensityDx,"component \"connections\" \"gridcon\"\n");
858 fprintf(DensityDx,"component \"data\" \"dat.%04i\"\n",i);
859 }
860 fprintf(DensityDx,"\nobject \"series\" class series\n");
861 for (i=0; i < (unsigned int)P->Files.OutVisStep+1; i++)
862 fprintf(DensityDx,"member %i \"obj.%04u\" position %f\n",i,i,(float)i);
863 fprintf(DensityDx,"end\n");
864
865 fclose(DensityDx);
866 Free(posname, "CreateDensityOutputGeneral: posname");
867 Free(datname, "CreateDensityOutputGeneral: datname");
868}
869
870/** Calculates the OutVis density of the RiemannTensor level.
871 * The usual pattern arises when a density is fftransformed:
872 * -# over all grid vectors up to MaxG
873 * -# over all doubly grid vectors up to MaxDoubleG
874 * -# call to fft_3d_complex_to_real()
875 *
876 * In this case here followed by call to OutVisPosRTransformPosNFRto0() and finally FileData::work
877 * is copied to FileData::PosR.
878 * \param *Lat Lattice structure, containing Lattice::plan and LatticeLevel
879 * \param *F FileData structure, containing FileData::PosC, FileData::PosR, FileData::work, FileData::Totalsize, FileData::LocalSizeR
880 */
881static void CalculateOutVisDensityPos(struct Lattice *Lat, struct FileData *F/*, const double FactorC_R, const double FactorR_C*/)
882{
883 struct fft_plan_3d *plan = Lat->plan;
884 struct RiemannTensor *RT = &Lat->RT;
885 struct LatticeLevel *LevR = RT->LevR;
886 fftw_complex *destC = F->PosC;
887 fftw_real *destR = F->PosR;
888 fftw_complex *work = F->work;
889 fftw_real *workR = (fftw_real*)work;
890 fftw_complex *PosFactor = F->PosFactor;
891 fftw_complex *posfac, *destpos, *destRCS, *destRCD;
892 fftw_complex *coeff;
893 fftw_complex source;
894 int i, Index, pos, n;
895 int NF = NDIM, MaxNUp = F->MaxNUp, TotalSize = F->TotalSize, LocalSizeR = F->LocalSizeR;
896 SetArrayToDouble0((double *)destC, TotalSize*2);
897 for (i=0;i < LevR->MaxG;i++) {
898 Index = LevR->GArray[i].Index;
899 posfac = &PosFactor[MaxNUp*i];
900 destpos = &destC[MaxNUp*Index*NF];
901 coeff = &RT->Coeff[i];
902 for (pos=0; pos < MaxNUp; pos++) {
903 for (n=0; n < NF; n++) {
904 source.re = coeff[n].re;
905 source.im = coeff[n].im;
906 destpos[n+NF*pos].re = source.re*posfac[pos].re-source.im*posfac[pos].im;
907 destpos[n+NF*pos].im = source.re*posfac[pos].im+source.im*posfac[pos].re;
908 }
909 }
910 }
911 for (i=0; i < LevR->MaxDoubleG; i++) {
912 destRCS = &destC[LevR->DoubleG[2*i]*MaxNUp*NF];
913 destRCD = &destC[LevR->DoubleG[2*i+1]*MaxNUp*NF];
914 for (pos=0; pos < MaxNUp; pos++) {
915 for (n=0; n < NF; n++) {
916 destRCD[n+NF*pos].re = destRCS[n+NF*pos].re;
917 destRCD[n+NF*pos].im = -destRCS[n+NF*pos].im;
918 }
919 }
920 }
921 fft_3d_complex_to_real(plan, LevR->LevelNo, FFTNFRVecUp0, destC, work);
922 OutVisPosRTransformPosNFRto0(RT, destR, workR, NF);
923 memcpy(destR,workR,sizeof(fftw_real)*LocalSizeR);
924}
925
926/** Prepare Density::DensityArray for output.
927 * Into FileData::work subsequently each node (all z, all y, all x) is written as \f$\log(1+x)\f$,
928 * where x is Density::DensityArray[TotalDensity]. In the end result is send to process 0 (yet not
929 * received here, see CombineOutVisArray()). In case of RiemannTensor use, some more complex calculations
930 * are made: FileData::PosR is used, the coefficient offset'ed to the current node and the log taken there.
931 * \param *P Problem at hand
932 * \param myPE this ranks process in the Psi communcator ParallelSimulationData::me_comm_ST_Psi
933 * \param *srcdens Pointer to DensityArray which is to be displayed
934 */
935static void OutputOutVisDensity(struct Problem *P, const int myPE, fftw_real *srcdens)
936{
937 int N[NDIM], n[NDIM], pos[NDIM];
938 int destpos = 0;
939 double fac[NDIM], posd[NDIM];
940 float posf[NDIM+1];
941 struct Lattice *Lat = &P->Lat;
942 struct LatticeLevel *Lev0 = &Lat->Lev[0];
943 fftw_real *srcpos = P->Files.PosR;
944 //fftw_real *srcdens = Lev0->Dens->DensityArray[ActualDensity]; //[TotalDensity]; trick to display single density
945 float *dest = (float *)P->Files.work;
946 int Nx = Lev0->Plan0.plan->N[0];
947 int i;
948 double min, max;
949
950 N[0] = Lev0->Plan0.plan->local_nx;
951 N[1] = Lev0->Plan0.plan->N[1];
952 N[2] = Lev0->Plan0.plan->N[2];
953
954 max = min = srcdens[0];
955 for (i=1;i<P->R.Lev0->Dens->LocalSizeR;i++) {
956 if (srcdens[i] < min) min = srcdens[i];
957 if (srcdens[i] > max) max = srcdens[i];
958 }
959 if (P->Call.out[PsiOut]) fprintf(stderr,"(%i)OutputOutVisDensity: min %e\tmax %e\n",P->Par.me, min, max);
960
961 // go through all nodes
962 for (n[0]=0; n[0] < N[0]; n[0]++) {
963 pos[0] = (n[0] == N[0] ? 0 : n[0]);
964 for (n[1]=0; n[1] <= N[1]; n[1]++) {
965 pos[1] = (n[1] == N[1] ? 0 : n[1]);
966 for (n[2]=0; n[2] <= N[2]; n[2]++) {
967 pos[2] = (n[2] == N[2] ? 0 : n[2]);
968 // depending on RiemannTensor use, fill FileData::work
969 switch (Lat->RT.ActualUse) {
970 case inactive:
971 case standby:
972 if ((srcdens[pos[2]+N[2]*(pos[1]+N[1]*pos[0])]) > 0.)
973 dest[destpos] = log(1.0+(srcdens[pos[2]+N[2]*(pos[1]+N[1]*pos[0])]));
974 else
975 dest[destpos] = 0.;
976 destpos++;
977 break;
978 case active:
979 posf[0] = srcpos[0+NDIM*(pos[2]+N[2]*(pos[1]+N[1]*pos[0]))];
980 posf[1] = srcpos[1+NDIM*(pos[2]+N[2]*(pos[1]+N[1]*pos[0]))];
981 posf[2] = srcpos[2+NDIM*(pos[2]+N[2]*(pos[1]+N[1]*pos[0]))];
982 fac[0] = ((n[0]+N[0]*myPE)/(double)Nx);
983 fac[1] = (n[1]/(double)N[1]);
984 fac[2] = (n[2]/(double)N[2]);
985 RMat33Vec3(posd, Lat->RealBasis, fac);
986 posf[0] += posd[0];
987 posf[1] += posd[1];
988 posf[2] += posd[2];
989 if ((srcdens[pos[2]+N[2]*(pos[1]+N[1]*pos[0])]) > 0.)
990 posf[3] = log(1.0+(srcdens[pos[2]+N[2]*(pos[1]+N[1]*pos[0])]));
991 else
992 posf[3] = 0.;
993 dest[destpos+0] = posf[0];
994 dest[destpos+1] = posf[1];
995 dest[destpos+2] = posf[2];
996 dest[destpos+3] = posf[3];
997 destpos += 4;
998 break;
999 }
1000 }
1001 }
1002 }
1003 if (myPE) MPI_Send(dest, destpos, MPI_FLOAT, 0, OutputDensTag, P->Par.comm_ST_Psi);
1004}
1005
1006/** Combines prepared electronic Psis density and output to file.
1007 * If we are process 0, open file suffixdensdat (only when RiemannTensor is used) and suffixdenspos, receive
1008 * FileData::work logarithmic coefficients sent by the other processes in OutputOutVisDensity(), go through all
1009 * nodes and save the coefficient to file - again depending on RiemannTensor use - followed by FileData::PosTemp
1010 * (for y and z nodes), close file(s).
1011 * \param *P Problem at hand
1012 * \param me this ranks process in the Psi communcator ParallelSimulationData::me_comm_ST_Psi
1013 * \param Maxme number of processes in this Psi communcator ParallelSimulationData::Max_me_comm_ST_Psi
1014 */
1015static void CombineOutVisDensity(struct Problem *P, const int me, const int Maxme)
1016{
1017 int i,n[NDIM], N[NDIM];
1018 float posf[NDIM+1];
1019 float *source = (float *)P->Files.work;
1020 double posd[NDIM], fac[NDIM];
1021 float *Temp = (float *)P->Files.PosTemp;
1022 struct Lattice *Lat = &P->Lat;
1023 struct LatticeLevel *Lev0 = &Lat->Lev[0];
1024 float step = P->Files.OutVisStep;
1025 int No=0, destpos;
1026 FILE *DensityData, *DensityPos;
1027 int Nx = Lev0->Plan0.plan->N[0]+1;
1028 MPI_Status status;
1029 if (me) return; // if we are process 0!
1030 N[0] = Lev0->Plan0.plan->local_nx;
1031 N[1] = Lev0->Plan0.plan->N[1]+1;
1032 N[2] = Lev0->Plan0.plan->N[2]+1;
1033 // Open respective file depending on RiemannTensor use
1034 switch (Lat->RT.ActualUse) {
1035 case active:
1036 OpenFileNo(P, &DensityPos, suffixdenspos, (int)step, "wb",P->Call.out[ReadOut]);
1037 case inactive:
1038 case standby:
1039 OpenFileNo(P, &DensityData, suffixdensdat, (int)step, "wb",P->Call.out[ReadOut]);
1040 break;
1041 }
1042 // for all processes in the communicator
1043 for (i=0; i< Maxme; i++) {
1044 if (i) { // if process != 0, receive from this process
1045 /* MPI_Probe( i, OutputDensTag, P->Par.comm_ST_Psi, &status );*/
1046 switch (Lat->RT.ActualUse) {
1047 case inactive:
1048 case standby:
1049 MPI_Recv( source, N[0]*N[1]*N[2], MPI_FLOAT, i, OutputDensTag, P->Par.comm_ST_Psi, &status );
1050 break;
1051 case active:
1052 MPI_Recv( source, N[0]*N[1]*N[2]*4, MPI_FLOAT, i, OutputDensTag, P->Par.comm_ST_Psi, &status );
1053 break;
1054 }
1055 }
1056 destpos = 0;
1057 // go through all nodes and save the coefficient to file DensityData, depending on RiemannTensor
1058 for (n[0]=0; n[0] < N[0]; n[0]++) {
1059 for (n[1]=0; n[1] < N[1]; n[1]++) {
1060 for (n[2]=0; n[2] < N[2]; n[2]++) {
1061 switch (Lat->RT.ActualUse) {
1062 case inactive:
1063 case standby:
1064 posf[3] = source[destpos];
1065 destpos++;
1066 (void)fwrite(&posf[3], sizeof(float), (size_t)(1), DensityData);
1067 No++;
1068 if (i==0 && n[0] == 0)
1069 Temp[(n[2]+N[2]*(n[1]+N[1]*n[0]))] = posf[3];
1070 break;
1071 case active:
1072 posf[0] = source[destpos+0];
1073 posf[1] = source[destpos+1];
1074 posf[2] = source[destpos+2];
1075 posf[3] = source[destpos+3];
1076 destpos += 4;
1077 (void)fwrite(posf, sizeof(float), (size_t)(NDIM), DensityPos);
1078 (void)fwrite(&posf[3], sizeof(float), (size_t)(1), DensityData);
1079 No++;
1080 if (i==0 && n[0] == 0) {
1081 fac[0] = ((n[0]+N[0]*i)/(double)(Nx-1));
1082 fac[1] = (n[1]/(double)(N[1]-1));
1083 fac[2] = (n[2]/(double)(N[2]-1));
1084 RMat33Vec3(posd, Lat->RealBasis, fac);
1085 posf[0] -= posd[0];
1086 posf[1] -= posd[1];
1087 posf[2] -= posd[2];
1088 fac[0] = ((Nx-1)/(double)(Nx-1));
1089 fac[1] = (n[1]/(double)(N[1]-1));
1090 fac[2] = (n[2]/(double)(N[2]-1));
1091 RMat33Vec3(posd, Lat->RealBasis, fac);
1092 posf[0] += posd[0];
1093 posf[1] += posd[1];
1094 posf[2] += posd[2];
1095 Temp[0+(NDIM+1)*(n[2]+N[2]*(n[1]+N[1]*n[0]))] = posf[0];
1096 Temp[1+(NDIM+1)*(n[2]+N[2]*(n[1]+N[1]*n[0]))] = posf[1];
1097 Temp[2+(NDIM+1)*(n[2]+N[2]*(n[1]+N[1]*n[0]))] = posf[2];
1098 Temp[3+(NDIM+1)*(n[2]+N[2]*(n[1]+N[1]*n[0]))] = posf[3];
1099 }
1100 break;
1101 }
1102 }
1103 }
1104 }
1105 }
1106 n[0] = N[0];
1107 for (n[1]=0; n[1] < N[1]; n[1]++) {
1108 for (n[2]=0; n[2] < N[2]; n[2]++) {
1109 switch (Lat->RT.ActualUse) {
1110 case inactive:
1111 case standby:
1112 (void)fwrite(&Temp[n[2]+N[2]*(n[1])], sizeof(float), (size_t)(1), DensityData);
1113 No++;
1114 break;
1115 case active:
1116 (void)fwrite(&Temp[(NDIM+1)*(n[2]+N[2]*(n[1]))], sizeof(float), (size_t)(NDIM), DensityPos);
1117 (void)fwrite(&Temp[(NDIM+1)*(n[2]+N[2]*(n[1]))+3], sizeof(float), (size_t)(1), DensityData);
1118 No++;
1119 break;
1120 }
1121 }
1122 }
1123 if (No != Nx*N[1]*N[2]) Error(SomeError,"CombineOutVisDensity: No != points");
1124 switch (Lat->RT.ActualUse) {
1125 case active:
1126 fclose(DensityPos);
1127 case inactive:
1128 case standby:
1129 fclose(DensityData);
1130 break;
1131 }
1132}
1133
1134/** Main output electronic Psis density for OpenDX.
1135 * If FileData::MeOutVis is set, calls OutputOutVisDensity() followed by CombineOutVisDensity().
1136 * Beforehand CalculateOutVisDensityPos() is called if RiemannTensor is used.
1137 * \param *P Problem at hand
1138 * \param *src_dens Pointer to DensityArray which is to be displayed
1139 */
1140static void OutVisDensity(struct Problem *P, fftw_real *src_dens)
1141{
1142 if (!P->Files.MeOutVis) return;
1143 if (P->Lat.RT.ActualUse == active) CalculateOutVisDensityPos(&P->Lat, &P->Files/*, P->Lat.FactorDensityR, P->Lat.FactorDensityC*/);
1144 OutputOutVisDensity(P, P->Par.me_comm_ST_Psi, src_dens);
1145 /* Achtung hier: P->Files.work (RT->TempC, Dens->DensityCArray[TempDensity]) fuer myPE == 0 nicht veraendern !!! */
1146 CombineOutVisDensity(P, P->Par.me_comm_ST_Psi, P->Par.Max_me_comm_ST_Psi);
1147}
1148
1149/** Opening and Initializing of output measurement files.
1150 * If this is process 0, opens and writes top line of FileData::ForcesFile, FileData::EnergyFile.
1151 * and sets FileData::MeOutVis and FileData::MeOutMes (if output desired) to 1, otherwise 0.
1152 * \param *P Problem at hand
1153 */
1154void InitOutputFiles(struct Problem *P)
1155{
1156 struct FileData *F = &P->Files;
1157 F->ForcesFile = NULL;
1158 F->EnergyFile = NULL;
1159 F->HamiltonianFile = NULL;
1160 F->MinimisationFile = NULL;
1161 F->SpreadFile = NULL;
1162 F->ReciSpreadFile = NULL;
1163 F->TemperatureFile = NULL;
1164 // process 0 ?
1165 F->MeOutVis = ((P->Par.my_color_comm_ST == 0 && P->Par.my_color_comm_ST_Psi == 0 && F->DoOutVis) ? 1 : 0);
1166 F->MeOutCurr = ((P->Par.my_color_comm_ST == 0 && P->Par.my_color_comm_ST_Psi == 0 && F->DoOutCurr) ? 1 : 0);
1167 F->MeOutMes = ((P->Par.me == 0 && F->DoOutMes) ? 1 : 0);
1168 OpenFile(P, &F->HamiltonianFile, suffixhamiltonianall, "w",P->Call.out[ReadOut]);
1169
1170 if (!F->MeOutMes) return;
1171 OpenFile(P, &F->ForcesFile, suffixforcesall, "w",P->Call.out[ReadOut]);
1172 if (F->ForcesFile == NULL) fprintf(stderr,"Error opening ForcesFile\n");
1173 // write header of forces file
1174 fprintf(F->ForcesFile, "%s\t%s\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t%s\t%s\t%s\t\t%s\t\t%s\n",
1175 "Type", "No",
1176 "Pos0", "Pos1", "Pos2",
1177 "Total0", "Total1", "Total2",
1178 "Local0", "Local1", "Local2",
1179 "NLocal0", "NLocal1", "NLocal2",
1180 "Magnetic0", "Magnetic1", "Magnetic2",
1181 "Ewald0", "Ewald1", "Ewald2");
1182 OpenFile(P, &F->EnergyFile, suffixenergyall, "w",P->Call.out[ReadOut]);
1183 if (F->EnergyFile == NULL) fprintf(stderr,"Error opening EnergyFile\n");
1184 // write header of energy file
1185 if (P->R.DoUnOccupied) {
1186 fprintf(F->EnergyFile, "%s\t\t%s\t\t%s\t%s\t\t%s\t%s\t\t%s\t%s\t%s\t\t%s\t\t%s\t%s\t\t%s\t\t%s\t\t%s\n",
1187 "Time",
1188 "Total",
1189 "Total+Gap",
1190 "Kinetic", "NonLocal",
1191 "GapPsi",
1192 "Correlation", "Exchange",
1193 "Pseudo", "Hartree",
1194 "GapDensity",
1195 "-Gauss",
1196 "Ewald",
1197 "IonKin",
1198 "ETotal");
1199 } else {
1200 fprintf(F->EnergyFile, "%s\t\t%s\t\t%s\t\t%s\t%s\t%s\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\n",
1201 "Time",
1202 "Total",
1203 "Kinetic", "NonLocal",
1204 "Correlation", "Exchange",
1205 "Pseudo", "Hartree",
1206 "-Gauss",
1207 "Ewald",
1208 "IonKin",
1209 "ETotal");
1210 }
1211 OpenFile(P, &F->MinimisationFile, suffixminall, "w",P->Call.out[ReadOut]);
1212 if (F->MinimisationFile == NULL) fprintf(stderr,"Error opening MinimsationFile\n");
1213 // write header of minimsation file
1214 fprintf(F->MinimisationFile, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n","Step", "Psi", "PsiStep", "TE", "ATE", "delta", "dEdt0", "ddEdtt0");
1215
1216 OpenFile(P, &F->TemperatureFile, suffixtempall, "w",P->Call.out[ReadOut]);
1217 if (F->TemperatureFile == NULL) fprintf(stderr,"Error opening TemperatureFile\n");
1218}
1219
1220/** Closes all output measurement files.
1221 * Closes FileData::ForcesFile and FileData::EnergyFile
1222 * \param *P Problem at hand
1223 */
1224void CloseOutputFiles(struct Problem *P)
1225{
1226 struct FileData *F = &P->Files;
1227 if (!F->MeOutMes) return;
1228 if (F->ForcesFile != NULL) fclose(F->ForcesFile); // only they've been opened (thus not pointing to NULL)
1229 if (F->EnergyFile != NULL) fclose(F->EnergyFile);
1230 if (F->HamiltonianFile != NULL) fclose(F->HamiltonianFile);
1231 if (F->MinimisationFile != NULL) fclose(F->MinimisationFile);
1232 if (F->SpreadFile != NULL) fclose(F->SpreadFile);
1233}
1234
1235/** Initialization of Problem::FileData structure for visual output.
1236 * If this is process 0 (and OutVis desired), allocate memory for FileData::PosTemp, FileData::work,
1237 * set the entries of FileData all to their corresponding values from RiemannTensor,
1238 * FileData::OutVisStep to zero.
1239 * \param *P Problem at hand
1240 */
1241void InitOutVisArray(struct Problem *P)
1242{
1243 struct FileData *F = &P->Files;
1244 struct Lattice *Lat = &P->Lat;
1245 struct RiemannTensor *RT = &Lat->RT;
1246 struct LatticeLevel *Lev0 = &Lat->Lev[0];
1247 F->OutputPosType = NULL;
1248 if (!F->MeOutVis) return;
1249 F->OutVisStep = 0;
1250 F->PosTemp = (fftw_complex *)
1251 Malloc(sizeof(float)*(Lev0->Plan0.plan->N[1]+1)*(Lev0->Plan0.plan->N[2]+1)*
1252 ((Lat->RT.Use != UseRT ? 0 : NDIM)+1), "InitOutVisArray: TempC");
1253 F->work = (fftw_complex *)Lev0->Dens->DensityCArray[TempDensity];
1254 if (Lat->RT.Use != UseRT) return;
1255 F->TotalSize = RT->TotalSize[RTAIRT]/NDIM;
1256 F->LocalSizeR = RT->LocalSizeR[RTAiGcg];
1257 F->LocalSizeC = RT->LocalSizeC[RTAiGcg];
1258 F->MaxNUp = RT->MaxNUp[RTPFRto0];
1259 F->PosC = RT->DensityC[RTAiGcg];
1260 F->PosR = (fftw_real *)F->PosC;
1261 F->work = RT->TempC;
1262 F->PosFactor = RT->PosFactor[RTPFRto0];
1263}
1264
1265static const char suffixionfor[] = ".ions.force"; //!< Suffix for ion forces file
1266static const char suffixionZ[] = ".ions.datZ"; //!< Suffix for ion datZ file
1267static const char suffixionpos[] = ".ions.pos"; //!< Suffix for ion positions file
1268static const char suffixiondx[] = ".ions.dx"; //!< Suffix for ions dx file
1269static const char suffixiondoc[] = ".ions.doc"; //!< Suffix for ions doc file
1270static const char suffixsrciondoc[] = ".srcion.doc"; //!< Suffix for state ions doc file
1271static const char suffixsrciondat[] = ".srcion.data"; //!< Suffix for state ions data file
1272
1273/** Output current ions state.
1274 * If this is process0, open file suffixsrciondoc for writing, output Ions::Max_Types and
1275 * Ions::Max_IonsOfType of each type - each in a new line - closes it.
1276 * Then opens suffixsrciondat for binary writing, outputs Lattice:RealBasis vectors and
1277 * position IonType::R and speed IonType::U, closes it.
1278 * \param *P Problem at hand
1279 * \note This is the ionic counterpart to the elecontric OutputSrcPsiDensity(), storing a so far made
1280 * calculation to file.
1281 */
1282static void OutSrcIons(struct Problem *P)
1283{
1284 struct Ions *I = &P->Ion;
1285 double *U, *pos;
1286 double data[2*NDIM];
1287 int is,ia,i;
1288 FILE *SrcIonDoc, *SrcIonData;
1289
1290 if (!(P->Par.me == 0)) return;
1291
1292 // output of ion types and numbers per type
1293 OpenFile(P, &SrcIonDoc, suffixsrciondoc, "w",P->Call.out[ReadOut]);
1294 fprintf(SrcIonDoc,"%i\n", I->Max_Types);
1295 for (is=0; is < I->Max_Types; is++)
1296 fprintf(SrcIonDoc,"%i\n", I->I[is].Max_IonsOfType);
1297 fclose(SrcIonDoc);
1298
1299 OpenFile(P, &SrcIonData, suffixsrciondat, "wb",P->Call.out[ReadOut]);
1300 (void)fwrite(P->Lat.RealBasis, sizeof(double), (size_t)(NDIM_NDIM), SrcIonData);
1301 for (is=0; is < I->Max_Types; is++) {
1302 for (ia=0; ia < I->I[is].Max_IonsOfType; ia++) {
1303 U = &I->I[is].U[NDIM*ia];
1304 pos = &I->I[is].R[NDIM*ia];
1305 for (i=0;i<NDIM;i++) {
1306 data[i] = pos[i];
1307 data[i+NDIM] = U[i];
1308 }
1309 (void)fwrite(&data, sizeof(double), (size_t)(2*NDIM), SrcIonData);
1310 }
1311 }
1312 fclose(SrcIonData);
1313}
1314
1315/** Read ions state from a file.
1316 * Reads the suffixsrciondoc file and checks it against the current state in Ions regarding
1317 * IonType::MaxTypes and IonType::Max_IonsOfType, closes it.
1318 * Afterwards, opens suffixsrciondat for binary reading, retrieves the basis checking it against
1319 * Problem::Lattice::RealBasis. If ok, reads position IonType::R and speed IonType::U, closes it.
1320 * \param *P Problem at hand
1321 * \return 1 - successful, 0 - failure
1322 * \note This is the ionic counterpart to the electronic ReadSrcPsiDensity(), see also OutSrcIons().
1323 */
1324int ReadSrcIons(struct Problem *P)
1325{
1326 struct Ions *I = &P->Ion;
1327 double *U, *pos;
1328 double data[2*NDIM];
1329 int is,ia,i;
1330 int Max_Types;
1331 int *Max_IonsOfType = NULL;
1332 double RealBasis[NDIM_NDIM];
1333 FILE *SrcIonDoc, *SrcIonData;
1334 // read the doc file and check
1335 if (OpenFile(P, &SrcIonDoc, suffixsrciondoc, "r",P->Call.out[ReadOut])) {
1336 if (fscanf(SrcIonDoc,"%i", &Max_Types) != 1)
1337 //Error(SomeError, "ReadSrcIons: read error");
1338 return 0;
1339 if (Max_Types != I->Max_Types)
1340 //Error(SomeError, "ReadSrcIons: srcion file does not fit to system, MaxTypes");
1341 return 0;
1342 Max_IonsOfType = (int *) Malloc(Max_Types*sizeof(int), "ReadSrcIons: Max_IonsOfType");
1343 for (is=0; is < Max_Types; is++) {
1344 if (fscanf(SrcIonDoc,"%i", &Max_IonsOfType[is]) != 1)
1345 //Error(SomeError, "ReadSrcIons: read error");
1346 return 0;
1347 if (Max_IonsOfType[is] != I->I[is].Max_IonsOfType)
1348 //Error(SomeError, "ReadSrcIons: srcion file does not fit to system, Max_IonsOfType");
1349 return 0;
1350 }
1351 fclose(SrcIonDoc);
1352 // read basis, then positions and speeds of ions
1353 if (OpenFile(P, &SrcIonData, suffixsrciondat, "rb",P->Call.out[ReadOut])) {
1354 if (fread(RealBasis, sizeof(double), (size_t)(NDIM_NDIM), SrcIonData) != NDIM_NDIM)
1355 //Error(SomeError, "ReadSrcIons: read error");
1356 return 0;
1357 for (i=0; i < NDIM_NDIM; i++)
1358 if (RealBasis[i] != P->Lat.RealBasis[i])
1359 //Error(SomeError, "ReadSrcIons: srcion file does not fit to system, RealBasis");
1360 return 0;
1361 for (is=0; is < I->Max_Types; is++) {
1362 for (ia=0; ia < I->I[is].Max_IonsOfType; ia++) {
1363 if (fread(&data, sizeof(double), (size_t)(2*NDIM), SrcIonData) != 2*NDIM)
1364 //Error(SomeError, "ReadSrcIons: read error");
1365 return 0;
1366 U = &I->I[is].U[NDIM*ia];
1367 pos = &I->I[is].R[NDIM*ia];
1368 for (i=0;i<NDIM;i++) {
1369 pos[i] = data[i];
1370 U[i] = data[i+NDIM];
1371 }
1372 }
1373 }
1374 fclose(SrcIonData);
1375 }
1376 if (Max_IonsOfType != NULL) Free(Max_IonsOfType, "Max_IonsOfType");
1377 }
1378 return 1;
1379}
1380
1381/** Output of ion doc, dx, forces and positions file for OpenDX.
1382 * If this is process 0,
1383 * open, fill and close IonDoc file suffixiondoc,
1384 * open, fill for each FileData::OutVisStep and close IonDX file suffixiondx
1385 * for every
1386 * open suffixionfor, suffixionpos (and suffixionZ in case of only FileData::OutVisStep), fill
1387 * them with the ion forces IonType::FIon and positions IonType::R of each type and each ion per type,
1388 * close them all.
1389 * \param *P Problem at hand
1390 */
1391static void OutVisIons(struct Problem *P)
1392{
1393 struct Ions *I = &P->Ion;
1394 struct FileData *F = &P->Files;
1395 int i,is,ia;
1396 double *fion, *pos;
1397 float data[6]; // holds temporarily twice NDIM values as write buffer
1398 int Z;
1399 char *datnamef, *datnameZ, *posname;
1400 FILE *IonsDataF, *IonsDataZ, *IonsPos, *IonsDoc, *IonsDx;
1401 if (!P->Files.MeOutVis && P->Par.me == 0) return;
1402 // generate file names
1403 datnamef = (char*)
1404 malloc(strlen(P->Files.mainname)+strlen(suffixionfor) + 1);
1405 sprintf(datnamef, "%s%s", P->Files.mainname, suffixionfor);
1406 datnameZ = (char*)
1407 malloc(strlen(P->Files.mainname)+strlen(suffixionZ) + 1);
1408 sprintf(datnameZ, "%s%s", P->Files.mainname, suffixionZ);
1409 posname = (char*)
1410 malloc(strlen(P->Files.mainname)+strlen(suffixionpos) + 1);
1411 sprintf(posname, "%s%s", P->Files.mainname, suffixionpos);
1412 // open, fill and close doc file
1413 if (OpenFile(P, &IonsDoc, suffixiondoc, "w",P->Call.out[ReadOut])) {
1414 fprintf(IonsDoc,"IonsPos file = %s.####\n", posname);
1415 fprintf(IonsDoc,"IonsForce file = %s.####\n", datnamef);
1416 fprintf(IonsDoc,"format = ieee float (Bytes %lu) %s = Force(3)\n",(unsigned long) sizeof(float),msb);
1417 fprintf(IonsDoc,"IonsZ file = %s.####\n", datnameZ);
1418 fprintf(IonsDoc,"format = int (Bytes %lu) %s = Z(1)\n",(unsigned long) sizeof(int),msb);
1419 fprintf(IonsDoc,"points = %i\n", I->Max_TotalIons);
1420 fprintf(IonsDoc,"majority = row\n");
1421 fprintf(IonsDoc,"TimeSeries = %i\n",F->OutVisStep+1);
1422 fclose(IonsDoc);
1423 }
1424 // open dx file and fill it with each output step, close it
1425 if (OpenFile(P, &IonsDx, suffixiondx, "w",P->Call.out[ReadOut])) {
1426 for (i=0; i < F->OutVisStep+1; i++) {
1427 if (i==0) {
1428 /* fprintf(IonsDx,"object \"ioncon\" class array type int rank 1 shape 2 items 0 data follows\nattribute \"element type\" string \"lines\"\nattribute \"ref\" string \"positions\"\n\n"); */
1429 fprintf(IonsDx,"object \"iondatZ\" class array type int rank 0 items %i %s binary\n",I->Max_TotalIons,msb);
1430 fprintf(IonsDx,"data file %s,0\n",datnameZ);
1431 fprintf(IonsDx,"attribute \"dep\" string \"positions\"\n\n");
1432 }
1433
1434 fprintf(IonsDx,"object \"ionpos.%04i\" class array type float rank 1 shape 3 items %i %s binary\n",i,I->Max_TotalIons,msb);
1435 fprintf(IonsDx,"data file %s.%04i,0\n\n",posname,i);
1436
1437 fprintf(IonsDx,"object \"iondatF.%04i\" class array type float rank 1 shape 3 items %i %s binary\n",i,I->Max_TotalIons,msb);
1438 fprintf(IonsDx,"data file %s.%04i,0\n",datnamef,i);
1439 fprintf(IonsDx,"attribute \"dep\" string \"positions\"\n\n");
1440
1441 fprintf(IonsDx,"object \"ionobjF.%04i\" class field\n",i);
1442 fprintf(IonsDx,"component \"positions\" \"ionpos.%04i\"\n",i);
1443 fprintf(IonsDx,"component \"data\" \"iondatF.%04i\"\n",i);
1444 /*fprintf(IonsDx,"component \"connections\" \"ioncon\"\n\n");*/
1445
1446 fprintf(IonsDx,"object \"ionobjZ.%04i\" class field\n",i);
1447 fprintf(IonsDx,"component \"positions\" \"ionpos.%04i\"\n",i);
1448 fprintf(IonsDx,"component \"data\" \"iondatZ\"\n");
1449 /* fprintf(IonsDx,"component \"connections\" \"ioncon\"\n\n");*/
1450 }
1451 fprintf(IonsDx,"\nobject \"ionseriesF\" class series\n");
1452 for (i=0; i < F->OutVisStep+1; i++)
1453 fprintf(IonsDx,"member %i \"ionobjF.%04i\" position %f\n",i,i,(float)i);
1454 fprintf(IonsDx,"\nobject \"ionseriesZ\" class series\n");
1455 for (i=0; i < F->OutVisStep+1; i++)
1456 fprintf(IonsDx,"member %i \"ionobjZ.%04i\" position %f\n",i,i,(float)i);
1457 fprintf(IonsDx,"end\n");
1458 fclose(IonsDx);
1459 }
1460 free(datnamef);
1461 free(datnameZ);
1462 free(posname);
1463 // open IonForces, IonZ and IonPosition file, write forces respectively positions for each ion of each type, close them
1464 if (OpenFileNo(P, &IonsDataF, suffixionfor, F->OutVisStep, "wb",P->Call.out[ReadOut])) {
1465 if (F->OutVisStep == 0)
1466 OpenFile(P, &IonsDataZ, suffixionZ, "wb",P->Call.out[ReadOut]);
1467 if (OpenFileNo(P, &IonsPos, suffixionpos, F->OutVisStep, "wb",P->Call.out[ReadOut])) {
1468 for (is=0; is < I->Max_Types; is++) {
1469 for (ia=0; ia < I->I[is].Max_IonsOfType; ia++) {
1470 fion = &I->I[is].FIon[NDIM*ia];
1471 pos = &I->I[is].R[NDIM*ia];
1472 for (i=0;i<3;i++) {
1473 data[i+3] = fion[i];
1474 data[i] = pos[i];
1475 }
1476 Z = I->I[is].Z;
1477 if (fwrite(&data[0],sizeof(float), (size_t)(3),IonsPos) != 3)
1478 Error(FileOpenParams, "Error writing ion positions!");
1479 if (F->OutVisStep == 0) (void)fwrite(&Z,sizeof(int), (size_t)(1),IonsDataZ);
1480 if (fwrite(&data[3],sizeof(float), (size_t)(3),IonsDataF) != 3)
1481 Error(FileOpenParams, "Error writing ionic forces!");
1482 }
1483 }
1484 fclose(IonsPos);
1485 }
1486 if (F->OutVisStep == 0)
1487 fclose(IonsDataZ);
1488 fclose(IonsDataF);
1489 }
1490}
1491
1492/** Output electronic density and ion state files with so far made calculations.
1493 * If CallOptions::WriteSrcFiles is set, OutputSrcPsiDensity() and OutSrcIons() are called.
1494 * \param *P Problem at hand
1495 * \param type which PsiTypeTag should be put to file
1496 */
1497void OutputVisSrcFiles(struct Problem *P, enum PsiTypeTag type)
1498{
1499 if (P->Call.WriteSrcFiles) {
1500 if(P->Call.out[NormalOut]) fprintf(stderr,"(%i) Writing %s srcpsi to disk\n", P->Par.me, P->R.MinimisationName[type]);
1501 OutputSrcPsiDensity(P, type);
1502 if(P->Call.out[NormalOut]) fprintf(stderr,"(%i) Writing srcion to disk\n", P->Par.me);
1503 OutSrcIons(P);
1504 }
1505 // if (!P->Files.MeOutVis) return;
1506 // P->Files.OutputPosType =
1507 // Realloc(P->Files.OutputPosType,sizeof(enum ModeType)*(P->Files.OutVisStep+1),"OutputVis");
1508 // P->Files.OutputPosType[P->Files.OutVisStep] = P->Lat.RT.ActualUse;
1509 // CreateDensityOutputGeneral(P, P->Par.me_comm_ST_Psi);
1510 // OutVisDensity(P);
1511 // OutVisIons(P);
1512 // if(P->Call.out[NormalOut]) fprintf(stderr,"(%i) Written OutVisStep %i to disk\n", P->Par.me, P->Files.OutVisStep);
1513 // /* P->Files.OutVisStep++; Genau ebend nicht hochzaehlen - wird immer ueberschrieben */
1514}
1515
1516/** Main output total electronic density and ion data for OpenDX.
1517 * Calls subsequently preparing CreateDensityOutputGeneral(), then output of electronic
1518 * densities OutVisDensity() and ion data OutVisIons(), increasing finally FileData::OutVisStep.
1519 * \param *P Problem at hand
1520 * \param *srcdens Pointer to DensityArray which is to be displayed
1521 * \note Output is made only RunStruct::OutVisStep steps and if FileData::MeOutVis is set.
1522 */
1523void OutputVis(struct Problem *P, fftw_real *srcdens)
1524{
1525 if (!P->Files.MeOutVis) return;
1526 P->Files.OutputPosType = (enum ModeType *) Realloc(P->Files.OutputPosType,sizeof(enum ModeType)*(P->Files.OutVisStep+1),"OutputVis");
1527 P->Files.OutputPosType[P->Files.OutVisStep] = P->Lat.RT.ActualUse;
1528
1529 CreateDensityOutputGeneral(P, P->Par.me_comm_ST_Psi);
1530 OutVisDensity(P, srcdens);
1531 OutVisIons(P);
1532 if(P->Call.out[MinOut]) fprintf(stderr,"(%i) Written OutVisStep %i to disk\n", P->Par.me, P->Files.OutVisStep);
1533 P->Files.OutVisStep++;
1534}
1535
1536/** Output of each current density component for OpenDX.
1537 * \param *P Problem at hand
1538 * \note Output is made only RunStruct::OutVisStep steps and if FileData::MeOutVis is set.
1539 */
1540void OutputCurrentDensity(struct Problem *P)
1541{
1542 int index, i, r;
1543 fftw_real *density = P->R.Lev0->Dens->DensityArray[ActualDensity];
1544 fftw_real *CurrentDensity[NDIM*NDIM];
1545 if (!P->Files.MeOutCurr) return;
1546
1547 P->Files.OutputPosType = (enum ModeType *) Realloc(P->Files.OutputPosType,sizeof(enum ModeType)*(P->Files.OutVisStep+(1)*NDIM),"OutputVis");
1548 for (i=0;i<(1)*NDIM;i++)
1549 P->Files.OutputPosType[P->Files.OutVisStep+i] = P->Lat.RT.ActualUse;
1550 if(P->Call.out[PsiOut]) fprintf(stderr,"(%i) OutVisStep %i, OutputPosType %p\n",P->Par.me, P->Files.OutVisStep, P->Files.OutputPosType);
1551
1552 // due to preprocessor values we can't put the following stuff into a loop
1553 CurrentDensity[0] = (fftw_real *) P->R.Lev0->Dens->DensityArray[CurrentDensity0];
1554 CurrentDensity[1] = (fftw_real *) P->R.Lev0->Dens->DensityArray[CurrentDensity1];
1555 CurrentDensity[2] = (fftw_real *) P->R.Lev0->Dens->DensityArray[CurrentDensity2];
1556 CurrentDensity[3] = (fftw_real *) P->R.Lev0->Dens->DensityArray[CurrentDensity3];
1557 CurrentDensity[4] = (fftw_real *) P->R.Lev0->Dens->DensityArray[CurrentDensity4];
1558 CurrentDensity[5] = (fftw_real *) P->R.Lev0->Dens->DensityArray[CurrentDensity5];
1559 CurrentDensity[6] = (fftw_real *) P->R.Lev0->Dens->DensityArray[CurrentDensity6];
1560 CurrentDensity[7] = (fftw_real *) P->R.Lev0->Dens->DensityArray[CurrentDensity7];
1561 CurrentDensity[8] = (fftw_real *) P->R.Lev0->Dens->DensityArray[CurrentDensity8];
1562
1563 // output current density, not vector component
1564 for (index=0;index<NDIM;index++) {
1565 // evaluate euclidian norm for given B component
1566 SetArrayToDouble0((double *)density,P->R.Lev0->Dens->TotalSize*2); // reset
1567 for (r=0;r<P->R.Lev0->Dens->LocalSizeR;r++) {
1568 for (i=0;i<NDIM;i++)
1569 density[r] += CurrentDensity[i + index*NDIM][r]*CurrentDensity[i + index*NDIM][r];
1570 density[r] = sqrt(density[r]);
1571 }
1572 // output
1573 CreateDensityOutputGeneral(P, P->Par.me_comm_ST_Psi);
1574 OutVisDensity(P, density);
1575 OutVisIons(P);
1576 if(P->Call.out[NormalOut]) fprintf(stderr,"(%i) Written OutVisStep %i to disk\n", P->Par.me, P->Files.OutVisStep);
1577 P->Files.OutVisStep++;
1578 }
1579}
1580
1581/** Output each orbital in a certain step order for OpenDX.
1582 * \param *P Problem at hand
1583 * \param offset from which step do we start
1584 * \param increment by which increment do we advance step-wise
1585 * \param type Only PsiTypeTag orbitals are displayed
1586 */
1587void OutputVisAllOrbital(struct Problem *P, int offset, int increment, enum PsiTypeTag type) {
1588 struct Lattice *Lat = &P->Lat;
1589 struct Psis *Psi = &Lat->Psi;
1590 struct RunStruct *R = &P->R;
1591 struct LatticeLevel *LevS = R->LevS;
1592 struct LatticeLevel *Lev0 = R->Lev0;
1593 struct Density *Dens0 = Lev0->Dens;
1594 struct OnePsiElement *OnePsiA, *LOnePsiA;
1595 MPI_Status status;
1596 int ElementSize = (sizeof(fftw_complex) / sizeof(double));
1597 fftw_complex *LPsiDatA;
1598 int i, p, RecvSource;
1599 int Num = Psi->NoOfPsis;
1600
1601 if (!P->Files.MeOutVis) return;
1602
1603 P->Files.OutputPosType = (enum ModeType *) Realloc(P->Files.OutputPosType,sizeof(enum ModeType)*(P->Files.OutVisStep+(Num)),"OutputVis");
1604
1605 P->Files.OutVisStep += offset;
1606 P->Files.OutputPosType[P->Files.OutVisStep] = P->Lat.RT.ActualUse;
1607 for (i=0; i < Psi->MaxPsiOfType+P->Par.Max_me_comm_ST_PsiT; i++) { // go through all wave functions (plus the extra one for each process)
1608 OnePsiA = &Psi->AllPsiStatus[i]; // grab the desired OnePsiA
1609 if (OnePsiA->PsiType == type) { // drop if extra one
1610 if (OnePsiA->my_color_comm_ST_Psi == P->Par.my_color_comm_ST_Psi) // local?
1611 LOnePsiA = &Psi->LocalPsiStatus[OnePsiA->MyLocalNo];
1612 else
1613 LOnePsiA = NULL;
1614 if (LOnePsiA == NULL) { // if it's not local ... receive it from respective process into TempPsi
1615 RecvSource = OnePsiA->my_color_comm_ST_Psi;
1616 MPI_Recv( LevS->LPsi->TempPsi, LevS->MaxG*ElementSize, MPI_DOUBLE, RecvSource, WannierTag1, P->Par.comm_ST_PsiT, &status );
1617 LPsiDatA=LevS->LPsi->TempPsi;
1618 } else { // .. otherwise send it to all other processes (Max_me... - 1)
1619 for (p=0;p<P->Par.Max_me_comm_ST_PsiT;p++)
1620 if (p != OnePsiA->my_color_comm_ST_Psi)
1621 MPI_Send( LevS->LPsi->LocalPsi[OnePsiA->MyLocalNo], LevS->MaxG*ElementSize, MPI_DOUBLE, p, WannierTag1, P->Par.comm_ST_PsiT);
1622 LPsiDatA=LevS->LPsi->LocalPsi[OnePsiA->MyLocalNo];
1623 } // LPsiDatA is now set to the coefficients of OnePsi either stored or MPI_Received
1624
1625 if (P->Files.MeOutVis) {
1626 P->Files.OutputPosType[P->Files.OutVisStep] = P->Lat.RT.ActualUse;
1627 // recalculate density for the specific wave function ...
1628 CalculateOneDensityR(Lat, LevS, Dens0, LPsiDatA, Dens0->DensityArray[ActualDensity], R->FactorDensityR, 0);
1629 // ... and output (wherein ActualDensity is used instead of TotalDensity)
1630 //OutputVis(P);
1631 CreateDensityOutputGeneral(P, P->Par.me_comm_ST_Psi);
1632 OutVisDensity(P, Dens0->DensityArray[ActualDensity]);
1633 OutVisIons(P);
1634 if(P->Call.out[NormalOut]) fprintf(stderr,"(%i) Written OutVisStep %i to disk\n", P->Par.me, P->Files.OutVisStep);
1635 P->Files.OutVisStep+=increment;
1636 P->Files.OutputPosType[P->Files.OutVisStep] = P->Lat.RT.ActualUse;
1637 }
1638 }
1639 }
1640}
1641
1642/** Read source files containing stored calculations.
1643 * Calls ReadSrcPsiDensity() and ReadSrcIons().
1644 * \param *P Problem at hand
1645 */
1646void ReadSrcFiles(struct Problem *P)
1647{
1648 if (P->Call.out[NormalOut]) fprintf(stderr, "(%i)ReadSrcPsiDensity\n", P->Par.me);
1649 ReadSrcPsiDensity(P, Occupied, 0, P->R.LevSNo);
1650 ReadSrcPsiDensity(P, UnOccupied, 0, P->R.LevSNo);
1651 if (P->Call.out[NormalOut]) fprintf(stderr, "(%i)ReadSrcIons\n", P->Par.me);
1652 ReadSrcIons(P);
1653}
1654
1655/** Plots a cut plane of the real density of one wave function.
1656 * \param *P Problem at hand
1657 * \param index index of axis (vector orthogonal to plane)
1658 * \param node node specifying where to cut at the given axis
1659 * \param wavenr global number of wave function
1660 * \param *density density array to plot
1661 * \sa PlotVectorPlane() - very similar
1662 */
1663void PlotSrcPlane(struct Problem *P, int index, double node, int wavenr, fftw_real *density)
1664{
1665 struct RunStruct *R = &P->R;
1666 struct Lattice *Lat = &P->Lat;
1667 struct LatticeLevel *Lev0 = R->Lev0;
1668 const int myPE = P->Par.me_comm_ST_Psi;
1669 char filename[255], spin[12];
1670 char *suchpointer;
1671 FILE *PlotFile = NULL;
1672 time_t seconds;
1673
1674
1675 // open file
1676 time(&seconds); // get current time
1677
1678 switch (Lat->Psi.PsiST) {
1679 case SpinDouble:
1680 sprintf(&filename[0], ".psi%i_cut%i.csv", wavenr, index);
1681 strncat(spin,"SpinDouble",12);
1682 break;
1683 case SpinUp:
1684 sprintf(&filename[0], ".psi%i_cut%i_up.csv", wavenr, index);
1685 strncat(spin,"SpinUp",12);
1686 break;
1687 case SpinDown:
1688 sprintf(&filename[0], ".psi%i_cut%i_down.csv", wavenr, index);
1689 strncat(spin,"SpinDown",12);
1690 break;
1691 }
1692
1693 if (!myPE) { // only process 0 writes to file
1694 OpenFile(P, &PlotFile, filename, "w", P->Call.out[ReadOut]);
1695 strcpy(filename, ctime(&seconds));
1696 suchpointer = strchr(filename, '\n');
1697 if (suchpointer != NULL)
1698 *suchpointer = '\0';
1699 if (PlotFile != NULL) {
1700 fprintf(PlotFile,"# Psi %i, type %s (real density) plot of plane perpendicular to direction e_%i at node %lg, seed %i, config %s, run on %s, #cpus %i", wavenr, spin, index, node, R->Seed, P->Files.default_path, filename, P->Par.Max_me_comm_ST_Psi);
1701 fprintf(PlotFile,"\n");
1702 } else { Error(SomeError, "PlotSrcPlane: Opening Plot File"); }
1703 }
1704
1705 // plot density
1706 PlotRealDensity(P, Lev0, PlotFile, index, node, density, density);
1707
1708 if (PlotFile != NULL) {
1709 // close file
1710 fclose(PlotFile);
1711 }
1712}
1713
1714/** plots a cut plane of a given 3d real density.
1715 * \param *P Problem at hand, contains pointer to Lattice structure
1716 * \param *Lev LatticeLevel of the real density
1717 * \param PlotFile file pointer (already open and valid)
1718 * \param index index of lattice axis
1719 * \param n_orth position on lattice axis where to cut
1720 * \param *density1 first real density array
1721 * \param *density2 second real density array (point to \a *density1 if not needed)
1722 */
1723void PlotRealDensity(struct Problem *P, struct LatticeLevel *Lev, FILE *PlotFile, int index, double n_orth, fftw_real *density1, fftw_real *density2)
1724{
1725 struct Lattice *Lat = &P->Lat;
1726 int n[NDIM], n0;
1727 int N[NDIM];
1728 N[0] = Lev->Plan0.plan->N[0];
1729 N[1] = Lev->Plan0.plan->N[1];
1730 N[2] = Lev->Plan0.plan->N[2];
1731 const int N0 = Lev->Plan0.plan->local_nx;
1732 const int myPE = P->Par.me_comm_ST_Psi;
1733 double fac[NDIM], x[NDIM];
1734 int i0, i = 0;
1735 int PE, zahl;
1736 double *buffer;
1737 MPI_Status status;
1738 int sizes[P->Par.Max_me_comm_ST_Psi], c0, c1;
1739 double nodes[NDIM], node[NDIM];
1740
1741 for(i=0;i<NDIM;i++) {
1742 nodes[i] = (i == index) ? n_orth : 0.;
1743 node[i] = 0.;
1744 }
1745 RMat33Vec3(node, Lat->ReciBasis, nodes); // transform cartesian coordinates into cell coordinates [0,1]^3
1746 for(i=0;i<NDIM;i++) // now N^3 within node range of discrete grid
1747 node[i] = (int)(node[i]*N[i]/(2.*PI));
1748 fprintf(stderr,"(%i) n_orth %lg, index %i converted to plane offset vector (%lg, %lg, %lg).\n", P->Par.me, n_orth, index, node[0], node[1], node[2]);
1749
1750 switch (index) {
1751 case 0:
1752 zahl = 4*N[1]*N[2];
1753 break;
1754 case 1:
1755 zahl = 4*N0*N[2];
1756 break;
1757 case 2:
1758 zahl = 4*N0*N[1];
1759 break;
1760 }
1761 fprintf(stderr,"(%i) buffer size %i\n", P->Par.me, zahl);
1762 buffer = Malloc(sizeof(double)*zahl,"PlotRealDensity: buffer");
1763
1764 c0 = cross(index,0);
1765 c1 = cross(index,1);
1766 // then for every point on the grid in real space ...
1767 i=0;
1768 for (n0=0;n0<N0;n0++) // only local points on x axis
1769 for (n[1]=0;n[1]<N[1];n[1]++)
1770 for (n[2]=0;n[2]<N[2];n[2]++) {
1771 n[0]=n0 + N0*myPE; // global relative coordinate: due to partitoning of x-axis in PEPGamma>1 case
1772 if (n[index] == (int)node[index]) { // only on the correct plane orthogonal to desired axis and at desired node ...
1773 fac[0] = (double)n[0]/(double)N[0];
1774 fac[1] = (double)n[1]/(double)N[1];
1775 fac[2] = (double)n[2]/(double)N[2];
1776 RMat33Vec3(x, Lat->RealBasis, fac); // relative coordinate times basis matrix gives absolute ones
1777 i0 = n[2]+N[2]*(n[1]+N[1]*n0); // index to local density array
1778
1779 buffer[i++] = x[c0]; // fill buffer
1780 buffer[i++] = x[c1];
1781 buffer[i++] = density1[i0];
1782 buffer[i++] = density2[i0];
1783 if (i > zahl) Error(SomeError, "PlotRealDensity: buffer too small!");
1784 }
1785 }
1786 // exchange sizes of each buffer
1787 MPI_Allgather(&i, 1, MPI_INT, sizes, 1, MPI_INT, P->Par.comm_ST_Psi);
1788 if (myPE == 0) {
1789 for (PE=0; PE < P->Par.Max_me_comm_ST_Psi; PE++) {
1790 if (PE != 0) {
1791 // receive them
1792 if (MPI_Recv(buffer, sizes[PE], MPI_DOUBLE, PE, PlotRealDensityTag, P->Par.comm_ST_Psi, &status) != MPI_SUCCESS)
1793 Error(SomeError, "PlotRealDensity: MPI_Recv failure!");
1794 MPI_Get_count(&status, MPI_DOUBLE, &zahl);
1795 if (zahl != sizes[PE])
1796 Error(SomeError, "PlotRealDensity: received unexpected amount of elements!");
1797 }
1798 //write them: local one (still in buffer) and received ones
1799 for (i0 = 0; i0 < sizes[PE];) {
1800 fprintf(PlotFile,"%e", buffer[(i0)++]);
1801 if ((i0 % 4) == 0) {
1802 fprintf(PlotFile,"\n");
1803 } else {
1804 fprintf(PlotFile,"\t");
1805 }
1806 }
1807 }
1808 } else { // send them
1809 if (MPI_Send(buffer, i, MPI_DOUBLE, 0, PlotRealDensityTag, P->Par.comm_ST_Psi) != MPI_SUCCESS)
1810 Error(SomeError, "PlotRealDensity: MPI_Send failure!");
1811 }
1812 Free(buffer, "PlotRealDensity: buffer");
1813}
Note: See TracBrowser for help on using the repository browser.