Coverage Report

Created: 2025-11-09 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openbabel/src/ops/partialcharges.cpp
Line
Count
Source
1
/**********************************************************************
2
partialcharges.cpp - The option --partialcharges (type) adds charges using different models
3
4
Copyright(C) 2010 by Geoffrey R. Hutchison
5
6
This file is part of the Open Babel project.
7
For more information, see <http://openbabel.org/>
8
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation version 2 of the License.
12
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
***********************************************************************/
18
#include <openbabel/babelconfig.h>
19
20
#include<openbabel/op.h>
21
#include<openbabel/mol.h>
22
#include <openbabel/atom.h>
23
#include <openbabel/obiter.h>
24
#include<openbabel/chargemodel.h>
25
#include <openbabel/obconversion.h>
26
27
#include <cstring>
28
namespace OpenBabel
29
{
30
31
class OpPartialCharge : public OBOp
32
{
33
public:
34
2
  OpPartialCharge(const char* ID) : OBOp(ID, false) {
35
2
    OBConversion::RegisterOptionParam(ID, nullptr, 1, OBConversion::GENOPTIONS);
36
2
  }
37
38
0
  const char* Description() override { return "<method> Calculate partial charges by specified method"; }
39
40
0
  bool WorksWith(OBBase* pOb) const override { return dynamic_cast<OBMol*>(pOb) != nullptr; }
41
  bool Do(OBBase* pOb, const char* OptionText=nullptr, OpMap* pOptions=nullptr,
42
      OBConversion* pConv=nullptr) override;
43
44
  OBChargeModel *_pChargeModel;
45
};
46
47
/////////////////////////////////////////////////////////////////
48
OpPartialCharge theOpPartialCharge("partialcharge"); //Global instance
49
50
/////////////////////////////////////////////////////////////////
51
bool OpPartialCharge::Do(OBBase* pOb, const char* OptionText, OpMap* pmap, OBConversion* pConv)
52
0
{
53
0
  char *arg = nullptr;
54
0
  const char *tok1= nullptr;
55
0
  const char *tok2= nullptr;
56
0
  OpMap::const_iterator iter;
57
0
  OBMol* pmol = dynamic_cast<OBMol*>(pOb);
58
0
  bool print = false;
59
60
0
  if(!pmol)
61
0
    return false;
62
63
0
  iter = pmap->find("print");
64
0
  if(iter!=pmap->end())
65
0
    print=true;
66
67
0
  if( OptionText ) {
68
0
    arg = strdup( OptionText );
69
0
    tok1 = strtok( arg, ":" );
70
0
    tok2 = strtok( nullptr, "\0" );
71
0
  }
72
0
  else {
73
0
    tok1 = OptionText;
74
0
  }
75
76
0
  _pChargeModel = OBChargeModel::FindType(tok1);
77
78
79
0
  if(!_pChargeModel)
80
0
    {
81
0
      obErrorLog.ThrowError(__FUNCTION__,
82
0
                            std::string("Unknown charge model ") + tok1, obError, onceOnly);
83
0
      return false;
84
0
    }
85
86
0
  bool success = _pChargeModel->ComputeCharges(*pmol, tok2);
87
88
0
  if (print) {
89
    // print them on stdout
90
0
    FOR_ATOMS_OF_MOL(atom, pmol) {
91
0
      std::cout << atom->GetPartialCharge() << '\n';
92
0
    }
93
0
    std::cout << std::endl; // extra blank and flush the stream
94
0
  }
95
96
0
  return success;
97
0
}
98
}//namespace