-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_concept.cpp
397 lines (343 loc) · 15.7 KB
/
test_concept.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
#include <iostream>
#include <fstream>
#include <iterator>
#include <string>
#include <list>
#include <array>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_multi_pass.hpp>
#include <boost/fusion/include/std_pair.hpp>
#include <critical_point.hpp>
//#include "critical_point.hpp"
using namespace boost::spirit;
//https://theboostcpplibraries.com/boost.spirit-grammar
//http://kiri11.ru/boost_spirit_qi_part2
typedef std::istreambuf_iterator<char> BaseIterator;
typedef multi_pass<BaseIterator> Iterator;
typedef std::list<std::list<std::string>> MgpBlocks;
struct MgpBlockGrammar : public qi::grammar<Iterator, MgpBlocks()>
{
MgpBlockGrammar() : MgpBlockGrammar::base_type(start)
{
//ignore any number of blank symbols at line start
trim = *(qi::blank - qi::eol);
//read block (a list of text lines)
block = +(trim >> +(qi::char_ - qi::eol) >> qi::eol);
//parse blocks delimited by blank lines (a list of blocks)
start = +block % +(trim >> qi::eol);
BOOST_SPIRIT_DEBUG_NODES((start));
}
private:
qi::rule<Iterator, MgpBlocks()> start;
qi::rule<Iterator, std::list<std::string>()> block;
qi::rule<Iterator> trim;
};
struct MgpAtoms {
struct MgpAtom{
std::string name;
float charge;
double x,y,z;
};
virtual bool check(const std::string& head){
return qi::parse(head.begin(), head.end(), "Nuclear Charges and Cartesian Coordinates:");
}
virtual void parse(std::list<std::string>& lines){
MgpAtom a;
for(auto & l : lines) {
if (qi::phrase_parse(l.begin(), l.end(),
"" >> lexeme[+qi::alnum] >> float_ >>
double_ >> double_ >> double_ >> "",
qi::space, a.name, a.charge, a.x, a.y, a.z)) {
atoms.push_back(a);
std::cout << l << std::endl;
}
}
}
std::list<MgpAtom> atoms;
};
struct MgpCriticalPoints {
MgpCriticalPoints(std::shared_ptr<string> path) : path{path} {}
typedef const std::shared_ptr<string> t_path;
//===================================================================
struct MGPCriticalPoint : public CriticalPoint{
MGPCriticalPoint(const std::array<double, 3>& xyz, CP_TYPE type, t_path path) :
CriticalPoint(xyz, type), path{path} {};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct Rho : public PointPropertyMap::Rho{
Rho(valtype val, t_path path) : PointPropertyMap::Rho(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "Rho =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct GradRho : public PointPropertyMap::GradRho{
GradRho(valtype val, t_path path) : PointPropertyMap::GradRho(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double_3arr(str, "GradRho =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct HessRhoEigVals : public PointPropertyMap::HessRhoEigVals{
HessRhoEigVals(valtype val, t_path path) : PointPropertyMap::HessRhoEigVals(val), path{path} {}
virtual std::string origin() {return *path;}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double_3arr(str, "HessRho_EigVals =");
};
private:
t_path path;
};
struct HessRhoEigVec1 : public PointPropertyMap::HessRhoEigVec1{
HessRhoEigVec1(valtype val, t_path path) : PointPropertyMap::HessRhoEigVec1(val), path{path} {}
virtual std::string origin() {return *path;}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double_3arr(str, "HessRho_EigVec1 =");
};
private:
t_path path;
};
struct HessRhoEigVec2 : public PointPropertyMap::HessRhoEigVec2{
HessRhoEigVec2(valtype val, t_path path) : PointPropertyMap::HessRhoEigVec2(val), path{path} {}
virtual std::string origin() {return *path;}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double_3arr(str, "HessRho_EigVec2 =");
};
private:
t_path path;
};
struct HessRhoEigVec3 : public PointPropertyMap::HessRhoEigVec3{
HessRhoEigVec3(valtype val, t_path path) : PointPropertyMap::HessRhoEigVec3(val), path{path} {}
virtual std::string origin() {return *path;}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double_3arr(str, "HessRho_EigVec3 =");
};
private:
t_path path;
};
struct DelSqRho : public PointPropertyMap::DelSqRho{
DelSqRho(valtype val, t_path path) : PointPropertyMap::DelSqRho(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "DelSqRho =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct V : public PointPropertyMap::V{
V(valtype val, t_path path) : PointPropertyMap::V(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "V =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct G : public PointPropertyMap::G{
G(valtype val, t_path path) : PointPropertyMap::G(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "G =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct K : public PointPropertyMap::K{
K(valtype val, t_path path) : PointPropertyMap::K(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "K =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct L : public PointPropertyMap::L{
L(valtype val, t_path path) : PointPropertyMap::L(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "L =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct DelSqV : public PointPropertyMap::DelSqV{
DelSqV(valtype val, t_path path) : PointPropertyMap::DelSqV(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "DelSqV =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct DelSqVen : public PointPropertyMap::DelSqVen{
DelSqVen(valtype val, t_path path) : PointPropertyMap::DelSqVen(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "DelSqVen =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct DelSqVrep : public PointPropertyMap::DelSqVrep{
DelSqVrep(valtype val, t_path path) : PointPropertyMap::DelSqVrep(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "DelSqVrep =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct DelSqG : public PointPropertyMap::DelSqG{
DelSqG(valtype val, t_path path) : PointPropertyMap::DelSqG(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "DelSqG =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
struct DelSqK : public PointPropertyMap::DelSqK{
DelSqK(valtype val, t_path path) : PointPropertyMap::DelSqK(val), path{path} {}
static std::shared_ptr<valtype> parse(const std::string& str){
return parse_double(str, "DelSqK =");
};
virtual std::string origin() {return *path;}
private:
t_path path;
};
//===================================================================
struct CP{
int n;
std::array<double, 3> xyz;
int l1, l2;
std::string type_name;
std::list<std::string> adjacent_atoms;
CriticalPoint::CP_TYPE type(){
if(qi::parse(type_name.begin(),type_name.end(),"NACP")){
return CriticalPoint::NACP;
}else if(qi::parse(type_name.begin(),type_name.end(),"NNACP")){
return CriticalPoint::NNACP;
}else if(qi::parse(type_name.begin(),type_name.end(),"BCP")){
return CriticalPoint::BCP;
}else if(qi::parse(type_name.begin(),type_name.end(),"RCP")){
return CriticalPoint::RCP;
}else if(qi::parse(type_name.begin(),type_name.end(),"CCP")){
return CriticalPoint::CCP;
}else{
std::cerr << "critical point type undefined" << std::endl;
return CriticalPoint::UNDEF;
}
}
};
virtual bool check(const std::string& head){
return qi::parse(head.begin(), head.end(), "CP#");
}
virtual void parse(std::list<std::string>& lines){
CP tcp;
if(!qi::phrase_parse(lines.front().begin(), lines.front().end(),
"CP#" >> qi::int_ >> "Coords =" >> double_ >> double_ >> double_ >> "" ,
qi::space, tcp.n, tcp.xyz[0], tcp.xyz[1], tcp.xyz[2])) return;
lines.pop_front();
if(!qi::phrase_parse(lines.front().begin(), lines.front().end(),
"Type =" >> ("(" >> qi::int_) >> "," >> (qi::int_ >> ")") >>
qi::lexeme[+qi::char_("A-Z")] >> *qi::lexeme[(+qi::char_("A-Z0-9"))] >> "",
qi::space, tcp.l1, tcp.l2, tcp.type_name, tcp.adjacent_atoms)) return;
lines.pop_front();
std::shared_ptr<MGPCriticalPoint> cp(new MGPCriticalPoint(tcp.xyz, tcp.type(), path));
while(!lines.empty()){
std::string& l = lines.front();
if(Rho::parse(l)){
cp->properties.add_prop(Rho(*Rho::parse(l), path));
std::cout << "Rho ok: " << cp->properties.get_prop<Rho>()->val << std::endl;
}else if(GradRho::parse(l)){
cp->properties.add_prop(GradRho(*GradRho::parse(l), path));
std::cout << "GradRho ok: " << cp->properties.get_prop<GradRho>()->val[0] << "..." << std::endl;
}else if(HessRhoEigVals::parse(l)){
cp->properties.add_prop(HessRhoEigVals(*HessRhoEigVals::parse(l), path));
std::cout << "HessRhoEigVals ok: " << cp->properties.get_prop<HessRhoEigVals>()->val[0] << "..." << std::endl;
}else if(HessRhoEigVec1::parse(l)){
cp->properties.add_prop(HessRhoEigVec1(*HessRhoEigVec1::parse(l), path));
std::cout << "HessRhoEigVec1 ok: " << cp->properties.get_prop<HessRhoEigVec1>()->val[0] << "..." << std::endl;
}else if(HessRhoEigVec2::parse(l)){
cp->properties.add_prop(HessRhoEigVec2(*HessRhoEigVec2::parse(l), path));
std::cout << "HessRhoEigVec2 ok: " << cp->properties.get_prop<HessRhoEigVec2>()->val[0] << "..." << std::endl;
}else if(HessRhoEigVec3::parse(l)){
cp->properties.add_prop(HessRhoEigVec3(*HessRhoEigVec3::parse(l), path));
std::cout << "HessRhoEigVec3 ok: " << cp->properties.get_prop<HessRhoEigVec3>()->val[0] << "..." << std::endl;
}else if(DelSqRho::parse(l)){
cp->properties.add_prop(DelSqRho(*DelSqRho::parse(l), path));
std::cout << "DelSqRho ok: " << cp->properties.get_prop<DelSqRho>()->val << std::endl;
}else if(V::parse(l)){
cp->properties.add_prop(V(*V::parse(l), path));
std::cout << "V ok: " << cp->properties.get_prop<V>()->val << std::endl;
}else if(G::parse(l)){
cp->properties.add_prop(G(*G::parse(l), path));
std::cout << "G ok: " << cp->properties.get_prop<G>()->val << std::endl;
}else if(K::parse(l)){
cp->properties.add_prop(K(*K::parse(l), path));
std::cout << "K ok: " << cp->properties.get_prop<K>()->val << std::endl;
}else if(L::parse(l)){
cp->properties.add_prop(L(*L::parse(l), path));
std::cout << "L ok: " << cp->properties.get_prop<L>()->val << std::endl;
}else if(DelSqV::parse(l)){
cp->properties.add_prop(DelSqV(*DelSqV::parse(l), path));
std::cout << "DelSqV ok: " << cp->properties.get_prop<DelSqV>()->val << std::endl;
}else if(DelSqVen::parse(l)){
cp->properties.add_prop(DelSqVen(*DelSqVen::parse(l), path));
std::cout << "DelSqVen ok: " << cp->properties.get_prop<DelSqVen>()->val << std::endl;
}else if(DelSqVrep::parse(l)){
cp->properties.add_prop(DelSqVrep(*DelSqVrep::parse(l), path));
std::cout << "DelSqVrep ok: " << cp->properties.get_prop<DelSqVrep>()->val << std::endl;
}else if(DelSqG::parse(l)){
cp->properties.add_prop(DelSqG(*DelSqG::parse(l), path));
std::cout << "DelSqG ok: " << cp->properties.get_prop<DelSqG>()->val << std::endl;
}else if(DelSqK::parse(l)){
cp->properties.add_prop(DelSqK(*DelSqK::parse(l), path));
std::cout << "DelSqK ok: " << cp->properties.get_prop<DelSqK>()->val << std::endl;
}
lines.pop_front();
}
cps.push_back(cp);
}
std::list<std::shared_ptr<MGPCriticalPoint>> cps;
private:
static std::shared_ptr<double> parse_double(const std::string& str, const std::string& prefix){
double val;
bool is_ok = qi::phrase_parse(str.begin(), str.end(),
prefix.c_str() >> qi::double_ ,
qi::space, val);
return is_ok ? std::shared_ptr<double>(new double(val)) : nullptr;
};
static std::shared_ptr<std::array<double,3>> parse_double_3arr(const std::string& str, const std::string& prefix){
std::array<double,3> val;
bool is_ok = qi::phrase_parse(str.begin(), str.end(),
prefix.c_str() >> qi::double_ >> qi::double_ >> qi::double_ ,
qi::space, val[0], val[1], val[2]);
return is_ok ? std::shared_ptr<std::array<double,3>>(new std::array<double,3>(val)) : nullptr;
};
private:
const std::shared_ptr<string> path;
};
int main(){
std::shared_ptr<string> path(new std::string("/home/morrigan/CLionProjects/DAIMAn/test/N4C2C2H.mgp"));
std::ifstream file(*path, std::ifstream::in);
Iterator fwd_begin = make_default_multi_pass(BaseIterator(file));
Iterator fwd_end = make_default_multi_pass(BaseIterator());
MgpBlocks result;
qi::parse(fwd_begin, fwd_end, MgpBlockGrammar(), result);
MgpAtoms mgp_atoms;
MgpCriticalPoints mgp_critical_points(path);
for(auto& block : result){
if(mgp_atoms.check(block.front())){
mgp_atoms.parse(block);
}else if(mgp_critical_points.check(block.front())){
mgp_critical_points.parse(block);
}
}
std::cout << "found " << mgp_critical_points.cps.size() << " critical points" << std::endl;
}