Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/include/dim.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ struct Invar
return {!InvarX::value, !InvarY::value, !InvarZ::value};
}

static bool is_invar(int dim)
// TODO c++20 make consteval?
static constexpr bool is_invar(int dim)
{
switch (dim) {
case 0: return InvarX::value;
Expand Down
12 changes: 9 additions & 3 deletions src/include/rng.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ struct Uniform

Real get() { return dist(*gen); }

Real get(Real min_override, Real max_override)
{
return std::uniform_real_distribution<Real>(min_override,
max_override)(*gen);
}

private:
detail::GeneratorPtr gen;
std::uniform_real_distribution<Real> dist;
Expand All @@ -81,11 +87,11 @@ struct Normal
Normal() : Normal(0, 1) {}

Real get() { return dist(*gen); }
// FIXME remove me, or make standalone func
Real get(Real mean, Real stdev)

Real get(Real mean_override, Real stdev_override)
{
// should be possible to pass params to existing dist
return std::normal_distribution<Real>(mean, stdev)(*gen);
return std::normal_distribution<Real>(mean_override, stdev_override)(*gen);
}

private:
Expand Down
85 changes: 85 additions & 0 deletions src/kg/include/kg/Vec3.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "cuda_compat.h"
#include <kg/Macros.h>
#include <psc/gtensor.h>
#include "psc_bits.h"

namespace kg
{
Expand Down Expand Up @@ -102,6 +103,72 @@ struct Vec : gt::sarray<T, N>
return *this;
}

KG_INLINE Vec& operator/=(T s)
{
for (size_t i = 0; i < N; i++) {
(*this)[i] /= s;
}
return *this;
}

KG_INLINE T sum() const
{
T sum{0};
for (size_t i = 0; i < N; i++) {
sum += (*this)[i];
}
return sum;
}

KG_INLINE T prod() const
{
T prod{1};
for (size_t i = 0; i < N; i++) {
prod *= (*this)[i];
}
return prod;
}

KG_INLINE T dot(const Vec& w) const
{
T sum{0};
for (size_t i = 0; i < N; i++) {
sum += (*this)[i] * w[i];
}
return sum;
}

KG_INLINE Vec cross(const Vec& w) const
{
return {(*this)[1] * w[2] - (*this)[2] * w[1],
(*this)[2] * w[0] - (*this)[0] * w[2],
(*this)[0] * w[1] - (*this)[1] * w[0]};
}

KG_INLINE T mag2() const { return this->dot(this); }

KG_INLINE T mag() const { return sqrt(this->mag2()); }

KG_INLINE T max() const
{
auto max = (*this)[0];
for (size_t i = 1; i < N; i++) {
max = std::max(max, (*this)[i]);
}
return max;
}

KG_INLINE Vec inv() const { return T(1) / *this; }

KG_INLINE Vec<int, N> fint() const
{
Vec<int, N> res;
for (size_t i = 0; i < N; i++) {
res[i] = ::fint((*this)[i]);
}
return res;
}

// conversion to pointer

KG_INLINE operator const T*() const { return this->data(); }
Expand Down Expand Up @@ -153,6 +220,24 @@ KG_INLINE Vec<T, N> operator*(T s, const Vec<T, N>& v)
return res;
}

template <typename T, std::size_t N>
KG_INLINE Vec<T, N> operator/(T s, const Vec<T, N>& v)
{
Vec<T, N> res;
for (size_t i = 0; i < N; i++) {
res[i] = s / v[i];
}
return res;
}

template <typename T, std::size_t N>
KG_INLINE Vec<T, N> operator/(const Vec<T, N>& v, T s)
{
Vec<T, N> res = v;
res /= s;
return res;
}

template <typename T, std::size_t N>
KG_INLINE Vec<T, N> operator/(const Vec<T, N>& v, const Vec<T, N>& w)
{
Expand Down
8 changes: 3 additions & 5 deletions src/libpsc/psc_push_particles/push_particles_1vb.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct PushParticlesVb
static void push_mprts(Mparticles& mprts, MfieldsState& mflds)
{
const auto& grid = mprts.grid();
Real3 dxi = Real3{1., 1., 1.} / Real3(grid.domain.dx);
Real3 dxi = Real3(grid.domain.dx).inv();
real_t dq_kind[MAX_NR_KINDS];
auto& kinds = grid.kinds;
assert(kinds.size() <= MAX_NR_KINDS);
Expand Down Expand Up @@ -60,10 +60,8 @@ struct PushParticlesVb
auto v = advance.calc_v(prt.u());
advance.push_x(prt.x(), v);

Int3 final_index;
Real3 final_pos_normalized;
find_idx_pos_1st_rel(prt.x(), dxi, final_index, final_pos_normalized,
real_t(0.));
Real3 final_pos_normalized = prt.x() * dxi;
Int3 final_index = final_pos_normalized.fint();

// CURRENT DENSITY BETWEEN (n+.5)*dt and (n+1.5)*dt
int lg[3];
Expand Down