27 #define HDF5_STATUS_CHECK(status) { \
29 std::cerr << __FILE__ << ":" << __LINE__ << \
30 ": Problem with writing to file. Status code=" \
31 << status << std::endl; \
56 mat.reset(
new double [
n*
m]);
66 mat.reset(
new double [
n*
m]);
78 mat = std::move(orig.mat);
85 mat.reset(
new double [
n*
m]);
96 for(
int i=0;i<
n*
m;i++)
159 assert(A.
n ==
n && B.
m ==
m);
161 dgemm_(&trans,&trans,&A.
n,&B.
m,&A.
m,&alpha,A.
mat.get(),&A.
n,B.
mat.get(),&B.
n,&beta,
mat.get(),&A.
n);
176 int count_sing = std::min(
n,
m);
178 std::unique_ptr<double []> sing_vals(
new double[count_sing]);
181 int lwork = std::max( 3*count_sing + std::max(
n,
m), 5*count_sing);
182 std::unique_ptr<double []> work(
new double[lwork]);
184 std::unique_ptr<double []> vt(
new double[
n*
n]);
188 dgesvd_(&jobu,&jobvt,&n,&
m,
mat.get(),&
n,sing_vals.get(),vt.get(),&
n,0,&
m,work.get(),&lwork,&info);
191 std::cerr <<
"svd failed. info = " << info << std::endl;
204 std::cout << i <<
" " << j <<
"\t" << (*
this)(i,j) << std::endl;
239 basis = std::move(orig.basis);
245 return basis[index].first;
251 return basis[index].second;
276 assert(index <
basis.size());
284 for(
int i=0;i<
basis.size();i++)
303 dim = dim_up * dim_down;
343 for(
unsigned int i=0;i<
basisblocks[K]->getdim();i++)
347 assert(0 &&
"Should never be reached");
360 for(
unsigned int i=0;i<
basisblocks[K]->getdim();i++)
364 assert(0 &&
"Should never be reached");
402 std::cout <<
"K = " << K <<
" (" <<
basisblocks[K]->getdim() <<
")" << std::endl;
429 std::vector<myint> baseUp;
430 std::vector<myint> baseDown;
435 baseUp.reserve(dim_up);
436 baseDown.reserve(dim_down);
440 for(
myint i=0;i<Hb;i++)
443 baseDown.push_back(i);
449 std::vector< std::tuple<myint,myint,int> > totalmom;
450 totalmom.reserve(
dim);
453 for(
unsigned int a=0;a<baseUp.size();a++)
454 for(
unsigned int b=0;b<baseDown.size();b++)
456 auto calcK = [] (
myint cur) ->
int
463 myint ksp = cur & (~cur + 1);
473 int K = calcK(baseUp[a]) + calcK(baseDown[b]);
477 totalmom.push_back(std::make_tuple(baseUp[a],baseDown[b],K));
485 std::sort(totalmom.begin(), totalmom.end(),
486 [](
const std::tuple<myint,myint,int> & a,
const std::tuple<myint,myint,int> & b) ->
bool
488 return std::get<2>(a) < std::get<2>(b);
495 std::for_each(totalmom.begin(), totalmom.end(), [
this](std::tuple<myint,myint,int> elem)
497 auto tmp = std::make_pair(std::get<0>(elem), std::get<1>(elem));
498 basisblocks[std::get<2>(elem)]->basis.push_back(tmp);
509 this->
L= orig.
getL();
513 int space_dim = orig.
getdimK(K);
518 std::cout <<
"Allocing SubBasis: " << space_dim * dim *
sizeof(double) * 1.0/1024/1024 <<
" MB" << std::endl;
525 assert(dim <= space_dim);
528 for(
int i=0;i<dim;i++)
551 std::cout <<
"Allocing clone SubBasis: " << orig.
coeffs->getn() * orig.
coeffs->getm() *
sizeof(double) * 1.0/1024/1024 <<
" MB" << std::endl;
568 coeffs = std::move(orig.coeffs);
569 basis = std::move(orig.basis);
581 std::cout <<
"Allocing move SubBasis: " << orig.
coeffs->getn() * orig.
coeffs->getm() *
sizeof(double) * 1.0/1024/1024 <<
" MB" << std::endl;
603 coeffs = std::move(orig.coeffs);
604 basis = std::move(orig.basis);
624 for(
int s=0;s<
getdim();s++)
628 std::cout << std::endl;
661 for(
int i=0;i<
basis->getdim();i++)
662 if(upket ==
basis->getUp(i) && downket ==
basis->getDown(i))
665 assert(0 &&
"Impossible index for subbasis");
688 myint ksp = cur & (~cur + 1);
696 myint fullstate = (upket <<
L) + downket;
705 int index =
getindex(upket ^ ksp, downket | ksp);
707 transform(index,i) = sign;
721 assert(index < basis->
getdim());
723 upket =
basis->getUp(index);
724 downket =
basis->getDown(index);
729 return (*
basis)[index];
744 (*coeffs)(i,j) = value;
757 for(
int i=0;i<
getdim();i++)
764 norm = 1.0/sqrt(norm);
824 if( K >=
L || S >
Smax || Sz > S)
842 assert(
Exists(K, S, Sz) &&
"Get on nonexisting block");
844 return list[K*
totS + (S*(S+1))/2 + Sz];
864 for(
int S=0;S<=
Smax;S++)
865 for(
int Sz=0;Sz<=S;Sz++)
869 std::cout <<
"Block: S=" << S <<
" Sz=" << Sz <<
" K=" << K << std::endl;
870 list[K*
totS + (S*(S+1))/2 + Sz].Print();
893 for(
int pS=S+1;pS<=
Smax;pS++)
899 std::unique_ptr<matrix> proj_matrix(
new matrix(dimK, dim));
902 for(
int pS=S+1;pS<=
Smax;pS++)
905 auto& cur_basis =
Get(K,pS,Sz);
907 assert(dimK == cur_basis.getspacedim());
909 std::memcpy(&(*proj_matrix)(0,s_count), &(cur_basis.GetCoeff(0,0)),
sizeof(
double) * cur_basis.getdim() * cur_basis.getspacedim());
911 s_count += cur_basis.getdim();
916 std::cout <<
"Doing SVD: K=" << K <<
" S=" << S <<
" Sz=" << Sz << std::endl;
919 auto sing_vals = proj_matrix->svd();
921 int sing_vals_start = 0;
922 while(sing_vals_start < dim && fabs(sing_vals[sing_vals_start]) > 1e-10)
925 auto &finalbasis =
Get(K,S,Sz);
927 assert(finalbasis.getdim() == (dimK-sing_vals_start));
928 assert(finalbasis.getspacedim() == dimK);
930 std::memcpy(&finalbasis.GetCoeff(0,0), &(*proj_matrix)(0,sing_vals_start),
sizeof(
double) * dimK * finalbasis.getdim());
938 for(
int S=
Smax;S>=Sz;S--)
939 for(
int cur_Sz=
Smax;cur_Sz>=Sz;cur_Sz--)
943 std::cout <<
"Deleting: S=" << S <<
" Sz=" << cur_Sz <<
" K=" << K << std::endl;
976 int Sz=(Nu-
Nd) >= 0 ? (Nu-Nd)/2 : (Nd-
Nu)/2;
977 int Smax = (Nu+
Nd)/2;
980 basis.reserve(L*Smax);
984 for(
int S=0;S<=Smax;S++)
987 basis.push_back(std::move(orig.
Get(K,S,Sz)));
988 ind.push_back(std::make_pair(K,S));
1010 return basis.size();
1019 hid_t file_id, group_id, dataset_id, dataspace_id, attribute_id, matspace_id;
1022 file_id = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
1025 group_id = H5Gcreate(file_id,
"/SpinBasis", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
1028 dataspace_id = H5Screate(H5S_SCALAR);
1030 attribute_id = H5Acreate (group_id,
"L", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1031 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &
L );
1033 status = H5Aclose(attribute_id);
1036 attribute_id = H5Acreate (group_id,
"Nu", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1037 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &
Nu );
1039 status = H5Aclose(attribute_id);
1042 attribute_id = H5Acreate (group_id,
"Nd", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1043 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &
Nd );
1045 status = H5Aclose(attribute_id);
1048 int num =
basis.size();
1049 attribute_id = H5Acreate (group_id,
"num", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1050 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &num );
1052 status = H5Aclose(attribute_id);
1055 for(
int i=0;i<
basis.size();i++)
1057 hsize_t dimarr =
basis[i].coeffs->getn()*
basis[i].coeffs->getm();
1059 matspace_id = H5Screate_simple(1, &dimarr, NULL);
1061 std::stringstream name;
1065 dataset_id = H5Dcreate(group_id, name.str().c_str(), H5T_IEEE_F64LE, matspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
1067 assert(
basis[i].coeffs);
1068 status = H5Dwrite(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
basis[i].coeffs->getpointer());
1071 int K =
ind[i].first;
1072 int S =
ind[i].second;
1074 attribute_id = H5Acreate (dataset_id,
"K", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1075 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &K );
1077 status = H5Aclose(attribute_id);
1080 attribute_id = H5Acreate (dataset_id,
"S", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1081 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &S );
1083 status = H5Aclose(attribute_id);
1086 int n =
basis[i].coeffs->getn();
1087 attribute_id = H5Acreate (dataset_id,
"n", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1088 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &n);
1090 status = H5Aclose(attribute_id);
1093 int m =
basis[i].coeffs->getm();
1094 attribute_id = H5Acreate (dataset_id,
"m", H5T_STD_I32LE, dataspace_id, H5P_DEFAULT, H5P_DEFAULT);
1095 status = H5Awrite (attribute_id, H5T_NATIVE_INT, &m);
1097 status = H5Aclose(attribute_id);
1100 status = H5Sclose(matspace_id);
1103 status = H5Dclose(dataset_id);
1107 status = H5Sclose(dataspace_id);
1110 status = H5Gclose(group_id);
1113 status = H5Fclose(file_id);
1119 hid_t file_id, group_id, attribute_id, dataset_id;
1122 file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
1125 group_id = H5Gopen(file_id,
"/SpinBasis", H5P_DEFAULT);
1128 attribute_id = H5Aopen(group_id,
"L", H5P_DEFAULT);
1130 status = H5Aread(attribute_id, H5T_NATIVE_INT, &
L);
1132 status = H5Aclose(attribute_id);
1135 attribute_id = H5Aopen(group_id,
"Nu", H5P_DEFAULT);
1137 status = H5Aread(attribute_id, H5T_NATIVE_INT, &
Nu);
1139 status = H5Aclose(attribute_id);
1142 attribute_id = H5Aopen(group_id,
"Nd", H5P_DEFAULT);
1144 status = H5Aread(attribute_id, H5T_NATIVE_INT, &
Nd);
1146 status = H5Aclose(attribute_id);
1152 attribute_id = H5Aopen(group_id,
"num", H5P_DEFAULT);
1154 status = H5Aread(attribute_id, H5T_NATIVE_INT, &num);
1156 status = H5Aclose(attribute_id);
1162 for(
int i=0;i<num;i++)
1168 std::stringstream name;
1173 dataset_id = H5Dopen(group_id, name.str().c_str(), H5P_DEFAULT);
1176 attribute_id = H5Aopen(dataset_id,
"K", H5P_DEFAULT);
1178 status = H5Aread(attribute_id, H5T_NATIVE_INT, &K);
1180 status = H5Aclose(attribute_id);
1183 attribute_id = H5Aopen(dataset_id,
"S", H5P_DEFAULT);
1185 status = H5Aread(attribute_id, H5T_NATIVE_INT, &S);
1187 status = H5Aclose(attribute_id);
1190 ind.push_back(std::make_pair(K,S));
1195 attribute_id = H5Aopen(dataset_id,
"n", H5P_DEFAULT);
1197 status = H5Aread(attribute_id, H5T_NATIVE_INT, &n);
1199 status = H5Aclose(attribute_id);
1202 attribute_id = H5Aopen(dataset_id,
"m", H5P_DEFAULT);
1204 status = H5Aread(attribute_id, H5T_NATIVE_INT, &m);
1206 status = H5Aclose(attribute_id);
1210 (*
basis[i].coeffs) = -2;
1212 status = H5Dread(dataset_id, H5T_NATIVE_DOUBLE, H5S_ALL, H5S_ALL, H5P_DEFAULT,
basis[i].coeffs->getpointer() );
1215 status = H5Dclose(dataset_id);
1219 status = H5Gclose(group_id);
1222 status = H5Fclose(file_id);
1245 return basis[index];