gdtk-uq / gdtk

The Gas Dynamics Toolkit (GDTk) is a set of software tools for simulating high speed fluid flow, maintained at The University of Queensland and the University of Southern Queensland, Australia.
https://gdtk.uqcloud.net/
Other
59 stars 15 forks source link

Separating utils #41

Closed Alex-Muirhead closed 6 months ago

Alex-Muirhead commented 8 months ago

The util files are currently intertwined with the ntypes files through the Complex datatype, as sometimes we need to call Lua binding functions with either doubles or Complex!doubles. This also causes a duplication of some Lua bindings & functions. With the potential introduction of Dual numbers as an alternative adjoint computation method, this would introduce more duplication.

This merge proposes the use of limited templates, to remove the duplication, and allow for the util files to be decoupled from specific number types. The trait canCastTo is introduced, which allows compile-time checks if a type can be cast to another, either explicitly (using the cast(T) or to!(T) notation) or implicitly (by sub-typing). This is similar to the traits isFloating and isIntegral, however those cannot be extended to user-defined types.

public template canCastTo(T, U) {
    import std.traits : ReturnType;
    // Check if T can be implicitly or explicitly cast to U
    static if ( is(T : U) || is(ReturnType!(T.opCast!U) == U) ) {
        enum canCastTo = true;
    } else {
        enum canCastTo = false;
    }
}
Alex-Muirhead commented 8 months ago

An unintended side-effect of this trait is that more types implement opCast!double than previously expected, including bool, int, and char. This should be taken into consideration as, while they would store the value correctly (as far as D is concerned), it does introduce duplication of effects for the existing lua_pushinteger and lua_pushboolean (although the former uses type size_t).