Commit 7d380e30 authored by Mohcine Chraibi's avatar Mohcine Chraibi

optimize includes. add cnpy lib

parent 4b74ccd5
Pipeline #5869 passed with stages
in 5 minutes and 16 seconds
......@@ -496,6 +496,9 @@ set(header_files
add_library(core STATIC ${source_files})
add_library(cnpy SHARED "cnpy/cnpy.cpp")
target_link_libraries(cnpy z)
#add_library ( core SHARED ${source_files} )
#Target
......@@ -515,7 +518,7 @@ if (Boost_FOUND)
# suppress warnings in boost libraries with attribute SYSTEM
include_directories(SYSTEM ${Boost_INCLUDE_DIR})
# todo: is this necessary?
target_link_libraries(core ${Boost_LIBRARIES})
target_link_libraries(core ${Boost_LIBRARIES} cnpy)
endif ()
target_link_libraries(jpscore core)
......
......@@ -104,17 +104,21 @@ void SmokeSensor::execute(const Pedestrian * pedestrian, CognitiveMap& cognitive
item->GetCrossing()->GetCentre(),
pedestrian->GetGlobalTime()).GetKnotValue(pedestrian->GetPos()._x,
pedestrian->GetPos()._y);
// std::cout << "\n =================================== \n";
// std::cout << "Ped: " << pedestrian->GetID() << ", at (" << pedestrian->GetPos()._x << ", " << pedestrian->GetPos()._y << ")" << std::endl;
// std::cout << "\tElevation: " << pedestrian->GetElevation() << std::endl;
// std::cout << "\titem->GetCrossing()->GetCentre(): " << item->GetCrossing()->GetCentre()._x << ", " << item->GetCrossing()->GetCentre()._y << std::endl;
// std::cout << "\t Time" << pedestrian->GetGlobalTime() << std::endl;
// std::cout << "\tKnotValue: " << _FMStorage->GetFDSMesh(pedestrian->GetElevation(), item->GetCrossing()->GetCentre(), pedestrian->GetGlobalTime()).GetKnotValue(pedestrian->GetPos()._x, pedestrian->GetPos()._y) << std::endl;
if(SmokeFactor > 2){
std::cout << "\n =================================== \n";
std::cout << "Ped: " << pedestrian->GetID() << ", at (" << pedestrian->GetPos()._x << ", " << pedestrian->GetPos()._y << ")" << std::endl;
std::cout << "\tElevation: " << pedestrian->GetElevation() << std::endl;
std::cout << "\titem->GetCrossing()->GetCentre(): " << item->GetCrossing()->GetCentre()._x << ", " << item->GetCrossing()->GetCentre()._y << std::endl;
std::cout << "\t Time: " << pedestrian->GetGlobalTime() << std::endl;
std::cout << "\tKnotValue: " << _FMStorage->GetFDSMesh(pedestrian->GetElevation(), item->GetCrossing()->GetCentre(), pedestrian->GetGlobalTime()).GetKnotValue(pedestrian->GetPos()._x, pedestrian->GetPos()._y) << std::endl;
std::cout << "SmokeFactor: " << SmokeFactor << std::endl;
std::cout << "Risktolerance: " << RiskTolerance << std::endl;
}
weight = 1 + (1-RiskTolerance) * SmokeFactor ;
}
/// Set Edge Weight
// std::cout << weight << std::endl;
//std::cout << "weight: "<< weight << std::endl;
item->SetFactor(weight,GetName());
}
......
......@@ -189,7 +189,10 @@ void FDSMesh::ReadMatrix(std::string line, std::ifstream &pFile)
void FDSMesh::SetKnotValuesFromFile(const std::string &filename)
{
///open File (reading)
std::ifstream pFile(filename);
if (pFile)
{
std::vector<std::string> strVec;
......
......@@ -195,13 +195,19 @@ void FDSMeshStorage::CreateFDSMeshes()
if (_doorlist.size() > 0) { // Smoke sensor active
for (auto &h:_quantitylist) //list of quantities
{
std::cout << "H: " << h << std::endl;
for (auto &j:_doorlist) //list of doors
{
std::cout << "door: " << j << std::endl;
//std::cout << "door " << j << std::endl;
for (auto &k:_timelist) //list of times
{
std::cout << "time: " << k << std::endl;
std::string str = h + "/" + j + "/t_"+std::to_string(k);
//std::cout << _filepath + str + ".csv" << std::endl;
std::cout << _filepath + str + ".csv" << std::endl;
FDSMesh mesh(_filepath + str + ".csv");
//std::string str = "t_"+std::to_string(i);
_fMContainer.insert(std::make_pair(str, mesh));
......@@ -292,9 +298,9 @@ const FDSMesh &FDSMeshStorage::GetFDSMesh(const double &pedElev, const Point &do
throw -1;
}
if (_fMContainer.count(str) == 1) {
std::cout << "INFO: requested sfgrid: " << str << std::endl;
}
// if (_fMContainer.count(str) == 1) {
// std::cout << "INFO: requested sfgrid: " << str << std::endl;
// }
return _fMContainer.at(str);
}
......
The MIT License
Copyright (c) Carl Rogers, 2011
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Purpose:
Numpy offers the save method for easy saving of arrays into .npy and savez for zipping multiple .npy arrays together into a .npz file. cnpy lets you read and write to these formats in C++. The motivation comes from scientific programming where large amounts of data are generated in C++ and analyzed in Python. Writing to .npy has the advantage of using low-level C++ I/O (fread and fwrite) for speed and binary format for size. The .npy file header takes care of specifying the size, shape, and data type of the array, so specifying the format of the data is unnecessary. Loading data written in numpy formats into C++ is equally simple, but requires you to type-cast the loaded data to the type of your choice.
Installation:
Default installation directory is /usr/local. To specify a different directory, add -DCMAKE_INSTALL_PREFIX=/path/to/install/dir to the cmake invocation in step 4.
1. get cmake at www.cmake.org
2. create a build directory, say $HOME/build
3. cd $HOME/build
4. cmake /path/to/cnpy
5. make
6. make install
Using:
To use, #include"cnpy.h" in your source code. Compile the source code mycode.cpp as
g++ -o mycode mycode.cpp -L/path/to/install/dir -lcnpy -lz --std=c++11
Description:
There are two functions for writing data: npy_save, npz_save.
There are 3 functions for reading. npy_load will load a .npy file. npz_load(fname) will load a .npz and return a dictionary of NpyArray structues. npz_load(fname,varname) will load and return the NpyArray for data varname from the specified .npz file.
The data structure for loaded data is below. Data is accessed via the data<T>() method, which returns a pointer of the specified type (which must match the underlying datatype of the data). The array shape and word size are read from the npy header.
struct NpyArray {
std::vector<size_t> shape;
size_t word_size;
template<typename T> T* data();
};
See example1.cpp for examples of how to use the library. example1 will also be build during cmake installation.
This diff is collapsed.
This diff is collapsed.
#include"cnpy.h"
#include<complex>
#include<cstdlib>
#include<iostream>
#include<map>
#include<string>
const int Nx = 128;
const int Ny = 64;
const int Nz = 32;
int main()
{
//set random seed so that result is reproducible (for testing)
srand(0);
//create random data
std::vector<std::complex<double>> data(Nx*Ny*Nz);
for(int i = 0;i < Nx*Ny*Nz;i++) data[i] = std::complex<double>(rand(),rand());
//save it to file
cnpy::npy_save("arr1.npy",&data[0],{Nz,Ny,Nx},"w");
//load it into a new array
cnpy::NpyArray arr = cnpy::npy_load("arr1.npy");
std::complex<double>* loaded_data = arr.data<std::complex<double>>();
//make sure the loaded data matches the saved data
assert(arr.word_size == sizeof(std::complex<double>));
assert(arr.shape.size() == 3 && arr.shape[0] == Nz && arr.shape[1] == Ny && arr.shape[2] == Nx);
for(int i = 0; i < Nx*Ny*Nz;i++) assert(data[i] == loaded_data[i]);
//append the same data to file
//npy array on file now has shape (Nz+Nz,Ny,Nx)
cnpy::npy_save("arr1.npy",&data[0],{Nz,Ny,Nx},"a");
//now write to an npz file
//non-array variables are treated as 1D arrays with 1 element
double myVar1 = 1.2;
char myVar2 = 'a';
cnpy::npz_save("out.npz","myVar1",&myVar1,{1},"w"); //"w" overwrites any existing file
cnpy::npz_save("out.npz","myVar2",&myVar2,{1},"a"); //"a" appends to the file we created above
cnpy::npz_save("out.npz","arr1",&data[0],{Nz,Ny,Nx},"a"); //"a" appends to the file we created above
//load a single var from the npz file
cnpy::NpyArray arr2 = cnpy::npz_load("out.npz","arr1");
//load the entire npz file
cnpy::npz_t my_npz = cnpy::npz_load("out.npz");
//check that the loaded myVar1 matches myVar1
cnpy::NpyArray arr_mv1 = my_npz["myVar1"];
double* mv1 = arr_mv1.data<double>();
assert(arr_mv1.shape.size() == 1 && arr_mv1.shape[0] == 1);
assert(mv1[0] == myVar1);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment