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

277 switch between double and float #296

Closed ZhenxiZhao closed 11 months ago

ZhenxiZhao commented 11 months ago

switch precision TODO: simbody and eigen

DrChiZhang commented 11 months ago

@ZhenxiZhao I have went through the modifications you have made in this pull request, and they are seems OK. Would you please update me of the existing issues of this improvement?

ZhenxiZhao commented 11 months ago

@ZhenxiZhao I have went through the modifications you have made in this pull request, and they are seems OK. Would you please update me of the existing issues of this improvement?

All the constants in the project (not variable, for instance, 1.0) should be rewite as Real(1.0) or ::One(). One related error is shown below,

/home/SPHinXsys/SPHINXsys/src/shared/materials/riemann_solver.cpp:30:77: error: no matching function for call to ‘SMAX(SPH::Real, double)’
   30 |   return rho0c0_geo_ave_ * u_jump * SMIN(3.0 * SMAX(u_jump * inv_c_ave_, 0.0), 1.0);

There are many cast type conversion in the project, for instance,

    template <class DampingAlgorithmType>
    bool DampingWithRandomChoice<DampingAlgorithmType>::RandomChoice()
    {
        return ((double)rand() / (RAND_MAX)) < random_ratio_ ? true : false;
    }

Besides, the words "SimTk::xxx" should be replaced by "SimTKxxx", for instance, on line 43, solid_body_supplementary.cpp

            body_part_volume * solid_body_density_, SimTK::Vec3(0), SimTK::UnitInertia(Ix, Iy, Iz));
DrChiZhang commented 11 months ago

Note that with clang and clang++ compiler, non-constexpr function 'sqrt' cannot be used in a constant expression, I will fix this later.

Xiangyu-Hu commented 11 months ago

@ZhenxiZhao I have went through the modifications you have made in this pull request, and they are seems OK. Would you please update me of the existing issues of this improvement?

All the constants in the project (not variable, for instance, 1.0) should be rewite as Real(1.0) or ::One(). One related error is shown below,

/home/SPHinXsys/SPHINXsys/src/shared/materials/riemann_solver.cpp:30:77: error: no matching function for call to ‘SMAX(SPH::Real, double)’
   30 |   return rho0c0_geo_ave_ * u_jump * SMIN(3.0 * SMAX(u_jump * inv_c_ave_, 0.0), 1.0);

There are many cast type conversion in the project, for instance,

  template <class DampingAlgorithmType>
  bool DampingWithRandomChoice<DampingAlgorithmType>::RandomChoice()
  {
      return ((double)rand() / (RAND_MAX)) < random_ratio_ ? true : false;
  }

Besides, the words "SimTk::xxx" should be replaced by "SimTKxxx", for instance, on line 43, solid_body_supplementary.cpp

          body_part_volume * solid_body_density_, SimTK::Vec3(0), SimTK::UnitInertia(Ix, Iy, Iz));

Yes. Seems that we need do it.

Xiangyu-Hu commented 11 months ago

Actually, using replace function in VS code, such replacement is not hard.

DrChiZhang commented 11 months ago

First Step: test with Real = double by doing the following steps. 1, DoubleVec -> BiVector 2, TripleVec -> TriVector 3, CaculateDoubleDotProduct -> CaculateBiDotProduct (clean the variable name with "double", and change all double to Real in code) 4, double -> Real 5, discards changes in polar_docompostion.

Xiangyu-Hu commented 11 months ago

@ZhenxiZhao could you pull from the main repository to solve the conflicts?

DrChiZhang commented 11 months ago

@ZhenxiZhao @Xiangyu-Hu I have moved the modification of branch "[ZhenxiZhao:277-switch-between-double-and-float]" to branch"[Xiangyu-Hu:277-switch-between-double-and-float]" , which we can continue this implementation.

DrChiZhang commented 11 months ago

@Xiangyu-Hu Do I have the access to push to this branch "[Xiangyu-Hu:277-switch-between-double-and-float]" ?

DrChiZhang commented 11 months ago

@ZhenxiZhao I encounter this error after setting "SimTk::xxx" replaced by "SimTKxxx"

Screenshot 2023-05-29 at 16 52 47

According to Simbody documentation

Screenshot 2023-05-29 at 16 57 15

Does this mean that we should build Simbody with float as the default precision or add another transfer form float vector to double vector there ?

ZhenxiZhao commented 11 months ago

@ZhenxiZhao I encounter this error after setting "SimTk::xxx" replaced by "SimTKxxx" Screenshot 2023-05-29 at 16 52 47

According to Simbody documentation

Screenshot 2023-05-29 at 16 57 15

Does this mean that we should build Simbody with float as the default precision or add another transfer form float vector to double vector there ?

Yes, I attenmpt to use the fVec or Vec explicitly, since the type is pre-defined before compling simbody in vcpkg. However, the class or struct in simbody use Vec, which has been pre-defined as double. Thus we encounter this error. I think if we can dual compile simbody to simply implement the type switch, one for float, one for double.

DrChiZhang commented 11 months ago

@ZhenxiZhao I encounter this error after setting "SimTk::xxx" replaced by "SimTKxxx" Screenshot 2023-05-29 at 16 52 47 According to Simbody documentation

Screenshot 2023-05-29 at 16 57 15

Does this mean that we should build Simbody with float as the default precision or add another transfer form float vector to double vector there ?

Yes, I attenmpt to use the fVec or Vec explicitly, since the type is pre-defined before compling simbody in vcpkg. However, the class or struct in simbody use Vec, which has been pre-defined as double. Thus we encounter this error. I think if we can dual compile simbody to simply implement the type switch, one for float, one for double.

That would introduce extra efforts for the installation. I am thinking that we can use double precision in SIMBODY, while transfer single precision of Eigen vector to double vector of SIMBODY if needed.

FabienPean-Virtonomy commented 11 months ago

I am thinking that we can use double precision in SIMBODY, while transfer single precision of Eigen vector to double vector of SIMBODY if needed.

I agree with this. Simbody needs far less state than a SPH, so preserving double there wouldn't have a big impact and would preserve FP accuracy. Also doing so does not increase the coupling between the two libraries.

ideally use of Simbody is moved to an optional module (i.e. CMake target based with a user-facing option)

DrChiZhang commented 11 months ago

@FabienPean-Virtonomy Thanks for the suggestion.

DrChiZhang commented 11 months ago

@Xiangyu-Hu @ZhenxiZhao Implementation done, while the following tests fails when using float due to the large error.

10:test_2d_dambreak_python 13:test_2d_elastic_gate 16:test_2d_eulerian_taylor_green 30:test_2d_self_contact 31:test_2d_shell 43:test_2d_oscillating_beam_UL 46:test_2d_stfb 47:test_2d_stlw 53:test_vector_functions_particle_relaxation 58:bernoulli_beam_struct_sim 76:test_3d_passive_cantilever 79:test_3d_roof 80:roof_under_self_weight.dp_4 83:test_3d_roof_parametric_cvt 90:test_3d_twisting_column

Xiangyu-Hu commented 11 months ago

this branch stop to use as another branch on the same function has been merged.