Xiangyu-Hu / SPHinXsys

SPHinXsys provides C++ APIs for engineering simulation and optimization. It aims at complex systems driven by fluid, structure, multi-body dynamics and beyond. The multi-physics library is based on a unique and unified computational framework by which strong coupling has been achieved for all involved physics.
https://www.sphinxsys.org/
Apache License 2.0
259 stars 199 forks source link

Introduce generalized global variables in particles #286

Closed Xiangyu-Hu closed 10 months ago

Xiangyu-Hu commented 12 months ago

We need a mechanism to register arbitrary number global variables with different types. Similar to discrete variables, such global variable will registered for physical models defined by particle dynamics class. Another objective is to decrease the number of control parameters in the constructor of a method class.

ChenxiZhaoTUM commented 11 months ago

`

template <typename GlobalVariableType>
void BaseParticles::
    registerGlobalVariable(const std::string &variable_name, GlobalVariableType initial_value)
    {
    constexpr int type_index = DataTypeIndex<GlobalVariableType>::value;

    UniquePtrsKeeper<GlobalVariable<GlobalVariableType>>& container = std::get<type_index>(all_global_data_);

    bool isRegistered = false;

    //attention: ptr_keepers_ is changed here to public property temporarily!
    for (size_t i = 0; i < container.ptr_keepers_.size(); ++i)
    {
        const GlobalVariable<GlobalVariableType> variable = container[i]; //wrong, container[i] is UniquePtrsKeeper

        if (variable.getName() == variable_name)
        {
            isRegistered = true;
            std::cout << "\n Error: the variable '" << variable_name << "' has already been registered!" << std::endl;
            std::cout << __FILE__ << ':' << __LINE__ << std::endl;
            exit(1);
        }
    }

    if (!isRegistered)
    {
        container.template createPtr<GlobalVariable<GlobalVariableType>>(variable_name, initial_value);
    }
}`

base_particles.hpp:83 const GlobalVariable variable = container[I];

I do not know how to obtain GlobalVariable in DataContainerUniquePtrAssemble

Xiangyu-Hu commented 11 months ago

You can learn the usage of UniquePtrsKeeper form the binary shape class https://github.com/Xiangyu-Hu/SPHinXsys/blob/f1a6c9f5e05845d4cb33627bb14b420220faa992/src/shared/geometries/base_geometry.h#L100. Basically, you need work with one keeper and another vector. This suggests that my idea of using single UniquePtrsKeeper assemble is wrong. You the the extra assemble Like this; https://github.com/Xiangyu-Hu/SPHinXsys/blob/f1a6c9f5e05845d4cb33627bb14b420220faa992/src/shared/common/base_data_package.h#L52 also we may consider how to managing discrete and global variables that they has same name, for example temperature.

Xiangyu-Hu commented 11 months ago

We will treat discrete and gloabl variables separately now,

ChenxiZhaoTUM commented 11 months ago

ok, I will try

ChenxiZhaoTUM commented 11 months ago

https://github.com/ChenxiZhaoTUM/SPHinXsys-learning/blob/d434332c90cf61a93f2b4a6c58a3762110577e39/src/shared/particles/base_particles.h#L116-L117 Using UniquePtrsKeeper and AddressAssemble has been achieved.

https://github.com/ChenxiZhaoTUM/SPHinXsys-learning/blob/d434332c90cf61a93f2b4a6c58a3762110577e39/tests/user_examples/ExtraSources/extra_sources_share/particle_dynamics_diffusion_reaction_with_boundary.hpp#LL75C3-L75C71 But Tinfinity cannot be passed as an initial value in registerGlobalVariable without its address https://github.com/ChenxiZhaoTUM/SPHinXsys-learning/blob/d434332c90cf61a93f2b4a6c58a3762110577e39/src/shared/particles/base_particles.hpp#LL73C99-L73C99

ChenxiZhaoTUM commented 11 months ago

There is no "Reference in new issue" in the files in my branch. Sorry for the way I labelled the code.

Xiangyu-Hu commented 11 months ago

You have the address for the global variable, so the value of the variable.

ChenxiZhaoTUM commented 11 months ago

The Robin case is tested successfully. I am running ctest on Linux.