Skip to content

Commit

Permalink
Parametric_tw_csv example (#10)
Browse files Browse the repository at this point in the history
* more bindings for Database classes.

* initial loading for local_tw_csv.py

* minor fix.

* parametric_tw_csv example.

* update the commands.

* fixed the command.
  • Loading branch information
dreamer2368 authored Sep 5, 2023
1 parent 72c8cdf commit 3199284
Show file tree
Hide file tree
Showing 10 changed files with 1,731 additions and 5 deletions.
38 changes: 38 additions & 0 deletions bindings/pylibROM/python_utils/cpp_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,42 @@ T* getVectorPointer(py::array_t<T> &u_in)

return static_cast<T*>(buf_info.ptr);
}

template<typename T>
py::buffer_info
get1DArrayBufferInfo(T *ptr, const int nelem)
{
return py::buffer_info(
ptr, /* Pointer to buffer */
sizeof(T), /* Size of one scalar */
py::format_descriptor<T>::format(), /* Python struct-style format descriptor */
1, /* Number of dimensions */
{ nelem }, /* Buffer dimensions */
{ sizeof(T) } /* Strides (in bytes) for each index */
);
}

template<typename T>
py::capsule
get1DArrayBufferHandle(T *ptr, const bool free_when_done=false)
{
if (free_when_done)
return py::capsule(ptr, [](void *f){
T *T_ptr = reinterpret_cast<T *>(f);
delete[] T_ptr;
});
else
return py::capsule([](){});
}

template<typename T>
py::array_t<T>
get1DArrayFromPtr(T *ptr, const int nelem, bool free_when_done=false)
{
// if empty array, no need to free when done.
free_when_done = free_when_done && (nelem > 0);
return py::array(get1DArrayBufferInfo(ptr, nelem),
get1DArrayBufferHandle(ptr, free_when_done));
}

#endif
63 changes: 63 additions & 0 deletions bindings/pylibROM/utils/pyCSVDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,68 @@ void init_CSVDatabase(pybind11::module_ &m) {
self.putIntegerArray(key, getVectorPointer(data), nelements);
});

csvdb.def("getIntegerArray", [](
CSVDatabase &self, const std::string& key, int nelements)
{
int *dataptr = new int[nelements];
self.getIntegerArray(key, dataptr, nelements);
return get1DArrayFromPtr(dataptr, nelements, true);
});

csvdb.def("getIntegerVector", [](
CSVDatabase &self, const std::string& key, bool append)
{
std::vector<int> *datavec = new std::vector<int>;
self.getIntegerVector(key, *datavec, append);
return get1DArrayFromPtr(datavec->data(), datavec->size(), true);
},
py::arg("key"), py::arg("append") = false);

csvdb.def("getDoubleArray", [](
CSVDatabase &self, const std::string& key, int nelements)
{
double *dataptr = new double[nelements];
self.getDoubleArray(key, dataptr, nelements);
return get1DArrayFromPtr(dataptr, nelements, true);
});

csvdb.def("getDoubleArray", [](
CSVDatabase &self, const std::string& key, int nelements, const std::vector<int>& idx)
{
double *dataptr = new double[nelements];
self.getDoubleArray(key, dataptr, nelements, idx);
return get1DArrayFromPtr(dataptr, nelements, true);
});

csvdb.def("getDoubleArray", [](
CSVDatabase &self, const std::string& key, int nelements,
int offset, int block_size, int stride)
{
double *dataptr = new double[nelements];
self.getDoubleArray(key, dataptr, nelements, offset, block_size, stride);
return get1DArrayFromPtr(dataptr, nelements, true);
});

csvdb.def("getDoubleVector", [](
CSVDatabase &self, const std::string& key, bool append)
{
std::vector<double> *datavec = new std::vector<double>();
self.getDoubleVector(key, *datavec, append);
return get1DArrayFromPtr(datavec->data(), datavec->size(), true);
},
py::arg("key"), py::arg("append") = false);

csvdb.def("getDoubleArraySize", &CSVDatabase::getDoubleArraySize);

csvdb.def("getStringVector", [](
CSVDatabase &self, const std::string& file_name, bool append)
{
std::vector<std::string> data;
self.getStringVector(file_name, data, append);
return py::cast(data);
}, py::arg("file_name"), py::arg("append") = false);

csvdb.def("getLineCount", &CSVDatabase::getLineCount);

}

8 changes: 7 additions & 1 deletion bindings/pylibROM/utils/pyDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ void init_Database(pybind11::module_ &m) {
.export_values();

// TODO(kevin): finish binding of member functions.

db.def("getInteger", [](
Database &self, const std::string& key)
{
int data;
self.getInteger(key, data);
return data;
});
}
35 changes: 35 additions & 0 deletions bindings/pylibROM/utils/pyHDFDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,41 @@ void init_HDFDatabase(pybind11::module_ &m) {
self.putIntegerArray(key, getVectorPointer(data), nelements);
});

hdfdb.def("getIntegerArray", [](
HDFDatabase &self, const std::string& key, int nelements)
{
int *dataptr = new int[nelements];
self.getIntegerArray(key, dataptr, nelements);
return get1DArrayFromPtr(dataptr, nelements, true);
});

hdfdb.def("getDoubleArray", [](
HDFDatabase &self, const std::string& key, int nelements)
{
double *dataptr = new double[nelements];
self.getDoubleArray(key, dataptr, nelements);
return get1DArrayFromPtr(dataptr, nelements, true);
});

hdfdb.def("getDoubleArray", [](
HDFDatabase &self, const std::string& key, int nelements, const std::vector<int>& idx)
{
double *dataptr = new double[nelements];
self.getDoubleArray(key, dataptr, nelements, idx);
return get1DArrayFromPtr(dataptr, nelements, true);
});

hdfdb.def("getDoubleArray", [](
HDFDatabase &self, const std::string& key, int nelements,
int offset, int block_size, int stride)
{
double *dataptr = new double[nelements];
self.getDoubleArray(key, dataptr, nelements, offset, block_size, stride);
return get1DArrayFromPtr(dataptr, nelements, true);
});

hdfdb.def("getDoubleArraySize", &HDFDatabase::getDoubleArraySize);

// hdfdb.def("__del__", [](HDFDatabase& self) { self.~HDFDatabase(); }); // Destructor

}
Expand Down
7 changes: 7 additions & 0 deletions examples/data/inline-quad.mesh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MFEM INLINE mesh v1.0

type = quad
nx = 4
ny = 4
sx = 1.0
sy = 1.0
33 changes: 33 additions & 0 deletions examples/dmd/heat_conduction_csv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

#SBATCH -N 1
#SBATCH -t 0:05:00
#SBATCH -p pdebug
#SBATCH -o sbatch.log
#SBATCH --open-mode truncate

rm -rf parameters.txt dmd_list dmd_data run/dmd_data

python3 ./parametric_heat_conduction.py -r 0.40 -save -csv -out dmd_data/dmd_par1 -no-vis > hc_par1.log
python3 ./parametric_heat_conduction.py -r 0.45 -save -csv -out dmd_data/dmd_par2 -no-vis > hc_par2.log
python3 ./parametric_heat_conduction.py -r 0.55 -save -csv -out dmd_data/dmd_par3 -no-vis > hc_par3.log
python3 ./parametric_heat_conduction.py -r 0.60 -save -csv -out dmd_data/dmd_par4 -no-vis > hc_par4.log
python3 ./parametric_heat_conduction.py -r 0.50 -save -csv -out dmd_data/dmd_par5 -no-vis > hc_par5.log

mkdir dmd_list
mv -f run/dmd_data .

awk 'END{print NR}' dmd_data/dmd_par1/step0/sol.csv > dmd_data/dim.csv

mv dmd_data/dmd_par1/snap_list.csv dmd_list/dmd_par1.csv
mv dmd_data/dmd_par2/snap_list.csv dmd_list/dmd_par2.csv
mv dmd_data/dmd_par3/snap_list.csv dmd_list/dmd_par3.csv
mv dmd_data/dmd_par4/snap_list.csv dmd_list/dmd_par4.csv
mv dmd_data/dmd_par5/snap_list.csv dmd_list/dmd_par5.csv

echo "dmd_par1,0.40,0.01,0,0" > dmd_list/dmd_train_parametric.csv
echo "dmd_par2,0.45,0.01,0,0" >> dmd_list/dmd_train_parametric.csv
echo "dmd_par3,0.55,0.01,0,0" >> dmd_list/dmd_train_parametric.csv
echo "dmd_par4,0.60,0.01,0,0" >> dmd_list/dmd_train_parametric.csv
echo "dmd_par5,0.50,0.01,0,0" > dmd_list/dmd_train_local.csv
echo "dmd_par5,0.50,0.01,0,0" > dmd_list/dmd_test.csv
25 changes: 25 additions & 0 deletions examples/dmd/heat_conduction_hdf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

#SBATCH -N 1
#SBATCH -t 0:05:00
#SBATCH -p pdebug
#SBATCH -o sbatch.log
#SBATCH --open-mode truncate

rm -rf parameters.txt dmd_list dmd_data run/dmd_data

python3 ./parametric_heat_conduction.py -r 0.40 -save -hdf -out dmd_data/sim0 -no-vis > hc_par0.log
python3 ./parametric_heat_conduction.py -r 0.45 -save -hdf -out dmd_data/sim1 -no-vis > hc_par1.log
python3 ./parametric_heat_conduction.py -r 0.55 -save -hdf -out dmd_data/sim2 -no-vis > hc_par2.log
python3 ./parametric_heat_conduction.py -r 0.60 -save -hdf -out dmd_data/sim3 -no-vis > hc_par3.log
python3 ./parametric_heat_conduction.py -r 0.50 -save -hdf -out dmd_data/sim4 -no-vis > hc_par4.log

mkdir dmd_list
mv -f run/dmd_data .

echo "0,0.40,0.01,0,0" > dmd_list/dmd_train_parametric.csv
echo "1,0.45,0.01,0,0" >> dmd_list/dmd_train_parametric.csv
echo "2,0.55,0.01,0,0" >> dmd_list/dmd_train_parametric.csv
echo "3,0.60,0.01,0,0" >> dmd_list/dmd_train_parametric.csv
echo "4,0.50,0.01,0,0" > dmd_list/dmd_train_local.csv
echo "4,0.50,0.01,0,0" > dmd_list/dmd_test.csv
Loading

0 comments on commit 3199284

Please sign in to comment.