Skip to content
Snippets Groups Projects
Commit 8a3f5aa7 authored by 数学の武士's avatar 数学の武士
Browse files

Need to implement sum_vec on GPU

parent 618cfa27
No related branches found
No related tags found
No related merge requests found
cmake_minimum_required (VERSION 3.17.0) cmake_minimum_required(VERSION 3.22)
project(mesh_test)
project(IceThermo VERSION 1.0.0)
option(INCLUDE_CUDA "GPU build in mode" OFF)
# C/C++ compilers
enable_language (CXX C) if(INCLUDE_CUDA)
enable_language(CUDA)
# set C++ standart find_package(CUDA REQUIRED)
if(NOT "${CMAKE_CXX_STANDARD}") include_directories(${CUDA_INCLUDE_DIRS})
set(CMAKE_CXX_STANDARD 17) set(MESH_MACRO -DINCLUDE_CUDA)
endif() endif(INCLUDE_CUDA)
# options add_subdirectory(mesh/)
option(USE_JSON_OUTPUT "Link library with nlohmann::json" OFF) # set_property(DIRECTORY mesh/ APPEND PROPERTY COMPILE_DEFINITIONS "-DINCLUDE_CUDA=${INCLUDE_CUDA}")
option(COMPILE_TESTS "Compile tests" OFF) # target_compile_definitions(mesh PRIVATE INCLUDE_CUDA=${INCLUDE_CUDA})
option(COMPILE_EXAMPLES "Compile examples" OFF)
option(COMPILE_ITSLAV "Compile IceThermo intefaces for SLAV model" OFF) set(SOURCE main.cpp)
option(COMPILE_ITINMCM "Compile IceThermo intefaces for INMCM model" OFF) add_executable(mesh_test ${SOURCE})
add_definitions(${MESH_MACRO})
# add Fortran support if SLAV interfaces are compiled target_include_directories(mesh_test PUBLIC mesh/include)
if (COMPILE_ITSLAV) target_link_libraries(mesh_test memproc mesh)
MESSAGE(STATUS "SLAV interfaces compilation ENABLED")
enable_language (Fortran)
endif()
# add Fortran support if INMCM interfaces are compiled
if (COMPILE_ITINMCM)
MESSAGE(STATUS "INMCM interfaces compilation ENABLED")
enable_language (Fortran)
endif()
# add subdirectory with source code
add_subdirectory(source)
if (COMPILE_ITSLAV)
set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
endif()
# make library
add_library(icethermo STATIC ${SOURCE} ${HEADER})
# include directories
target_include_directories(icethermo PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(icethermo PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/source/headers")
target_include_directories(icethermo PUBLIC "${CMAKE_CURRENT_BINARY_DIR}")
if (COMPILE_ITINMCM)
target_include_directories(icethermo PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/source/applications/itinmcm")
endif()
# link library with nlohmann::json
if (USE_JSON_OUTPUT)
target_compile_definitions(icethermo PUBLIC USE_JSON_OUTPUT)
find_package(nlohmann_json 3.2.0 REQUIRED)
target_link_libraries(icethermo PUBLIC nlohmann_json::nlohmann_json)
endif()
# compile tests
if(COMPILE_TESTS)
MESSAGE(STATUS "Test compiling ENABLED")
add_subdirectory(tests)
endif(COMPILE_TESTS)
# compile examples
if (COMPILE_EXAMPLES)
MESSAGE(STATUS "Examples compiling ENABLED")
add_subdirectory(examples)
endif()
main.cpp 0 → 100644
#include "mesh.hpp"
#include "TemplateParameters.h"
using namespace icethermo;
int main(void)
{
Mesh<float, MemType::GPU> mesh1(10, 1.0f);
auto cells_temp = mesh1.CreateCellsData("cells_temperature");
auto cells_capacity = mesh1.CreateCellsData("cells_capacity", true);
auto cells_enthalpy = mesh1.CreateCellsData("cells_enthalpy", false);
auto cells_thickness = mesh1.GetCellsThickness();
const int n = mesh1.GetCellsNum();
mesh1.PullCPUArray(*cells_thickness.get(), n);
for (int i = 0; i < n; i++)
{
printf("%f\n", mesh1.SubBuffer[i]);
}
mesh1.SaveTXT("./mesh1");
return 0;
}
\ No newline at end of file
cmake_minimum_required (VERSION 3.0.0)
project(MeshIceThermo)
# C/C++ compilers
enable_language (CXX)
option(INCLUDE_CUDA "GPU build in mode" OFF)
option(INCLUDE_JSON "JSON build in mode" OFF)
if(INCLUDE_CUDA)
enable_language(CUDA)
find_package(CUDA REQUIRED)
include_directories(${CUDA_INCLUDE_DIRS})
set(MESH_MACRO -DINCLUDE_CUDA)
endif(INCLUDE_CUDA)
if(INCLUDE_JSON)
set(JSON_GIT git@github.com:nlohmann/json.git)
include(FetchContent)
FetchContent_Declare(json
GIT_REPOSITORY ${JSON_GIT}
GIT_TAG origin/develop
)
FetchContent_MakeAvailable(json)
endif(INCLUDE_JSON)
set(MEMPROC_GIT git@tesla.parallel.ru:Lizzzka007/memory_processing.git)
include(FetchContent)
FetchContent_Declare(memory_processing
GIT_REPOSITORY ${MEMPROC_GIT}
GIT_TAG origin/main
)
FetchContent_MakeAvailable(memory_processing)
add_library(memory_processing INTERFACE)
target_compile_definitions(memory_processing INTERFACE INCLUDE_CUDA=${INCLUDE_CUDA})
set(SOURCES_CXX
src/mesh.cpp
src/matvec.cpp
)
set(HEADERS_CXX
include/mesh.hpp
include/matvec.hpp
include/defines.hpp
)
add_library(mesh ${HEADERS_CXX} ${SOURCES_CXX})
add_definitions(${MESH_MACRO})
target_include_directories(mesh PUBLIC ${memory_processing_SOURCE_DIR}/include)
target_include_directories(mesh PUBLIC ${json_SOURCE_DIR}/include)
if(INCLUDE_JSON)
target_link_libraries(mesh memproc json)
else(INCLUDE_JSON)
target_link_libraries(mesh memproc)
endif(INCLUDE_JSON)
\ No newline at end of file
File moved
#pragma once
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
#include "defines.hpp"
namespace icethermo
{
template <typename NumType> std::vector<NumType> operator+(const std::vector<NumType>& vec1,
const std::vector<NumType>& vec2);
template <typename NumType> std::vector<NumType> operator-(const std::vector<NumType>& vec1,
const std::vector<NumType>& vec2);
template <typename NumType> std::vector<NumType> operator*(const std::vector<NumType>& vec1,
const std::vector<NumType>& vec2);
template <typename NumType> NumType L2_norm(const std::vector<NumType>& vec);
template <typename NumType> std::vector<NumType> operator*(const std::vector<NumType>& vec, NumType scal);
template <typename NumType> std::vector<NumType> operator*(NumType scal, const std::vector<NumType>& vec);
template <typename NumType> std::vector<NumType> concatenate(const std::vector<NumType>& vec1, const std::vector<NumType>& vec2);
template <typename NumType> std::vector<NumType> concatenate(const std::vector<std::vector<NumType>>& vecs);
template <typename NumType> std::vector<NumType> cumsum(const std::vector<NumType>& vec);
template <typename NumType> std::vector<NumType> exp_vec(const std::vector<NumType>& vec);
template <typename NumType> std::vector<NumType> reverse_vec(const std::vector<NumType>& vec);
template <typename NumType> std::ostream& operator<<(std::ostream& os, const std::vector<NumType>& vec);
template <typename NumType> NumType sum_vec(const std::vector<NumType>& vec);
template <typename NumType> NumType sum_vec(const std::vector<NumType>& vec, int start, int end);
}
\ No newline at end of file
...@@ -15,13 +15,12 @@ ...@@ -15,13 +15,12 @@
using json = nlohmann::json; using json = nlohmann::json;
#endif #endif
#include "TemplateParameters.h"
#include "defines.hpp" #include "defines.hpp"
#include "matvec.hpp"
namespace icethermo namespace icethermo
{ {
template <typename NumType> template <typename NumType, MemType memtype>
class Mesh class Mesh
{ {
public: public:
...@@ -29,34 +28,34 @@ namespace icethermo ...@@ -29,34 +28,34 @@ namespace icethermo
Mesh(); // default constructor for empty mesh Mesh(); // default constructor for empty mesh
Mesh(NumType thickness); // constructor with 10 uniform layers with given thicknes Mesh(NumType thickness); // constructor with 10 uniform layers with given thicknes
Mesh(int n_uniform_layers, NumType thickness); // constructor with given number of uniform layers and given thickness Mesh(int n_uniform_layers, NumType thickness); // constructor with given number of uniform layers and given thickness
Mesh(const std::vector<NumType>& unit_segment_decomposition, NumType thickness); // constructor with manual partition for sigma layer grid and thickness // Mesh(const NumType* unit_segment_decomposition, NumType thickness); // constructor with manual partition for sigma layer grid and thickness
Mesh(const Mesh<NumType>& other); // copy constructor Mesh(const Mesh<NumType, memtype>& other); // copy constructor
~Mesh(); // destructor ~Mesh(); // destructor
// cells and nodes number getters // // cells and nodes number getters
int GetCellsNum() const; int GetCellsNum() const;
int GetNodesNum() const; int GetNodesNum() const;
// Creators of single, cells and nodes data // // Creators of single, cells and nodes data
std::shared_ptr<NumType> CreateSingleData(const std::string& varname, bool visible = true); std::shared_ptr<NumType> CreateSingleData(const std::string& varname, bool visible = true);
std::shared_ptr<std::vector<NumType>> CreateCellsData(const std::string& varname, bool visible = true); std::shared_ptr<NumType*> CreateCellsData(const std::string& varname, bool visible = true);
std::shared_ptr<std::vector<NumType>> CreateNodesData(const std::string& varname, bool visible = true); std::shared_ptr<NumType*> CreateNodesData(const std::string& varname, bool visible = true);
// Deleters of single, cells and nodes data // // Deleters of single, cells and nodes data
void DeleteSingleData(const std::string& varname); void DeleteSingleData(const std::string& varname);
void DeleteCellsData(const std::string& varname); void DeleteCellsData(const std::string& varname);
void DeleteNodesData(const std::string& varname); void DeleteNodesData(const std::string& varname);
// Getters of single, cells and nodes data // // Getters of single, cells and nodes data
std::shared_ptr<NumType> GetSingleData(const std::string& varname); std::shared_ptr<NumType> GetSingleData(const std::string& varname);
std::shared_ptr<std::vector<NumType>> GetCellsData(const std::string& varname); std::shared_ptr<NumType*> GetCellsData(const std::string& varname);
std::shared_ptr<std::vector<NumType>> GetNodesData(const std::string& varname); std::shared_ptr<NumType*> GetNodesData(const std::string& varname);
// Getter for cell thicknesses and total thickness // // Getter for cell thicknesses and total thickness
std::shared_ptr<std::vector<NumType>> GetCellsThickness(); std::shared_ptr<NumType*> GetCellsThickness();
NumType GetTotalThickness() const; // NumType GetTotalThickness() const;
// Muters and Unmuters // // Muters and Unmuters
void MuteSingleData(const std::string& varname); void MuteSingleData(const std::string& varname);
void MuteCellData(const std::string& varname); void MuteCellData(const std::string& varname);
void MuteNodeData(const std::string& varname); void MuteNodeData(const std::string& varname);
...@@ -65,14 +64,13 @@ namespace icethermo ...@@ -65,14 +64,13 @@ namespace icethermo
void UnmuteCellData(const std::string& varname); void UnmuteCellData(const std::string& varname);
void UnmuteNodeData(const std::string& varname); void UnmuteNodeData(const std::string& varname);
// Write mesh to File // // Write mesh to File
void SaveTXT(const std::string& filename) const; void SaveTXT(const std::string& filename);
void SaveTXT(const std::string& filename, int postscript) const; void SaveTXT(const std::string& filename, int postscript);
#ifdef USE_JSON_OUTPUT #ifdef USE_JSON_OUTPUT
// void SaveJSON(const std::string& filename);
void SaveJSON(const std::string& filename) const; // void SaveJSON(const std::string& filename, int postscript);
void SaveJSON(const std::string& filename, int postscript) const;
#endif #endif
// Check existency of data // Check existency of data
...@@ -80,17 +78,27 @@ namespace icethermo ...@@ -80,17 +78,27 @@ namespace icethermo
bool CheckNodesDataExistency(const std::string& varname) const; bool CheckNodesDataExistency(const std::string& varname) const;
bool CheckSingleDataExistency(const std::string& varname) const; bool CheckSingleDataExistency(const std::string& varname) const;
// Check GPU results subtools
void PullCPUArray(const NumType* array, const int n);
private: private:
// vector of cell thicknesses // vector of cell thicknesses
std::shared_ptr<std::vector<NumType>> cells_thickness; std::shared_ptr<NumType*> cells_thickness;
size_t cells_thickness_size_t;
// container for stand-alone variables // container for stand-alone variables
std::map<std::string, std::pair<std::shared_ptr<NumType>, bool>> single_data; std::map<std::string, std::pair<std::shared_ptr<NumType>, bool>> single_data;
// container for cell data [name : {vector_of_values, is_visible}] // container for cell data [name : {vector_of_values, is_visible}]
std::map<std::string, std::pair<std::shared_ptr<std::vector<NumType>>, bool>> cells_data; std::map<std::string, std::pair<std::shared_ptr<NumType*>, bool>> cells_data;
std::map<std::string, size_t> cells_data_size_t;
// container for node data [name : {vector_of_values, is_visible}] // container for node data [name : {vector_of_values, is_visible}]
std::map<std::string, std::pair<std::shared_ptr<std::vector<NumType>>, bool>> nodes_data; std::map<std::string, std::pair<std::shared_ptr<NumType*>, bool>> nodes_data;
std::map<std::string, size_t> nodes_data_size_t;
// Check GPU results subtools
public:
NumType* SubBuffer;
size_t SubBuffer_size;
}; };
} }
\ No newline at end of file
#include "../include/matvec.hpp"
namespace icethermo
{
template <typename NumType> std::vector<NumType> concatenate(const std::vector<std::vector<NumType>>& vecs)
{
std::vector<NumType> res;
for (int i = 0; i < vecs.size(); i++)
{
res.insert
(
res.end(),
vecs[i].begin(),
vecs[i].end()
);
}
return res;
}
template <typename NumType> std::vector<NumType> concatenate(const std::vector<NumType>& vec1, const std::vector<NumType>& vec2)
{
std::vector<NumType> res = vec1;
res.insert
(
res.end(),
vec2.begin(),
vec2.end()
);
return res;
}
template <typename NumType> std::vector<NumType> cumsum(const std::vector<NumType>& vec)
{
std::vector<NumType> res = vec;
for (int i = 1; i < vec.size(); ++i)
{
res[i] = res[i-1] + vec[i];
}
return res;
}
template <typename NumType> std::vector<NumType> exp_vec(const std::vector<NumType>& vec)
{
std::vector<NumType> res = vec;
for (int i = 0; i < vec.size(); ++i)
{
res[i] = std::exp(vec[i]);
}
return res;
}
template <typename NumType> std::vector<NumType> reverse_vec(const std::vector<NumType>& vec)
{
std::vector<NumType> res = vec;
for (int i = 0; i < vec.size(); ++i)
{
res[i] = vec[vec.size() - 1 - i];
}
return res;
}
template <typename NumType>
std::vector<NumType> operator+(const std::vector<NumType>& vec1,
const std::vector<NumType>& vec2)
{
if (vec1.size() != vec2.size())
{
THERMO_ERR("can't compute the sum of two vectors with differrent size!");
}
std::vector<NumType> vec_sum(vec1);
for (int i = 0; i < vec2.size(); i++)
{
vec_sum[i] += vec2[i];
}
return vec_sum;
}
template <typename NumType>
std::vector<NumType> operator-(const std::vector<NumType>& vec1,
const std::vector<NumType>& vec2)
{
if (vec1.size() != vec2.size())
{
THERMO_ERR("can't compute the diff of two vectors with differrent size!");
}
std::vector<NumType> vec_diff(vec1);
for (int i = 0; i < vec2.size(); i++)
{
vec_diff[i] -= vec2[i];
}
return vec_diff;
}
template <typename NumType>
NumType operator*(const std::vector<NumType>& vec1,
const std::vector<NumType>& vec2)
{
if (vec1.size() != vec2.size())
{
THERMO_ERR("can't compute scalar product of two vectors with differrent size!");
}
NumType res = 0.0;
for (int i = 0; i < vec2.size(); i++)
{
res += vec1[i]*vec2[i];
}
return res;
}
template <typename NumType>
NumType L2_norm(const std::vector<NumType>& vec)
{
NumType res;
for (int i = 0; i < vec.size(); i++)
{
res += vec[i]*vec[i];
}
return std::sqrt(res);
}
template <typename NumType>
std::vector<NumType> operator*(const std::vector<NumType>& vec, NumType scal)
{
auto res_vec = vec;
for (int i = 0 ; i < vec.size(); i++)
{
res_vec[i] *= scal;
}
return res_vec;
}
template <typename NumType>
std::vector<NumType> operator*(NumType scal, const std::vector<NumType>& vec)
{
auto res_vec = vec;
for (int i = 0 ; i < vec.size(); i++)
{
res_vec[i] *= scal;
}
return res_vec;
}
template <typename NumType>
std::ostream& operator<<(std::ostream& os, const std::vector<NumType>& vec)
{
os << "[";
for (auto it = vec.begin(); it != vec.end(); it++)
{
os << *it;
if (std::next(it) != vec.end())
os << ", ";
}
os << "]";
return os;
}
template <typename NumType>
NumType sum_vec(const std::vector<NumType>& vec)
{
return std::accumulate(vec.begin(), vec.end(), NumType(0));
}
template <typename NumType>
NumType sum_vec(const std::vector<NumType>& vec, int start, int end)
{
auto first = vec.begin();
std::advance(first, start);
auto last = vec.begin();
std::advance(last, end);
return std::accumulate(first, last, NumType(0));
}
// explicit instantaion
template std::vector<int> concatenate(const std::vector<int>& vec1, const std::vector<int>& vec2);
template std::vector<float> concatenate(const std::vector<float>& vec1, const std::vector<float>& vec2);
template std::vector<double> concatenate(const std::vector<double>& vec1, const std::vector<double>& vec2);
template std::vector<int> concatenate(const std::vector<std::vector<int>>& vecs);
template std::vector<float> concatenate(const std::vector<std::vector<float>>& vecs);
template std::vector<double> concatenate(const std::vector<std::vector<double>>& vecs);
template std::vector<int> cumsum(const std::vector<int>& vec);
template std::vector<float> cumsum(const std::vector<float>& vec);
template std::vector<double> cumsum(const std::vector<double>& vec);
template std::vector<float> exp_vec(const std::vector<float>& vec);
template std::vector<double> exp_vec(const std::vector<double>& vec);
template std::vector<int> reverse_vec(const std::vector<int>& vec);
template std::vector<float> reverse_vec(const std::vector<float>& vec);
template std::vector<double> reverse_vec(const std::vector<double>& vec);
template std::vector<float> operator+(const std::vector<float>& vec1,
const std::vector<float>& vec2);
template std::vector<double> operator+(const std::vector<double>& vec1,
const std::vector<double>& vec2);
template std::vector<float> operator-(const std::vector<float>& vec1,
const std::vector<float>& vec2);
template std::vector<double> operator-(const std::vector<double>& vec1,
const std::vector<double>& vec2);
template float operator*(const std::vector<float>& vec1,
const std::vector<float>& vec2);
template double operator*(const std::vector<double>& vec1,
const std::vector<double>& vec2);
template float L2_norm(const std::vector<float>& vec);
template double L2_norm(const std::vector<double>& vec);
template std::vector<float> operator*(const std::vector<float>& vec, float scal);
template std::vector<double> operator*(const std::vector<double>& vec, double scal);
template std::vector<float> operator*(float scal, const std::vector<float>& vec);
template std::vector<double> operator*(double scal, const std::vector<double>& vec);
template std::ostream& operator<<(std::ostream& os, const std::vector<float>& vec);
template std::ostream& operator<<(std::ostream& os, const std::vector<double>& vec);
template float sum_vec(const std::vector<float>& vec);
template double sum_vec(const std::vector<double>& vec);
template float sum_vec(const std::vector<float>& vec, int start, int end);
template double sum_vec(const std::vector<double>& vec, int start, int end);
}
\ No newline at end of file
#include "mesh.hpp" #include "../include/mesh.hpp"
#include "../include/matvec.hpp"
#include "MemoryProcessing.h"
#ifdef INCLUDE_CUDA
#include "MemoryProcessing.cuh"
#endif
namespace icethermo namespace icethermo
{ {
template<typename NumType> template <typename NumType, MemType memtype>
Mesh<NumType>::Mesh() Mesh<NumType, memtype>::Mesh()
{ {
}; SubBuffer = nullptr;
SubBuffer_size = 0;
}
template<typename NumType> template <typename NumType, MemType memtype>
Mesh<NumType>::Mesh(int n_uniform_layers, NumType thickness) Mesh<NumType, memtype>::Mesh(int n_uniform_layers, NumType thickness)
{ {
if (n_uniform_layers <= 0) if (n_uniform_layers <= 0)
{ {
THERMO_ERR("Number of layers in mesh should be greater than 1!"); THERMO_ERR("Number of layers in mesh should be greater than 1!");
} }
std::vector<NumType> thicknesses(n_uniform_layers); NumType* thicknesses = new NumType[n_uniform_layers];
for (int i =0; i < n_uniform_layers; ++i) for (int i =0; i < n_uniform_layers; ++i)
{ {
thicknesses[i] = thickness/n_uniform_layers; thicknesses[i] = thickness/n_uniform_layers;
} }
cells_thickness = std::make_shared<std::vector<NumType>>(std::move(thicknesses));
}
template<typename NumType> #ifdef INCLUDE_CUDA
Mesh<NumType>::Mesh(NumType thickness): Mesh(10, thickness) if(memtype == MemType::GPU)
{ {
NumType* dev_thicknesses;
cells_thickness_size_t = 0;
memproc::realloc<memtype>((void *&)(dev_thicknesses), cells_thickness_size_t, n_uniform_layers * sizeof(NumType));
memproc::memcopy<MemType::GPU, MemType::CPU>(dev_thicknesses, thicknesses, cells_thickness_size_t);
cells_thickness = std::make_shared<NumType*>(std::move(dev_thicknesses));
delete[] thicknesses;
} }
else
template<typename NumType> #endif
Mesh<NumType>::Mesh(const std::vector<NumType>& unit_segment_decomposition, NumType thickness)
{ {
NumType sum_decomp = sum_vec(unit_segment_decomposition); cells_thickness_size_t = n_uniform_layers * sizeof(NumType);
cells_thickness = std::make_shared<NumType*>(std::move(thicknesses));
}
if (std::abs(sum_decomp - 1.0) > 1e-5) SubBuffer = nullptr;
{ SubBuffer_size = 0;
THERMO_ERR("Unit segment decomposition of length 1.0 should be given!");
} }
std::vector<NumType> thicknesses = unit_segment_decomposition*thickness; template <typename NumType, MemType memtype>
cells_thickness = std::make_shared<std::vector<NumType>>(thicknesses); Mesh<NumType, memtype>::Mesh(NumType thickness): Mesh(10, thickness)
{
SubBuffer = nullptr;
SubBuffer_size = 0;
} }
template<typename NumType> // TODO: sum_vec GPU
Mesh<NumType>::Mesh(const Mesh<NumType>& other) // template <typename NumType, MemType memtype>
// Mesh<NumType, memtype>::Mesh(const NumType* unit_segment_decomposition, NumType thickness)
// {
// NumType sum_decomp = sum_vec(unit_segment_decomposition);
// if (std::abs(sum_decomp - 1.0) > 1e-5)
// {
// THERMO_ERR("Unit segment decomposition of length 1.0 should be given!");
// }
// NumType* thicknesses = unit_segment_decomposition*thickness;
// cells_thickness = std::make_shared<NumType*>(thicknesses);
// }
template <typename NumType, MemType memtype>
Mesh<NumType, memtype>::Mesh(const Mesh<NumType, memtype>& other)
{ {
this->cells_thickness = std::make_shared<std::vector<NumType>>(*(other.cells_thickness)); this->cells_thickness = std::make_shared<NumType*>(*(other.cells_thickness));
this->cells_thickness_size_t = other.cells_thickness_size_t;
for (auto item: other.single_data) for (auto item: other.single_data)
{ {
...@@ -69,11 +99,13 @@ namespace icethermo ...@@ -69,11 +99,13 @@ namespace icethermo
(this->cells_data)[key] = (this->cells_data)[key] =
std::make_pair std::make_pair
( (
std::make_shared<std::vector<NumType>>(*(value.first)), std::make_shared<NumType*>(*(value.first)),
value.second value.second
); );
} }
this->cells_data_size_t = std::map<std::string, size_t> (other.cells_data_size_t);
for (auto item: other.nodes_data) for (auto item: other.nodes_data)
{ {
auto key = item.first; auto key = item.first;
...@@ -82,18 +114,21 @@ namespace icethermo ...@@ -82,18 +114,21 @@ namespace icethermo
(this->nodes_data)[key] = (this->nodes_data)[key] =
std::make_pair std::make_pair
( (
std::make_shared<std::vector<NumType>>(*(value.first)), std::make_shared<NumType*>(*(value.first)),
value.second value.second
); );
} }
this->nodes_data_size_t = std::map<std::string, size_t> (other.nodes_data_size_t);
SubBuffer = nullptr;
SubBuffer_size = 0;
} }
template<typename NumType> template <typename NumType, MemType memtype>
Mesh<NumType>::~Mesh() Mesh<NumType, memtype>::~Mesh()
{ {
//delete cells_thickness.get(); memproc::dealloc<memtype>((void*&)(*cells_thickness.get()), cells_thickness_size_t);
//std::cout << 111 << std::endl;
//for (auto item: this->single_data) //for (auto item: this->single_data)
//{ //{
...@@ -102,41 +137,40 @@ namespace icethermo ...@@ -102,41 +137,40 @@ namespace icethermo
// delete (value.first).get(); // delete (value.first).get();
//} //}
//std::cout << 222 << std::endl; for (auto item: this->cells_data)
{
//for (auto item: this->cells_data) auto value = item.second;
//{ memproc::dealloc<memtype>((void*&)(*(value.first).get()), cells_data_size_t[item.first]);
// auto value = item.second; }
// delete (value.first).get();
//}
//std::cout << 333 << std::endl;
//for (auto item: this->nodes_data)
//{
// auto value = item.second;
// delete (value.first).get(); for (auto item: this->nodes_data)
//} {
auto value = item.second;
memproc::dealloc<memtype>((void*&)(*(value.first).get()), cells_data_size_t[item.first]);
}
//std::cout << 444 << std::endl; if(SubBuffer_size != 0)
{
delete[] SubBuffer;
SubBuffer_size = 0;
}
} }
template<typename NumType> template <typename NumType, MemType memtype>
int Mesh<NumType>::GetCellsNum() const int Mesh<NumType, memtype>::GetCellsNum() const
{ {
return cells_thickness->size(); return int(cells_thickness_size_t / sizeof(NumType));
} }
template<typename NumType> template <typename NumType, MemType memtype>
int Mesh<NumType>::GetNodesNum() const int Mesh<NumType, memtype>::GetNodesNum() const
{ {
return cells_thickness->size() + 1; return int(cells_thickness_size_t / sizeof(NumType)) + 1;
// return cells_thickness->size() + 1;
} }
template<typename NumType> template <typename NumType, MemType memtype>
std::shared_ptr<NumType> Mesh<NumType>::CreateSingleData(const std::string& varname, bool visible) std::shared_ptr<NumType> Mesh<NumType, memtype>::CreateSingleData(const std::string& varname, bool visible)
{ {
if (single_data.count(varname) != 0) if (single_data.count(varname) != 0)
{ {
...@@ -147,22 +181,24 @@ namespace icethermo ...@@ -147,22 +181,24 @@ namespace icethermo
return single_data[varname].first; return single_data[varname].first;
} }
template<typename NumType> template <typename NumType, MemType memtype>
std::shared_ptr<std::vector<NumType>> Mesh<NumType>::CreateCellsData(const std::string& varname, bool visible) std::shared_ptr<NumType*> Mesh<NumType, memtype>::CreateCellsData(const std::string& varname, bool visible)
{ {
if (cells_data.count(varname) != 0) if (cells_data.count(varname) != 0)
{ {
THERMO_ERR("Variable \'" + varname + "\' already exists, could not create cell variable!"); THERMO_ERR("Variable \'" + varname + "\' already exists, could not create cell variable!");
} }
// make zero vector NumType* zero_vec; //zero_vec(cells_thickness->size());
std::vector<NumType> zero_vec(cells_thickness->size()); cells_data_size_t[varname] = 0;
cells_data[varname] = {std::make_shared<std::vector<NumType>>(std::move(zero_vec)), visible}; memproc::realloc<memtype>((void *&)(zero_vec), cells_data_size_t[varname], cells_thickness_size_t);
cells_data[varname] = {std::make_shared<NumType*>(std::move(zero_vec)), visible};
return cells_data[varname].first; return cells_data[varname].first;
} }
template<typename NumType> template <typename NumType, MemType memtype>
std::shared_ptr<std::vector<NumType>> Mesh<NumType>::CreateNodesData(const std::string& varname, bool visible) std::shared_ptr<NumType*> Mesh<NumType, memtype>::CreateNodesData(const std::string& varname, bool visible)
{ {
if (nodes_data.count(varname) != 0) if (nodes_data.count(varname) != 0)
{ {
...@@ -170,13 +206,15 @@ namespace icethermo ...@@ -170,13 +206,15 @@ namespace icethermo
} }
// make zero vector // make zero vector
std::vector<NumType> zero_vec(cells_thickness->size() + 1); NumType* zero_vec; //zero_vec(cells_thickness->size());
nodes_data[varname] = {std::make_shared<std::vector<NumType>>(std::move(zero_vec)), visible}; nodes_data_size_t[varname] = 0;
memproc::realloc<memtype>((void *&)(zero_vec), nodes_data_size_t[varname], GetNodesNum() * sizeof(NumType));
nodes_data[varname] = {std::make_shared<NumType*>(std::move(zero_vec)), visible};
return nodes_data[varname].first; return nodes_data[varname].first;
} }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::DeleteSingleData(const std::string& varname) void Mesh<NumType, memtype>::DeleteSingleData(const std::string& varname)
{ {
if (single_data.count(varname) == 0) if (single_data.count(varname) == 0)
{ {
...@@ -186,30 +224,32 @@ namespace icethermo ...@@ -186,30 +224,32 @@ namespace icethermo
single_data.erase(varname); single_data.erase(varname);
} }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::DeleteCellsData(const std::string& varname) void Mesh<NumType, memtype>::DeleteCellsData(const std::string& varname)
{ {
if (cells_data.count(varname) == 0) if (cells_data.count(varname) == 0)
{ {
return; return;
} }
cells_data[varname].first.reset(); // cells_data[varname].first.reset();
memproc::dealloc<memtype>((void*&)(*(cells_data[varname].first).get()), cells_data_size_t[varname]);
cells_data.erase(varname); cells_data.erase(varname);
} }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::DeleteNodesData(const std::string& varname) void Mesh<NumType, memtype>::DeleteNodesData(const std::string& varname)
{ {
if (nodes_data.count(varname) == 0) if (nodes_data.count(varname) == 0)
{ {
return; return;
} }
nodes_data[varname].first.reset(); // nodes_data[varname].first.reset();
memproc::dealloc<memtype>((void*&)(*(nodes_data[varname].first).get()), nodes_data_size_t[varname]);
nodes_data.erase(varname); nodes_data.erase(varname);
} }
template<typename NumType> template <typename NumType, MemType memtype>
std::shared_ptr<NumType> Mesh<NumType>::GetSingleData(const std::string& varname) std::shared_ptr<NumType> Mesh<NumType, memtype>::GetSingleData(const std::string& varname)
{ {
if (single_data.count(varname) == 0) if (single_data.count(varname) == 0)
{ {
...@@ -218,8 +258,8 @@ namespace icethermo ...@@ -218,8 +258,8 @@ namespace icethermo
return single_data[varname].first; return single_data[varname].first;
} }
template<typename NumType> template <typename NumType, MemType memtype>
std::shared_ptr<std::vector<NumType>> Mesh<NumType>::GetCellsData(const std::string& varname) std::shared_ptr<NumType*> Mesh<NumType, memtype>::GetCellsData(const std::string& varname)
{ {
if (cells_data.count(varname) == 0) if (cells_data.count(varname) == 0)
{ {
...@@ -228,8 +268,8 @@ namespace icethermo ...@@ -228,8 +268,8 @@ namespace icethermo
return cells_data[varname].first; return cells_data[varname].first;
} }
template<typename NumType> template <typename NumType, MemType memtype>
std::shared_ptr<std::vector<NumType>> Mesh<NumType>::GetNodesData(const std::string& varname) std::shared_ptr<NumType*> Mesh<NumType, memtype>::GetNodesData(const std::string& varname)
{ {
if (nodes_data.count(varname) == 0) if (nodes_data.count(varname) == 0)
{ {
...@@ -238,20 +278,21 @@ namespace icethermo ...@@ -238,20 +278,21 @@ namespace icethermo
return nodes_data[varname].first; return nodes_data[varname].first;
} }
template<typename NumType> template <typename NumType, MemType memtype>
std::shared_ptr<std::vector<NumType>> Mesh<NumType>::GetCellsThickness() std::shared_ptr<NumType*> Mesh<NumType, memtype>::GetCellsThickness()
{ {
return cells_thickness; return cells_thickness;
} }
template<typename NumType> // TODO: sum_vec GPU
NumType Mesh<NumType>::GetTotalThickness() const // template <typename NumType, MemType memtype>
{ // NumType Mesh<NumType, memtype>::GetTotalThickness() const
return sum_vec(*cells_thickness); // {
} // return sum_vec(*cells_thickness);
// }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::MuteSingleData(const std::string& varname) void Mesh<NumType, memtype>::MuteSingleData(const std::string& varname)
{ {
if (single_data.count(varname) == 0) if (single_data.count(varname) == 0)
{ {
...@@ -260,8 +301,8 @@ namespace icethermo ...@@ -260,8 +301,8 @@ namespace icethermo
single_data[varname].second = false; single_data[varname].second = false;
} }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::MuteCellData(const std::string& varname) void Mesh<NumType, memtype>::MuteCellData(const std::string& varname)
{ {
if (cells_data.count(varname) == 0) if (cells_data.count(varname) == 0)
{ {
...@@ -270,8 +311,8 @@ namespace icethermo ...@@ -270,8 +311,8 @@ namespace icethermo
cells_data[varname].second = false; cells_data[varname].second = false;
} }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::MuteNodeData(const std::string& varname) void Mesh<NumType, memtype>::MuteNodeData(const std::string& varname)
{ {
if (nodes_data.count(varname) == 0) if (nodes_data.count(varname) == 0)
{ {
...@@ -280,8 +321,8 @@ namespace icethermo ...@@ -280,8 +321,8 @@ namespace icethermo
nodes_data[varname].second = false; nodes_data[varname].second = false;
} }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::UnmuteSingleData(const std::string& varname) void Mesh<NumType, memtype>::UnmuteSingleData(const std::string& varname)
{ {
if (single_data.count(varname) == 0) if (single_data.count(varname) == 0)
{ {
...@@ -290,8 +331,8 @@ namespace icethermo ...@@ -290,8 +331,8 @@ namespace icethermo
single_data[varname].second = true; single_data[varname].second = true;
} }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::UnmuteCellData(const std::string& varname) void Mesh<NumType, memtype>::UnmuteCellData(const std::string& varname)
{ {
if (cells_data.count(varname) == 0) if (cells_data.count(varname) == 0)
{ {
...@@ -300,8 +341,8 @@ namespace icethermo ...@@ -300,8 +341,8 @@ namespace icethermo
cells_data[varname].second = true; cells_data[varname].second = true;
} }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::UnmuteNodeData(const std::string& varname) void Mesh<NumType, memtype>::UnmuteNodeData(const std::string& varname)
{ {
if (nodes_data.count(varname) == 0) if (nodes_data.count(varname) == 0)
{ {
...@@ -310,8 +351,8 @@ namespace icethermo ...@@ -310,8 +351,8 @@ namespace icethermo
nodes_data[varname].second = true; nodes_data[varname].second = true;
} }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::SaveTXT(const std::string& filename) const void Mesh<NumType, memtype>::SaveTXT(const std::string& filename)
{ {
std::string filename_txt = filename + ".txt"; std::string filename_txt = filename + ".txt";
std::fstream* ofs = new std::fstream; std::fstream* ofs = new std::fstream;
...@@ -323,16 +364,18 @@ namespace icethermo ...@@ -323,16 +364,18 @@ namespace icethermo
} }
// cell thickness info // cell thickness info
int count = cells_thickness_size_t / sizeof(NumType);
PullCPUArray(*cells_thickness.get(), count);
*ofs << "### cells_thickness_array ###\n"; *ofs << "### cells_thickness_array ###\n";
for (int i = 0; i < cells_thickness->size(); i++) for (int i = 0; i < count; i++)
{ {
if (i != cells_thickness->size() - 1) if (i != count - 1)
{ {
*ofs << (*cells_thickness)[i] << " "; *ofs << SubBuffer[i] << " ";
} }
else else
{ {
*ofs << (*cells_thickness)[i]; *ofs << SubBuffer[i];
} }
} }
*ofs << "\n"; *ofs << "\n";
...@@ -362,15 +405,17 @@ namespace icethermo ...@@ -362,15 +405,17 @@ namespace icethermo
if (val.second) if (val.second)
{ {
*ofs << key + "\n"; *ofs << key + "\n";
for (int i = 0; i < val.first->size(); i++) count = cells_data_size_t[key] / sizeof(NumType);
PullCPUArray(*val.first.get(), count);
for (int i = 0; i < count; i++)
{ {
if (i != val.first->size() - 1) if (i != count - 1)
{ {
*ofs << (*val.first)[i] << " "; *ofs << SubBuffer[i] << " ";
} }
else else
{ {
*ofs << (*val.first)[i]; *ofs << SubBuffer[i];
} }
} }
*ofs << "\n"; *ofs << "\n";
...@@ -387,15 +432,17 @@ namespace icethermo ...@@ -387,15 +432,17 @@ namespace icethermo
if (val.second) if (val.second)
{ {
*ofs << key + "\n"; *ofs << key + "\n";
for (int i = 0; i < val.first->size(); i++) count = nodes_data_size_t[key] / sizeof(NumType);
PullCPUArray(*val.first.get(), count);
for (int i = 0; i < count; i++)
{ {
if (i != val.first->size() - 1) if (i != count - 1)
{ {
*ofs << (*val.first)[i] << " "; *ofs << SubBuffer[i] << " ";
} }
else else
{ {
*ofs << (*val.first)[i]; *ofs << SubBuffer[i];
} }
} }
*ofs << "\n"; *ofs << "\n";
...@@ -408,8 +455,8 @@ namespace icethermo ...@@ -408,8 +455,8 @@ namespace icethermo
std::cout << "Mesh saved to \'" + filename_txt + "\'\n"; std::cout << "Mesh saved to \'" + filename_txt + "\'\n";
} }
template<typename NumType> template <typename NumType, MemType memtype>
void Mesh<NumType>::SaveTXT(const std::string& filename, int postscript) const void Mesh<NumType, memtype>::SaveTXT(const std::string& filename, int postscript)
{ {
std::string file = filename; std::string file = filename;
std::stringstream ss; std::stringstream ss;
...@@ -418,86 +465,86 @@ namespace icethermo ...@@ -418,86 +465,86 @@ namespace icethermo
SaveTXT(file); SaveTXT(file);
} }
#ifdef USE_JSON_OUTPUT // #ifdef USE_JSON_OUTPUT
template<typename NumType> // template <typename NumType, MemType memtype>
void Mesh<NumType>::SaveJSON(const std::string& filename) const // void Mesh<NumType, memtype>::SaveJSON(const std::string& filename) const
{ // {
// create empty json object // // create empty json object
json j; // json j;
// cells thickness // // cells thickness
j["cells_thickness_array"] = *cells_thickness; // j["cells_thickness_array"] = *cells_thickness;
// single data // // single data
for (auto item: single_data) // for (auto item: single_data)
{ // {
auto key = item.first; // auto key = item.first;
auto val = item.second; // auto val = item.second;
if (val.second) // if (val.second)
{ // {
j["single data"][key] = *(val.first); // j["single data"][key] = *(val.first);
} // }
} // }
// cells data // // cells data
for (auto item: cells_data) // for (auto item: cells_data)
{ // {
auto key = item.first; // auto key = item.first;
auto val = item.second; // auto val = item.second;
if (val.second) // if (val.second)
{ // {
j["cells data"][key] = *(val.first); // j["cells data"][key] = *(val.first);
} // }
} // }
// nodes data // // nodes data
for (auto item: nodes_data) // for (auto item: nodes_data)
{ // {
auto key = item.first; // auto key = item.first;
auto val = item.second; // auto val = item.second;
if (val.second) // if (val.second)
{ // {
j["nodes data"][key] = *(val.first); // j["nodes data"][key] = *(val.first);
} // }
} // }
// write json object to file // // write json object to file
std::string filename_json = filename + ".json"; // std::string filename_json = filename + ".json";
std::fstream* ofs = new std::fstream; // std::fstream* ofs = new std::fstream;
ofs->open(filename_json, std::ios::out); // ofs->open(filename_json, std::ios::out);
if (!ofs->is_open()) // if (!ofs->is_open())
{ // {
THERMO_ERR("can't open file "+ filename_json + " for logging!"); // THERMO_ERR("can't open file "+ filename_json + " for logging!");
} // }
*ofs << std::setw(4) << j; // *ofs << std::setw(4) << j;
ofs->close(); // ofs->close();
delete ofs; // delete ofs;
std::cout << "Mesh saved to \'" + filename_json + "\'\n"; // std::cout << "Mesh saved to \'" + filename_json + "\'\n";
} // }
#endif // #endif
#ifdef USE_JSON_OUTPUT // #ifdef USE_JSON_OUTPUT
template<typename NumType> // template <typename NumType, MemType memtype>
void Mesh<NumType>::SaveJSON(const std::string& filename, int postscript) const // void Mesh<NumType, memtype>::SaveJSON(const std::string& filename, int postscript) const
{ // {
std::string file = filename; // std::string file = filename;
std::stringstream ss; // std::stringstream ss;
ss << std::setfill('0') << std::setw(5) << postscript; // ss << std::setfill('0') << std::setw(5) << postscript;
file += ss.str(); // file += ss.str();
SaveJSON(file); // SaveJSON(file);
} // }
#endif // #endif
template<typename NumType> template <typename NumType, MemType memtype>
bool Mesh<NumType>::CheckCellsDataExistency(const std::string& varname) const bool Mesh<NumType, memtype>::CheckCellsDataExistency(const std::string& varname) const
{ {
if (cells_data.count(varname) == 0) if (cells_data.count(varname) == 0)
{ {
...@@ -509,8 +556,8 @@ namespace icethermo ...@@ -509,8 +556,8 @@ namespace icethermo
} }
} }
template<typename NumType> template <typename NumType, MemType memtype>
bool Mesh<NumType>::CheckNodesDataExistency(const std::string& varname) const bool Mesh<NumType, memtype>::CheckNodesDataExistency(const std::string& varname) const
{ {
if (nodes_data.count(varname) == 0) if (nodes_data.count(varname) == 0)
{ {
...@@ -522,8 +569,8 @@ namespace icethermo ...@@ -522,8 +569,8 @@ namespace icethermo
} }
} }
template<typename NumType> template <typename NumType, MemType memtype>
bool Mesh<NumType>::CheckSingleDataExistency(const std::string& varname) const bool Mesh<NumType, memtype>::CheckSingleDataExistency(const std::string& varname) const
{ {
if (single_data.count(varname) == 0) if (single_data.count(varname) == 0)
{ {
...@@ -535,7 +582,24 @@ namespace icethermo ...@@ -535,7 +582,24 @@ namespace icethermo
} }
} }
template <typename NumType, MemType memtype>
void Mesh<NumType, memtype>::PullCPUArray(const NumType* array, const int n)
{
if(n * sizeof(NumType) > SubBuffer_size)
{
delete[] SubBuffer;
SubBuffer_size = n * sizeof(NumType);
SubBuffer = new NumType[n];
}
memproc::memcopy<MemType::CPU, memtype>(SubBuffer, array, n * sizeof(NumType));
}
// explicit instantiation of classes // explicit instantiation of classes
template class Mesh<float>; template class Mesh<float, MemType::CPU>;
template class Mesh<double>; template class Mesh<double, MemType::CPU>;
#ifdef INCLUDE_CUDA
template class Mesh<float, MemType::GPU>;
template class Mesh<double, MemType::GPU>;
#endif
} }
\ No newline at end of file
add_subdirectory(constants_tests)
add_subdirectory(secant_test)
add_subdirectory(thomas_tests)
add_subdirectory(concat_tests)
add_subdirectory(matrix_test)
add_subdirectory(mesh_tests)
add_subdirectory(solver_tests)
\ No newline at end of file
project(mesh_test)
set(SOURCE main.cpp)
add_executable(mesh_test ${SOURCE})
target_link_libraries(mesh_test icethermo)
\ No newline at end of file
#include "icethermo.hpp"
using namespace icethermo;
int main()
{
// ### examples of Mesh class ###
// constructor with given total thickness (default is 10 cells)
Mesh<float> mesh1(1.0f);
// how to create cells data (lhs is shared ptr to vector)
auto cells_temp = mesh1.CreateCellsData("cells_temperature");
auto cells_capacity = mesh1.CreateCellsData("cells_capacity", true);
auto cells_enthalpy = mesh1.CreateCellsData("cells_enthalpy", false);
// one can modify cell data
(*cells_temp)[0] = 1.0f; (*cells_temp)[1] = 2.0f; (*cells_temp)[2] = 3.0f;
// how to create nodes data (lhs is shared ptr to vector)
auto nodes_k = mesh1.CreateNodesData("nodes_k");
auto nodes_enthalpy = mesh1.CreateNodesData("nodes_enthalpy", false);
// one can modify nodes data
(*nodes_k)[0] = -5.0f; nodes_k->back() = -3.0f;
// how to create single data
auto temp_ib = mesh1.CreateSingleData("temp_ib");
auto temp_is = mesh1.CreateSingleData("temp_is");
// one can modify single data
(*temp_ib) = -1.0f; (*temp_is) = 2.0f;
// one can delete cells, nodes or single data
mesh1.DeleteCellsData("cells_enthalpy");
mesh1.DeleteNodesData("nodes_enthalpy");
mesh1.DeleteSingleData("temp_is");
// its is better to avoid this, but one can get another pointer to created data
auto another_cells_temp = mesh1.GetCellsData("cells_temperature");
auto another_nodes_k = mesh1.GetNodesData("nodes_k");
auto another_temp_ib= mesh1.GetSingleData("temp_ib");
(*another_cells_temp)[0] = -5.0f;
(*another_nodes_k)[0] *= 2.0f;
(*another_temp_ib) = -30.0f;
// one can get vector with cell thickness
std::cout << "current cell thickness array: " << (*mesh1.GetCellsThickness()) << std::endl;
// one can get total thickness
std::cout << "current total cell thickness: " << mesh1.GetTotalThickness() << std::endl;
// one could manually mute and unmute variables (muted variables will not be writed to the output)
mesh1.MuteCellData("cells_temperature");
mesh1.UnmuteCellData("cells_temperature");
mesh1.MuteNodeData("nodes_enthalpy");
mesh1.UnmuteNodeData("nodes_enthalpy");
// one can save mesh to .txt file
mesh1.SaveTXT("./mesh");
// one can save mesh to .txt file with postfix number (relevant for time series)
mesh1.SaveTXT("./mesh", 1488);
// one can save file to json
#ifdef USE_JSON_OUTPUT
mesh1.SaveJSON("./mesh");
#endif
// one can save mesh to .json file with postfix number (relevant for time series)
#ifdef USE_JSON_OUTPUT
mesh1.SaveJSON("./mesh", 2007);
#endif
// ### examples of another Mesh class constructor ###
// construct uniform mesh with given cells num and total thickness
Mesh<double> mesh2(15, 1.0);
mesh2.SaveTXT("./mesh2");
// construct arbitrary mesh with given unit segment partition and total thickness
Mesh<double> mesh3({0.5, 0.5}, 5.0);
mesh3.SaveTXT("./mesh3");
// test mesh for visualization
Mesh<double> mesh_vis(15, 4.0);
auto cells_thick = mesh_vis.CreateCellsData("cells_temp_array");
int N = mesh_vis.GetCellsNum();
for (int i = 0; i < N; ++i)
{
(*cells_thick)[i] = -5.0 + i*1.0/N * (-5.0);
}
auto down_temp = mesh_vis.CreateSingleData("down_temp");
auto up_temp = mesh_vis.CreateSingleData("up_temp");
*(down_temp) = -5.0; *(up_temp) = -10.0;
auto cells_dens = mesh_vis.CreateCellsData("cells_density_array");
for (auto& it: *(cells_dens))
{
it = Params<double>::Density(Dparam::FreshIce, 0.0, 0.0);
}
#ifdef USE_JSON_OUTPUT
mesh_vis.SaveJSON("./mesh_vis");
#endif
// wrong constructor (it should be unit segment partition 0.5 + 0.4 != 1.0)
Mesh<float> mesh4({0.5, 0.4}, 5.0);
mesh4.SaveTXT("./mesh4");
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment