00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "mdparallel.h"
00010
00011
00012 class VaspBox : public MDPARALLELFrame
00013 {
00014 public:
00015 char outcarfile[500];
00016 char zxcgrfile[500];
00017 double vaspstress[6], dstress[6], dstrain[6];
00018 double Scompliance[6][6];
00019 Matrix33 gh;
00020 double vaspboxstepsize;
00021
00022 VaspBox():vaspboxstepsize(1){};
00023
00024 void potential()
00025 {
00026
00027
00028 INFO("Empty potential");
00029 }
00030 virtual void initvars()
00031 {
00032 MDPARALLELFrame::initvars();
00033 _RLIST=3.8;
00034 }
00035 virtual void initparser();
00036 virtual int exec(char *name);
00037
00038 void readoutcarstress();
00039 int searchstringfromfile(char *inLine, const char *match,
00040 int len, FILE *fp);
00041 void calgh();
00042 void movebox();
00043 void scalebox();
00044 void writeZXCGR();
00045 };
00046
00047 void VaspBox::initparser()
00048 {
00049 MDPARALLELFrame::initparser();
00050
00051 bindvar("outcarfile",outcarfile,STRING);
00052 bindvar("zxcgrfile",zxcgrfile,STRING);
00053 bindvar("Scompliance",Scompliance,DOUBLE);
00054 bindvar("vaspboxstepsize",&vaspboxstepsize,DOUBLE);
00055 }
00056
00057 int VaspBox::exec(char *name)
00058 {
00059 if(MDPARALLELFrame::exec(name)==0) return 0;
00060 bindcommand(name,"readoutcarstress",readoutcarstress());
00061 bindcommand(name,"movebox",movebox());
00062 bindcommand(name,"scalebox",scalebox());
00063
00064 return -1;
00065 }
00066
00067 int VaspBox::searchstringfromfile(char *inLine, const char *match,
00068 int len, FILE *fp)
00069 {
00070 while(fgets(inLine, len, fp)!=NULL)
00071 {
00072 if(strncmp(inLine,match,strlen(match))==0)
00073 {
00074 INFO(inLine);
00075 return 0;
00076 break;
00077 }
00078 }
00079 return -1;
00080 }
00081
00082 void VaspBox::readoutcarstress()
00083 {
00084 FILE *fp;
00085 char extname[500], inLine[500], *p;
00086 int i, succ;
00087
00088 INFO("readoutcarstress: "<<outcarfile);
00089 LFile::SubHomeDir(outcarfile,extname);
00090 INFO("readoutcarstress: "<<extname);
00091
00092 fp=fopen(extname,"r");
00093 if(fp==NULL)
00094 {
00095 FATAL("readoutcarstress: open file failure");
00096 }
00097
00098 inLine[0]=0; succ=0;
00099 while(1)
00100 {
00101 if(searchstringfromfile(inLine,
00102 " energy without entropy=", 500, fp)!=-1)
00103 {
00104 p=strrchr(inLine, '=');
00105 sscanf(p+1,"%lf",&_EPOT);
00106 }
00107 if(searchstringfromfile(inLine, " FORCE on cell", 500, fp)==-1)
00108 break ;
00109 searchstringfromfile(inLine, " Direction", 500, fp);
00110 searchstringfromfile(inLine, " in kB", 500, fp);
00111 sscanf(inLine+strlen(" in kB"),"%lf %lf %lf %lf %lf %lf",
00112 vaspstress+0,vaspstress+1,vaspstress+2,
00113 vaspstress+3,vaspstress+4,vaspstress+5);
00114 for(i=0;i<6;i++)
00115 vaspstress[i]*=100;
00116
00117 for(i=0;i<6;i++)
00118 vaspstress[i]*=-1;
00119
00120 INFO_Printf("vaspstress (in MPa) ="
00121 " (%3.3f %3.3f %3.3f %3.3f %3.3f %3.3f)\n",
00122 vaspstress[0],vaspstress[1],vaspstress[2],
00123 vaspstress[3],vaspstress[4],vaspstress[5]);
00124 succ=1;
00125 }
00126 if(!succ)
00127 FATAL("readoutcarstress: didn't find stress data");
00128
00129 INFO("Energy = "<<_EPOT);
00130
00131 fclose(fp);
00132 }
00133
00134 void VaspBox::calgh()
00135 {
00136 int i, j;
00137 double sum;
00138
00139 dstress[0]=vaspstress[0]-_EXTSTRESS[0][0];
00140 dstress[1]=vaspstress[1]-_EXTSTRESS[1][1];
00141 dstress[2]=vaspstress[2]-_EXTSTRESS[2][2];
00142 dstress[3]=vaspstress[3]-_EXTSTRESS[0][1];
00143 dstress[4]=vaspstress[4]-_EXTSTRESS[1][2];
00144 dstress[5]=vaspstress[5]-_EXTSTRESS[2][0];
00145
00146 for(i=0;i<6;i++)
00147 {
00148 sum = 0;
00149 for(j=0;j<6;j++)
00150 sum += Scompliance[i][j]*dstress[j];
00151 dstrain[i]=-sum;
00152 }
00153 gh.clear();
00154
00155 gh[0][0] = _H[0][0] * dstrain[0] * vaspboxstepsize;
00156 gh[1][1] = _H[1][1] * dstrain[1] * vaspboxstepsize;
00157 gh[2][2] = _H[2][2] * dstrain[2] * vaspboxstepsize;
00158 gh[0][1] = _H[1][1] * dstrain[3] * vaspboxstepsize;
00159 gh[2][0] = _H[0][0] * dstrain[5] * vaspboxstepsize;
00160 gh[2][1] = _H[0][0] * dstrain[4] * vaspboxstepsize;
00161 }
00162
00163 void VaspBox::writeZXCGR()
00164 {
00165 FILE *fp;
00166 char extname[500], inLine[500];
00167 int n;
00168
00169 INFO("writeZXCGR: "<<zxcgrfile);
00170 LFile::SubHomeDir(zxcgrfile,extname);
00171 INFO("writeZXCGR: "<<extname);
00172
00173 fp=fopen(extname,"r");
00174 conj_step = 0;
00175 if(fp==NULL)
00176 {
00177 fp=fopen(extname,"w");
00178 }
00179 else
00180 {
00181 while(searchstringfromfile(inLine,
00182 "step =", 500, fp)!=-1)
00183 {
00184 sscanf(inLine+strlen("step ="),"%d",&conj_step);
00185 }
00186 fclose(fp);
00187 INFO("last step = "<<conj_step);
00188 fp=fopen(extname,"a");
00189 }
00190 conj_step ++;
00191
00192 fprintf(fp,"step = %d\n",conj_step);
00193
00194 n = 9;
00195
00196 fprintf(fp,"%d %e\n",n,_EPOT);
00197
00198 fprintf(fp,"%e %e %e\n",_H[0][0],gh[0][0],vaspstress[0]);
00199 fprintf(fp,"%e %e %e\n",_H[1][0],gh[1][0],vaspstress[3]);
00200 fprintf(fp,"%e %e %e\n",_H[2][0],gh[2][0],vaspstress[5]);
00201 fprintf(fp,"%e %e %e\n",_H[0][1],gh[0][1],vaspstress[3]);
00202 fprintf(fp,"%e %e %e\n",_H[1][1],gh[1][1],vaspstress[1]);
00203 fprintf(fp,"%e %e %e\n",_H[2][1],gh[2][1],vaspstress[4]);
00204 fprintf(fp,"%e %e %e\n",_H[0][2],gh[0][2],vaspstress[5]);
00205 fprintf(fp,"%e %e %e\n",_H[1][2],gh[1][2],vaspstress[4]);
00206 fprintf(fp,"%e %e %e\n",_H[2][2],gh[2][2],vaspstress[2]);
00207
00208
00209
00210
00211 fclose(fp);
00212 }
00213
00214 void VaspBox::movebox()
00215 {
00216 int i, j;
00217
00218 calgh();
00219 writeZXCGR();
00220
00221 for(i=0;i<3;i++)
00222 for(j=0;j<3;j++)
00223 _H[i][j]+=gh[i][j];
00224 }
00225
00226 void VaspBox::scalebox()
00227 {
00228 int i, j;
00229 double pres, invB;
00230
00231
00232 pres = ( (vaspstress[0]-_EXTSTRESS[0][0])
00233 +(vaspstress[1]-_EXTSTRESS[1][1])
00234 +(vaspstress[2]-_EXTSTRESS[2][2]) ) / 3;
00235
00236 invB = Scompliance[0][0] + Scompliance[0][1] + Scompliance[0][2];
00237
00238 for(i=0;i<3;i++)
00239 for(j=0;j<3;j++)
00240 {
00241 gh[i][j] = -_H[i][j] * pres * invB * vaspboxstepsize;
00242 }
00243
00244 writeZXCGR();
00245
00246 for(i=0;i<3;i++)
00247 for(j=0;j<3;j++)
00248 _H[i][j]+=gh[i][j];
00249 }
00250
00251 class VaspBox sim;
00252
00253
00254 #include "main.cpp"
00255