Skip to content

Commit

Permalink
tables dir location search algorithm updated
Browse files Browse the repository at this point in the history
  • Loading branch information
okolo committed Sep 16, 2024
1 parent 800ae05 commit 1dd07c0
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 48 deletions.
Binary file removed bin/tables/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion src/app/crbeam/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ CRbeam.h
)
add_link_options(-fopenmp)
add_executable(CRbeam ${SOURCE_FILES})
target_link_libraries(CRbeam mcray ${BOOST_SYS_LIBRARY} ${BOOST_FS_LIBRARY} ${C_LIBRARY} ${GSL_LIBRARY} ${GSLCBLAS_LIBRARY})
target_link_libraries(CRbeam mcray ${BOOST_SYS_LIBRARY} ${BOOST_FS_LIBRARY} ${C_LIBRARY} ${GSL_LIBRARY} ${GSLCBLAS_LIBRARY} ${CMAKE_DL_LIBS})


2 changes: 1 addition & 1 deletion src/app/crbeam/CRbeam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ using namespace cors::cmdline;
*/
int user_main(int argc, char** argv) {
std::cout << "Number of available threads: " << omp_thread_count() << std::endl;
std::cout << "tables path: " << shared_tables_path() << std::endl;
std::cout << "Using tables path: " << tables_dir << std::endl;

CRbeam prog(argc, argv);
return prog.run();
Expand Down
4 changes: 2 additions & 2 deletions src/lib/Background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void TableBackground::Init(std::string aDir, const char** aFileList, bool aIsLog
{
//the files should be ordered increasingly?
//x scale should by monotonic with increasing order
aDir = TABLES_DIR + aDir;
aDir = tables_dir + aDir;
double lastZ = -1;
for(int iZ=0; aFileList[iZ]; iZ++)
{
Expand Down Expand Up @@ -162,7 +162,7 @@ MatrixBackground::MatrixBackground(std::string aName, std::string aTableFile, bo
const size_t bufSize = 1048576;//1M
fBuffer = new char[bufSize];
std::istream_iterator<double> eos;
aTableFile = TABLES_DIR + aTableFile;
aTableFile = tables_dir + aTableFile;
std::ifstream file(aTableFile.c_str());

if(!file.good())
Expand Down
1 change: 1 addition & 0 deletions src/lib/Deflection3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ namespace Interactions {
double Bgauss = 10.;
MonochromaticMF mf(r,lambdaMpc,Bgauss);
mf.Print(lambdaMpc);
return 0;
}

int Deflection3D::UnitTest() {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/GZK.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ GZK::GZK(BackgroundIntegral* aBackground, int aRandSeed):fBackground(aBackground

Function* GZK::InitSigma(ParticleType aPrim)
{
std::string tablesDir = TABLES_DIR "sophia2/";
std::string tablesDir = tables_dir + "sophia2/";
Utils::TableReader reader(tablesDir+(aPrim==Proton?"p":"n"), 2);
std::vector<double>& s = reader.getColumn(0);
std::vector<double>& sigma = reader.getColumn(1);
Expand Down
1 change: 1 addition & 0 deletions src/lib/MathUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,7 @@ template bool MathUtils::SampleLogscaleDistribution<double>(const Function& aDis
//Sampler4<double> dil;
NRSampler<double> dil;
dil.UnitTest();
return 0;
}

double MathUtils::Integration_qag (
Expand Down
5 changes: 3 additions & 2 deletions src/lib/Stecker16Background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
namespace Backgrounds {

Stecker16LowerBackground::Stecker16LowerBackground() :
Stecker16Background(TABLES_DIR "stecker_ebl16/comoving_enerdens_lo.csv", "Stecker_16_lower") {
Stecker16Background(tables_dir + "stecker_ebl16/comoving_enerdens_lo.csv", "Stecker_16_lower") {
}

Stecker16UpperBackground::Stecker16UpperBackground() :
Stecker16Background(TABLES_DIR "stecker_ebl16/comoving_enerdens_up.csv", "Stecker_16_upper")
Stecker16Background(tables_dir + "stecker_ebl16/comoving_enerdens_up.csv", "Stecker_16_upper")
{
}

Expand All @@ -55,6 +55,7 @@ int Stecker16Background::UnitTest()
BackgroundUtils::UnitTest(backgrUpper);
Stecker16LowerBackground backgrLower;
BackgroundUtils::UnitTest(backgrLower);
return 0;
}

}
2 changes: 1 addition & 1 deletion src/lib/SteckerEBL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Backgrounds {

#define IRO_DATA_FILE "iro_stecker2005"
Stecker2005EBL::Stecker2005EBL():
CTableWithHeaderReader(TABLES_DIR IRO_DATA_FILE),
CTableWithHeaderReader((tables_dir + IRO_DATA_FILE).c_str()),
m_accuracyE(0),
m_accuracyZ(0),
m_curE(0)
Expand Down
58 changes: 20 additions & 38 deletions src/lib/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,52 +36,34 @@ namespace fs = boost::filesystem;

namespace Utils {

const char PathSeparator =
#ifdef _WIN32
'\\';
#else
'/';
#endif
const char PathSeparator = DIR_DELIMITER_STR[0];


std::string shared_tables_path(){
std::string default_tables_path(){
// On conda env installations tables are stored in shared folder
// For standalone installations tables are stored in bin folder
std::string tables_folder_name("tables");
fs::path prog_path = boost::dll::program_location();
fs::path prog_name = prog_path.stem().string();
fs::path prog_dir = prog_path.parent_path();
fs::path shared_tables_dir = prog_dir.parent_path() / "shared" / prog_name / tables_folder_name;
fs::path exe_tables_dir = prog_dir / tables_folder_name;

// for some reason boost::filesystem::path.parent_path() and fs::canonical didn't work properly os my Mac OS so we do it manually
if(fs::is_directory(shared_tables_dir))
return shared_tables_dir.string();
if(fs::is_directory(exe_tables_dir))
return exe_tables_dir.string();

std::stringstream path(prog_path.string());
std::string segment;
std::vector<std::string> seglist;
// we don't check for existence of './tables' and
// don't throw exception here since on some platforms fs::is_directory doesn't work properly
std::cerr << "WARNING: Neither " << shared_tables_dir.string() << " nor \n" << exe_tables_dir.string() <<
" was found.\n You can download latest tables from\n" <<
"https://github.com/okolo/mcray/tree/main/bin/tables\n";

while(std::getline(path, segment, PathSeparator))
{
seglist.push_back(segment);
}

if(seglist.size() >= 3){
std::string app_name = seglist.back();
seglist.pop_back();
seglist.pop_back();
seglist.push_back(std::string("shared"));
seglist.push_back(app_name);
seglist.push_back(tables_folder_name);
std::ostringstream os;
for(std::string& seg : seglist){
os << seg << PathSeparator;
}
std::string table_dir = os.str();
table_dir.pop_back();
std::cout << "extracted path:" << table_dir << "\n\n";
fs::path p = fs::path(table_dir);
if(fs::exists(fs::status(p)))
return table_dir;
else
std::cout << "type " << fs::status(p).type() << "\n\n";
}
return tables_folder_name + PathSeparator;
return tables_folder_name;
}

std::string tables_dir = default_tables_path() + DIR_DELIMITER_STR;

int omp_thread_count() {
int n = 0;
#pragma omp parallel reduction(+:n)
Expand Down
12 changes: 10 additions & 2 deletions src/lib/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@

namespace Utils {

std::string shared_tables_path();
std::string default_tables_path();



#ifdef _WIN32
#define DIR_DELIMITER_STR "\\"
#else
#define DIR_DELIMITER_STR "/"
#define TABLES_DIR "tables" DIR_DELIMITER_STR
#endif

extern std::string tables_dir;
//#define TABLES_DIR "tables" DIR_DELIMITER_STR

class Exception
{
Expand Down

0 comments on commit 1dd07c0

Please sign in to comment.