HepLib
KIRA.cpp
Go to the documentation of this file.
1 
6 #include "IBP.h"
7 #include <cmath>
8 
9 namespace HepLib {
10 
14  string KIRA::Fout(const ex & expr) {
15  ex f = expr;
16  if(is_a<lst>(f)) f = F(f);
17  else if(expr.match(F(w1,w2))) f = F(f.op(1));
18  string fstr = ex2str(f);
19  string_replace_all(fstr,"{","");
20  string_replace_all(fstr,"}","");
21  string_replace_all(fstr,"(","[");
22  string_replace_all(fstr,")","]");
23  return fstr;
24  }
25 
29  ex KIRA::Fin(const string & expr) {
30  string fstr = expr;
31  string_replace_all(fstr,"[","("+to_string(ProblemNumber)+",{");
32  string_replace_all(fstr,"]","})");
33  return str2ex(fstr);
34  }
35 
39  void KIRA::Export() {
40 
41  if(Integral.nops()<1) return;
42 
43  if(WorkingDir.length()<1) WorkingDir = to_string(getpid())+"IBP";
44  string job_dir = WorkingDir + "/" + to_string(ProblemNumber);
45  auto rc = system(("rm -rf "+job_dir).c_str());
46  if(!dir_exists(job_dir)) system(("mkdir -p "+job_dir).c_str());
47 
48  string config_dir = job_dir + "/config";
49  rc = system(("rm -rf "+config_dir).c_str());
50  if(!dir_exists(config_dir)) rc = system(("mkdir -p "+config_dir).c_str());
51 
52  ostringstream oss;
53 
54  // integralfamilies.yaml
55  oss << "integralfamilies:" << endl;
56  oss << " - name: F" << endl;
57  oss << " loop_momenta: [";
58  for(int i=0; i<Internal.nops(); i++) {
59  oss << Internal.op(i);
60  if(i+1<Internal.nops()) oss << ", ";
61  else oss << "]" << endl;
62  }
63 
64  vector<int> symbolic_ibp;
65  for(auto kv : Shift) {
66  if(kv.second.is_zero()) continue;
67  symbolic_ibp.push_back(kv.first);
68  }
69  if(symbolic_ibp.size()>0) {
70  oss << " symbolic_ibp: [";
71  for(int i=0; i<symbolic_ibp.size(); i++) {
72  if(i>0) oss << ",";
73  oss << symbolic_ibp[i];
74  }
75  oss << "]" << endl;
76  }
77 
78  //oss << " top_level_sectors: [127]" << endl;
79  oss << " propagators:" << endl;
80  for(int i=0; i<Propagator.nops(); i++) {
81  oss << " - [ \"" << Propagator.op(i) << "\", 0]" << endl;
82  }
83  if(Cut.nops()>0) {
84  oss << " cut_propagators: [";
85  for(auto i=0; i<Cut.nops(); i++) {
86  oss << Cut.op(i);
87  if(i+1<Cut.nops()) oss << ", ";
88  else oss << "]" << endl;
89  }
90  }
91  ofstream if_out(config_dir+"/integralfamilies.yaml");
92  if_out << oss.str() << endl;
93  if_out.close();
94  oss.str("");
95  oss.clear();
96 
97 
98  // kinematics.yaml
99  oss << "kinematics:" << endl;
100  oss << " incoming_momenta: [";
101  for(int i=0; i<External.nops(); i++) {
102  oss << External.op(i);
103  if(i+1<External.nops()) oss << ", ";
104  else oss << "]" << endl;
105  }
106  //oss << " outgoing_momenta: []" << endl;
107  //oss << " momentum_conservation: [p0,-p1-p2]" << endl;
108 
109  if(!has_w(Replacement)) {
110  lst repl;
111  for(auto item : Replacement) {
112  repl.append(item);
113  repl.append(w*item.op(0) == w*item.op(1));
114  }
115  Replacement = repl;
116  }
117 
118  auto vars = gather_symbols(lst{Propagator, Replacement});
119  exset vset_all;
120  for(auto vi : vars) vset_all.insert(vi);
121  exset vset_mom;
122  for(auto vi : Internal) vset_mom.insert(vi);
123  for(auto vi : External) vset_mom.insert(vi);
124  exset vset;
125  for(auto vi : vset_all) {
126  if(vset_mom.find(vi) == vset_mom.end()) vset.insert(vi);
127  }
128  if(vset.size()>0) {
129  oss << " kinematic_invariants:" << endl;
130  for(auto vi : vset) oss << " - [ " << vi << ", 0 ]" << endl;
131  for(auto i : symbolic_ibp) oss << " - [ b" << (i-1) << ", 0 ]" << endl;
132  }
133 
134  oss << " scalarproduct_rules:" << endl;
135  for(int i=0; i<External.nops(); i++) {
136  auto pi = External.op(i);
137  for(int j=i; j<External.nops(); j++) {
138  auto pj = External.op(j);
139  oss << " - [ [" << pi << "," << pj << "], ";
140  oss << subs(pi*pj, Replacement) << "]" << endl;
141  }
142  }
143 
144  //oss << " symbol_to_replace_by_one: s" << endl;
145 
146  ofstream km_out(config_dir+"/kinematics.yaml");
147  km_out << oss.str() << endl;
148  km_out.close();
149  oss.str("");
150  oss.clear();
151 
152  // handle rmax, smax, dmax
153  int _rmax = -1;
154  int _smax = -1;
155  if(_rmax < 0 || _smax < 0) {
156  int rrmax = 0;
157  int ssmax = 0;
158  for(auto integral : Integral) {
159  int rr = 0;
160  int ss = 0;
161  for(auto item : integral) {
162  if(item>0) rr += ex2int(item);
163  else ss -= ex2int(item);
164  }
165  if(rrmax<rr) rrmax = rr;
166  if(ssmax<ss) ssmax = ss;
167  }
168  if(rrmax > _rmax) _rmax = rrmax;
169  if(ssmax > _smax) _smax = ssmax;
170  }
171  _rmax += ra;
172  _smax += sa;
173 
174  // job.yaml
175  oss << "jobs:" << endl;
176  oss << " - reduce_sectors:" << endl;
177  oss << " reduce: " << endl;
178  oss << " - {r: " << _rmax << ", s: " << _smax << "}" << endl;
179  oss << " select_integrals: " << endl;
180  oss << " select_mandatory_recursively: " << endl;
181  oss << " - {r: " << _rmax << ", s: " << _smax;
182  if(dmax>0) oss << ", d: " << dmax;
183  oss << "}" << endl;
184  oss << " run_initiate: true" << endl;
185  oss << " run_triangular: true" << endl;
186  oss << " run_back_substitution: true" << endl;
187  if(PIntegral.nops()>0) {
188  ostringstream oss2;
189  int nn = PIntegral.nops();
190  for(int i=0; i<nn; i++) oss2 << Fout(PIntegral.op(i)) << endl;
191  ofstream pref_out(job_dir+"/preferred");
192  pref_out << oss2.str() << endl;
193  pref_out.close();
194  oss << " preferred_masters: preferred" << endl;
195  }
196  oss << " - kira2file:" << endl;
197  oss << " target:" << endl;
198  oss << " - [F,integrals]" << endl;
199  ofstream job_out(job_dir+"/job.yaml");
200  job_out << oss.str() << endl;
201  job_out.close();
202  oss.str("");
203  oss.clear();
204 
205  // integrals
206  for(auto integral : Integral) oss << Fout(integral) << endl;
207  ofstream intg_out(job_dir+"/integrals");
208  intg_out << oss.str() << endl;
209  intg_out.close();
210  oss.str("");
211  oss.clear();
212  }
213 
217  void KIRA::Run() {
218  string job_dir = WorkingDir + "/" + to_string(ProblemNumber);
219  ostringstream cmd;
220  cmd << "cd " << job_dir << " && kira " << KArgs << " --silent job.yaml >/dev/null 2>&1";
221  auto rc = system(cmd.str().c_str());
222  }
223 
227  void KIRA::Import() {
228  string job_dir = WorkingDir + "/" + to_string(ProblemNumber);
229  ostringstream fn;
230  fn << job_dir << "/results/F/kira_integrals.kira";
231  auto strvec = file2strvec(fn.str());
232 
233  exmap bMAP;
234  for(auto kv : Shift) {
235  Symbol bi("b"+to_string(kv.first-1));
236  bMAP[bi] = kv.second;
237  }
238 
239  ex exL=0, exR=0;
240  map<ex,int,ex_is_less> flags;
241  lst exRs;
242  for(auto intg : Integral) flags[F(ProblemNumber,intg)] = 1;
243  Rules.remove_all();
244  for(auto line : strvec) {
245  if(line.size()==0) {
246  if(!is_zero(exL)) {
247  exR = exR.subs(bMAP);
248  Rules.append(exL==exR);
249  flags[exL] = 0;
250  exRs.append(exR);
251  }
252  exL = exR = 0;
253  } else if(is_zero(exL)) {
254  exL -= Fin(line);
255  if(!exL.match(F(w1,w2))) {
256  cout << line << endl;
257  throw Error("KIRA::Import error found.");
258  }
259  } else {
260  exR += Fin(line);
261  }
262  }
263  if(!is_zero(exL)) {
264  exR = exR.subs(bMAP);
265  Rules.append(exL==exR);
266  flags[exL] = 0;
267  exRs.append(exR);
268  }
269  MIntegral.remove_all();
270  for(auto kv : flags) {
271  if(kv.second!=0) MIntegral.append(kv.first);
272  }
273  exset miset;
274  find(exRs,F(w1,w2),miset);
275  for(auto mi : miset) MIntegral.append(mi);
276  MIntegral.sort();
277  MIntegral.unique();
278 
279  }
280 
281 }
IBP header file.
class used to wrap error message
Definition: BASIC.h:242
lst Internal
Definition: IBP.h:27
lst MIntegral
Definition: IBP.h:42
map< int, ex > Shift
Definition: IBP.h:36
lst PIntegral
Definition: IBP.h:40
lst Replacement
Definition: IBP.h:29
lst Cut
Definition: IBP.h:32
lst Integral
Definition: IBP.h:31
lst External
Definition: IBP.h:28
lst Propagator
Definition: IBP.h:30
int ProblemNumber
Definition: IBP.h:39
string WorkingDir
Definition: IBP.h:38
lst Rules
Definition: IBP.h:43
void Export() override
Export input data for KIRA.
Definition: KIRA.cpp:39
int dmax
Definition: IBP.h:97
void Import() override
import kira result
Definition: KIRA.cpp:227
void Run() override
invoke kira program for reduction
Definition: KIRA.cpp:217
static string KArgs
Definition: IBP.h:93
int sa
Definition: IBP.h:96
int ra
Definition: IBP.h:95
class extended to GiNaC symbol class, represent a positive symbol
Definition: BASIC.h:113
HepLib namespace.
Definition: BASIC.cpp:17
lst gather_symbols(const ex &e)
get all symbols from input expression
Definition: BASIC.cpp:539
bool has_w(const ex &e)
Definition: BASIC.cpp:2223
ex str2ex(const string &expr, symtab stab)
convert string to ex expression, using Parser internally
Definition: BASIC.cpp:670
ex w
Definition: Init.cpp:90
vector< string > file2strvec(string filename, bool skip_empty)
read file content to string vector
Definition: BASIC.cpp:809
void string_replace_all(string &str, const string &from, const string &to)
Definition: Functions.cpp:148
string ex2str(const ex &expr)
convert ex to output string, the defalut printer format will be used
Definition: BASIC.cpp:715
bool dir_exists(string dir)
Definition: BASIC.h:297
ex w1
Definition: BASIC.h:499
ex w2
Definition: BASIC.h:499
int ex2int(ex num)
ex to integer
Definition: BASIC.cpp:893
ex subs(const ex &e, init_list sl)
Definition: BASIC.h:51