IPCC  1.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
KNCommandFileParser.cpp
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <algorithm>
11 #include "KNCommandFileParser.h"
12 //#include "KNGeometricShape.h"
13 
14 #define MAX_INPUTOPTION_HASH_NUMBER 20
15 #define HASH_SARP '#'
16 
19  /*A*/{ "a_x", "a_y", "a_z", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
20  /*B*/{ "Band_size", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
21  /*C*/{ "ConvCriteriaTol", "Check_iteration_period", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
22  /*D*/{ "Direction", "domain_mat(1)", "domain_orig(1)", "domain_size(1)", "domain_shape(1)", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
23  /*E*/{ "evmin", "evmax", "Eigenvector_Flag", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
24  /*F*/{ "Find_eigenvalue_number", "Find_degenerated_ev_number", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
25  /*G*/{ "Geometry_Origin", "Geometry_Size", "Geometry_Shape", "Geometry_Material", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
26  /*H*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
27  /*I*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
28  /*J*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
29  /*K*/{ "k_points", "kx", "ky", "kz", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
30  /*L*/{ "Load_in_MIC", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
31  /*M*/{ "MatrixFileName", "Matrix_Dimension", "Max_iteration_Number", "MPI_Level", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
32  /*N*/{ "Number_of_nonzero_entries", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
33  /*O*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
34  /*P*/{ "periodicity_elec", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
35  /*Q*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
36  /*R*/{ "Reorthogonalization", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
37  /*S*/{ "Sorting_before_CSR_building", "Save_atom_map", "Save_Hamiltonian", "Structure_type", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
38  /*T*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
39  /*U*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
40  /*V*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
41  /*W*/{ "Wave_Function_Flag", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
42  /*X*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
43  /*Y*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
44  /*Z*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" },
45  /*#1*/{ "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" }
46 };
47 
49 {
50 }
51 
53 {
54 }
55 
59 void CKNCommandFileParser::TrimString(char *pszBuffer)
60 {
61  char szTemp[1024], ch;
62  int i, nIndex, nLength = strlen(pszBuffer);
63 
64  nIndex = 0;
65  for (i = 0; i < nLength; ++i)
66  {
67  ch = pszBuffer[i];
68  if (' ' != ch && '\r' != ch && '\n' != ch && '\t' != ch && '[' != ch && ']' != ch)
69  szTemp[nIndex++] = pszBuffer[i];
70  }
71  szTemp[nIndex] = NULL;
72  strcpy(pszBuffer, szTemp);
73 }
74 
79 {
80  char szTemp[1024], ch;
81  int i, nIndex, nLength = strlen(pszBuffer);
82 
83  nIndex = 0;
84  for (i = 0; i < nLength; ++i)
85  {
86  ch = pszBuffer[i];
87  if ('\r' != ch && '\n' != ch && '\t' != ch && '[' != ch && ']' != ch)
88  szTemp[nIndex++] = pszBuffer[i];
89  }
90  szTemp[nIndex] = NULL;
91  strcpy(pszBuffer, szTemp);
92 }
93 
98 {
99  char szTemp[1024], ch;
100  int i, nIndex, nLength = strlen(pszBuffer);
101 
102  nIndex = 0;
103  for (i = 0; i < nLength; ++i)
104  {
105  ch = pszBuffer[i];
106  if (' ' != ch)
107  break;
108  }
109 
110  for (; i < nLength; ++i)
111  {
112  ch = pszBuffer[i];
113  szTemp[nIndex++] = pszBuffer[i];
114  }
115 
116  szTemp[nIndex] = NULL;
117  strcpy(pszBuffer, szTemp);
118 }
119 
125 void CKNCommandFileParser::ExtractParam(char *pszBuffer, double *pParam, int nParamSize)
126 {
127  char *token = NULL;
128  char strSperate[] = " ";
129  int nCount = 0;
130 
131  token = strtok(pszBuffer, strSperate);
132 
133  while (token != NULL)
134  {
135  pParam[nCount++] = atof(token);
136 
137  if (nParamSize == nCount)
138  break;
139 
140  token = strtok(NULL, strSperate);
141  }
142 }
143 
150 void CKNCommandFileParser::SetOptionParam(LPINPUT_CMD_PARAM lpParam, char *pszOption, int nHash, int nOptIndex)
151 {
152  char szValue[1024] = "";
153 
154  GetValueAfterEqualString(pszOption, szValue);
155 
156  switch (nHash)
157  {
158  case 0:
159  switch (nOptIndex)
160  {
161  case 0:
162  lpParam->fUnitcellLength[_X] = atof(szValue);
163  break;
164  case 1:
165  lpParam->fUnitcellLength[_Y] = atof(szValue);
166  break;
167  case 2:
168  lpParam->fUnitcellLength[_Z] = atof(szValue);
169  break;
170  }
171  break;
172  case 1:
173  switch(nOptIndex)
174  {
175  case 0:
176  lpParam->nBandSize = atoi(szValue);
177  break;
178  }
179  case 2:
180  switch (nOptIndex)
181  {
182  case 0:
183  lpParam->fConvergeceCriteria = atof(szValue);
184  break;
185  case 1:
186  lpParam->nCheckEigenvalueInterval = atoi(szValue);
187  break;
188  }
189  break;
190  case 3:
191  switch (nOptIndex)
192  {
193  case 0:
194  {
195  double param[3];
196 
197  TrimStringEx(szValue);
198  TrimSpaceEdge(szValue);
199  ExtractParam(szValue, param, 3);
200  lpParam->fDirection[_X] = param[0];
201  lpParam->fDirection[_Y] = param[1];
202  lpParam->fDirection[_Z] = param[2];
203  lpParam->nDirectionSingle = 100 * (int)param[0];
204  lpParam->nDirectionSingle += (10 * (int)param[1]);
205  lpParam->nDirectionSingle += (int)param[2];
206  if (100 != lpParam->nDirectionSingle)
207  lpParam->bNeedRotate = true;
208  else
209  lpParam->bNeedRotate = false;
210  }
211  break;
212  case 1:
213  /*TrimString(szValue);
214  strcpy(lpParam->szDomainMat, szValue);*/
215  break;
216  case 2:
217  break;
218  case 3:
219  break;
220  }
221  case 4:
222  switch (nOptIndex)
223  {
224  case 0:
225  lpParam->fevMin = atof(szValue);
226  break;
227  case 1:
228  lpParam->fevMax = atof(szValue);
229  break;
230  case 2:
231  lpParam->bCalculateEigenVectors = atoi(szValue) == 1 ? true : false;
232  break;
233  }
234  break;
235  case 5:
236  switch (nOptIndex)
237  {
238  case 0:
239  lpParam->nFindingEigenValueCount = atoi(szValue);
240  break;
241  case 1:
242  lpParam->nFindingDegeneratedEVCount = atoi(szValue);
243  break;
244  }
245  break;
246  case 6:
247  switch (nOptIndex)
248  {
249  case 0:
250  {
251  double param[3];
252  unsigned int nSubIndex = ExtractOptionIndex(pszOption, nHash, nOptIndex);
253 
254  TrimStringEx(szValue);
255  TrimSpaceEdge(szValue);
256  ExtractParam(szValue, param, 3);
257  lpParam->fOrigin[nSubIndex - 1][_X] = param[0];
258  lpParam->fOrigin[nSubIndex - 1][_Y] = param[1];
259  lpParam->fOrigin[nSubIndex - 1][_Z] = param[2];
260  }
261  break;
262  case 1:
263  {
264  double param[3];
265  unsigned int nSubIndex = ExtractOptionIndex(pszOption, nHash, nOptIndex);
266 
267  TrimStringEx(szValue);
268  TrimSpaceEdge(szValue);
269  ExtractParam(szValue, param, 3);
270  lpParam->fShapeLength[nSubIndex - 1][_X] = param[0];
271  lpParam->fShapeLength[nSubIndex - 1][_Y] = param[1];
272  lpParam->fShapeLength[nSubIndex - 1][_Z] = param[2];
273  lpParam->nSubDomainNumber = nSubIndex - 1;
274  }
275  break;
276  case 2:
277  {
278  unsigned int nSubIndex = ExtractOptionIndex(pszOption, nHash, nOptIndex);
279 
280  TrimString(szValue);
281  strcpy(lpParam->szShape[nSubIndex - 1], szValue);
282  if (!strcmp(lpParam->szShape[nSubIndex - 1], "Box"))
283  lpParam->nShape[nSubIndex - 1] = BOX_SHAPE;
284  else if (!strcmp(lpParam->szShape[nSubIndex - 1], "Cylinder"))
285  lpParam->nShape[nSubIndex - 1] = CYLINDER_SHAPE;
286  break;
287  }
288  case 3:
289  {
290  unsigned int nSubIndex = ExtractOptionIndex(pszOption, nHash, nOptIndex);
291 
292  TrimString(szValue);
293  strcpy(lpParam->szDomainMat[nSubIndex - 1], szValue);
294  break;
295  }
296  }
297  break;
298  case 10:
299  switch (nOptIndex)
300  {
301  case 0:
302  lpParam->fKPoints = atoi(szValue);
303  break;
304  case 1:
305  {
306  double param[2];
307  TrimStringEx(szValue);
308  TrimSpaceEdge(szValue);
309  ExtractParam(szValue, param, 2);
310  lpParam->fKValueInit[0] = param[0];
311  lpParam->fKValueFinal[0] = param[1];
312  }
313  break;
314  case 2:
315  {
316  double param[2];
317  TrimStringEx(szValue);
318  TrimSpaceEdge(szValue);
319  ExtractParam(szValue, param, 2);
320  lpParam->fKValueInit[1] = param[0];
321  lpParam->fKValueFinal[1] = param[1];
322  }
323  break;
324  case 3:
325  {
326  double param[2];
327  TrimStringEx(szValue);
328  TrimSpaceEdge(szValue);
329  ExtractParam(szValue, param, 2);
330  lpParam->fKValueInit[2] = param[0];
331  lpParam->fKValueFinal[2] = param[1];
332  }
333  break;
334  }
335  break;
336  case 11:
337  switch (nOptIndex)
338  {
339  case 0:
340  lpParam->load_in_MIC = atof(szValue);
341  break;
342  }
343  break;
344  case 12:
345  switch (nOptIndex)
346  {
347  case 0:
348  TrimString(szValue);
349  strcpy(lpParam->szDataFileName, szValue);
350  break;
351  case 1:
352  lpParam->nMatrixDemension = atoi(szValue);
353  break;
354  case 2:
355  lpParam->nLanczosIterationCount = atoi(szValue);
356  break;
357  case 3:
358  lpParam->nMPILevel = atoi(szValue);
359  break;
360  }
361  break;
362  case 13:
363  switch (nOptIndex)
364  {
365  case 0:
366  lpParam->nMatrixNonzeroElementCount = atoi(szValue);
367  break;
368  }
369  break;
370  case 15:
371  switch (nOptIndex)
372  {
373  case 0:
374  {
375  double param[3];
376 
377  TrimStringEx(szValue);
378  TrimSpaceEdge(szValue);
379  ExtractParam(szValue, param, 3);
380  lpParam->bConsiderBoundaryCondition[_X] = param[0] == 1. ? true : false;
381  lpParam->bConsiderBoundaryCondition[_Y] = param[1] == 1. ? true : false;
382  lpParam->bConsiderBoundaryCondition[_Z] = param[2] == 1. ? true : false;
383  }
384  break;
385  }
386  break;
387  case 17:
388  switch (nOptIndex)
389  {
390  case 0:
391  lpParam->bDoSelectiveReorthogonalization = atoi(szValue) == 1 ? true : false;
392  break;
393  }
394  break;
395  case 18:
396  switch (nOptIndex)
397  {
398  case 0:
399  lpParam->bSortBeforeCSRBuilding = atoi(szValue) == 1 ? true : false;
400  break;
401  case 1:
402  lpParam->bSaveMapFile = atoi(szValue) == 1 ? true : false;
403  break;
404  case 2:
405  lpParam->bSaveHamiltonian = atoi(szValue) == 1 ? true : false;
406  break;
407  case 3:
408  TrimString(szValue);
409  strcpy(lpParam->szStructureType, szValue);
410  break;
411  }
412  break;
413  case 20:
414  switch (nOptIndex)
415  {
416  case 0:
417  lpParam->bUsingCubeModel = atoi(szValue) == 1 ? true : false;
418  if ( lpParam->bUsingCubeModel )
419  lpParam->bNeedRotate = false;
420  break;
421  }
422  case 22:
423  switch (nOptIndex)
424  {
425  case 0:
426  lpParam->bCalculateWaveFunction = atoi(szValue) == 1 ? true : false;
427  break;
428  }
429  break;
430  }
431 
432 }
433 
438 void CKNCommandFileParser::GetValueAfterEqualString(char *pszOption, char *pszValue)
439 {
440  strcpy(pszValue, strrchr(pszOption, '=') + 1);;
441 }
442 
447 {
448  LPINPUT_CMD_PARAM lpRtn = NULL;
449  FILE *opt = NULL;
450  char szBuffer[1024];
451  int nHash;
452  int i;
453 
454  int k = 0;
455 
456  opt = fopen(pszInputCommnadFileName, "rt");
457  if (NULL == opt)
458  return lpRtn;
459 
460 
461  lpRtn = (LPINPUT_CMD_PARAM)malloc(sizeof(INPUT_CMD_PARAM));
462  memset(lpRtn, NULL, sizeof(INPUT_CMD_PARAM));
463  strcpy(lpRtn->szDataFileName, pszInputCommnadFileName);
464  InitData(lpRtn);
465 
466  while (fgets(szBuffer, 1024, opt))
467  {
468  nHash = (int)szBuffer[0];
469 
470  if (HASH_SARP == nHash)
471  continue;
472 
473  if (nHash >= 48 && nHash <= 57)
474  {
475  nHash = 26;
476  }
477  else
478  {
479  nHash -= 65;
480  if (nHash > 26)
481  nHash -= 32;
482  }
483 
484  if (nHash < 0 || nHash > 26)
485  continue;
486 
487  for (i = 0; i < MAX_INPUTOPTION_HASH_NUMBER; i++)
488  {
489  if (0 == strlen(g_inputOption[nHash][i]))
490  continue;
491 
492  if (!strncmp(g_inputOption[nHash][i], szBuffer, strlen(g_inputOption[nHash][i])))
493  {
494  SetOptionParam(lpRtn, szBuffer, nHash, i);
495  break;
496  }
497  }
498  }
499 
500  fclose(opt);
501 
502  return lpRtn;
503 }
504 
509 {
510  lpParam->nMPILevel = 1;
511  lpParam->nFindingDegeneratedEVCount = 1;
512  lpParam->load_in_MIC = 0;
513 }
514 
520 unsigned int CKNCommandFileParser::ExtractOptionIndex(char *pszBuffer, int nHash, int nOptIndex)
521 {
522  int nStart = strlen(g_inputOption[nHash][nOptIndex]) + 1;
523  char szNumber[10];
524  unsigned int nLastIndex, i;
525 
526  nLastIndex = 99999;
527  for (i = nStart; i < strlen(pszBuffer); ++i)
528  {
529  if (')' == pszBuffer[i])
530  {
531  nLastIndex = i;
532  break;
533  }
534  }
535 
536  if (99999 == nLastIndex)
537  return 0;
538 
539  strncpy(szNumber, pszBuffer + nStart, nLastIndex - nStart);
540  szNumber[nLastIndex - nStart] = NULL;
541  if (0 == strlen(szNumber))
542  return 0;
543  else
544  return atoi(szNumber);
545 }
char szDataFileName[1024]
Hamiltonian data file name only for calculating eigen value from file.
unsigned int nMPILevel
MPI Grouping level.
#define CYLINDER_SHAPE
Definition: CKNGlobal.h:89
static void TrimStringEx(char *pszBuffer)
Trim string using white spapce not including ' '.
static void GetValueAfterEqualString(char *pszOption, char *pszValue)
Extracting string after equal charater postion.
static unsigned int ExtractOptionIndex(char *pszBuffer, int nHash, int nOptIndex)
Get Number of Index. ex) Geometry_Origin(1)=[ 0 0 0 ], -> Get '1' from option string.
#define HASH_SARP
Command file parameter names.
#define MAX_INPUTOPTION_HASH_NUMBER
Definition: CKNGlobal.h:105
static void InitData(LPINPUT_CMD_PARAM lpParam)
Initialize some default parameter.
bool bSaveHamiltonian
Save Hamiltonian matrix or not.
This class for parsing input command file.
static void ExtractParam(char *pszBuffer, double *pParam, int nParamSize)
Extracing paramter in string.
double fKValueInit[3]
Init k point value.
double fKValueFinal[3]
Final k point value.
bool bSortBeforeCSRBuilding
When solve lanczos without geometric building, determine sorting data before building CSR...
int nShape[NMAXDOMAIN]
Geometric Shape.
bool bDoSelectiveReorthogonalization
Option for doing selective reorthogonalization or not.
Definition: CKNGlobal.h:105
static void SetOptionParam(LPINPUT_CMD_PARAM lpParam, char *pszOption, int nHash, int nOptIndex)
Setting option variable.
bool bCalculateEigenVectors
Option for doing calculating eigen vectors or not.
unsigned int nBandSize
Band basis size.
static LPINPUT_CMD_PARAM ParsingInputCommand(char *pszInputCommnadFileName)
Parsing Commaind file.
double fevMin
Eigen value minimun value boundary.
int nFindingEigenValueCount
Eigen value count for want to find.
char * g_inputOption[27][MAX_INPUTOPTION_HASH_NUMBER]
bool bNeedRotate
Calculation parameter that need to apply rotation matrix.
bool bCalculateWaveFunction
Option for doing calculating wave function or not.
int nLanczosIterationCount
A counts of lanczos interation.
Definition: CKNGlobal.h:105
unsigned int nFindingDegeneratedEVCount
Degenerated eigenvalue count for want to find.
Structure for save input command file parsing result.
double fShapeLength[NMAXDOMAIN][3]
Shap length x, y, z, direction.
double fevMax
Eigen value maximun value boundary.
double fDirection[3]
Direction x, y, z.
bool bSaveMapFile
Save atom map or not.
char szDomainMat[NMAXDOMAIN][1024]
Material type.
int nSubDomainNumber
Numbers of Subdomain, except main domain.
#define BOX_SHAPE
Definition: CKNGlobal.h:88
double fUnitcellLength[3]
Unitcell length x, y, z direction.
bool bUsingCubeModel
Option for using cube model or not.
int nMatrixNonzeroElementCount
Non zero count in Hamiltonian matrix only for calculating eigen value from file.
char szStructureType[1024]
Structure type.
double fConvergeceCriteria
Eigen value convergence criteria.
char szShape[NMAXDOMAIN][1024]
Geometric Shape.
static void TrimSpaceEdge(char *pszBuffer)
Trim space both side of string.
struct CKNCommandFileParser::INPUT_CMD_PARAM * LPINPUT_CMD_PARAM
int nMatrixDemension
Hamiltonian matrix demension.
int nCheckEigenvalueInterval
A interval for checking T matrix eigenvlaue.
bool bConsiderBoundaryCondition[3]
Considering bondary condition or not x, y, z direction.
static void TrimString(char *pszBuffer)
Trim string using white spapce including ' '.
double fOrigin[NMAXDOMAIN][3]
Coordinator original coordinate.