materialsproject / pymatgen

Python Materials Genomics (pymatgen) is a robust materials analysis code that defines classes for structures and molecules with support for many electronic structure codes. It powers the Materials Project.
https://pymatgen.org
Other
1.51k stars 864 forks source link

pymatgen.io.gaussian write_file() gives wrong output when cart_coords=False #1944

Closed wudihua closed 1 year ago

wudihua commented 4 years ago

Describe the bug In some cases pymatgen.io.gaussian write_file() gives wrong output when cart_coords=False

To Reproduce Here is the code to reproduce this issue.

#!/usr/bin/env python

import pymatgen.io.xyz as xyzio
import pymatgen.io.gaussian as gaussianio

xyz_filename = "input.xyz"
xyz = xyzio.Molecule.from_file(xyz_filename)
gjf = gaussianio.GaussianInput(xyz)
gjf_filename = "output.gjf"
gjf.write_file(gjf_filename,cart_coords=False) # Bad structure
gjf.write_file("cart_"+gjf_filename,cart_coords=True) # Good structure

The input.xyz is the following:

368

 N                  0.00000000    0.00000000    0.00000000
 B                  0.00000000    0.00000000    1.46015992
 N                  1.25732289    0.00000000    2.19736263
 B                  1.25261991    0.03549742    3.65365385
 N                  2.51549326    0.02806676    4.38132181
 B                  2.50997160    0.05650466    5.83894909
 N                  3.77191199    0.04215517    6.56692842
 B                  3.76815411    0.06965821    8.02550727
 N                  5.03084433    0.06452678    8.75174618
 B                  5.02796191    0.08708214   10.21180961
 N                  6.29541849    0.10821986   10.93564957
 B                  6.28247833    0.08613305   12.39412455
 N                  7.54559625    0.05381271   13.12803913
 B                  7.53859372    0.06111702   14.58548129
 N                  8.80105702    0.02888990   15.31550304
 B                  8.79422011    0.03328746   16.77313675
 N                 10.05390966   -0.01015904   17.50354864
 B                 10.05848155   -0.00720206   18.96076520
 N                 11.32066296   -0.00507892   19.69651073
 B                 12.50823722   -0.49521155   18.99936572
 N                 12.42784733   -0.84171398   17.58874183
 B                 11.23326591   -0.51355407   16.82113136
 N                 11.18595667   -0.79470248   15.39253350
 B                  9.98296323   -0.47528008   14.63326139
 N                  9.92873699   -0.77653922   13.20758313
 B                  8.72572982   -0.45592657   12.44766477
 N                  8.66426132   -0.77010942   11.02535181
 B                  7.46679841   -0.43204174   10.26058297
 N                  7.41426708   -0.75811093    8.83858839
 B                  6.21076930   -0.44889080    8.07512200
 N                  6.15572850   -0.77496332    6.65447245
 B                  4.95489366   -0.46575924    5.88889983
 N                  4.90501635   -0.78018609    4.46570478
 B                  3.70004121   -0.47834236    3.70327870
 N                  3.64509580   -0.80329604    2.28397418
 B                  2.43908193   -0.51079315    1.52301176
 N                  2.37663618   -0.83942908    0.10474481
 B                  1.20081757   -0.48645210   -0.67534843
 N                  1.22875767   -0.74705963   -2.11447842
 H                  0.53113171   -0.34190091   -2.70899625
 B                  2.15835612   -1.69649261   -2.63987571
 N                  3.13052271   -2.33492346   -1.78536904
 B                  3.30956812   -1.81919518   -0.43121460
 N                  4.36428491   -2.34687670    0.42456895
 B                  4.56623515   -1.79868914    1.75811355
 N                  5.61926523   -2.33739901    2.60951705
 B                  5.81745733   -1.78763845    3.94418212
 N                  6.86296302   -2.33553573    4.79948324
 B                  7.06246323   -1.78540046    6.13371688
 N                  8.09985094   -2.33972811    6.99384643
 B                  8.30954971   -1.78143039    8.32310709
 N                  9.34090811   -2.34692278    9.18213336
 B                  9.56855522   -1.78236097   10.50524829
 N                 10.61670239   -2.33707801   11.35128121
 B                 10.83576163   -1.78656858   12.68308433
 N                 11.88989680   -2.33652718   13.52474454
 B                 12.09739434   -1.79764370   14.86294576
 N                 13.14785801   -2.34760033   15.70808360
 B                 13.35896265   -1.82201432   17.05087326
 N                 14.44338760   -2.33576343   17.88344134
 B                 14.97390583   -3.65981947   17.56194895
 N                 15.89826717   -4.36187136   18.45105491
 H                 16.38854770   -3.85425336   19.16329573
 B                 15.97921696   -5.78763522   18.40120182
 N                 15.23760025   -6.54177996   17.41978309
 B                 14.58074949   -5.81423585   16.33732109
 N                 14.49646037   -4.36256788   16.38167510
 B                 13.66111877   -3.67326770   15.40813357
 N                 13.25541043   -4.35432476   14.18665968
 B                 12.40420627   -3.66530386   13.22660885
 N                 11.97985672   -4.35513094   12.01574734
 B                 11.13137224   -3.66559552   11.05430665
 N                 10.70457908   -4.35880481    9.84731483
 B                  9.86580439   -3.66912986    8.87973099
 N                  9.44550270   -4.36049304    7.67053874
 B                  8.61466649   -3.66732964    6.69646725
 N                  8.20630316   -4.35756927    5.48233758
 B                  7.37839660   -3.66474755    4.50485910
 N                  6.97565847   -4.35427115    3.28676130
 B                  6.13969052   -3.66424149    2.31291592
 N                  5.73252451   -4.35395400    1.09682725
 B                  4.88343778   -3.67177516    0.13063051
 N                  4.46357019   -4.35976118   -1.08283998
 B                  3.67548758   -3.65765463   -2.08415536
 N                  3.36173997   -4.36271266   -3.32684330
 H                  3.00264322   -3.85468526   -4.11263531
 B                  3.44733728   -5.78827440   -3.37149594
 N                  3.91577765   -6.54049805   -2.23223635
 B                  4.53060924   -5.81299530   -1.12604741
 N                  5.13616935   -6.53484222   -0.01490971
 B                  5.79024668   -5.80753041    1.06250132
 N                  6.39699997   -6.54081361    2.16588065
 B                  7.03687711   -5.80874421    3.24970467
 N                  7.63660688   -6.54245797    4.35667817
 B                  8.27490771   -5.81084556    5.44106734
 N                  8.87774519   -6.54372119    6.54540391
 B                  9.52104423   -5.81319065    7.62654587
 N                 10.12930522   -6.54375920    8.72777042
 B                 10.77875598   -5.81199678    9.80583430
 N                 11.39383451   -6.54285969   10.90340089
 B                 12.04608607   -5.80915740   11.97930944
 N                 12.66149846   -6.54034088   13.07714876
 B                 13.31557762   -5.80839202   14.15322073
 N                 13.93059353   -6.53697309   15.25197311
 B                 13.52017657   -7.91058664   15.49186440
 N                 13.88999372   -8.56481271   16.73941721
 B                 14.82521454   -7.92549620   17.65197408
 N                 15.23662860   -8.66696975   18.84294054
 H                 16.00777971   -8.33964884   19.39405359
 B                 14.42944986   -9.74940991   19.31077419
 N                 13.27718017  -10.19613210   18.56605478
 B                 13.07289221   -9.66900337   17.21952841
 N                 11.99365867  -10.17824777   16.38375816
 B                 11.82380348   -9.68853796   15.02407436
 N                 12.65000788   -8.59091289   14.54295781
 B                 12.26768540   -7.92256159   13.30741522
 N                 11.39294436   -8.60236926   12.36043952
 B                 11.00402350   -7.92785309   11.13024243
 N                 10.12468016   -8.60193167   10.18459935
 B                  9.74125221   -7.92922587    8.95228256
 N                  8.86566177   -8.60233952    8.00350861
 B                  8.48657940   -7.92829804    6.76965379
 N                  7.61285745   -8.60030691    5.81940333
 B                  7.23848771   -7.92503681    4.58401248
 N                  6.36382062   -8.59588722    3.63278903
 B                  5.98980271   -7.91984223    2.39708373
 N                  5.10812427   -8.58657840    1.44984275
 B                  4.72615144   -7.90865962    0.21912240
 N                  3.83351329   -8.56531340   -0.72633981
 B                  3.50968962   -7.92389719   -1.99140546
 N                  2.68061839   -8.66281699   -2.94301291
 H                  2.60388163   -8.34553590   -3.89054968
 B                  1.88128741   -9.75461196   -2.48360252
 N                  1.94314120  -10.19364292   -1.11019617
 B                  3.00869694   -9.67044317   -0.26053792
 N                  3.19395165  -10.18459723    1.09067416
 B                  4.28019602   -9.68649530    1.92157379
 N                  4.45476132  -10.20365745    3.27288611
 B                  5.53898872   -9.70031340    4.10423275
 N                  5.71367101  -10.21862500    5.45555715
 B                  6.79443936   -9.71002565    6.28904691
 N                  6.97202888  -10.22817532    7.63929805
 B                  8.05071061   -9.71483848    8.47303827
 N                  8.23039097  -10.22984228    9.82356039
 B                  9.31000269   -9.71379289   10.65580521
 N                  9.49154699  -10.22737507   12.00712228
 B                 10.57049722   -9.70649348   12.83799927
 N                 10.74524635  -10.20960689   14.19385952
 B                  9.61586943  -10.85693611   14.84680604
 N                  9.65506800  -11.11777353   16.27997567
 B                 10.86582935  -10.82190587   17.03475556
 N                 10.90617094  -11.05838577   18.47183207
 B                 12.13760589  -10.84282565   19.21586436
 N                 12.15067946  -11.18603379   20.63693686
 H                 13.02016013  -11.21808311   21.13587892
 B                 10.91345445  -11.27842448   21.34653134
 N                  9.65039216  -11.15709956   20.65976768
 B                  9.64951309  -11.15346994   19.19909560
 N                  8.39253330  -11.15008223   18.46164727
 B                  8.39494231  -11.19142462   17.00627149
 N                  7.13166254  -11.18820529   16.27807768
 B                  7.13719008  -11.21389795   14.82063435
 N                  8.39752067  -11.12989862   14.09447815
 B                  8.35900520  -10.87150937   12.66056390
 N                  7.14038351  -11.14148944   11.90799550
 B                  7.10059799  -10.88022662   10.47505320
 N                  5.88269139  -11.15045936    9.72131008
 B                  5.84290100  -10.88121032    8.28940915
 N                  4.62504528  -11.14888160    7.53563592
 B                  4.58537054  -10.87348000    6.10497678
 N                  3.36705502  -11.13770919    5.35053527
 B                  3.32748949  -10.85968718    3.91994628
 N                  2.10844084  -11.12121251    3.16611439
 B                  2.06384865  -10.82774545    1.74003964
 N                  0.84279827  -11.07214516    0.98405567
 B                  0.81655605  -10.85367998   -0.45424683
 N                 -0.40470552  -11.20382549   -1.17896077
 H                 -0.39477524  -11.25498834   -2.17991969
 B                 -1.63817734  -11.31309847   -0.46579726
 N                 -1.67912519  -11.17887246    0.97056085
 B                 -0.41716411  -11.17388619    1.70498886
 N                 -0.41227627  -11.17359552    3.16266586
 B                  0.84876864  -11.20407178    3.89083559
 N                  0.84411039  -11.19194744    5.34836603
 B                  2.10626429  -11.21981437    6.07609470
 N                  2.10186652  -11.19860570    7.53350640
 B                  3.36403679  -11.22814361    8.26215439
 N                  3.35931725  -11.20681425    9.71922403
 B                  4.62156436  -11.22929426   10.44779880
 N                  4.61741855  -11.20295975   11.90463908
 B                  5.87963116  -11.22383206   12.63362858
 N                  5.87481733  -11.19738685   14.09132103
 B                  4.68898696  -10.69106746   14.77063362
 N                  4.74206996  -10.37763818   16.19347424
 B                  5.94639795  -10.68001403   16.95594756
 N                  5.99887638  -10.36290674   18.37771286
 B                  7.20819487  -10.64688342   19.13753239
 N                  7.27114642  -10.32115753   20.55671819
 B                  8.44963756  -10.66762421   21.33566073
 N                  8.42513940  -10.40474199   22.77388754
 H                  9.13091225  -10.79957234   23.36659589
 B                  7.49445972   -9.45680985   23.29926690
 N                  6.51103987   -8.83016694   22.44935696
 B                  6.33591440   -9.34321131   21.09293122
 N                  5.27962931   -8.81914463   20.23767427
 B                  5.07664260   -9.36645803   18.90381974
 N                  4.02080406   -8.83159715   18.05533621
 B                  3.82134326   -9.37835433   16.71960205
 N                  2.76987365   -8.83754399   15.86924455
 B                  2.56704714   -9.38360165   14.53356689
 N                  3.48792874  -10.38230027   14.00680524
 B                  3.43217733  -10.69755158   12.58554634
 N                  2.22963968  -10.38869010   11.82331328
 B                  2.17469320  -10.70121988   10.40155998
 N                  0.97110252  -10.39182096    9.64053825
 B                  0.91619045  -10.69984110    8.21799141
 N                 -0.28933535  -10.39304242    7.45770539
 B                 -0.34159919  -10.69497009    6.03282738
 N                 -1.54250598  -10.37814020    5.27124170
 B                 -1.59226735  -10.66965942    3.84564390
 N                 -2.78898912  -10.34816945    3.07882678
 B                 -2.86913219  -10.69843291    1.66909074
 N                 -4.12743905  -10.44142454    0.96915499
 H                 -4.28851463  -10.84590485    0.06644884
 B                 -5.04917982   -9.49091790    1.50659015
 N                 -4.79993438   -8.85076481    2.77590715
 B                 -3.72100656   -9.36663143    3.61381655
 N                 -3.51230555   -8.83684157    4.95498617
 B                 -2.46230698   -9.38358715    5.80253766
 N                 -2.25988004   -8.84552324    7.14141366
 B                 -1.20530740   -9.39057178    7.98610461
 N                 -0.99750311   -8.84425626    9.32029769
 B                  0.05424619   -9.38996972   10.16845657
 N                  0.25549007   -8.84685615   11.50444645
 B                  1.31058295   -9.38771501   12.35060976
 N                  1.51454784   -8.84225310   13.68516889
 B                  1.00003867   -7.51056113   13.97819460
 N                  1.41710280   -6.81957993   15.18994400
 B                  2.24982811   -7.50887591   16.16351764
 N                  2.65702354   -6.81917708   17.38086916
 B                  3.49632907   -7.50577545   18.35237831
 N                  3.89963496   -6.81689409   19.57118301
 B                  4.75764098   -7.49516409   20.53174671
 N                  5.18215398   -6.80510217   21.74224059
 B                  5.96693139   -7.50569394   22.74589731
 N                  6.27662948   -6.80134341   23.98936431
 H                  6.64874232   -7.30714260   24.77088502
 B                  6.19463234   -5.37541574   24.03117823
 N                  5.71558917   -4.62387344   22.89666387
 B                  5.10622626   -5.35273968   21.78739871
 N                  4.49636641   -4.63231202   20.67836235
 B                  3.84745138   -5.36229039   19.59913386
 N                  3.24464862   -4.63124766   18.49338875
 B                  2.60123409   -5.36417370   17.41122702
 N                  2.00289335   -4.63370756   16.30387076
 B                  1.35646654   -5.36500512   15.22178715
 N                  0.74792318   -4.63336174   14.12125508
 B                  0.10493095   -5.36663469   13.04072998
 N                  0.15752770   -6.82266381   13.01089996
 B                 -0.25371173   -7.51292959   11.79775293
 N                 -1.08993396   -6.82178290   10.82603563
 B                 -1.51134952   -7.51392933    9.61775604
 N                 -2.35573743   -6.82512777    8.65024781
 B                 -2.77360134   -7.51566553    7.43809939
 N                 -3.61322953   -6.82517813    6.46934433
 B                 -4.02386620   -7.50966307    5.25206113
 N                 -4.85990045   -6.82277430    4.27715027
 B                 -5.32944235   -7.52609392    3.09315142
 N                 -6.24344871   -6.82013102    2.19555898
 H                 -6.74470838   -7.32794491    1.49184615
 B                 -6.32054884   -5.39405090    2.24449534
 N                 -5.56625015   -4.64258953    3.21820053
 B                 -4.92420748   -5.36930597    4.30922536
 N                 -4.26849738   -4.64583454    5.39052383
 B                 -3.66781147   -5.37173004    6.49935382
 N                 -3.02071043   -4.63870783    7.57932062
 B                 -2.41125516   -5.37079122    8.68115878
 N                 -1.75587327   -4.63779573    9.75502187
 B                 -1.14921600   -5.36775486   10.86183824
 N                 -0.51120262   -4.63348169   11.94283779
 B                 -0.09293379   -3.25942320   11.70579703
 N                  0.76997725   -2.57452940   12.65622339
 B                  1.15873433   -3.25683938   13.88329746
 N                  2.04022915   -2.58631647   14.83053741
 B                  2.41093693   -3.25632139   16.06763892
 N                  3.28767297   -2.58390518   17.01809904
 B                  3.65758395   -3.25538532   18.25598013
 N                  4.53612364   -2.58587838   19.20560638
 B                  4.91464372   -3.26175134   20.43777337
 N                  5.80680323   -2.60531358   21.38426252
 B                  6.12308940   -3.24088296   22.65363009
 N                  6.94257187   -2.49625696   23.60864024
 H                  7.02944191   -2.82166307   24.55319544
 B                  7.75038154   -1.41239787   23.14638313
 N                  7.68711471   -0.96789630   21.77516356
 B                  6.62815436   -1.49793699   20.92025144
 N                  6.44589399   -0.98419244   19.56880906
 B                  5.36392673   -1.48582312   18.73357008
 N                  5.18992881   -0.96875749   17.38264760
 B                  4.11182179   -1.48034142   16.54575729
 N                  3.93978732   -0.96677070   15.19366762
 B                  2.86258134   -1.48310981   14.35707589
 N                  2.68665397   -0.96687277   13.00687920
 B                  1.60667041   -1.48292634   12.17749820
 N                  1.43244338   -0.96751320   10.82373238
 B                  0.35243905   -1.47944702    9.99545216
 N                 -0.45326259   -2.59821119   10.45634711
 B                 -1.34962777   -3.26080197    9.52110891
 N                 -1.73270709   -2.58661959    8.28801452
 B                 -2.61300187   -3.26153654    7.34276355
 N                 -2.98930009   -2.59395106    6.10478726
 B                 -3.85910406   -3.27241099    5.15427681
 N                 -4.23180091   -2.61343976    3.90981892
 B                 -5.15958910   -3.25712070    2.99229947
 N                 -5.56744629   -2.51770967    1.79807460
 H                 -6.34646146   -2.83672263    1.25417309
 B                 -4.77284263   -1.42171207    1.34115456
 N                 -3.61867163   -0.97972793    2.08641918
 B                 -3.41661992   -1.50579177    3.43316053
 N                 -2.34225058   -0.99105525    4.27267262
 B                 -2.16770549   -1.49182724    5.62854570
 N                 -1.09012425   -0.97111214    6.46071665
 B                 -0.91041625   -1.48333441    7.81197218
 N                  0.17784137   -0.97814128    8.63867123
 B                  1.29571836   -0.30050393    7.99553899
 N                  2.50768004   -0.01689388    8.75023859
 B                  2.55475625   -0.29622781   10.18084044
 N                  3.76645440   -0.00894387   10.93452331
 B                  3.81108764   -0.30345527   12.36102319
 N                  5.02469976   -0.02199885   13.11919405
 B                  5.06354804   -0.30314612   14.54776996
 N                  6.27807862   -0.02787978   15.30843332
 B                  6.31607528   -0.31008184   16.73788161
 N                  7.53344545   -0.04484877   17.49571357
 B                  7.57650541   -0.34163399   18.92092206
 N                  8.79769800   -0.10287234   19.67999227
 B                  8.82112831   -0.32010882   21.11804391
 N                 10.04321734    0.02072243   21.84528732
 H                 10.03651820    0.04936395   22.84785559
 B                 11.27878121    0.11309705   21.13396190
 H                 12.28815840    0.26892673   21.75421617
 N                  1.24999766   -0.03072387    6.56407890
 B                  0.03427020   -0.31158005    5.81163413
 N                 -0.00638278   -0.04628751    4.37919206
 B                 -1.21549447   -0.34251288    3.62335575
 N                 -1.25675372   -0.09544106    2.18769673
 B                 -2.48579535   -0.31980854    1.44175037
 N                 -2.49846789    0.02491368    0.02007666
 H                 -3.36874994    0.07438847   -0.47444194
 B                 -1.26205552    0.13159492   -0.68764563
 H                 -1.28863944    0.30140216   -1.86971352
 H                 -5.06165947   -0.89185186    0.31034230
 H                  8.50231439   -0.88903735   23.91350298
 H                 -6.99962888   -4.82762687    1.44129617
 H                  6.55425422   -4.80863221   25.01969780
 H                 -6.02118869   -9.21048235    0.87167950
 H                  7.55877182   -9.16971642   24.45727188
 H                 -2.64580463  -11.48819410   -1.08304845
 H                 10.94174107  -11.43738954   22.53046121
 H                  1.13387797  -10.28266112   -3.25120851
 H                 14.71270342  -10.26917121   20.34882092
 H                  3.09548669   -6.35465812   -4.36271212
 H                 16.65790372   -6.35256731   19.20617874
 B                 14.68428695   -1.70930919   19.16112728
 N                 13.76352579   -0.76028547   19.70120501
 H                 13.92075178   -0.36613478   20.60968486
 H                 15.65250496   -1.99782391   19.79913888
 H                  2.09920511   -1.97800276   -3.79924276

Expected behavior output.gjf and cart_output.gjf should be the same, but only cart_output.gjf gives the right answer.

Desktop OS: Mac Version: 2020.8.13

shyuep commented 4 years ago

I am confused. How do you know that the cart=False version is wrong? That is in the Zmatrix format.

wudihua commented 4 years ago

I am confused. How do you know that the cart=False version is wrong? That is in the Zmatrix format.

I use Gaussview to visualize the two files and they are different.

cart_output.gjf 图片

output.gjf 图片

gVallverdu commented 4 years ago

Hi

For me the issue comes from the conversion into z-matrix and not from the Gaussian class. I cannot exactly say why but my guess is that your atoms are not well sorted and the conversion algorithm from cartesian coords to z-matrix fails. In your case successive atoms may be far away from each other. When the program tries to build the z-matrix it will consider distances, angles and dihedrals that do not correspond to "real" internal coordinate. As you have a lot of atoms, at some point, maybe you consider aligned atoms (or closed to be aligned), or another kind of wrong geometry for a z-matrix and from this point the geometry is wrong.

I sorted the atom of your nanotube according to the axis of the nanotube and at the end the final z-matrix is ok. Both gjf input files (in cartesian coordinates or z-matrix) are correctly written (see the picture).

image

Here is what I did to sort the atoms. It is not so elegant but to be quick I used pandas sorting facilities.

import pymatgen as mg
import numpy as np
import pandas as pd

# input.xyz is the xyz file you put in your message
mol = mg.Molecule.from_file("input.xyz")

# I rotated the nanotube around y axes to aligned the nanotube axis and the z-axis
# this is not exactly 30 degrees but it is not so far
op = mg.SymmOp.from_axis_angle_and_translation(axis=[0, 1, 0], angle=-30)
mol.apply_operation(op)
mol.to(fmt="xyz", filename="input2.xyz")

mol2 = mg.Molecule.from_file("input2.xyz")
coords = mol2.cart_coords

# geometric barycenter of the nanotube (without masses)
G = coords.sum(axis=0) / len(coords)
coords -= G

df = pd.DataFrame({"species": mol2.species, "x": coords[:, 0],  "y": coords[:, 1], "z": coords[:, 2]})

# compute the radius of the nanotube
df["r"] = np.sqrt(df["x"]**2 + df["y"]**2)

# compute the angle theta with x axis.
df["theta"] = np.degrees(np.arctan2(df.y, df.x))
df["theta"] = np.where(df["theta"] < 0, 360 + df["theta"], df["theta"])
df.describe()
df.head()

# from now, you can use r, theta, z as cylindrical coordinates
# now the key part, I sort according first to z and second theta (maybe theta is not necessary
df.sort_values(by=["z", "theta"], ascending=True, inplace=True)

# set up the sorted molecule
coords = df[["x", "y", "z"]].values
m_sorted = mg.Molecule(species=df.species.values, coords=coords)

gau = mg.io.gaussian.GaussianInput(m_sorted)
gau.write_file("gau_sorted.gjf", cart_coords=False)
m_sorted.to("xyz", "sorted.xyz")

May be you do not need to compute radius and angle. Sorting according to z may be enough.

wudihua commented 4 years ago

Thank you for your thorough response, it's very helpful. It also inspired me to recheck this issue.

After I looked at this strange problem closely, I think it's the atom 165 that causes the problem, the line is: N 164 B164 163 A164 144 D164 However, 164, 163 and 144 is almost on a line, so it causes numerial precision issue when calculating dihedral angle and then the error accumulate.

The get_zmatix function (in pymatgen.io.gaussian.GaussianInput class) uses a function called _find_nn_pos_before_site(i) function, this function does not guarantee that the three closest atoms are not (approximatly) collinear.

I actually make the program work properly by use the following code (line 400 in pymatgen.io.gaussian):

                    #nn = self._find_nn_pos_before_site(i)
                    nn = [i-1,i-2,i-3]

But this would also not always work. I would suggest the program to check the nn[0] - nn[1] - nn[2] angle, if it's close to +/-180 or 0 degree then del nn[2] and recheck. If len(nn)<3 then raise error.

Another solution is to tackle the numerical precision issue in dihedral angle calculation, but I think use dihedral angle in dangerous region might not be an approprate practice.

shyuep commented 4 years ago

Thanks for the debug. I think I know what is happening. But I need to think how best to solve this. There is no unique way of defining the z-matrix and in fact, for something like a nanotube, it is probably even clearer to define it relative to the center axis using virtual atoms since that allows symmetric angles to be defined precisely.