void deriv_yy(const ScalarField<T, S>& f, ScalarField<S, S>& out) { const GridParams& p = f.grid_params(); assert(p == out.grid_params()); assert(p.ny >= 3); const double d = 1./(p.eps_y*p.eps_y); for (int i = 0; i < p.nx; ++i) { for (int j = 0; j < p.ny; ++j) { T a, b, c; if (j == 0) { a = f(i,j); b = f(i,j+1); c = f(i,j+2); } else if (j == p.ny-1) { a = f(i,j-2); b = f(i,j-1); c = f(i,j); } else { a = f(i,j-1); b = f(i,j); c = f(i,j+1); } out(i,j) = (a - 2.*b + c) * d; } } }
void deriv_xx(const ScalarField<T, S>& f, ScalarField<S, S>& out) { const GridParams& p = f.grid_params(); assert(p == out.grid_params()); assert(p.nx >= 3); const double d = 1./(p.eps_x*p.eps_x); for (int i = 0; i < p.nx; ++i) { for (int j = 0; j < p.ny; ++j) { T a, b, c; if (i == 0) { a = f(i,j); b = f(i+1,j); c = f(i+2,j); } else if (i == p.nx-1) { a = f(i-2,j); b = f(i-1,j); c = f(i,j); } else { a = f(i-1,j); b = f(i,j); c = f(i+1,j); } out(i,j) = (a - 2.*b + c) * d; } } }
void apply_flow(const ScalarField<ElemT, ExprT>& phi, const DoubleField& u_x, const DoubleField& u_y, ScalarField<ExprT, ExprT>& out) { const GridParams& gp = phi.grid_params(); assert(u_x.grid_params() == gp && u_y.grid_params() == gp && out.grid_params() == gp); for (int i = 0; i < gp.nx; ++i) { for (int j = 0; j < gp.ny; ++j) { auto xy = gp.to_xy(i, j); out(i,j) = phi.eval_xy(xy.first - u_x(i,j), xy.second - u_y(i,j)); } } }
void deriv_y(const ScalarField<T, S>& f, ScalarField<S, S>& g_y) { const GridParams& p = f.grid_params(); assert(p == g_y.grid_params()); // derivatives in y direction for (int i = 0; i < p.nx; ++i) { for (int j = 0; j < p.ny - 1; ++j) { g_y(i,j) = (f(i,j+1) - f(i,j)) * (1./p.eps_y); } // copy over last column g_y(i,p.ny-1) = g_y(i,p.ny-2); } }
void deriv_x(const ScalarField<T, S>& f, ScalarField<S, S>& g_x) { const GridParams& p = f.grid_params(); assert(p == g_x.grid_params()); // derivatives in x direction for (int i = 0; i < p.nx - 1; ++i) { for (int j = 0; j < p.ny; ++j) { g_x(i,j) = (f(i+1,j) - f(i,j)) * (1./p.eps_x); } } // copy over last row for (int j = 0; j < p.ny; ++j) { g_x(p.nx-1,j) = g_x(p.nx-2,j); } }
void deriv_y_central(const ScalarField<T, S>& f, ScalarField<S, S>& g_y) { const GridParams& p = f.grid_params(); assert(p == g_y.grid_params()); // derivatives in y direction // forward/backward differences for edges, central for everything else for (int i = 0; i < p.nx; ++i) { for (int j = 0; j < p.ny; ++j) { if (j == 0) { g_y(i,j) = (f(i,j+1) - f(i,j)) * (1./p.eps_y); } else if (j == p.ny-1) { g_y(i,j) = (f(i,j) - f(i,j-1)) * (1./p.eps_y); } else { g_y(i,j) = (f(i,j+1) - f(i,j-1)) * (1./(2.*p.eps_y)); } } } }