├── README.md ├── density_cal.m ├── example ├── CHG ├── CHG.jpg ├── POSCAR ├── README.md ├── XDATCAR ├── density_cal.m └── read_xdatcar.m └── read_xdatcar.m /README.md: -------------------------------------------------------------------------------- 1 | ## What is AIMD_Toolkit 2 | 3 | AIMD_Toolkit is a set of Matlab scripts to process the ab-initio molecular dynamics trajectories. 4 | 5 | ## Current features 6 | With AIMD_Toolkit you can 7 | - Read the trajectories from a VASP output XDATCAR 8 | - Create nuclear density with the CHG format, which is readable by VESTA 9 | 10 | ## Citing AIMD_Toolkit 11 | Please cite the following paper if you use this code 12 | - Lu, Ziheng, et al. Physical Chemistry Chemical Physics 17.48 (2015): 32547-32555. 13 | - Lu, Ziheng, and Francesco Ciucci. Journal of Materials Chemistry A 4.45 (2016): 17740-17748. 14 | -------------------------------------------------------------------------------- /density_cal.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MIT License 3 | % 4 | % Copyright (c) 2017 Ziheng LU 5 | % 6 | % Permission is hereby granted, free of charge, to any person obtaining a copy 7 | % of this software and associated documentation files (the "Software"), to deal 8 | % in the Software without restriction, including without limitation the rights 9 | % to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | % copies of the Software, and to permit persons to whom the Software is 11 | % furnished to do so, subject to the following conditions: 12 | % 13 | % The above copyright notice and this permission notice shall be included in all 14 | % copies or substantial portions of the Software. 15 | % 16 | % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | % IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | % FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | % AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | % LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | % SOFTWARE. 23 | % 24 | % 25 | % To use the code, please copy the POSCAR and XDATCAR file 26 | % to matlab working directory. Set the user defined parameters and run the code. 27 | % 28 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 29 | clear; 30 | clc; 31 | 32 | %user defined parameters 33 | trajname='XDATCAR'; %trajectory name in XDATCAR format 34 | N_step=10; %number of simulation steps 35 | N=30; %define N grid 36 | Nparticle=1:8; %define particles to be included in the density calculation 37 | r=0.8; %radius 38 | 39 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 40 | %starting reading data 41 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 42 | [data,lattice]=read_xdatcar(trajname,N_step); 43 | fprintf(' done reading data\n start calculating density...\n'); 44 | 45 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 46 | %starting reading data 47 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 48 | spacing=1.0/(N-1); 49 | Nsteps=N_step;%size(data); 50 | density=zeros(N,N,N); 51 | 52 | %Doing the counting 53 | for s_index=1:Nsteps 54 | for p_index=Nparticle 55 | for x_ii=1:N 56 | for y_ii=1:N 57 | for z_ii=1:N 58 | x_coor=rem(data(p_index,2,s_index)+999,1); 59 | y_coor=rem(data(p_index,3,s_index)+999,1); 60 | z_coor=rem(data(p_index,4,s_index)+999,1); 61 | vector=[((x_ii)/N-x_coor) ((y_ii)/N-y_coor) ((z_ii)/N-z_coor)]; 62 | 63 | if norm(lattice*vector')<=r 64 | density(x_ii,y_ii,z_ii)=density(x_ii,y_ii,z_ii)+1; 65 | end 66 | end 67 | end 68 | end 69 | end 70 | s_index 71 | end 72 | density=density/N_step; 73 | fprintf(' done calculating raw density (normalized by simulation steps)\n'); 74 | 75 | temp=density(:,:,1)+density(:,:,N); 76 | density(:,:,1)=temp; 77 | density(:,:,N)=temp; 78 | temp=density(:,1,:)+density(:,N,:); 79 | density(:,1,:)=temp; 80 | density(:,N,:)=temp; 81 | temp=density(1,:,:)+density(N,:,:); 82 | density(1,:,:)=temp; 83 | density(N,:,:)=temp; 84 | 85 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 86 | % writing to CHG file 87 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 88 | %append the POSCAR to CHGCAR first 89 | if exist('CHG', 'file') 90 | % File exists. Do stuff.... 91 | system('del CHG'); 92 | end 93 | 94 | system('type POSCAR>>CHG'); 95 | 96 | %open file and write the grid number 97 | fileID = fopen('CHG','a'); 98 | fprintf(fileID,'\n\r'); 99 | %fprintf(fileID,' ',size(density)); 100 | fprintf(fileID,'%d ',size(density)); 101 | fprintf(fileID,'\n'); 102 | 103 | % four data in a line 104 | count=0; 105 | for k=1:N 106 | for j=1:N 107 | for i=1:N 108 | count=count+1; 109 | if rem(count,4)==0 110 | fprintf(fileID,' %E\n',density(i,j,k)); 111 | else 112 | fprintf(fileID,' %E',density(i,j,k)); 113 | end 114 | end 115 | end 116 | end 117 | fclose(fileID); 118 | -------------------------------------------------------------------------------- /example/CHG.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luzihen/AIMD_Toolkit/1f5eda3b5b2b3198747653569cb71120a5ec1d8b/example/CHG.jpg -------------------------------------------------------------------------------- /example/POSCAR: -------------------------------------------------------------------------------- 1 | Building Supercell From CONTCAR File 2 | 1.00000000000000 3 | 11.6929931641000007 0.0000000000000000 0.0000000000000000 4 | 0.0000000000000000 17.4283905029999993 0.0000000000000000 5 | 7.4164336527000003 0.0000000000000000 11.6569747749000001 6 | Mg B H 7 | 8 96 96 8 | Direct 9 | 0.960290420000000 0.00519919000000000 0.280912060000000 10 | 0.960473900000000 0.505262340000000 0.280110740000000 11 | 0.0333642300000000 0.465531060000000 0.733258000000000 12 | 0.0333599500000000 0.965562390000000 0.732850540000000 13 | 0.536760970000000 0.291348240000000 0.240472680000000 14 | 0.537847020000000 0.791322220000000 0.239943920000000 15 | 0.482319910000000 0.207890730000000 0.747738200000000 16 | 0.483737760000000 0.706242650000000 0.746407970000000 17 | 0.949243310000000 0.436035860000000 0.929065810000000 18 | 0.948265100000000 0.937086040000000 0.929693900000000 19 | 0.172547780000000 0.480732340000000 0.428719200000000 20 | 0.172752300000000 0.981469160000000 0.429741540000000 21 | 0.824064000000000 0.00224373000000000 0.555693830000000 22 | 0.824909230000000 0.501669680000000 0.556424100000000 23 | 0.0234031800000000 0.0469099300000000 0.0923876000000000 24 | 0.0230825200000000 0.547585750000000 0.0922185900000000 25 | 0.0897808200000000 0.484770130000000 0.348195280000000 26 | 0.0890075600000000 0.984215490000000 0.348727340000000 27 | 0.946782350000000 0.0544620400000000 0.929281530000000 28 | 0.946174790000000 0.554126340000000 0.930031310000000 29 | 0.0329029700000000 0.449205190000000 0.0997928100000000 30 | 0.0319288800000000 0.947836850000000 0.0996505900000000 31 | 0.896697170000000 0.498713800000000 0.643057730000000 32 | 0.896254300000000 0.996959220000000 0.643739590000000 33 | 0.525105690000000 0.320284030000000 0.0632216600000000 34 | 0.523717810000000 0.820169490000000 0.0651204600000000 35 | 0.419819090000000 0.244728650000000 0.625175660000000 36 | 0.419401210000000 0.744954700000000 0.625634640000000 37 | 0.639928960000000 0.281820870000000 0.348462330000000 38 | 0.640632610000000 0.781586160000000 0.347548010000000 39 | 0.430235540000000 0.188923100000000 0.958567190000000 40 | 0.428937380000000 0.689438820000000 0.959604240000000 41 | 0.466907890000000 0.162530990000000 0.519295100000000 42 | 0.466814160000000 0.662601440000000 0.519800080000000 43 | 0.649330930000000 0.279150980000000 0.939646810000000 44 | 0.648892100000000 0.779348040000000 0.938755960000000 45 | 0.301198650000000 0.229172470000000 0.0793212200000000 46 | 0.303838260000000 0.729406390000000 0.0770277200000000 47 | 0.585375170000000 0.359316680000000 0.461171050000000 48 | 0.585158290000000 0.859215030000000 0.462019680000000 49 | 0.0958644700000000 0.494481670000000 0.874665580000000 50 | 0.0962995100000000 0.995261200000000 0.874141190000000 51 | 0.105744090000000 0.0649408600000000 0.412998240000000 52 | 0.107132620000000 0.565708620000000 0.411343860000000 53 | 0.873774240000000 0.420136910000000 0.575549130000000 54 | 0.876732020000000 0.921068960000000 0.574613800000000 55 | 0.866481190000000 0.482622190000000 0.165771740000000 56 | 0.864331740000000 0.981421810000000 0.165992470000000 57 | 0.0619000400000000 0.411702950000000 0.433987050000000 58 | 0.0609021600000000 0.911173120000000 0.434023730000000 59 | 0.813935880000000 0.496306900000000 0.0547106400000000 60 | 0.814218700000000 0.996244750000000 0.0543289600000000 61 | 0.162881720000000 0.499269800000000 0.971877610000000 62 | 0.160865660000000 0.998117450000000 0.972236480000000 63 | 0.942066420000000 0.0821621800000000 0.543640630000000 64 | 0.942299970000000 0.582162880000000 0.542713300000000 65 | 0.526499060000000 0.340228630000000 0.939486530000000 66 | 0.527150490000000 0.839793550000000 0.939630080000000 67 | 0.587772130000000 0.204233170000000 0.549037170000000 68 | 0.588128890000000 0.703728780000000 0.548525040000000 69 | 0.486202340000000 0.320695040000000 0.418650240000000 70 | 0.486064080000000 0.820817620000000 0.417905580000000 71 | 0.431665110000000 0.158553690000000 0.0754223400000000 72 | 0.429685420000000 0.659205320000000 0.0749088400000000 73 | 0.377618430000000 0.242761920000000 0.523899680000000 74 | 0.378713070000000 0.743231740000000 0.521689550000000 75 | 0.568773150000000 0.216775650000000 0.0689388100000000 76 | 0.568097830000000 0.715595040000000 0.0692940500000000 77 | 0.389887080000000 0.281729850000000 0.947684000000000 78 | 0.391109980000000 0.781331890000000 0.946311840000000 79 | 0.694742310000000 0.280328660000000 0.435918300000000 80 | 0.693911790000000 0.780456700000000 0.438023420000000 81 | 0.375323890000000 0.329986470000000 0.0718160700000000 82 | 0.373444170000000 0.830445460000000 0.0725689300000000 83 | 0.569576460000000 0.308020990000000 0.578196120000000 84 | 0.570728570000000 0.809238250000000 0.576624750000000 85 | 0.501764200000000 0.208223680000000 0.398651620000000 86 | 0.500770110000000 0.708813060000000 0.399391480000000 87 | 0.598213390000000 0.182552310000000 0.940598660000000 88 | 0.598258940000000 0.682375890000000 0.940710270000000 89 | 0.0955324200000000 0.412908680000000 0.929837860000000 90 | 0.0952491600000000 0.913210800000000 0.929222970000000 91 | 0.0768494100000000 0.0475280200000000 0.550521670000000 92 | 0.0773611800000000 0.547378820000000 0.551702910000000 93 | 0.919852750000000 0.452578280000000 0.439405800000000 94 | 0.920480880000000 0.953328760000000 0.440262400000000 95 | 0.900213990000000 0.0716472100000000 0.0781986400000000 96 | 0.900474240000000 0.571367290000000 0.0789012700000000 97 | 0.383289940000000 0.248289440000000 0.158203950000000 98 | 0.381650430000000 0.748384460000000 0.158076360000000 99 | 0.424221850000000 0.337764170000000 0.563628500000000 100 | 0.423974830000000 0.837192290000000 0.565215370000000 101 | 0.639230110000000 0.187725710000000 0.399021410000000 102 | 0.639621060000000 0.688388840000000 0.400373950000000 103 | 0.555564940000000 0.250782050000000 0.874688160000000 104 | 0.556739450000000 0.749537290000000 0.875079170000000 105 | 0.896440330000000 0.415587980000000 0.0599747700000000 106 | 0.894851490000000 0.915178960000000 0.0609881300000000 107 | 0.0576896600000000 0.447265500000000 0.556692250000000 108 | 0.0584755000000000 0.948221590000000 0.555891590000000 109 | 0.958905280000000 0.0572215500000000 0.421581180000000 110 | 0.958875320000000 0.556855720000000 0.422497270000000 111 | 0.0833431000000000 0.0828799100000000 0.959837050000000 112 | 0.0824398200000000 0.584631090000000 0.960275020000000 113 | 0.910048660000000 0.396540570000000 0.874819880000000 114 | 0.908247930000000 0.897437180000000 0.873320950000000 115 | 0.269420010000000 0.475950730000000 0.422320450000000 116 | 0.272755080000000 0.974655110000000 0.423434110000000 117 | 0.717712700000000 0.0239845000000000 0.568222500000000 118 | 0.708845780000000 0.525916940000000 0.572382620000000 119 | 0.0217238500000000 0.0996007300000000 0.153504940000000 120 | 0.0267133800000000 0.596728470000000 0.150526120000000 121 | 0.138891200000000 0.486534220000000 0.237402470000000 122 | 0.133262400000000 0.986600300000000 0.245477820000000 123 | 0.975123210000000 0.101065430000000 0.842213260000000 124 | 0.968161150000000 0.602586600000000 0.850479960000000 125 | 0.0682205900000000 0.412873190000000 0.154633420000000 126 | 0.0632811000000000 0.914019400000000 0.154187780000000 127 | 0.836333190000000 0.0188578200000000 0.748351880000000 128 | 0.832443420000000 0.517841530000000 0.752136290000000 129 | 0.509528980000000 0.366373680000000 0.140454380000000 130 | 0.513328030000000 0.864883400000000 0.133193520000000 131 | 0.338676540000000 0.251246780000000 0.733387640000000 132 | 0.338550760000000 0.750547710000000 0.728892300000000 133 | 0.719764280000000 0.286886320000000 0.238351340000000 134 | 0.718936580000000 0.786689280000000 0.237984400000000 135 | 0.388630190000000 0.141385510000000 0.915904220000000 136 | 0.395497840000000 0.639430630000000 0.912865450000000 137 | 0.439160920000000 0.0995225600000000 0.503811040000000 138 | 0.440878000000000 0.601176120000000 0.502382220000000 139 | 0.782353530000000 0.280911660000000 0.867494330000000 140 | 0.786439360000000 0.781584270000000 0.871609530000000 141 | 0.181288270000000 0.221782150000000 0.131231900000000 142 | 0.178928230000000 0.724011470000000 0.134321710000000 143 | 0.644889140000000 0.417429650000000 0.450745470000000 144 | 0.644482490000000 0.918489530000000 0.448626860000000 145 | 0.173660280000000 0.00964619000000000 0.766960790000000 146 | 0.170988320000000 0.512245470000000 0.768418480000000 147 | 0.176328860000000 0.121820280000000 0.358884410000000 148 | 0.163984120000000 0.627497490000000 0.361832840000000 149 | 0.824861300000000 0.353509870000000 0.629529400000000 150 | 0.830114830000000 0.857054110000000 0.626993060000000 151 | 0.803514450000000 0.462896100000000 0.262312580000000 152 | 0.800193300000000 0.960999780000000 0.261543540000000 153 | 0.0667661700000000 0.337833860000000 0.428084500000000 154 | 0.0667638500000000 0.835612020000000 0.428782210000000 155 | 0.692790720000000 0.00354634000000000 0.0938708800000000 156 | 0.694324340000000 0.504308400000000 0.0928749200000000 157 | 0.276027940000000 0.491540470000000 0.926266650000000 158 | 0.283609550000000 0.993768170000000 0.920739520000000 159 | 0.918326120000000 0.142132080000000 0.572761250000000 160 | 0.917272980000000 0.645873070000000 0.573819100000000 161 | 0.564469690000000 0.400294890000000 0.892484220000000 162 | 0.565920950000000 0.904037120000000 0.894483330000000 163 | 0.624881670000000 0.153050710000000 0.595613790000000 164 | 0.619590960000000 0.654115390000000 0.595528540000000 165 | 0.405609000000000 0.354517500000000 0.399455530000000 166 | 0.403851440000000 0.855396390000000 0.404627280000000 167 | 0.411296970000000 0.104330480000000 0.137237820000000 168 | 0.410436070000000 0.602410900000000 0.133769150000000 169 | 0.266428460000000 0.265152170000000 0.538876470000000 170 | 0.267230280000000 0.763219840000000 0.537352670000000 171 | 0.611781530000000 0.194629220000000 0.127348890000000 172 | 0.604255580000000 0.692901300000000 0.132453550000000 173 | 0.351626650000000 0.304848050000000 0.880756840000000 174 | 0.353589430000000 0.808802200000000 0.882634130000000 175 | 0.810832880000000 0.296208770000000 0.409232700000000 176 | 0.815527840000000 0.795398840000000 0.406593740000000 177 | 0.289211160000000 0.377867980000000 0.131219670000000 178 | 0.293472660000000 0.878551940000000 0.136539840000000 179 | 0.619870740000000 0.333649830000000 0.626231660000000 180 | 0.616375970000000 0.833514830000000 0.629892910000000 181 | 0.480321770000000 0.201744320000000 0.333875820000000 182 | 0.475924150000000 0.701922930000000 0.333652000000000 183 | 0.655146310000000 0.123121780000000 0.894079720000000 184 | 0.653909350000000 0.621344730000000 0.901978740000000 185 | 0.153983390000000 0.352238680000000 0.886822220000000 186 | 0.152052100000000 0.853322590000000 0.890720050000000 187 | 0.0868730200000000 0.0775983300000000 0.627825690000000 188 | 0.0810005600000000 0.579903120000000 0.627122880000000 189 | 0.869050500000000 0.416036000000000 0.387674960000000 190 | 0.867914340000000 0.918334890000000 0.390623450000000 191 | 0.830143270000000 0.131350110000000 0.130625730000000 192 | 0.832291960000000 0.628077690000000 0.137319300000000 193 | 0.343476720000000 0.261475710000000 0.252990100000000 194 | 0.341691260000000 0.762843940000000 0.249140030000000 195 | 0.365988600000000 0.385217430000000 0.623909150000000 196 | 0.359347150000000 0.884903490000000 0.623800800000000 197 | 0.712894930000000 0.136197630000000 0.339252770000000 198 | 0.719388760000000 0.635326340000000 0.332504970000000 199 | 0.616890960000000 0.268759620000000 0.768905370000000 200 | 0.621558860000000 0.769576840000000 0.762436820000000 201 | 0.857692970000000 0.349605140000000 0.110816700000000 202 | 0.855722090000000 0.849192670000000 0.113431970000000 203 | 0.113667400000000 0.396349260000000 0.585978060000000 204 | 0.109524060000000 0.894934880000000 0.586535180000000 205 | 0.912288480000000 0.0857136200000000 0.377552830000000 206 | 0.912533130000000 0.585102370000000 0.377802510000000 207 | 0.149588040000000 0.146937380000000 0.922084080000000 208 | 0.152519000000000 0.647235610000000 0.920600030000000 -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | ## How to run 2 | To run this code, simply add the example folder to your matlab working directory. Then run density_cal.m. This should give you a CHG file which is readable by VESTA. (http://jp-minerals.org/vesta/en/) -------------------------------------------------------------------------------- /example/density_cal.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MIT License 3 | % 4 | % Copyright (c) 2017 Ziheng LU 5 | % 6 | % Permission is hereby granted, free of charge, to any person obtaining a copy 7 | % of this software and associated documentation files (the "Software"), to deal 8 | % in the Software without restriction, including without limitation the rights 9 | % to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | % copies of the Software, and to permit persons to whom the Software is 11 | % furnished to do so, subject to the following conditions: 12 | % 13 | % The above copyright notice and this permission notice shall be included in all 14 | % copies or substantial portions of the Software. 15 | % 16 | % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | % IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | % FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | % AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | % LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | % SOFTWARE. 23 | % 24 | % 25 | % To use the code, please copy the POSCAR and XDATCAR file 26 | % to matlab working directory. Set the user defined parameters and run the code. 27 | % 28 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 29 | clear; 30 | clc; 31 | 32 | %user defined parameters 33 | trajname='XDATCAR'; %trajectory name in lammpstrj format 34 | N_step=10; %number of simulation steps 35 | N=30; %define N grid 36 | Nparticle=1:8; %define particles to be included in the density calculation 37 | r=0.8; %radius 38 | 39 | 40 | 41 | 42 | 43 | 44 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 45 | %starting reading data 46 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 47 | [data,lattice]=read_xdatcar(trajname,N_step); 48 | fprintf(' done reading data\n start calculating density...\n'); 49 | 50 | 51 | 52 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 53 | %starting reading data 54 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 55 | spacing=1.0/(N-1); 56 | Nsteps=N_step;%size(data); 57 | density=zeros(N,N,N); 58 | 59 | %Doing the counting 60 | for s_index=1:Nsteps 61 | for p_index=Nparticle 62 | for x_ii=1:N 63 | for y_ii=1:N 64 | for z_ii=1:N 65 | x_coor=rem(data(p_index,2,s_index)+999,1); 66 | y_coor=rem(data(p_index,3,s_index)+999,1); 67 | z_coor=rem(data(p_index,4,s_index)+999,1); 68 | vector=[((x_ii)/N-x_coor) ((y_ii)/N-y_coor) ((z_ii)/N-z_coor)]; 69 | 70 | if norm(lattice*vector')<=r 71 | density(x_ii,y_ii,z_ii)=density(x_ii,y_ii,z_ii)+1; 72 | end 73 | end 74 | end 75 | end 76 | end 77 | s_index 78 | end 79 | density=density/N_step; 80 | fprintf(' done calculating raw density (normalized by simulation steps)\n'); 81 | 82 | temp=density(:,:,1)+density(:,:,N); 83 | density(:,:,1)=temp; 84 | density(:,:,N)=temp; 85 | temp=density(:,1,:)+density(:,N,:); 86 | density(:,1,:)=temp; 87 | density(:,N,:)=temp; 88 | temp=density(1,:,:)+density(N,:,:); 89 | density(1,:,:)=temp; 90 | density(N,:,:)=temp; 91 | 92 | 93 | 94 | 95 | 96 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 97 | % writing to CHG file 98 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%% 99 | %append the POSCAR to CHGCAR first 100 | if exist('CHG', 'file') 101 | % File exists. Do stuff.... 102 | system('del CHG'); 103 | end 104 | 105 | system('type POSCAR>>CHG'); 106 | 107 | %open file and write the grid number 108 | fileID = fopen('CHG','a'); 109 | fprintf(fileID,'\n\r'); 110 | %fprintf(fileID,' ',size(density)); 111 | fprintf(fileID,'%d ',size(density)); 112 | fprintf(fileID,'\n'); 113 | 114 | % four data in a line 115 | count=0; 116 | for k=1:N 117 | for j=1:N 118 | for i=1:N 119 | count=count+1; 120 | if rem(count,4)==0 121 | fprintf(fileID,' %E\n',density(i,j,k)); 122 | else 123 | fprintf(fileID,' %E',density(i,j,k)); 124 | end 125 | end 126 | end 127 | end 128 | fclose(fileID); 129 | -------------------------------------------------------------------------------- /example/read_xdatcar.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MIT License 3 | % 4 | % Copyright (c) 2017 Ziheng LU 5 | % 6 | % Permission is hereby granted, free of charge, to any person obtaining a copy 7 | % of this software and associated documentation files (the "Software"), to deal 8 | % in the Software without restriction, including without limitation the rights 9 | % to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | % copies of the Software, and to permit persons to whom the Software is 11 | % furnished to do so, subject to the following conditions: 12 | % 13 | % The above copyright notice and this permission notice shall be included in all 14 | % copies or substantial portions of the Software. 15 | % 16 | % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | % IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | % FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | % AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | % LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | % SOFTWARE. 23 | % 24 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 25 | 26 | function [data,lattice] = read_xdatcar(filename_in,fframes_in) 27 | % this function reads in the XDATCAR of a vasp AIMD simulations 28 | 29 | % Input: fileanme_in - the name of your vasp XDATCAR in a string format. 30 | % Usually it is 'XDATCAR'. This version does not support varible cell MD. 31 | % Pls carefully check the XDATCAR format before using it. 32 | 33 | % Output: data - a 3-dimentional matrix(N,M,t). N is the number of 34 | % particles. M = 1 is the atom type. M = 2:4 are the x,y,z fractional 35 | % coordinates. t is the simulation steps. lattice - a 3x3 matrix 36 | % of the lattice vector. 37 | 38 | 39 | filename=filename_in; 40 | fframes=fframes_in; 41 | 42 | 43 | fid = fopen(filename); 44 | 45 | fprintf('start reading XDATCAR file\n'); 46 | % line 1 system_name 47 | sys_name = fgetl(fid); 48 | % line 2 lattice_mutiplier 49 | lattice_mutiplier = fgetl(fid); 50 | % lattice vector 1 51 | tline = fgetl(fid); 52 | lattice_a=strread(tline); 53 | % lattice vector 2 54 | tline = fgetl(fid); 55 | lattice_b=strread(tline); 56 | % lattice vector 3 57 | tline = fgetl(fid); 58 | lattice_c=strread(tline); 59 | % number of atoms 60 | tline = fgetl(fid); 61 | tline = fgetl(fid); 62 | num_atoms=strread(tline); 63 | if num_atoms(end)==0 64 | num_atoms(end)=[]; 65 | end 66 | tot_num=sum(num_atoms); 67 | 68 | %creat an array for coordinate 69 | data=[]; 70 | 71 | 72 | 73 | frames=1; 74 | while frames<=fframes 75 | data_temp=[]; 76 | %read corredinate 77 | tline = fgetl(fid);%line 'Direct' 78 | for num_type=1:size(num_atoms,2)%read type by type 79 | for atom=1:num_atoms(num_type) 80 | tline = fgetl(fid); 81 | coordinate=strread(tline); 82 | %if num_atoms(end)==0 83 | if size(coordinate,2)==4 84 | coordinate(4)=[]; 85 | end 86 | coordinate=[num_type coordinate]; 87 | data_temp=[data_temp;coordinate]; 88 | end 89 | end 90 | data(:,:,frames)=data_temp; 91 | % for ii=1:6 92 | % tline = fgetl(fid); 93 | % end 94 | frames=frames+1; 95 | end 96 | fclose(fid); 97 | 98 | lattice=[lattice_a;lattice_b;lattice_c]; 99 | end -------------------------------------------------------------------------------- /read_xdatcar.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % MIT License 3 | % 4 | % Copyright (c) 2017 Ziheng LU 5 | % 6 | % Permission is hereby granted, free of charge, to any person obtaining a copy 7 | % of this software and associated documentation files (the "Software"), to deal 8 | % in the Software without restriction, including without limitation the rights 9 | % to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | % copies of the Software, and to permit persons to whom the Software is 11 | % furnished to do so, subject to the following conditions: 12 | % 13 | % The above copyright notice and this permission notice shall be included in all 14 | % copies or substantial portions of the Software. 15 | % 16 | % THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | % IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | % FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | % AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | % LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | % OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | % SOFTWARE. 23 | % 24 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 25 | 26 | function [data,lattice] = read_xdatcar(filename_in,fframes_in) 27 | % this function reads in the XDATCAR of a vasp AIMD simulations 28 | 29 | % Input: fileanme_in - the name of your vasp XDATCAR in a string format. 30 | % Usually it is 'XDATCAR'. This version does not support varible cell MD. 31 | % Pls carefully check the XDATCAR format before using it. 32 | 33 | % Output: data - a 3-dimentional matrix(N,M,t). N is the number of 34 | % particles. M = 1 is the atom type. M = 2:4 are the x,y,z fractional 35 | % coordinates. t is the simulation steps. lattice - a 3x3 matrix 36 | % of the lattice vector. 37 | 38 | 39 | filename=filename_in; 40 | fframes=fframes_in; 41 | 42 | 43 | fid = fopen(filename); 44 | 45 | fprintf('start reading XDATCAR file\n'); 46 | % line 1 system_name 47 | sys_name = fgetl(fid); 48 | % line 2 lattice_mutiplier 49 | lattice_mutiplier = fgetl(fid); 50 | % lattice vector 1 51 | tline = fgetl(fid); 52 | lattice_a=strread(tline); 53 | % lattice vector 2 54 | tline = fgetl(fid); 55 | lattice_b=strread(tline); 56 | % lattice vector 3 57 | tline = fgetl(fid); 58 | lattice_c=strread(tline); 59 | % number of atoms 60 | tline = fgetl(fid); 61 | tline = fgetl(fid); 62 | num_atoms=strread(tline); 63 | if num_atoms(end)==0 64 | num_atoms(end)=[]; 65 | end 66 | tot_num=sum(num_atoms); 67 | 68 | %creat an array for coordinate 69 | data=[]; 70 | 71 | 72 | 73 | frames=1; 74 | while frames<=fframes 75 | data_temp=[]; 76 | %read corredinate 77 | tline = fgetl(fid);%line 'Direct' 78 | for num_type=1:size(num_atoms,2)%read type by type 79 | for atom=1:num_atoms(num_type) 80 | tline = fgetl(fid); 81 | coordinate=strread(tline); 82 | %if num_atoms(end)==0 83 | if size(coordinate,2)==4 84 | coordinate(4)=[]; 85 | end 86 | coordinate=[num_type coordinate]; 87 | data_temp=[data_temp;coordinate]; 88 | end 89 | end 90 | data(:,:,frames)=data_temp; 91 | % for ii=1:6 92 | % tline = fgetl(fid); 93 | % end 94 | frames=frames+1; 95 | end 96 | fclose(fid); 97 | 98 | lattice=[lattice_a;lattice_b;lattice_c]; 99 | end --------------------------------------------------------------------------------