1 #ifndef OBS_DIAGCORR_HPP
2 #define OBS_DIAGCORR_HPP
4 #include "latlib/o815/o815.h"
6 #include "latlib/writeout.h"
8 #include "latlib/obstat.hpp"
15 #include <gsl/gsl_complex.h>
16 #include <gsl/gsl_complex_math.h>
17 #include <gsl/gsl_math.h>
18 #include <gsl/gsl_eigen.h>
19 #include <gsl/gsl_linalg.h>
20 #include <gsl/gsl_cblas.h>
21 #include <gsl/gsl_blas.h>
25 class obs_diagcorr : public o815::obs {
28 obs_diagcorr(o815 *_O815);
32 void _meas(bool loadedobs, const int& nthmeas);
36 static complex<double> effMass(vector < complex<double> > *preCalculated, vector< complex<double> > *excludedMeas, int nmeas, void *para);
37 static void preEffMass(vector< vector < complex<double> > > *allVals, vector < complex<double> > *preCalculated, void *para);
43 complex<double> *OM[16+1];
45 gsl_matrix_complex ***measurements;
47 gsl_vector_complex **measurements_disco;
49 static void cdiag (gsl_vector *v, gsl_matrix_complex *evec, gsl_matrix_complex *m);
52 obs_diagcorr::obs_diagcorr(o815 *_O815) : o815::obs("diagcorr",
53 _O815->paraQ->getParaNames() +
56 _O815, sizeof(complex<double>) * ( 16*_O815->comargs.lsize[1]/2 + 1*4 ) ) {
57 for (int ivar = 0; ivar<16+1; ivar++)
58 OM[ivar] = (complex<double>*)( obsMem + ivar*sizeof(complex<double>)*( _O815->comargs.lsize[1]/2 ) );
60 Sim = (sim*)O815->Sim;
61 spatialV = O815->comargs.lsize[0] * O815->comargs.lsize[0] * O815->comargs.lsize[0];
63 measurements = new gsl_matrix_complex**[O815->comargs.nmeas];
64 for (int imeas = 0; imeas<O815->comargs.nmeas; imeas++) {
65 measurements[imeas] = new gsl_matrix_complex*[_O815->comargs.lsize[1]/2];
66 for (int itsep=0; itsep<_O815->comargs.lsize[1]/2; itsep++)
67 measurements[imeas][itsep] = gsl_matrix_complex_alloc(4,4);
70 measurements_disco = new gsl_vector_complex*[O815->comargs.nmeas];
71 for (int imeas = 0; imeas<O815->comargs.nmeas; imeas++)
72 measurements_disco[imeas] = gsl_vector_complex_alloc(4);
75 void obs_diagcorr::_start() {
76 // *out << O815->comargs.nmeas << endl;
78 //*out << "OBS_test: start" << endl;
81 void obs_diagcorr::_meas(bool loadedobs, const int& nthmeas) {
85 for (int icorr=0; icorr<4; icorr++)
86 for (int jcorr=0; jcorr<4; jcorr++)
87 for (int itsep=0; itsep<O815->comargs.lsize[1]/2; itsep++) {
88 gsl_matrix_complex_set(measurements[nthmeas][itsep], icorr, jcorr,
89 gsl_complex_rect( OM[icorr*4+jcorr][itsep].real(), OM[icorr*4+jcorr][itsep].imag() ));
92 for (int icorr=0; icorr<4; icorr++) {
93 gsl_vector_complex_set(measurements_disco[nthmeas], icorr,
94 gsl_complex_rect( OM[16][icorr].real(), OM[16][icorr].imag() ) );
98 void obs_diagcorr::cdiag (gsl_vector *v, gsl_matrix_complex *evec, gsl_matrix_complex *m)
100 gsl_eigen_hermv_workspace *wspace = gsl_eigen_hermv_alloc(100);
102 gsl_eigen_hermv(m, v, evec, wspace);
103 gsl_eigen_hermv_sort(v, evec, GSL_EIGEN_SORT_VAL_ASC);
105 gsl_eigen_hermv_free(wspace);
106 gsl_matrix_complex_free(evec);
109 void obs_diagcorr::_finish() {
110 gsl_matrix_complex *totalval = gsl_matrix_complex_alloc(4,4);
111 gsl_matrix_complex *tmpmatrix = gsl_matrix_complex_alloc(4,4);
112 gsl_matrix_complex *tmpmatrix2 = gsl_matrix_complex_alloc(4,4);
113 gsl_vector *tmpvec = gsl_vector_alloc(4);
114 gsl_vector_complex *tmpvecc = gsl_vector_complex_alloc(4);
115 gsl_vector *tmpvec2 = gsl_vector_alloc(4);
116 gsl_vector *jackres = gsl_vector_alloc(4);
117 gsl_vector *jackerrornorm = gsl_vector_alloc(4);
118 gsl_vector_complex *totaldisco = gsl_vector_complex_alloc(4);
119 gsl_matrix_complex *evecres = gsl_matrix_complex_alloc(4,4);
120 gsl_matrix_complex *evecerrornorm = gsl_matrix_complex_alloc(4,4);
122 gsl_vector_complex_set_zero(totaldisco);
123 for (int imeas=0; imeas<O815->comargs.nmeas; imeas++)
124 gsl_vector_complex_add( totaldisco, measurements_disco[imeas] );
126 for (int itsep = 0; itsep < O815->comargs.lsize[1]/2; itsep++) {
127 gsl_matrix_complex_set_zero(totalval);
128 gsl_vector_set_zero(jackerrornorm);
129 gsl_matrix_complex_set_zero(evecerrornorm);
131 for (int imeas=0; imeas<O815->comargs.nmeas; imeas++)
132 gsl_matrix_complex_add( totalval, measurements[imeas][itsep] );
134 gsl_matrix_complex_memcpy (tmpmatrix, totalval);
135 gsl_matrix_complex_scale (tmpmatrix, gsl_complex_rect(1.0/O815->comargs.nmeas, 0.0) );
136 for (int icorr=0; icorr<4; icorr++)
137 for (int jcorr=0; jcorr<4; jcorr++) {
138 gsl_complex discopart = gsl_complex_mul( gsl_vector_complex_get(totaldisco, icorr),
139 gsl_complex_conjugate( gsl_vector_complex_get(totaldisco, jcorr) )
141 discopart = gsl_complex_mul_real(discopart, pow(1.0/O815->comargs.nmeas,2));
142 gsl_matrix_complex_set( tmpmatrix, icorr, jcorr,
143 gsl_complex_sub( gsl_matrix_complex_get( tmpmatrix, icorr, jcorr ),
148 cdiag(jackres, evecres, tmpmatrix);
150 for (int imeas=0; imeas<O815->comargs.nmeas; imeas++) {
151 gsl_matrix_complex_memcpy (tmpmatrix, totalval);
152 gsl_matrix_complex_sub (tmpmatrix, measurements[imeas][itsep]);
153 gsl_matrix_complex_scale ( tmpmatrix, gsl_complex_rect(1.0/(O815->comargs.nmeas-1), 0.0) );
154 gsl_vector_complex_memcpy( tmpvecc, totaldisco );
155 gsl_vector_complex_sub( tmpvecc, measurements_disco[imeas] );
156 for (int icorr=0; icorr<4; icorr++)
157 for (int jcorr=0; jcorr<4; jcorr++) {
158 gsl_complex discopart = gsl_complex_mul( gsl_vector_complex_get(tmpvecc, icorr),
159 gsl_complex_conjugate( gsl_vector_complex_get(tmpvecc, jcorr) )
161 discopart = gsl_complex_mul_real(discopart, pow(1.0/(O815->comargs.nmeas-1),2));
162 gsl_matrix_complex_set( tmpmatrix, icorr, jcorr,
163 gsl_complex_sub( gsl_matrix_complex_get( tmpmatrix, icorr, jcorr ),
168 cdiag(tmpvec, tmpmatrix2, tmpmatrix);
170 gsl_vector_sub(tmpvec, jackres);
171 gsl_vector_memcpy(tmpvec2, tmpvec);
172 gsl_vector_mul(tmpvec, tmpvec2);
173 gsl_vector_add(jackerrornorm, tmpvec);
175 gsl_matrix_complex_sub(tmpmatrix2, evecres);
176 for (int i=0; i<4; i++)
177 for (int j=0; j<4; j++) {
179 gsl_complex_rect( pow(GSL_REAL( gsl_matrix_complex_get( tmpmatrix2, i, j ) ),2),
180 pow(GSL_IMAG( gsl_matrix_complex_get( tmpmatrix2, i, j ) ),2));
181 gsl_matrix_complex_set(tmpmatrix, i, j, tmperr);
183 gsl_matrix_complex_add( evecerrornorm, tmpmatrix );
185 gsl_vector_scale( jackerrornorm,
186 (double)(O815->comargs.nmeas-1)/O815->comargs.nmeas );
187 gsl_matrix_complex_scale( evecerrornorm, gsl_complex_rect( (double)(O815->comargs.nmeas-1)/O815->comargs.nmeas, 0.0 ) );
189 *out << O815->paraQ->getParaVals();
190 *out << "\t" << itsep;
191 for (int iev = 0; iev < 4; iev++) {
192 *out << "\t" << gsl_vector_get(jackres,iev)
193 << "\t" << sqrt(gsl_vector_get(jackerrornorm,iev));
194 for (int iinter=0; iinter<4; iinter++)
195 *out << "\t" << GSL_REAL(gsl_matrix_complex_get(evecres, iinter, iev))
196 << "\t" << sqrt( GSL_REAL(gsl_matrix_complex_get(evecerrornorm, iinter, iev)) );
201 gsl_matrix_complex_free(totalval);
202 gsl_matrix_complex_free(tmpmatrix);
203 gsl_matrix_complex_free(evecres);
204 gsl_vector_free(tmpvec);
205 gsl_vector_complex_free(tmpvecc);
206 gsl_vector_free(tmpvec2);
207 gsl_vector_free(jackres);
208 gsl_vector_free(jackerrornorm);
209 gsl_vector_complex_free(totaldisco);
212 void obs_diagcorr::corrCompute()
214 complex<double> phislice[4][O815->comargs.lsize[1]];
216 for (int icorr=0; icorr<4; icorr++)
219 for (int it = 0; it < O815->comargs.lsize[1]; it++) {
220 for (int icorr=0; icorr<4; icorr++)
221 phislice[icorr][it] = 0;
223 for (int ix = 0; ix < spatialV; ix++) {
224 phislice[0][it] += conj(Sim->phi[ 0*Sim->lsize4 + it*spatialV + ix ]) * Sim->phi[ 0*Sim->lsize4 + it*spatialV + ix ];
225 phislice[1][it] += Sim->phi[ 0*Sim->lsize4 + it*spatialV + ix ] * Sim->phi[ 1*Sim->lsize4 + it*spatialV + ix ];
226 phislice[2][it] += conj(Sim->phi[ 0*Sim->lsize4 + it*spatialV + ix ]) * conj(Sim->phi[ 1*Sim->lsize4 + it*spatialV + ix ]);
227 phislice[3][it] += conj(Sim->phi[ 1*Sim->lsize4 + it*spatialV + ix ]) * Sim->phi[ 1*Sim->lsize4 + it*spatialV + ix ];
230 for (int icorr=0; icorr<4; icorr++) {
231 phislice[icorr][it] /= spatialV;
232 OM[16][icorr] += phislice[icorr][it];
236 for (int icorr = 0; icorr < 4; icorr++) {
237 for (int jcorr = 0; jcorr < 4; jcorr++) {
238 for (int itsep = 0; itsep < O815->comargs.lsize[1]/2; itsep++) {
239 OM[icorr*4+jcorr][itsep] = 0;
241 for (int it = 0; it < O815->comargs.lsize[1]; it++)
242 OM[icorr*4+jcorr][itsep] += phislice[icorr][ (it+itsep)%O815->comargs.lsize[1] ] * conj( phislice[jcorr][it] );
244 OM[icorr*4+jcorr][itsep] /= O815->comargs.lsize[1];
248 OM[16][icorr] /= O815->comargs.lsize[1];