-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmopac.cpp
676 lines (527 loc) · 15.2 KB
/
mopac.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// Please see license.txt for licensing and copyright information //
// Author: Paul Zimmerman, University of Michigan //
#include "mopac.h"
//#include "utils.h"
using namespace std;
#include "constants.h"
//NOTE: max time for MOPAC: 45M
#define MOPAC_LEVEL "PM6 NOSYM T=10M AUX LOCALIZE"
#define MOPAC_LEVEL_ENERGY "PM6 NOSYM 1SCF GEO-OK T=45M AUX LOCALIZE"
//NOTE: UHF breaks nbo.cpp
void Mopac::write_ic_input(ofstream& inpfile, int anum, ICoord ic)
{
//printf("\n\n in write_ic_input() for atom %i \n",anum);
// printf(" nbonds: %i \n",ic.nbonds);
for (int i=0;i<nfrz0;i++)
{
if (anum==frzlist[i] || anum < 4)
{
//printf(" this atom was moved or is in the first 3, doing xyz \n");
inpfile << xyz[3*anum+0] << " 0 " << xyz[3*anum+1] << " 0 " << xyz[3*anum+2] << " 0 " << endl;
return;
}
}
// printf(" atom attached to moved atom, freezing bond \n");
int b1,c1,d1;
for (int i=0;i<ic.nbonds;i++)
{
b1 = -1;
if (ic.bonds[i][0] == anum || ic.bonds[i][1] == anum)
for (int j=0;j<nfrz0;j++)
{
//printf(" found %i, checking for bond partner %i \n",anum,frzlist[j]);
if (ic.bonds[i][0]==frzlist[j]
|| ic.bonds[i][1]==frzlist[j])
{
b1 = frzlist[j];
break;
}
}
if (b1>-1)
{
//printf(" found attachment: %i %i \n",b1,anum);
break;
}
}
//could change this to "xyz list" if statement
c1 = -1; d1 = -1;
for (int i=0;i<natoms;i++)
{
if (c1==-1)
for (int j=0;j<i;j++)
{
if (!frzlistb[i] && !frzlistb[j])
{
c1 = i; d1 = j;
//printf(" found angle/tor possibility: %i %i \n",c1,d1);
break;
}
}
}
if (b1==-1 || c1==-1 || d1==-1 || b1==d1)
{
printf(" failed to find IC for atom %i, writing xyz \n",anum);
inpfile << xyz[3*anum+0] << " 0 " << xyz[3*anum+1] << " 0 " << xyz[3*anum+2] << " 0 " << endl;
}
else
{
double rv,anglev,torv;
rv = ic.distance(anum,b1);
anglev = ic.angle_val(anum,b1,c1);
torv = ic.torsion_val(anum,b1,c1,d1);
//printf(" r: %1.3f angle: %1.3f tor: %1.3f \n",rv,anglev,torv);
inpfile << " " << rv << " 0 " << anglev << " 1 " << torv << " 1 " << b1+1 << " " << c1+1 << " " << d1+1 << endl;
}
return;
}
double Mopac::opt() {
string filename = "scratch/testmopac.mop";
#if NOMOOPT
return sp_energy(filename);
#endif
energy = opt(filename);
return energy;
}
void Mopac::opt_write() {
string filename = "scratch/testmopac.mop";
opt_write(filename);
return;
}
void Mopac::energy_header(ofstream& inpfile) {
inpfile << MOPAC_LEVEL_ENERGY << endl;
inpfile << " MOPAC run " << endl;
inpfile << " ready to go? " << endl;
return;
}
void Mopac::opt_header(ofstream& inpfile) {
inpfile << MOPAC_LEVEL << endl;
inpfile << " MOPAC run " << endl;
inpfile << " ready to go? " << endl;
return;
}
double Mopac::opt(string filename, ICoord icoords) {
#if 0
printf(" WARNING: bypassing ICs! \n");
return opt(filename);
#endif
//printf(" in mopac/opt() w/IC freeze \n");
energy0 = energy = 0;
#if NOMOOPT
return sp_energy(filename);
#endif
#if SKIPMOPAC
printf(" skipping mopac opt! \n");
#endif
ofstream inpfile;
string inpfile_string = filename;
ofstream xyzfile;
string xyzfile_string = "scratch/testmopac.xyz";
#if !SKIPMOPAC
inpfile.open(inpfile_string.c_str());
inpfile.setf(ios::fixed);
inpfile.setf(ios::left);
inpfile << setprecision(6);
xyzfile.open(xyzfile_string.c_str());
xyzfile.setf(ios::fixed);
xyzfile.setf(ios::left);
xyzfile << setprecision(6);
xyzfile << " " << natoms << endl << endl;
opt_header(inpfile);
for (int i=0;i<natoms;i++)
{
if (!frzlistb[i])
inpfile << " " << anames[i] << " " << xyz[3*i+0] << " 1 " << xyz[3*i+1] << " 1 " << xyz[3*i+2] << " 1 " << endl;
else
{
inpfile << " " << anames[i] << " ";
write_ic_input(inpfile,i,icoords);
}
xyzfile << " " << anames[i] << " " << xyz[3*i+0] << " " << xyz[3*i+1] << " " << xyz[3*i+2] << endl;
}
string cmd = "/tmp/MOPAC2016.exe "+filename;
// string cmd = "/export/users/paulzim/mopac/MOPAC2012.exe "+filename;
system(cmd.c_str());
#endif
energy = read_output(filename);
//printf(" initial energy: %1.4f final energy: %1.4f \n",energy0,energy);
// need to retrieve final geometry, write to xyz
xyz_read_aux(inpfile_string);
xyz_save(inpfile_string+".xyz");
xyzfile.close();
inpfile.close();
if (abs(energy)<0.00001)
{
printf(" energy zero, mopac failed \n");
return 10000;
}
return energy;
}
void Mopac::opt_write(string filename, ICoord icoords) {
#if 0
printf(" WARNING: bypassing ICs! \n");
return opt(filename);
#endif
printf(" in mopac/opt_write() w/IC freeze ");
printf(" filename: %s \n",filename.c_str());
energy0 = energy = 0;
ofstream inpfile;
string inpfile_string = filename;
ofstream xyzfile;
string xyzfile_string = "scratch/testmopac.xyz";
inpfile.open(inpfile_string.c_str());
inpfile.setf(ios::fixed);
inpfile.setf(ios::left);
inpfile << setprecision(6);
xyzfile.open(xyzfile_string.c_str());
xyzfile.setf(ios::fixed);
xyzfile.setf(ios::left);
xyzfile << setprecision(6);
xyzfile << " " << natoms << endl << endl;
opt_header(inpfile);
for (int i=0;i<natoms;i++)
{
if (!frzlistb[i])
inpfile << " " << anames[i] << " " << xyz[3*i+0] << " 1 " << xyz[3*i+1] << " 1 " << xyz[3*i+2] << " 1 " << endl;
else
{
inpfile << " " << anames[i] << " ";
write_ic_input(inpfile,i,icoords);
}
xyzfile << " " << anames[i] << " " << xyz[3*i+0] << " " << xyz[3*i+1] << " " << xyz[3*i+2] << endl;
}
xyzfile.close();
inpfile.close();
return;
}
double Mopac::sp_energy(string filename)
{
energy0 = energy = 0;
#if SKIPMOPAC
printf(" skipping mopac single point energy! \n");
#endif
ofstream inpfile;
string inpfile_string = filename;
#if !SKIPMOPAC
inpfile.open(inpfile_string.c_str());
inpfile.setf(ios::fixed);
inpfile.setf(ios::left);
inpfile << setprecision(6);
energy_header(inpfile);
for (int i=0;i<natoms;i++)
{
if (!frzlistb[i])
inpfile << " " << anames[i] << " " << xyz[3*i+0] << " 1 " << xyz[3*i+1] << " 1 " << xyz[3*i+2] << " 1 " << endl;
else
inpfile << " " << anames[i] << " " << xyz[3*i+0] << " 0 " << xyz[3*i+1] << " 0 " << xyz[3*i+2] << " 0 " << endl;
}
string cmd = "/tmp/MOPAC2016.exe "+filename;
system(cmd.c_str());
#endif
energy = read_output(filename);
//printf(" initial energy: %1.4f final energy: %1.4f \n",energy0,energy);
// need to retrieve final geometry, write to xyz
inpfile.close();
if (abs(energy)<0.00001)
{
printf(" energy zero, mopac failed \n");
return 10000;
}
return energy;
}
// standard opt
double Mopac::opt(string filename)
{
//printf(" in mopac/opt() \n");
if (natoms<1)
{
printf(" ERROR in MOPAC: no atoms! \n");
exit(1);
}
energy0 = energy = 0;
#if NOMOOPT
return sp_energy(filename);
#endif
#if SKIPMOPAC
printf(" skipping mopac opt! \n");
#endif
ofstream inpfile;
string inpfile_string = filename;
ofstream xyzfile;
string xyzfile_string = "scratch/testmopac.xyz";
#if !SKIPMOPAC
inpfile.open(inpfile_string.c_str());
inpfile.setf(ios::fixed);
inpfile.setf(ios::left);
inpfile << setprecision(6);
xyzfile.open(xyzfile_string.c_str());
xyzfile.setf(ios::fixed);
xyzfile.setf(ios::left);
xyzfile << setprecision(6);
xyzfile << " " << natoms << endl << endl;
opt_header(inpfile);
for (int i=0;i<natoms;i++)
{
if (!frzlistb[i])
inpfile << " " << anames[i] << " " << xyz[3*i+0] << " 1 " << xyz[3*i+1] << " 1 " << xyz[3*i+2] << " 1 " << endl;
else
inpfile << " " << anames[i] << " " << xyz[3*i+0] << " 0 " << xyz[3*i+1] << " 0 " << xyz[3*i+2] << " 0 " << endl;
xyzfile << " " << anames[i] << " " << xyz[3*i+0] << " " << xyz[3*i+1] << " " << xyz[3*i+2] << endl;
}
// xyzfile << " " << natoms << endl << endl;
// string cmd = "/home2/paulzim/mopac/MOPAC2012.exe "+filename;
string cmd = "/tmp/MOPAC2016.exe "+filename;
// string cmd = "/export/users/paulzim/mopac/MOPAC2012.exe "+filename;
system(cmd.c_str());
#endif
energy = read_output(filename);
//printf(" initial energy: %1.4f final energy: %1.4f \n",energy0,energy);
// need to retrieve final geometry, write to xyz
xyz_read_aux(inpfile_string);
xyz_save(inpfile_string+".xyz");
xyzfile.close();
inpfile.close();
if (abs(energy)<0.00001)
{
printf(" energy zero, mopac failed \n");
return 10000;
}
return energy;
}
void Mopac::opt_write(string filename) {
//printf(" in mopac/opt() \n");
energy0 = energy = 0;
#if SKIPMOPAC
printf(" skipping mopac opt! \n");
#endif
ofstream inpfile;
string inpfile_string = filename;
ofstream xyzfile;
string xyzfile_string = "scratch/testmopac.xyz";
#if !SKIPMOPAC
inpfile.open(inpfile_string.c_str());
inpfile.setf(ios::fixed);
inpfile.setf(ios::left);
inpfile << setprecision(6);
xyzfile.open(xyzfile_string.c_str());
xyzfile.setf(ios::fixed);
xyzfile.setf(ios::left);
xyzfile << setprecision(6);
xyzfile << " " << natoms << endl << endl;
opt_header(inpfile);
for (int i=0;i<natoms;i++)
{
if (!frzlistb[i])
inpfile << " " << anames[i] << " " << xyz[3*i+0] << " 1 " << xyz[3*i+1] << " 1 " << xyz[3*i+2] << " 1 " << endl;
else
inpfile << " " << anames[i] << " " << xyz[3*i+0] << " 0 " << xyz[3*i+1] << " 0 " << xyz[3*i+2] << " 0 " << endl;
xyzfile << " " << anames[i] << " " << xyz[3*i+0] << " " << xyz[3*i+1] << " " << xyz[3*i+2] << endl;
}
xyzfile.close();
inpfile.close();
#endif
return;
}
double Mopac::read_output(string filename) {
//double energy = -1;
energy = 10000;
string oname = filename+".out";
ifstream output(oname.c_str(),ios::in);
string line;
vector<string> tok_line;
while(!output.eof())
{
getline(output,line);
// cout << " RR " << line << endl;
if (line.find("CYCLE: 1")!=string::npos)
{
cout << "Initial: " << line << endl;
tok_line = StringTools::tokenize(line, " \t");
// energy0=atof(tok_line[10].c_str());
}
if (line.find("FINAL")!=string::npos)
{
cout << "Final: " << line << endl;
tok_line = StringTools::tokenize(line, " \t");
energy=atof(tok_line[5].c_str());
}
}
return energy;
}
void Mopac::alloc(int natoms_i) {
// printf(" in mopac/alloc() \n");
natoms = natomsmax = natoms_i;
anumbers = new int[natoms];
anames = new string[natoms];
xyz0 = new double[3*natoms];
xyz = new double[3*natoms];
nfrz = 0;
frzlist = new int[natoms_i];
frzlistb = new int[natoms_i];
for (int i=0;i<natoms_i;i++)
frzlist[i] = frzlistb[i] = 0;
return;
}
void Mopac::init(int natoms_i, int* anumbers_i, string* anames_i, double* xyz_i) {
// printf(" in mopac/init() \n");
natoms = natoms_i;
anumbers = new int[natoms];
anames = new string[natoms];
xyz0 = new double[3*natoms];
xyz = new double[3*natoms];
for (int i=0;i<natoms;i++)
anumbers[i] = anumbers_i[i];
for (int i=0;i<natoms;i++)
anames[i] = anames_i[i];
for (int i=0;i<3*natoms;i++)
xyz0[i] = xyz[i] = xyz_i[i];
nfrz = 0;
frzlist = new int[natoms_i];
frzlistb = new int[natoms_i];
return;
}
void Mopac::freemem() {
delete [] xyz0;
delete [] xyz;
delete [] anumbers;
delete [] anames;
delete [] frzlist;
delete [] frzlistb;
return;
}
void Mopac::freeze(int* frzlist_new, int nfrz_new, int nfrz0_new) {
nfrz0 = nfrz0_new;
nfrz = nfrz_new;
for (int i=0;i<natoms;i++)
frzlist[i] = 0;
for (int i=0;i<natoms;i++)
frzlistb[i] = 0;
for (int i=0;i<nfrz;i++)
frzlistb[frzlist_new[i]] = 1;
for (int i=0;i<nfrz;i++)
frzlist[i] = frzlist_new[i];
printf(" freeze list: ");
for (int i=0;i<nfrz;i++)
printf("%i ",frzlist[i]);
printf("\n");
return;
}
void Mopac::reset(int natoms_i, int* anumbers_i, string* anames_i, double* xyz_i) {
// printf(" in mopac/reset() \n");
if (natoms_i>natomsmax)
{
printf(" mopac reset failed, too many atoms \n");
return;
}
natoms = natoms_i;
for (int i=0;i<natoms;i++)
anumbers[i] = anumbers_i[i];
for (int i=0;i<natoms;i++)
anames[i] = anames_i[i];
for (int i=0;i<3*natoms;i++)
xyz0[i] = xyz[i] = xyz_i[i];
nfrz = 0;
return;
}
void Mopac::xyz_save(string filename){
ofstream xyzfile;
string xyzfile_string = "xyzfile.txt";
xyzfile.open(filename.c_str());
xyzfile.setf(ios::fixed);
xyzfile.setf(ios::left);
xyzfile << setprecision(6);
xyzfile << " " << natoms << endl;
xyzfile << " " << endl;
for (int i=0;i<natoms;i++)
{
xyzfile << " " << anames[i];
xyzfile << " " << xyz[3*i+0] << " " << xyz[3*i+1] << " " << xyz[3*i+2];
xyzfile << endl;
}
}
void Mopac::xyz_read(string filename){
// printf(" in xyz_read \n");
string oname = filename+".out";
ifstream output(oname.c_str(),ios::in);
string line;
vector<string> tok_line;
int count = 0;
int i = 0;
while(!output.eof())
{
getline(output,line);
// cout << " RR " << line << endl;
if (count == 2)
{
if (!StringTools::cleanstring(line)) break;
vector<string> tok_line = StringTools::tokenize(line, " \t");
xyz[3*i+0]=atof(tok_line[2].c_str());
xyz[3*i+1]=atof(tok_line[3].c_str());
xyz[3*i+2]=atof(tok_line[4].c_str());
// cout << tok_line[0] << " " << tok_line[1] << " " << tok_line[2] << " " << endl;
i++;
}
if (line.find("CARTESIAN COORDINATES")!=string::npos && count == 0)
{
// cout << "Initial: " << line << endl;
count++;
}
else if (line.find("CARTESIAN COORDINATES")!=string::npos && count == 1)
{
// cout << "Final: " << line << endl;
count++;
getline(output,line);
getline(output,line);
getline(output,line);
}
}
}
void Mopac::xyz_read_aux(string filename){
string oname = filename+".aux";
printf(" in xyz_read_aux: %s \n",oname.c_str());
ifstream output(oname.c_str(),ios::in);
string line;
vector<string> tok_line;
int count = 0;
int i = 0;
while(!output.eof())
{
getline(output,line);
// cout << " RR " << line << endl; fflush(stdout);
if (line.find("ATOM_X_OPT")!=string::npos)
{
count++;
getline(output,line);
}
if (count>0 && i<natoms)
{
if (anames[i]!="X")
{
if (!StringTools::cleanstring(line)) break;
vector<string> tok_line = StringTools::tokenize(line, " \t");
xyz[3*i+0]=atof(tok_line[0].c_str());
xyz[3*i+1]=atof(tok_line[1].c_str());
xyz[3*i+2]=atof(tok_line[2].c_str());
//cout << tok_line[0] << " " << tok_line[1] << " " << tok_line[2] << " " << endl;
}
i++;
}
if (i>=natoms) break;
}
}
#if 0
//currently accepts data from geom1, but not actual class instantiation
void do_mp1(ICoord geom0, ICoord geom1, int i, int rem1, int mov1) {
printf(" in do_mp1: %i %i %i \n",i,rem1,mov1);
geom1.reset(geom0.natoms,geom0.anames,geom0.anumbers,geoms0.coords);
geom1.ic_create();
geom1.nbonds = geoms[id].nbonds;
// geom1.bonds[rem1][0]=addsubmp[i][0];
geom1.bonds[rem1][0]=i;
geom1.bonds[rem1][1]=mov1;
geom1.ic_create_nobonds(); // add to this, to do imptor if needed!!
geom1.mm_init();
geom1.update_ic();
// geom1.print_ic();
return;
}
#endif