HepLib
Loading...
Searching...
No Matches
Contours.cpp
Go to the documentation of this file.
1
6#include "SD.h"
7#include <math.h>
8#include <cmath>
9
10namespace HepLib::SD {
11
17 void SecDec::Contours(const string & key, const string & pkey) {
18 if(IsZero) return;
19
20 if(key != "") {
22 ostringstream garfn;
23 garfn << key << ".ci.gar";
24 archive ar;
25 ifstream in(garfn.str());
26 in >> ar;
27 in.close();
28 auto c = ar.unarchive_ex("c");
29 if(c!=19790923) throw Error("Contours: *.ci.gar error!");
30 FT_N_XN = ex_to<lst>(ar.unarchive_ex("ftnxn"));
32 }
33
34 //change 2->1 from GiNaC 1.7.7
35 if(FT_N_XN.nops()<1) return;
36 if(Verbose > 0) cout << Color_HighLight << " Contours @ " << now() << RESET << endl;
37
38 if(Minimizer==NULL) Minimizer = new HookeJeeves();
39
40 exvector ftnxn_vec;
41 //change 1->0 from GiNaC 1.7.7
42 for(int i=0; i<FT_N_XN.nops(); i++) ftnxn_vec.push_back(FT_N_XN.op(i));
43
44 auto pid = getpid();
45 string spid = to_string(pid);
46 if(get_env("GiNaC_Parallel_Host").length()>0) spid = get_env("GiNaC_Parallel_Host")+"_"+spid;
47 ostringstream cmd;
48 cmd << "mkdir -p " << spid;
49 if(!dir_exists(spid)) auto rc = system(cmd.str().c_str());
50
51 ostringstream fsofn;
52 if(key != "") {
53 fsofn << key << "F.so";
54 } else {
55 fsofn << spid << "F.so";
56 }
57 void* module = dlopen(fsofn.str().c_str(), RTLD_NOW);
58 if (module == nullptr) {
59 module = dlopen(("./"+fsofn.str()).c_str(), RTLD_NOW);
60 if (module == nullptr) {
61 cerr << ErrColor << "Contours: could not open compiled module!" << RESET << endl;
62 cout << "dlerror(): " << dlerror() << endl;
63 exit(1);
64 }
65 }
66
67 GiNaC_Parallel_NP["LAS"]=0;
68 GiNaC_Parallel_RM["LAS"] = !Debug;
69 auto res =
70 GiNaC_Parallel(ftnxn_vec.size(), [&ftnxn_vec,this,module](int idx)->ex {
71 // return lst{ ft_n, lst{lambda-i, lambda-max} }
72 // with I*[lambda-i]*lambda, lambda < lambda-max
73 // note that lambda sequence only matches to x sequence in F-term
74 if(Verbose>5) cout << " λ: " << idx << " / " << ftnxn_vec.size() << flush;
75 auto ftnxn = ftnxn_vec[idx];
76 int npara = -1;
77 for(auto kv : Parameter) if(npara<kv.first) npara = kv.first;
78 dREAL paras[npara+1];
79 lst plRepl;
80 for(auto kv : Parameter) {
81 paras[kv.first] = (dREAL)ex2q(kv.second);
82 plRepl.append(PL(kv.first)==kv.second);
83 }
84
85 auto ft = ftnxn.op(0);
86 ft = ft.subs(plRepl);
87 if(xSign(ft)>0 || CTLaMax<0) {
88 if(Verbose>5) {
89 cout << "\r \r";
90 cout << " λ: xSign>0 or CTLaMax<0, back to REAL mode!" << endl;
91 }
92 return lst{ ftnxn.op(1), 1979 }; // ft_id, las
93 }
94
95 int nvars = ex_to<numeric>(ftnxn.op(2)).to_int();
96 ostringstream fname;
97
98 if(CTMaxF) {
99 MinimizeBase::FunctionType fp = NULL, fp2 = NULL;
100 fname.clear();
101 fname.str("");
102 fname << "minF_"<<ftnxn.op(1);
103 fp = (MinimizeBase::FunctionType)dlsym(module, fname.str().c_str());
104 fname.clear();
105 fname.str("");
106 fname << "minFM_"<<ftnxn.op(1);
107 fp2 = (MinimizeBase::FunctionType)dlsym(module, fname.str().c_str());
108 if(fp==NULL || fp2==NULL) {
109 cerr << ErrColor << "Contours: fp==NULL/fp2==NULL" << RESET << endl;
110 cout << "dlerror(): " << dlerror() << endl;
111 exit(1);
112 }
113
114 dREAL maxf = Minimizer->FindMinimum(nvars, fp, paras);
115 if(maxf>0) {
116 if(Verbose>5) {
117 cout << "\r \r";
118 cout << " λ: F>0 Found, back to REAL mode!" << endl;
119 }
120 return lst{ ftnxn.op(1), 1979 }; // ft_id, las
121 }
122
123 maxf = Minimizer->FindMinimum(nvars, fp2, paras);
124 if(maxf>0) {
125 if(Verbose>5) {
126 cout << "\r \r";
127 cout << " λ: F<0 Found, back to REAL mode!" << endl;
128 }
129 return lst{ ftnxn.op(1), 1979 }; // ft_id, las
130 }
131 }
132
133 dREAL nlas[nvars];
134 dREAL max_df = -1, max_f;
135 for(int i=0; i<nvars; i++) {
137 fname.clear();
138 fname.str("");
139 fname << "dirC_"<<ftnxn.op(1)<<"_"<<i;
140 dfp = (MinimizeBase::FunctionType)dlsym(module, fname.str().c_str());
141 if(dfp==NULL) {
142 cerr << ErrColor << "Contours: dfp==NULL" << RESET << endl;
143 cout << "dlerror(): " << dlerror() << endl;
144 exit(1);
145 }
146
147 dREAL maxdf = Minimizer->FindMinimum(nvars, dfp, paras);
148 maxdf = -maxdf;
149 nlas[i] = maxdf;
150 if(max_df<maxdf) max_df = maxdf;
151 }
152
153 for(int i=0; i<nvars; i++) { // From FIESTA
154 if(nlas[i] < 1) nlas[i] = 1;
155 else nlas[i] = 1/nlas[i];
156 }
157
158 lst las;
159 for(int i=0; i<nvars; i++) {
160 las.append(q2ex(nlas[i]));
161 }
162
164 fname.clear();
165 fname.str("");
166 fname << "imgF_"<<ftnxn.op(1);
167 fp = (MinimizeBase::FunctionType)dlsym(module, fname.str().c_str());
168 if(fp==NULL) {
169 cerr << ErrColor << "Contours: fp==NULL" << RESET << endl;
170 cout << "dlerror(): " << dlerror() << endl;
171 exit(1);
172 }
173
174 dREAL laBegin = 0, laEnd = CTLaMax, min;
175 dREAL UB[nvars+1];
176 for(int i=0; i<nvars+1; i++) UB[i] = 1;
177
178 min = laEnd;
179 while(true) {
180 UB[nvars] = min;
181 dREAL res = Minimizer->FindMinimum(nvars+1, fp, paras, nlas, UB, NULL, NULL, true, CTTryPTS, CTSavePTS);
182 if(res < -1E-5) laEnd = min;
183 else laBegin = min;
184
185 if(laEnd < 1E-10) {
186 cout << "\r \r";
187 cout << " λ: " << ErrColor << "too small lambda!" << RESET << endl;
188 break;
189 }
190
191 if(laEnd-laBegin <= 0.01*laEnd) break;
192 min = (laBegin + laEnd) / 2.0;
193 }
194 min = laBegin;
195
196 las.append(q2ex(min));
197 if(Verbose>5) {
198 cout << "\r \r";
199 cout << " λ: " << NN(las,3) << endl;
200 }
201
202 return lst{ ftnxn.op(1), las }; // ft_id, las
203
204 }, "LAS");
205
206 if(use_dlclose) dlclose(module);
207
208 ostringstream garfn;
209 if(key != "") {
210 garfn << key;
211 if(pkey != "") garfn << "-" << pkey;
212 garfn << ".las.gar";
213 lst gar_res;
214 for(auto &item : res) gar_res.append(item);
215 archive ar;
216 ar.archive_ex(gar_res, "res");
217 ar.archive_ex(19790923, "c");
218 ofstream out(garfn.str());
219 out << ar;
220 out.close();
221 } else {
222 for(auto &item : res) LambdaMap[item.op(0)] = item.op(1);
223 }
224
225 cmd.clear();
226 cmd.str("");
227 cmd << "rm -rf " << spid;
228 if(!Debug) auto rc = system(cmd.str().c_str());
229 }
230
231}
#define RESET
Definition BASIC.h:79
SecDec header file.
class used to wrap error message
Definition BASIC.h:242
class to minimize a function using HookeJeeves
Definition SD.h:279
virtual dREAL FindMinimum(int nvars, FunctionType func, dREAL *PL=NULL, dREAL *las=NULL, dREAL *UB=NULL, dREAL *LB=NULL, dREAL *IP=NULL, bool compare0=false, int TryPTS=0, int SavePTS=0)=0
dREAL(* FunctionType)(int nvars, dREAL *x, dREAL *pl, dREAL *las)
Definition SD.h:269
dREAL CTLaMax
Definition SD.h:447
MinimizeBase * Minimizer
Definition SD.h:430
void Contours(const string &key="", const string &pkey="")
Contours, note that here we need to provide the specific Parameter.
Definition Contours.cpp:17
namespace for Numerical integration with Sector Decomposition method
Definition AsyMB.cpp:10
long double dREAL
Definition SD.h:122
const char * Color_HighLight
Definition BASIC.cpp:248
void reset_precision()
Definition BASIC.cpp:2325
ex q2ex(__float128 num)
__float128 to ex
Definition BASIC.cpp:869
int NNDigits
Definition Init.cpp:158
ex NN(ex expr, int digits)
the nuerical evaluation
Definition BASIC.cpp:1280
map< string, bool > GiNaC_Parallel_RM
Definition Init.cpp:151
const char * ErrColor
Definition BASIC.cpp:246
string now(bool use_date)
date/time string
Definition BASIC.cpp:527
bool Debug
Definition Init.cpp:144
map< string, int > GiNaC_Parallel_NP
Definition Init.cpp:147
int Verbose
Definition Init.cpp:142
bool dir_exists(string dir)
Definition BASIC.h:297
void set_precision(long prec, bool push)
Definition BASIC.cpp:2316
int xSign(ex const expr)
the always sign for expr
Definition BASIC.cpp:1314
exvector GiNaC_Parallel(int ntotal, std::function< ex(int)> f, const string &key)
GiNaC Parallel Evaluation using fork.
Definition BASIC.cpp:260
string get_env(const string &name)
Definition BASIC.cpp:2371