Codebase list votca-xtp / debian/1.5-1 src / libxtp / aobasis.cc
debian/1.5-1

Tree @debian/1.5-1 (Download .tar.gz)

aobasis.cc @debian/1.5-1raw · history · blame

  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
/*
 *            Copyright 2009-2018 The VOTCA Development Team
 *                       (http://www.votca.org)
 *
 *      Licensed under the Apache License, Version 2.0 (the "License")
 *
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include "votca/xtp/aobasis.h"
#include "votca/xtp/qmatom.h"
#include "votca/xtp/aomatrix.h"
#include <votca/tools/constants.h>




namespace votca { namespace xtp {

 

AOShell& AOBasis::addShell( const Shell& shell, const QMAtom& atom, int startIndex ){
        _aoshells.push_back(AOShell( shell, atom, startIndex ));
        return _aoshells.back();
        }

AOShell& AOBasis::addECPShell( const Shell& shell, const QMAtom& atom, int startIndex, bool nonlocal ){
        _aoshells.push_back(AOShell( shell, atom, startIndex,nonlocal ));
        return _aoshells.back();
        }

void AOBasis::ReorderMOs(Eigen::MatrixXd &v, const std::string& start, const std::string& target) {

    if (start == target) {
        return;
    }
    
    if(target=="orca" || target=="nwchem"){
        std::vector<int> multiplier = getMultiplierVector(target,start);
        // and reorder rows of _orbitals->_mo_coefficients() accordingly
        MultiplyMOs(v, multiplier);
    }

    // get reordering vector _start -> target
    std::vector<int> order = getReorderVector(start, target);
    
    // Sanity check
    if (v.rows() != int(order.size())) {
        std::cerr << "Size mismatch in ReorderMOs" << v.rows() << ":" << order.size() << std::endl;
        throw std::runtime_error("Abort!");
    }

    // actual swapping of coefficients
    for (unsigned _i_orbital = 0; _i_orbital < v.cols(); _i_orbital++) {
        for (unsigned s = 1, d; s < order.size(); ++s) {
            for (d = order[s]; d < s; d = order[d]) {
                ;
            }
            if (d == s) while (d = order[d], d != s) std::swap(v(s,_i_orbital), v(d,_i_orbital));
        }
    }

    // NWChem has some strange minus in d-functions
    if (start == "nwchem" || start == "orca") {
        std::vector<int> multiplier = getMultiplierVector(start, target);
        // and reorder rows of _orbitals->_mo_coefficients() accordingly
        MultiplyMOs(v, multiplier);
    }
    return;
}

void AOBasis::ReorderMatrix(Eigen::MatrixXd &v,const std::string& start,const std::string& target ){
    if (start==target){
        return;
    }
    std::vector<int> order = getReorderVector(start, target);
    std::vector<int> multiplier=getMultiplierVector(start,target);
    
     if (v.cols() != int(order.size())) {
        std::cerr << "Size mismatch in ReorderMatrix" << v.cols() << ":" << order.size() << std::endl;
        throw std::runtime_error("Abort!");
    }

    if (start != "xtp") {
      std::vector<int>newmultiplier=std::vector<int>(multiplier.size());
      for(unsigned i=0;i<newmultiplier.size();i++){
        newmultiplier[i]=multiplier[order[i]];
      }
    multiplier=newmultiplier;
    }
    
    Eigen::MatrixXd temp=v;
    for(int i=0;i<temp.cols();i++){
        int i_index=order[i];
        for(int j=0;j<temp.rows();j++){
            int j_index=order[j];
            v(i_index,j_index)=multiplier[i]*multiplier[j]*temp(i,j);
        }
    }
    
    return;
}


void AOBasis::MultiplyMOs(Eigen::MatrixXd &v, std::vector<int> const &multiplier )  {
          // Sanity check
          if ( v.cols() != int(multiplier.size()) ) {
              std::cerr << "Size mismatch in MultiplyMOs" << v.cols() << ":" << multiplier.size() << std::endl;
              throw std::runtime_error( "Abort!");
          }
          for ( int i_basis = 0; i_basis < v.cols(); i_basis++ ){
            for ( int i_orbital = 0; i_orbital < v.rows(); i_orbital++ ){
                   v(i_basis ,i_orbital) = multiplier[i_basis] * v(i_basis,i_orbital);
               }
           }
          return;
    }


    //this is for gaussian only to transform from gaussian ordering cartesian to gaussian spherical
    Eigen::MatrixXd AOBasis::getTransformationCartToSpherical(const std::string& package) {
      Eigen::MatrixXd trafomatrix;
      if (package != "gaussian") {
        std::cout << " I should not have been called, will do nothing! " << std::endl;
      } else {
        // go through basisset, determine function sizes
        int dim_sph = 0;
        int dim_cart = 0;
        for (const AOShell& shell:(*this)) {
          const std::string& _type = shell.getType();

          dim_sph += NumFuncShell(_type);
          dim_cart += NumFuncShell_cartesian(_type);

        }
        trafomatrix = Eigen::MatrixXd::Zero(dim_sph, dim_cart);

        int row_start = 0;
        int col_start = 0;
         for (const AOShell& shell:(*this)) {
          const std::string& type = shell.getType();
          int row_end = row_start + NumFuncShell(type);
          int col_end = col_start + NumFuncShell_cartesian(type);
          Eigen::Block<Eigen::MatrixXd> block = trafomatrix.block(row_start, col_start, NumFuncShell(type), NumFuncShell_cartesian(type));
          addTrafoCartShell(shell, block);
          row_start = row_end;
          col_start = col_end;

        }
      }
      return trafomatrix;
    }


void AOBasis::addTrafoCartShell( const AOShell& shell , Eigen::Block<Eigen::MatrixXd>& submatrix ){

    // fill _local according to _lmax;
    int lmax = shell.getLmax();
    std::string type = shell.getType();

    int sph_size =NumFuncShell( type ) + OffsetFuncShell( type );
    int cart_size = NumFuncShell_cartesian( type ) + OffsetFuncShell_cartesian( type )  ;
    Eigen::MatrixXd local = Eigen::MatrixXd::Zero(sph_size,cart_size);

    // s-functions
    local(0,0) = 1.0; // s
    // p-functions
    if ( lmax > 0 ){
        local(1,1) = 1.0;
        local(2,2) = 1.0;
        local(3,3) = 1.0;
    }
    // d-functions
    if ( lmax > 1 ){
        local(4,4) = -0.5;             // d3z2-r2 (dxx)
        local(4,5) = -0.5;             // d3z2-r2 (dyy)
        local(4,6) =  1.0;             // d3z2-r2 (dzz)
        local(5,8) =  1.0;             // dxz
        local(6,9) =  1.0;             // dyz
        local(7,4) = 0.5*sqrt(3.0);    // dx2-y2 (dxx)
        local(7,5) = -local(7,4);      // dx2-y2 (dyy)
        local(8,7) = 1.0;              // dxy
     }
    if ( lmax > 2 ){
        std::cerr << " Gaussian input with f- functions or higher not yet supported!" << std::endl;
        exit(1);
    }
    // now copy to _trafo
    for ( int i_sph = 0 ; i_sph < NumFuncShell( type ) ; i_sph++ ){
        for  ( int i_cart = 0 ; i_cart < NumFuncShell_cartesian( type ) ; i_cart++ ){
            submatrix( i_sph , i_cart ) = local( i_sph + OffsetFuncShell( type ) , i_cart +  OffsetFuncShell_cartesian( type ) );
        }
    }
    return;
}

std::vector<int> AOBasis::getMultiplierVector( const std::string& start, const std::string& target){
    std::vector<int> multiplier;
    multiplier.reserve(_AOBasisSize);
    std::string s;
    std::string t;
    if(start=="xtp"){
      s=target;
      t=start;
    }else{
      s=start;
      t=target;
    }
    // go through basisset
    for (const AOShell& shell:(*this)) {
        addMultiplierShell(  s, t, shell.getType(), multiplier );
    }
    return multiplier;
    }

void AOBasis::addMultiplierShell(const std::string& start, const std::string& target, const std::string& shell_type, std::vector<int>& multiplier) {
//multipliers were all found using code, hard to establish

    if (target == "xtp") {
        // current length of vector
        //int _cur_pos = multiplier.size() - 1;

        // single type shells defined here
        if (shell_type.length() == 1) {
            if (shell_type == "S") {
                multiplier.push_back(1);
            }
            else if (shell_type == "P") {
                multiplier.push_back(1);
                multiplier.push_back(1);
                multiplier.push_back(1);
            }
            else if (shell_type == "D") {
                if (start == "nwchem") {
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(-1);
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                } else if (start == "orca"){
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                } else {
                    std::cerr << "Tried to get multipliers d-functions from package " << start << ".";
                    throw std::runtime_error("Multiplication not implemented yet!");
                }
            }
            else if (shell_type == "F") {
                if ( start == "orca" ){
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(-1);
                    multiplier.push_back(-1);

                }else if (start == "nwchem"){
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(-1);
                    multiplier.push_back(+1);
                    multiplier.push_back(+1);
                    multiplier.push_back(+1);
                    multiplier.push_back(-1);
                } else {
                std::cerr << "Tried to get multipliers f-functions from package " << start << ".";
                throw std::runtime_error("Multiplication not implemented yet!");
                }
            }
            else if (shell_type == "G") {
                if ( start == "orca" ){
                    //Not checked yet
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(1);
                    multiplier.push_back(-1);
                    multiplier.push_back(-1);
                    multiplier.push_back(-1);
                    multiplier.push_back(-1);

                } else {
                std::cerr << "Tried to get multipliers g-functions from package " << start << ".";
                throw std::runtime_error("Multiplication not implemented yet!");
                }
            }else{
                std::cerr << "Tried to get multipliers h-functions . ";
                throw std::runtime_error("Multiplication not implemented yet!");
            }
        } else {
            // for combined shells, iterate over all contributions
            //_nbf = 0;
            for (unsigned i = 0; i < shell_type.length(); ++i) {
                std::string local_shell = std::string(shell_type, i, 1);
                addMultiplierShell(start, target, local_shell, multiplier);
            }
        }
    } else {

        std::cerr << "Tried to reorder functions (multiplier)  from " << start << " to " << target << std::endl;
        throw std::runtime_error("Reordering not implemented yet!");


    }
    return;
}


std::vector<int>  AOBasis::getReorderVector(const std::string& start,const std::string& target){
    std::vector<int> neworder;
    neworder.reserve(_AOBasisSize);
    std::string s;
    std::string t;
    if(start=="xtp"){
      s=target;
      t=start;
    }else{
      s=start;
      t=target;
    }
    // go through basisset
     for (const AOShell& shell:(*this)) {
        addReorderShell( s, t, shell.getType(), neworder );
    }
     if(start=="xtp"){
       neworder=invertOrder(neworder);
     } 
    return neworder;
}

std::vector<int> AOBasis::invertOrder(const std::vector<int>& order ){
 
    std::vector<int>neworder=std::vector<int>(order.size());
    for(unsigned i=0;i<order.size();i++){
        neworder[order[i]]=int(i);
    }
    return neworder;
    }

    void AOBasis::addReorderShell(const std::string& start, const std::string& target, const std::string& shell_type, std::vector<int>& order) {
        //Reordering is given by email from gaussian, orca output MOs, and http://www.nwchem-sw.org/index.php/Release66:Basis for nwchem
        
      // current length of vector

      int cur_pos = order.size() - 1;

      if (target == "xtp") {
        // single type shells defined here
        if (shell_type.length() == 1) {
          if (shell_type == "S") {
            order.push_back(cur_pos + 1);
          }//for S

            //votca order is z,y,x e.g. Y1,0 Y1,-1 Y1,1
          else if (shell_type == "P") {
            if (start == "orca") {
                //orca order is z,x,y Y1,0,Y1,1,Y1,-1
              order.push_back(cur_pos + 1);
              order.push_back(cur_pos + 3);
              order.push_back(cur_pos + 2);
            } else if (start == "gaussian" || start == "nwchem") {
                //nwchem gaussian x,y,z Y1,1 Y1,-1 Y1,0
              order.push_back(cur_pos + 3);
              order.push_back(cur_pos + 2);
              order.push_back(cur_pos + 1);
            } else if (start == "votca") {//for usage with old orb files
                 //old votca x,y,z Y1,1 Y1,-1 Y1,0
              order.push_back(cur_pos + 3);
              order.push_back(cur_pos + 2);
              order.push_back(cur_pos + 1);
            } else {
              std::cerr << "Tried to reorder p-functions from package " << start << ".";
              throw std::runtime_error("Reordering not implemented yet!");
            }
          }//for P
            //votca order is d3z2-r2 dyz dxz dxy dx2-y2 e.g. Y2,0 Y2,-1 Y2,1 Y2,-2 Y2,2
          else if (shell_type == "D") {
            //orca order is d3z2-r2 dxz dyz dx2-y2 dxy e.g. Y2,0 Y2,1 Y2,-1 Y2,2 Y2,-2
            if (start == "gaussian" || start == "orca") {
              order.push_back(cur_pos + 1);
              order.push_back(cur_pos + 3);
              order.push_back(cur_pos + 2);
              order.push_back(cur_pos + 5);
              order.push_back(cur_pos + 4);
            } else if (start == "nwchem") {
              // nwchem order is dxy dyz d3z2-r2 -dxz dx2-y2, e.g. Y2,-2 Y2,-1 Y2,0 Y2,1 Y2,2 
              order.push_back(cur_pos + 4);
              order.push_back(cur_pos + 2);
              order.push_back(cur_pos + 1);
              order.push_back(cur_pos + 3);
              order.push_back(cur_pos + 5);
            } else if (start == "votca") { //for usage with old orb files
              order.push_back(cur_pos + 3);
              order.push_back(cur_pos + 2);
              order.push_back(cur_pos + 4);
              order.push_back(cur_pos + 1);
              order.push_back(cur_pos + 5);
            } else {
              std::cerr << "Tried to reorder d-functions from package " << start << ".";
              throw std::runtime_error("Reordering not implemented yet!");
            }
          } else if (shell_type == "F") {
               //ordering for votca is Yl,0 Yl,-1 Yl,1 ......Yl,-m Yl,m
            if (start == "gaussian" || start == "orca") {
                //ordering for gaussian and orca is Yl,0 Yl,1 Yl,-1 ......Yl,m Yl,-m
              order.push_back(cur_pos + 1);
              order.push_back(cur_pos + 3);
              order.push_back(cur_pos + 2);
              order.push_back(cur_pos + 5);
              order.push_back(cur_pos + 4);
              order.push_back(cur_pos + 7);
              order.push_back(cur_pos + 6);
            } else if (start == "nwchem") {
                //ordering for nwchem is fxxy-yyy, fxyz,fyzz-xxy-yyy,fzzz-xxz-yyz,f-xzz+xxx+xyy,fxxz-yyz,fxyy-xxx
                // e.g. Y3,-3 Y3,-2 Y3,-1 Y3,0 Y3,1 Y3,2 Y3,3
              order.push_back(cur_pos + 6);
              order.push_back(cur_pos + 4);
              order.push_back(cur_pos + 2);
              order.push_back(cur_pos + 1);
              order.push_back(cur_pos + 3);
              order.push_back(cur_pos + 5);
              order.push_back(cur_pos + 7);
            } else {
              std::cerr << "Tried to reorder f-functions from package " << start << ".";
              throw std::runtime_error("Reordering not implemented yet!");
            }
          }else if (shell_type == "G") {
               //ordering for votca is Yl,0 Yl,-1 Yl,1 ......Yl,-m Yl,m
            if (start == "gaussian" || start == "orca") {
                 //ordering for gaussian and orca is Yl,0 Yl,1 Yl,-1 ......Yl,m Yl,-m
              order.push_back(cur_pos + 1);
              order.push_back(cur_pos + 3);
              order.push_back(cur_pos + 2);
              order.push_back(cur_pos + 5);
              order.push_back(cur_pos + 4);
              order.push_back(cur_pos + 7);
              order.push_back(cur_pos + 6);
              order.push_back(cur_pos + 9);
              order.push_back(cur_pos + 8);
            }else  {
              std::cerr << "Tried to reorder g-functions from package " << start << ".";
              throw std::runtime_error("Reordering not implemented");
            }
          }else {
            std::cerr << "Tried to reorder functions  of shell type " << shell_type << std::endl;
            throw std::runtime_error("Reordering not implemented");
          }
        } else {
          // for combined shells, iterate over all contributions
          //_nbf = 0;
          for (unsigned i = 0; i < shell_type.length(); ++i) {
            std::string local_shell = std::string(shell_type, i, 1);
            this->addReorderShell(start, target, local_shell, order);
          }
        }

      } else {
        std::cerr << "Tried to reorder functions (neworder) from " << start << " to " << target << std::endl;
        throw std::runtime_error("Reordering not implemented yet!");
      }
      return;
    }

    const std::vector<const AOShell*> AOBasis::getShellsofAtom(int AtomId)const {
      std::vector<const AOShell*> result;
      for (const auto& aoshell : _aoshells) {
        if (aoshell.getAtomIndex() == AtomId) {
          result.push_back(&aoshell);
        }
      }
      return result;
    }

    void AOBasis::AOBasisFill(const BasisSet& bs,  const QMMolecule& atoms, int fragbreak) {
      _AOBasisSize = 0;
      _AOBasisFragA = 0;
      _AOBasisFragB = 0;
      _FuncperAtom=std::vector<int>(0);
      // loop over atoms
      for (const QMAtom& atom : atoms) {
        int atomfunc=0;
        const std::string& name = atom.getElement();
        const Element& element = bs.getElement(name);
        for (const Shell& shell:element) {
          int numfuncshell = NumFuncShell(shell.getType());
          AOShell& aoshell = addShell(shell, atom, _AOBasisSize);
          _AOBasisSize += numfuncshell;
          atomfunc+=numfuncshell;
          for (const GaussianPrimitive& gaussian:shell) {
            aoshell.addGaussian(gaussian);
          }
          aoshell.CalcMinDecay();
          aoshell.normalizeContraction();
        }
        if (atom.getAtomID() < fragbreak) _AOBasisFragA = _AOBasisSize;
        _FuncperAtom.push_back(atomfunc);
      }

      if (fragbreak < 0) {
        _AOBasisFragA = _AOBasisSize;
        _AOBasisFragB = 0;
      } else {
        _AOBasisFragB = _AOBasisSize - _AOBasisFragA;
      }
      return;
    }

    std::vector<std::string> AOBasis::ECPFill(const BasisSet& bs,  QMMolecule& atoms) {
      _FuncperAtom=std::vector<int>(0);
      _AOBasisSize = 0;

      std::vector<std::string> non_ecp_elements;
      for (QMAtom& atom : atoms) {
        std::string name = atom.getElement();
        int atomfunc=0;
        bool element_exists=true;
        
        try{
            bs.getElement(name);
        }catch(std::runtime_error& error){
            _FuncperAtom.push_back(0);
            element_exists=false;
            if(std::find(non_ecp_elements.begin(), non_ecp_elements.end(), name) != non_ecp_elements.end()) {
            non_ecp_elements.push_back(name);
            }
        }
        

        if(element_exists){
            const Element& element = bs.getElement(name);
            atom._ecpcharge = element.getNcore();
            int lmax = element.getLmax();
            for (const Shell& shell:element) {
              if (shell.getType().size() > 1) {
                throw std::runtime_error("In ecps no combined shells e.g. SP are allowed");
              }
              //Local part is with L=Lmax
              bool nonlocal = false;
              if (shell.getLmax() < lmax) {
                nonlocal = true;
              }
              AOShell& aoshell = addECPShell(shell, atom, _AOBasisSize, nonlocal);
              _AOBasisSize += NumFuncShell(shell.getType());
              atomfunc+=NumFuncShell(shell.getType());
             for (const GaussianPrimitive& gaussian:shell) {
                aoshell.addGaussian(gaussian);
              }
              aoshell.CalcMinDecay();
            }
            _FuncperAtom.push_back(atomfunc);
        }
      }
      return non_ecp_elements;
    }

}}