Import ABACUS++G_8 code; cleanup files; new README

This commit is contained in:
J.-S. Caux
2018-02-09 17:34:05 +01:00
parent 61084ef3b3
commit 103cbe84d6
172 changed files with 46745 additions and 0 deletions
+244
View File
@@ -0,0 +1,244 @@
/**********************************************************
This software is part of J.-S. Caux's ABACUS library.
Copyright (c).
-----------------------------------------------------------
File: NRG_DFF_Matrix_Block_builder.cc
Purpose: calculate matrix elements of density operator between
selected states in NRG for a selected block of the whole matrix.
For collaboration with Robert Konik.
***********************************************************/
#include "JSC.h"
using namespace std;
using namespace JSC;
namespace JSC {
void Build_DME_Matrix_Block_for_NRG (DP c_int, DP L, int N, int iKmin, int iKmax, int Nstates_required, bool symmetric_states, int iKmod,
int weighing_option, int nrglabel_left_begin, int nrglabel_left_end, int nrglabel_right_begin, int nrglabel_right_end,
int block_option, DP* DME_block_1, DP* DME_block_2, Vect_DP Kweight)
{
// Given a list of states produced by Select_States_for_NRG, this function
// computes all the density form factors < lstate| \rho | rstate >
// for all id_left_begin <= id_left <= id_left_end (similarly for right states).
// The block_option flag determines what kind of data is written in the 2 DME blocks.
// If !symmetric_states, then only block 1 is filled.
// If symmetric_states, then
// if block_option == 1, only block 1 is filled with the symmetric states ME.
// if block_option == 2, both blocks 1 and 2 are filled with ME and ME (after parity) respectively.
// ASSUMPTIONS:
// We assume the DME_blocks are already reserved in memory.
// If !symmetric states, DME_block_2 doesn't need to be allocated.
if (nrglabel_left_begin < 0 || nrglabel_right_begin < 0) JSCerror("beginning nrglabels negative in Build_DME_Matrix_Block_for_NRG");
if (nrglabel_left_end >= Nstates_required) JSCerror("nrglabel_left_end too large in Build_DME_Matric_Block_for_NRG");
if (nrglabel_right_end >= Nstates_required) JSCerror("nrglabel_right_end too large in Build_DME_Matric_Block_for_NRG");
if (nrglabel_left_begin > nrglabel_left_end) JSCerror("nrglabels of left states improperly chosen in DME block builder.");
if (nrglabel_right_begin > nrglabel_right_end) JSCerror("nrglabels of right states improperly chosen in DME block builder.");
// DME_block is a pointer of size row_l * col_l, where
int block_row_length = nrglabel_right_end - nrglabel_right_begin + 1;
int block_col_length = nrglabel_left_end - nrglabel_left_begin + 1;
// iKmod is an integer specifying that we're only interested in momenta multiples of
// a base unit. In other words, if the perturbation potential repeats itself iKmod times
// on the periodic cycle on which the theory is defined, then we only need states such
// that iKl - iKr = (integer) * iKmod. We impose this constraint at the same time as
// the check on the weight of the state.
// We start by reading off the list of selected states:
stringstream NRG_stringstream;
string NRG_string;
NRG_stringstream << "States_";
NRG_stringstream << "c_" << c_int << "_L_" << L << "_N_" << N
<< "_iKmin_" << iKmin << "_iKmax_" << iKmax << "_Nstates_" << Nstates_required
<< "_Sym_" << symmetric_states << "_iKmod_" << iKmod << "_wopt_" << weighing_option << ".nrg";
NRG_string = NRG_stringstream.str();
const char* NRG_Cstr = NRG_string.c_str();
ifstream infile;
infile.open(NRG_Cstr);
if (infile.fail()) {
cout << NRG_Cstr << endl;
JSCerror("The input file was not opened successfully in Build_DME_Matrix_between_Selected_States_for_NRG. ");
}
// Read the whole data file:
//const int MAXDATA = 5000000;
const int MAXDATA = JSC::max(nrglabel_left_end, nrglabel_right_end) + 10; // 10 for safety...
int* nrglabel = new int[MAXDATA];
DP* omega = new DP[MAXDATA];
int* iK = new int[MAXDATA];
string* label = new string[MAXDATA];
bool* sym = new bool[MAXDATA];
DP* weight = new DP[MAXDATA];
int Ndata = 0;
while (((infile.peek()) != EOF) && (Ndata < MAXDATA)) {
infile >> nrglabel[Ndata];
if (nrglabel[Ndata] != Ndata) JSCerror("reading states nrglabels wrong in NRG_DME_Matrix_Block_builder");
infile >> omega[Ndata];
infile >> iK[Ndata];
infile >> label[Ndata];
infile >> sym[Ndata];
infile >> weight[Ndata];
Ndata++;
}
infile.close();
// We first reconstruct all the needed eigenstates:
// Vector containing all the kept states:
Vect<LiebLin_Bethe_State> Kept_States_left(block_col_length);
Vect<LiebLin_Bethe_State> Kept_States_right(block_row_length);
// Dummy state for calculations
LiebLin_Bethe_State Scanstate (c_int, L, N);
// State list containing all the types of states
Scan_State_List<LiebLin_Bethe_State> List_of_Basic_States ('Z', Scanstate);
for (int nrglabel_left = nrglabel_left_begin; nrglabel_left <= nrglabel_left_end; ++nrglabel_left) {
Scanstate = List_of_Basic_States.Return_State (Extract_Base_Label(label[nrglabel_left]));
Scanstate.Set_to_Label (label[nrglabel_left]);
Scanstate.Compute_All(true);
Kept_States_left[nrglabel_left - nrglabel_left_begin] = Scanstate;
if (!Scanstate.conv) cout << "State of label " << label[nrglabel_left] << " did not converge after " << Scanstate.iter_Newton
<< " Newton step; diffsq = " << Scanstate.diffsq << endl;
} // for nrglabel_left
for (int nrglabel_right = nrglabel_right_begin; nrglabel_right <= nrglabel_right_end; ++nrglabel_right) {
Scanstate = List_of_Basic_States.Return_State (Extract_Base_Label(label[nrglabel_right]));
Scanstate.Set_to_Label (label[nrglabel_right]);
Scanstate.Compute_All(true);
Kept_States_right[nrglabel_right - nrglabel_right_begin] = Scanstate;
if (!Scanstate.conv) cout << "State of label " << label[nrglabel_right] << " did not converge after " << Scanstate.iter_Newton
<< " Newton step; diffsq = " << Scanstate.diffsq << endl;
} // for nrglabel_left
// Now that we have all the states, we can do the full calculation of all cross form factors:
for (int ll = 0; ll < block_col_length; ++ll) {
for (int lr = 0; lr < block_row_length; ++lr) {
if (Kept_States_left[ll].conv && Kept_States_right[lr].conv
//&& abs(Kept_States[il].iK - Kept_States[ir].iK) % iKmod == 0)
&& abs(Kept_States_left[ll].iK) % iKmod == 0
&& abs(Kept_States_right[lr].iK) % iKmod == 0) {
if (!symmetric_states) {
DME_block_1[ll* block_row_length + lr] = // By convention, put the lowest energy state to the left
Kweight[abs(Kept_States_left[ll].iK - Kept_States_right[lr].iK)]
* (Kept_States_left[ll].E <= Kept_States_right[lr].E
? real(exp(ln_Density_ME (Kept_States_left[ll], Kept_States_right[lr])))
: real(exp(ln_Density_ME (Kept_States_right[lr], Kept_States_left[ll]))));
// We don't do anything with block 2, we don't even assume it's been allocated.
}
else if (symmetric_states) {
// Check parity of both states:
bool Lstate_parity_inv = Kept_States_left[ll].Check_Symmetry();
bool Rstate_parity_inv = Kept_States_right[lr].Check_Symmetry();
if (Lstate_parity_inv && Rstate_parity_inv) {
DME_block_1[ll* block_row_length + lr] = Kweight[abs(Kept_States_left[ll].iK - Kept_States_right[lr].iK)]
* real(exp(ln_Density_ME (Kept_States_left[ll], Kept_States_right[lr])));
if (block_option == 2) DME_block_2[ll* block_row_length + lr] = 0.0;
}
else if (!Lstate_parity_inv && !Rstate_parity_inv) {
LiebLin_Bethe_State PRstate = Kept_States_right[lr];
PRstate.Parity_Flip();
if (block_option == 1) {
DME_block_1[ll* block_row_length + lr] =
(Kweight[abs(Kept_States_left[ll].iK - Kept_States_right[lr].iK)] * real(exp(ln_Density_ME (Kept_States_left[ll], Kept_States_right[lr])))
+ Kweight[abs(Kept_States_left[ll].iK - PRstate.iK)] * real(exp(ln_Density_ME (Kept_States_left[ll], PRstate))));
// We don't do anything with block 2, we don't even assume it's been allocated.
}
else if (block_option == 2) {
DME_block_1[ll* block_row_length + lr] =
Kweight[abs(Kept_States_left[ll].iK - Kept_States_right[lr].iK)] * real(exp(ln_Density_ME (Kept_States_left[ll], Kept_States_right[lr])));
DME_block_2[ll* block_row_length + lr] =
Kweight[abs(Kept_States_left[ll].iK - PRstate.iK)] * real(exp(ln_Density_ME (Kept_States_left[ll], PRstate)));
}
}
else if (Lstate_parity_inv) {
LiebLin_Bethe_State PRstate = Kept_States_right[lr];
PRstate.Parity_Flip();
if (block_option == 1) {
DME_block_1[ll* block_row_length + lr] = sqrt(0.5) *
(Kweight[abs(Kept_States_left[ll].iK - Kept_States_right[lr].iK)] * real(exp(ln_Density_ME (Kept_States_left[ll], Kept_States_right[lr])))
+ Kweight[abs(Kept_States_left[ll].iK - PRstate.iK)] * real(exp(ln_Density_ME (Kept_States_left[ll], PRstate))));
// We don't do anything with block 2, we don't even assume it's been allocated.
}
else if (block_option == 2) {
DME_block_1[ll* block_row_length + lr] = sqrt(0.5) *
Kweight[abs(Kept_States_left[ll].iK - Kept_States_right[lr].iK)] * real(exp(ln_Density_ME (Kept_States_left[ll], Kept_States_right[lr])));
DME_block_2[ll* block_row_length + lr] = sqrt(0.5) *
Kweight[abs(Kept_States_left[ll].iK - PRstate.iK)] * real(exp(ln_Density_ME (Kept_States_left[ll], PRstate)));
}
}
else { // only R state is parity invariant, flip left:
LiebLin_Bethe_State PLstate = Kept_States_left[ll];
PLstate.Parity_Flip();
if (block_option == 1) {
DME_block_1[ll* block_row_length + lr] = sqrt(0.5) *
(Kweight[abs(Kept_States_left[ll].iK - Kept_States_right[lr].iK)] * real(exp(ln_Density_ME (Kept_States_left[ll], Kept_States_right[lr])))
+ Kweight[abs(PLstate.iK - Kept_States_right[lr].iK)] * real(exp(ln_Density_ME (PLstate, Kept_States_right[lr]))));
// We don't do anything with block 2, we don't even assume it's been allocated.
}
else if (block_option == 2) {
DME_block_1[ll* block_row_length + lr] = sqrt(0.5) *
Kweight[abs(Kept_States_left[ll].iK - Kept_States_right[lr].iK)] * real(exp(ln_Density_ME (Kept_States_left[ll], Kept_States_right[lr])));
DME_block_2[ll* block_row_length + lr] = sqrt(0.5) *
Kweight[abs(PLstate.iK - Kept_States_right[lr].iK)] * real(exp(ln_Density_ME (PLstate, Kept_States_right[lr])));
}
}
} // if (symmetric_states)
} // if conv & iKmod condition fulfilled
else {
DME_block_1[ll* block_row_length + lr] = 0.0;
if (symmetric_states && block_option == 2) DME_block_2[ll* block_row_length + lr] = 0.0; // condition, to prevent segfault if DME_block_2 not allocated.
}
//cout << ll << "\t" << lr << "\t" << DME_block[ll* block_row_length + lr] << endl;
} // for lr
} // for ll
return;
}
} // namespace JSC
+47
View File
@@ -0,0 +1,47 @@
/**********************************************************
This software is part of J.-S. Caux's ABACUS library.
Copyright (c).
-----------------------------------------------------------
File: NRG_K_Weight_integrand.cc
Purpose: for NRG, this function specifies the momentum-dependent
part of the perturbation prefactor.
Collaboration with Robert Konik.
***********************************************************/
#include "JSC.h"
using namespace std;
using namespace JSC;
namespace JSC {
// This function defines the integrand for momentum-dependent perturbation prefactor
DP K_Weight_integrand (Vect_DP args)
{
// Momentum-dependent part of weight used in selecting kept form factors.
// This function gives the integrand, with the weight being
// \int_{-1/2}^{1/2} dx integrand(x)
DP value;
{ // Case of a harmonic trap of strength omega_trap centered on 0:
// args[0] == x
// args[1] == iK
// args[2] == L
// args[3] == omega_trap
value = 12.0 * args[0] * args[0] * cos(twoPI * args[0] * args[1]) * pow(args[2], 2.0) * pow(args[3], 3.0);
}
return(value);
}
} // namespace JSC
+461
View File
@@ -0,0 +1,461 @@
/**********************************************************
This software is part of J.-S. Caux's ABACUS library.
Copyright (c).
-----------------------------------------------------------
File: NRG_State_Selector.cc
Purpose: select states for numerical RG method,
collaboration with Robert Konik.
***********************************************************/
#include "JSC.h"
using namespace std;
using namespace JSC;
namespace JSC {
DP Estimate_Contribution_of_Single_ph_Annihilation_Path_to_2nd_Order_PT (LiebLin_Bethe_State& TopState,
LiebLin_Bethe_State& GroundState,
//Vect_DP Weight_integral)
Vect<complex <DP> >& FT_of_potential)
{
//cout << TopState.label << "\t" << GroundState.label << endl;
//cout << TopState.lambdaoc << endl;
DP contrib_estimate = 0.0;
// Define OriginIx2 for labelling:
Vect<int> OriginIx2 = GroundState.Ix2;
// Calculate the number of particles in this state:
State_Label_Data topdata = Read_State_Label (TopState.label, OriginIx2);
int nphpairs = topdata.nexc[0];
if (nphpairs == 0) JSCerror("Trying to annihilate ground state in Estimate_Contribution...");
DP densityME = real(exp(ln_Density_ME (GroundState, TopState)));
if (is_nan(densityME)) {
cout << "ME is nan: label = " << TopState.label << endl;
JSCerror("ME didn't return value.");
}
int nr_cont = 0;
// Add first-order PT contribution, and 2nd order from ground state (V_{00} == 1)
//contrib_estimate += Weight_integral[Weight_integral.size()/2 + (TopState.iK - GroundState.iK)]
//* (densityME/(GroundState.E - TopState.E)) * (1.0 - (GroundState.N/GroundState.L)/(GroundState.E - TopState.E));
contrib_estimate += abs(FT_of_potential[FT_of_potential.size()/2 + (TopState.iK - GroundState.iK)]
* (densityME/(GroundState.E - TopState.E))
* (1.0 - (GroundState.N/GroundState.L)/(GroundState.E - TopState.E)));
nr_cont++;
// Add second order PT contribution coming from TopState:
//contrib_estimate += Weight_integral[Weight_integral.size()/2 + 0]
//* Weight_integral[Weight_integral.size()/2 + (TopState.iK - GroundState.iK)]
//* 1.0 * densityME * (GroundState.N/GroundState.L) // 1.0 is V_{TopState, TopState}
///((GroundState.E - TopState.E) * (GroundState.E - TopState.E));
contrib_estimate += abs(FT_of_potential[FT_of_potential.size()/2 + 0]
* FT_of_potential[FT_of_potential.size()/2 + (TopState.iK - GroundState.iK)]
* 1.0 * densityME * (GroundState.N/GroundState.L) // 1.0 is V_{TopState, TopState}
/((GroundState.E - TopState.E) * (GroundState.E - TopState.E)));
nr_cont++;
//cout << "Here b" << endl;
// Now add 2nd order terms coming from single particle-hole annihilation paths:
// this is only to be included for states with at least 4 excitations (2 ph pairs)
if (nphpairs >= 2) {
for (int ipart = 0; ipart < nphpairs; ++ipart) {
for (int ihole = 0; ihole < nphpairs; ++ihole) {
LiebLin_Bethe_State DescendedState = TopState;
//cout << "Here 2a" << "\tipart " << ipart << "\tihole " << ihole << " out of " << nphpairs << " nphpairs, label " << TopState.label << endl;
//cout << "TopState.Ix2 " << TopState.Ix2 << endl;
//cout << "DescendedIx2 " << DescendedState.Ix2 << endl;
DescendedState.Annihilate_ph_pair(ipart, ihole, OriginIx2);
DescendedState.Compute_All(true);
//cout << "DescendedIx2 " << DescendedState.Ix2 << endl;
DP densityME_top_desc = real(exp(ln_Density_ME (TopState, DescendedState)));
DP densityME_desc_ground = real(exp(ln_Density_ME (DescendedState, GroundState)));
//contrib_estimate += Weight_integral[Weight_integral.size()/2 + (TopState.iK - DescendedState.iK)]
//* Weight_integral[Weight_integral.size()/2 + (DescendedState.iK - GroundState.iK)]
//* densityME_top_desc * densityME_desc_ground
///((GroundState.E - TopState.E) * (GroundState.E - DescendedState.E));
// if intermediate state has momentum within allowable window, OK, otherwise discard contribution:
if (abs(TopState.iK - DescendedState.iK) < FT_of_potential.size()/2 &&
abs(DescendedState.iK - GroundState.iK) < FT_of_potential.size()/2) {
contrib_estimate += abs(FT_of_potential[FT_of_potential.size()/2 + (TopState.iK - DescendedState.iK)]
* FT_of_potential[FT_of_potential.size()/2 + (DescendedState.iK - GroundState.iK)]
* densityME_top_desc * densityME_desc_ground
/((GroundState.E - TopState.E) * (GroundState.E - DescendedState.E)));
nr_cont++;
}
if (nphpairs >= 3) { // go one step further
for (int ipart2 = ipart; ipart2 < nphpairs - 1; ++ipart2) {
for (int ihole2 = ihole; ihole2 < nphpairs - 1; ++ihole2) {
LiebLin_Bethe_State DescendedState2 = DescendedState;
//cout << "Here 3a" << "\tipart2 " << ipart2 << "\tihole2 " << ihole2 << endl;
//cout << DescendedState2.Ix2 << endl;
DescendedState2.Annihilate_ph_pair(ipart2, ihole2, OriginIx2);
DescendedState2.Compute_All(true);
//cout << DescendedState2.Ix2 << endl;
//cout << DescendedState2.lambdaoc << endl;
DP densityME_top_desc2 = real(exp(ln_Density_ME (TopState, DescendedState2)));
DP densityME_desc2_ground = real(exp(ln_Density_ME (DescendedState2, GroundState)));
//contrib_estimate += Weight_integral[Weight_integral.size()/2 + (TopState.iK - DescendedState2.iK)]
//* Weight_integral[Weight_integral.size()/2 + (DescendedState2.iK - GroundState.iK)]
//* densityME_top_desc2 * densityME_desc2_ground
///((GroundState.E - TopState.E) * (GroundState.E - DescendedState2.E));
// if intermediate state has momentum within allowable window, OK, otherwise discard contribution:
if (abs(TopState.iK - DescendedState2.iK) < FT_of_potential.size()/2 &&
abs(DescendedState2.iK - GroundState.iK) < FT_of_potential.size()/2) {
contrib_estimate += abs(FT_of_potential[FT_of_potential.size()/2 + (TopState.iK - DescendedState2.iK)]
* FT_of_potential[FT_of_potential.size()/2 + (DescendedState2.iK - GroundState.iK)]
* densityME_top_desc2 * densityME_desc2_ground
/((GroundState.E - TopState.E) * (GroundState.E - DescendedState2.E)));
nr_cont++;
}
if (nphpairs >= 4) { // go one step further
for (int ipart3 = ipart2; ipart3 < nphpairs - 2; ++ipart3) {
for (int ihole3 = ihole2; ihole3 < nphpairs - 2; ++ihole3) {
LiebLin_Bethe_State DescendedState3 = DescendedState2;
//cout << "Here 4a" << "\tipart3 " << ipart3 << "\tihole3 " << ihole3 << endl;
DescendedState3.Annihilate_ph_pair(ipart3, ihole3, OriginIx2);
//cout << DescendedState3.Ix2 << endl;
DescendedState3.Compute_All(true);
//cout << DescendedState3.Ix2 << endl;
DP densityME_top_desc3 = real(exp(ln_Density_ME (TopState, DescendedState3)));
DP densityME_desc3_ground = real(exp(ln_Density_ME (DescendedState3, GroundState)));
//contrib_estimate += Weight_integral[Weight_integral.size()/2 + (TopState.iK - DescendedState3.iK)]
//* Weight_integral[Weight_integral.size()/2 + (DescendedState3.iK - GroundState.iK)]
//* densityME_top_desc3 * densityME_desc3_ground
///((GroundState.E - TopState.E) * (GroundState.E - DescendedState3.E));
// if intermediate state has momentum within allowable window, OK, otherwise discard contribution:
if (abs(TopState.iK - DescendedState3.iK) < FT_of_potential.size()/2 &&
abs(DescendedState3.iK - GroundState.iK) < FT_of_potential.size()/2) {
contrib_estimate += abs(FT_of_potential[FT_of_potential.size()/2 + (TopState.iK - DescendedState3.iK)]
* FT_of_potential[FT_of_potential.size()/2 + (DescendedState3.iK - GroundState.iK)]
* densityME_top_desc3 * densityME_desc3_ground
/((GroundState.E - TopState.E) * (GroundState.E - DescendedState3.E)));
nr_cont++;
}
} // for ihole3
} // for ipart3
} // if (nphpairs >= 4)
} // for ihole2
} // for ipart2
} // if (nphpairs >= 3)
} // for ihole
} // for ipart
} // if nphpairs >= 2
//cout << "Here b" << endl;
//cout << "type_id " << TopState.type_id << "\tid " << TopState.id << "\tcontrib_est = " << contrib_estimate << "\tnr_cont = " << nr_cont << endl;
return(contrib_estimate);
}
/*
DP Estimate_Contribution_of_Base_to_2nd_Order_PT (LiebLin_Bethe_State& ScanState, LiebLin_Bethe_State& GroundState,
Vect_DP Weight_integral, LiebLin_States_and_Density_ME_of_Base& StatesBase)
{
// This function calculates the second-order perturbation theory contribution to
// state ScanState coming from states of base defined in StatesBase.
DP sum_contrib = 0.0;
for (int i = 0; i <= StatesBase.maxid; ++i)
sum_contrib += Weight_integral[Weight_integral.size()/2 + (ScanState.iK - StatesBase.State[i].iK)]
* Weight_integral[Weight_integral.size()/2 + (StatesBase.State[i].iK - GroundState.iK)]
* real(exp(ln_Density_ME (ScanState, StatesBase.State[i]))) * StatesBase.densityME[i]
/((GroundState.E - StatesBase.State[i].E) * (GroundState.E - ScanState.E));
// The strange vector size is so that iK == 0 corresponds to index size/2, as per convention
return(sum_contrib);
}
*/
void Select_States_for_NRG (DP c_int, DP L, int N, int iKmin, int iKmax, int Nstates_required, bool symmetric_states, int iKmod,
//int weighing_option, DP (*weight_integrand_fn) (Vect_DP), Vect_DP& args_to_weight_integrand)
int weighing_option, Vect<complex <DP> >& FT_of_potential)
{
// This function reads an existing partition function file and determines whether
// each state is to be included in NRG by applying an energy, momentum and form factor criterion.
// The weighing function flag determines what kind of ordering is required:
// weighing_option == 0: ordering in energy
// weighing_option == 1: ordering according to perturbation theory in single p-h annihilation path
// weighing_option == 2: same as 1, but output of list is ordered in weight
stringstream filenameprefix;
Data_File_Name (filenameprefix, 'Z', c_int, L, N, iKmin, iKmax, 0.0, 0.0, "");
string prefix = filenameprefix.str();
stringstream RAW_stringstream; string RAW_string;
RAW_stringstream << prefix << ".raw";
stringstream NRG_stringstream; string NRG_string;
NRG_stringstream << "States_c_" << c_int << "_L_" << L << "_N_" << N
<< "_iKmin_" << iKmin << "_iKmax_" << iKmax << "_Nstates_" << Nstates_required
<< "_Sym_" << symmetric_states << "_iKmod_" << iKmod << "_wopt_" << weighing_option << ".nrg";
RAW_string = RAW_stringstream.str();
const char* RAW_Cstr = RAW_string.c_str();
NRG_string = NRG_stringstream.str();
const char* NRG_Cstr = NRG_string.c_str();
ifstream infile;
infile.open(RAW_Cstr);
if (infile.fail()) {
cout << RAW_Cstr << endl;
JSCerror("The input file was not opened successfully in Select_States_for_NRG. ");
}
ofstream NRG_outfile;
NRG_outfile.open(NRG_Cstr);
if (NRG_outfile.fail()) JSCerror("Could not open NRG_outfile... ");
//NRG_outfile.setf(ios::scientific);
NRG_outfile.precision(16);
// Read the whole data file:
/*
// estimate its size:
struct stat statbuf;
stat (RAW_Cstr, &statbuf);
int filesize = statbuf.st_size;
// Determine the number of entries approximately
int entry_size = 1* sizeof(double) + 2*sizeof(int) + 3* sizeof(long long int);
int estimate_nr_entries = filesize/entry_size;
*/
// Count the number of entries in raw file:
int estimate_nr_entries = 0;
string line;
while (!infile.eof()) {
getline(infile, line);
estimate_nr_entries++;
}
const int MAXDATA = estimate_nr_entries;
//cout << "estimate_nr_entries: " << estimate_nr_entries << endl;
DP* E = new DP[MAXDATA];
int* iK = new int[MAXDATA];
//int* conv = new int[MAXDATA];
string* label = new string[MAXDATA];
bool* sym = new bool[MAXDATA];
int Ndata = 0;
infile.close();
infile.open(RAW_Cstr);
while (((infile.peek()) != EOF) && (Ndata < MAXDATA)) {
infile >> E[Ndata];
infile >> iK[Ndata];
//infile >> conv[Ndata];
infile >> label[Ndata];
Ndata++;
}
//cout << "input " << Ndata << " data lines." << endl;
infile.close();
// Define the ground state:
LiebLin_Bethe_State GroundState (c_int, L, N);
GroundState.Compute_All(true);
// Define OriginIx2 for labelling:
Vect<int> OriginIx2 = GroundState.Ix2;
Scan_State_List<LiebLin_Bethe_State> ScanStateList ('d', GroundState);
// Build the momentum-dependent weight integral matrix:
// To cover negative and positive momenta (in case potential is not symmetric),
// we define the Weight_integral vector entry with index size/2 as corresponding to iK == 0.
/*
// DEPRECATED 25/1/2012: from now on (ABACUS++T_8 onwards), use FT of potential as argument to this function.
Vect_DP Weight_integral (0.0, 4* (iKmax - iKmin)); // we give a large window
//Vect_DP args(0.0, 2);
DP req_rel_prec = 1.0e-6;
DP req_abs_prec = 1.0e-6;
int max_nr_pts = 10000;
for (int iK = -Weight_integral.size()/2; iK < Weight_integral.size()/2; ++iK) {
args_to_weight_integrand[1] = DP(iK);
Integral_result answer = Integrate_optimal (weight_integrand_fn, args_to_weight_integrand,
0, 0.0, 0.5, req_rel_prec, req_abs_prec, max_nr_pts);
Weight_integral[Weight_integral.size()/2 + iK] = answer.integ_est;
}
*/
// Calculate weight of states using selection criterion function
//DP absdensityME = 0.0;
DP* weight = new DP[Ndata];
// For weighing using 2nd order PT, we only trace over 2 excitation states (1 p-h pair)
// Start by constructing these four classes once and for all:
for (int i = 0; i < Ndata; ++i) {
//cout << i << " out of " << Ndata << "\tlabel = " << label[i] << endl;
if (abs(iK[i]) % iKmod != 0) { // if iK not a multiple of iKmod: give stupidly high weight.
weight[i] = 1.0e+100;
sym[i] = false; // doesn't matter
}
else {
// Construct the state again, so that the density ME can be calculated
LiebLin_Bethe_State ScanState = ScanStateList.Return_State(Extract_Base_Label(label[i]));
ScanState.Set_to_Label (label[i]);
if (weighing_option == 1 || weighing_option == 2) ScanState.Compute_All(true);
sym[i] = ScanState.Check_Symmetry();
//cout << "Setting state " << i << "\t to label " << label[i] << "\tsym: " << sym[i] << endl;
// WEIGHING THE STATES: **********************************************
State_Label_Data currentdata = Read_State_Label (label[i], OriginIx2);
if (currentdata.nexc[0] == 0) weight[i] = 0.0;
else if (symmetric_states && iK[i] < 0 || iK[i] < iKmin || iK[i] > iKmax) weight[i] = 1.0e+100;
//else if (symmetric_states && iK[i] == 0 && !ScanState.Check_Symmetry()) {
else if (symmetric_states && iK[i] == 0 && !sym[i]) {
// This state is at zero momentum but not symmetric. we keep it only if
// the first non-symmetric pair of quantum numbers is right-weighted:
int icheck = 0;
while (ScanState.Ix2[N-1-icheck] == -ScanState.Ix2[icheck]) icheck++;
if (ScanState.Ix2[N-1-icheck] > -ScanState.Ix2[icheck]) {
if (weighing_option == 0) weight[i] = E[i];
else if (weighing_option == 1 || weighing_option == 2)
weight[i] = 1.0/(1.0e-100 + fabs(Estimate_Contribution_of_Single_ph_Annihilation_Path_to_2nd_Order_PT
//(ScanState, GroundState, Weight_integral)));
(ScanState, GroundState, FT_of_potential)));
}
else weight[i] = 1.0e+100;
}
else {
if (weighing_option == 0) weight[i] = E[i];
else if (weighing_option == 1 || weighing_option == 2)
weight[i] = 1.0/(1.0e-100 + fabs(Estimate_Contribution_of_Single_ph_Annihilation_Path_to_2nd_Order_PT
//(ScanState, GroundState, Weight_integral)));
(ScanState, GroundState, FT_of_potential)));
}
//cout << i << " out of " << Ndata << "\tlabel = " << label[i] << "\tweight = " << weight[i] << endl;
}
} // for i
// Now order the states in increasing weight
int* index = new int[Ndata];
for (int i = 0; i < Ndata; ++i) index[i] = i;
QuickSort(weight, index, 0, Ndata - 1);
// Select states by increasing weight, with a max of Nstates_required entries
DP* E_kept = new DP[Nstates_required];
int* iK_kept = new int[Nstates_required];
//int* conv_kept = new int[Nstates_required];
string* label_kept = new string[Nstates_required];
bool* sym_kept = new bool[Nstates_required];
DP* weight_kept = new DP[Nstates_required];
// Copy selected states into new vectors:
for (int i = 0; i < JSC::min(Ndata, Nstates_required); ++i) {
E_kept[i] = E[index[i] ];
iK_kept[i] = iK[index[i] ];
//conv_kept[i] = conv[index[i] ];
label_kept[i] = label[index[i] ];
sym_kept[i] = sym[index[i] ];
weight_kept[i] = weight[i];
}
// If needed, order selected states by increasing energy:
int* index_kept = new int[Nstates_required];
for (int i = 0; i < Nstates_required; ++i) index_kept[i] = i;
if (weighing_option == 1) // only need to do this if energy ordering is chosen
QuickSort (E_kept, index_kept, 0, Nstates_required - 1);
// Output selected states:
for (int i = 0; i < Nstates_required; ++i) {
if (i > 0) NRG_outfile << endl;
NRG_outfile << i << "\t" << E_kept[i] << "\t" << iK_kept[index_kept[i] ]
//<< "\t" << conv_kept[index_kept[i] ]
<< "\t" << label_kept[index_kept[i] ]
<< "\t" << sym_kept[index_kept[i] ] << "\t" << weight_kept[index_kept[i] ];
}
delete[] E;
delete[] iK;
//delete[] conv;
delete[] label;
delete[] sym;
delete[] E_kept;
delete[] iK_kept;
//delete[] conv_kept;
delete[] label_kept;
delete[] sym_kept;
delete[] weight;
NRG_outfile.close();
return;
}
} // namespace JSC