// exact solution scalar Fn(double u) { double s, c; fresnl(sqrt(2/M_PI) * u, &s , &c); scalar fres = cplx(c,-s); scalar a = cplx(0.0, M_PI/4); scalar b = cplx(0.0, u*u); return 0.5*sqrt(M_PI) * exp(b) * (exp(-a) - sqrt(2.0)*(fres)); }
scalar der_Hr(double x, double y) { double r = sqrt(x*x + y*y); double t = atan2(y,x); scalar a = cplx(0.0, M_PI/4 - k*r); scalar i = cplx(0.0,1.0); return 1/sqrt(M_PI) * exp(a) * ( (-i*k)*(Fn(sqrt(2*k*r)*sin(t/2 - M_PI/8)) + Fn(sqrt(2*k*r)*sin(t/2 + M_PI/8))) + (Fder(sqrt(2*k*r)*sin(t/2 - M_PI/8))*(sqrt(k)/sqrt(2*r)*sin(t/2 - M_PI/8)) + Fder(sqrt(2*k*r)*sin(t/2 + M_PI/8))*(sqrt(k)/sqrt(2*r)*sin(t/2 + M_PI/8)))); }
scalar Fder(double u) { scalar a = cplx(0.0, M_PI/4); scalar b = cplx(0.0, u*u); scalar d = cplx(0.0, 2.0*u); double s, c; fresnl(sqrt(2/M_PI) * u, &s , &c); scalar fres = cplx(c,-s); scalar fresder = exp(-b); return 0.5*sqrt(M_PI) * exp(b) * ( d * (exp(-a) - sqrt(2.0)*(fres)) - sqrt(2.0)*fresder*sqrt(2.0/M_PI) ); }
scalar Fder2(double u) { scalar a = cplx(0.0, M_PI/4); scalar i = cplx(0.0,1.0); scalar b = cplx(0.0, u*u); scalar d = cplx(0.0, 2.0*u); double s, c; fresnl(sqrt(2/M_PI) * u, &s , &c); scalar fres = cplx(c,-s); scalar fresder = exp(-b); scalar fresder2 = exp(-b)*(-2.0 * i * u); return 2.0 * u * i * Fder(u) + 0.5 * sqrt(M_PI) * exp(b) * ( 2.0 * i * (exp(-a) - sqrt(2.0)*(fres)) + d * (-sqrt(2.0)*fresder*sqrt(2.0/M_PI)) - sqrt(2.0) * fresder2 * sqrt(2.0/M_PI) ); }
Scalar bilinear_form(int n, double *wt, Func<Scalar> *u_ext[], Func<Real> *u, Func<Real> *v, Geom<Real> *e, ExtData<Scalar> *ext) { cplx ikappa = cplx(0.0, kappa); return 1.0/mu_r * int_curl_e_curl_f<Real, Scalar>(n, wt, u, v) - ikappa * sqrt(mu_0 / e_0) * int_F_e_f<Real, Scalar>(n, wt, gam, u, v, e) - sqr(kappa) * int_F_e_f<Real, Scalar>(n, wt, er, u, v, e); }
void process_periods(cplx tau1, cplx tau2) { if (tau1 == cplx(0.0)) { throw std::string("period cannot be zero"); } cplx tau(tau2/tau1); cplx f(1.0/tau1); if (tau.imag() == 0) { throw std::string("degenerate periods"); } if (tau.imag() < 0) { tau = 1.0/tau; f = 1.0/tau2; } tau.real() -= floor(tau.real() + 0.5); while (norm(tau) < 1.0) { tau = -1.0/tau; tau.real() -= floor(tau.real() + 0.5); f *= tau; } f_ = f; f2_ = f*f; tau_ = tau; }
scalar liform_surf(int n, double *wt, Func<scalar> *u_ext[], Func<double> *v, Geom<double> *e, ExtData<scalar> *ext) { cplx ii = cplx(0.0, 1.0); scalar result = 0; for (int i = 0; i < n; i++) { scalar dx[3], dy[3], dz[3]; scalar3 ev; ev[0] = exact(e->x[i], e->y[i], e->z[i], dx, dy, dz)[0]; ev[1] = exact(e->x[i], e->y[i], e->z[i], dx, dy, dz)[0]; ev[2] = exact(e->x[i], e->y[i], e->z[i], dx, dy, dz)[0]; scalar curl_e[3]; calc_curl(dx, dy, dz, curl_e); scalar tpe[3]; calc_tan_proj(e->nx[i], e->ny[i], e->nz[i], ev, tpe); scalar g[3] = { (e->nz[i] * curl_e[1] - e->ny[i] * curl_e[2]) - ii * kappa * tpe[0], (e->nx[i] * curl_e[2] - e->nz[i] * curl_e[0]) - ii * kappa * tpe[1], (e->ny[i] * curl_e[0] - e->nx[i] * curl_e[1]) - ii * kappa * tpe[2], }; // tpv is tangencial projection of v (test function) scalar vv[3] = { v->val0[i], v->val1[i], v->val2[i] }; scalar tpv[3]; calc_tan_proj(e->nx[i], e->ny[i], e->nz[i], vv, tpv); result += wt[i] * (g[0] * tpv[0] + g[1] * tpv[1] + g[2] * tpv[2]); } return result; }
void CooMatrix::copy_into(Matrix *m) { m->free_data(); int index = 0; if (this->complex) { for(std::map<size_t, std::map<size_t, cplx> >::const_iterator it_row = A_cplx.begin(); it_row != A_cplx.end(); ++it_row) { for(std::map<size_t, cplx>::const_iterator it_col = it_row->second.begin(); it_col != it_row->second.end(); ++it_col) { m->add(it_row->first, it_col->first, cplx(it_col->second.real(), it_col->second.imag())); index++; } } } else { for(std::map<size_t, std::map<size_t, double> >::const_iterator it_row = A.begin(); it_row != A.end(); ++it_row) { for(std::map<size_t, double>::const_iterator it_col = it_row->second.begin(); it_col != it_row->second.end(); ++it_col) { m->add(it_row->first, it_col->first, it_col->second); index++; } } } }
scalar der_Hrr(double x, double y) { double r = sqrt(x*x + y*y); double t = atan2(y,x); scalar a = cplx(0.0, M_PI/4 - k*r); scalar i = cplx(0.0,1.0); scalar f1_d = Fder(sqrt(2*k*r)*sin(t/2 - M_PI/8)); scalar f2_d = Fder(sqrt(2*k*r)*sin(t/2 + M_PI/8)); scalar f1_d2 = Fder2(sqrt(2*k*r)*sin(t/2 - M_PI/8)); scalar f2_d2 = Fder2(sqrt(2*k*r)*sin(t/2 + M_PI/8)); scalar b1 = (sqrt(k/(2*r))*sin(t/2 - M_PI/8)); scalar b2 = (sqrt(k/(2*r))*sin(t/2 + M_PI/8)); return -i*k*der_Hr(x,y) + 1/sqrt(M_PI) * exp(a) * ( (-i*k)*(f1_d*b1 + f2_d*b2) + ( f1_d2*b1*b1 + f2_d2*b2*b2) + f1_d*(-0.5*sqrt(k/(2*r*r*r))*sin(t/2 - M_PI/8)) + f2_d*(-0.5*sqrt(k/(2*r*r*r))*sin(t/2 + M_PI/8))); }
scalar exact1(double x, double y, scalar& dx, scalar& dy) { double r = sqrt(x*x + y*y); double theta = atan2(y,x); scalar Hr = der_Hr(x,y); scalar Ht = der_Ht(x,y); scalar i = cplx(0.0,1.0); return i * ( Hr * x/r - Ht * y/(r*r)); }
scalar der_Ht(double x, double y) { double r = sqrt(x*x + y*y); double t = atan2(y,x); scalar a = cplx(0.0, M_PI/4 - k*r); return 1/sqrt(M_PI) * exp(a) * (Fder(sqrt(2*k*r)*sin(t/2 - M_PI/8))*(sqrt(k*r/2)*cos(t/2 - M_PI/8)) + Fder(sqrt(2*k*r)*sin(t/2 + M_PI/8))*(sqrt(k*r/2)*cos(t/2 + M_PI/8))); }
scalar der_Htr(double x, double y) { double r = sqrt(x*x + y*y); double t = atan2(y,x); scalar i = cplx(0.0,1.0); scalar a = cplx(0.0, M_PI/4 - k*r); scalar f1_d = Fder(sqrt(2*k*r)*sin(t/2 - M_PI/8)); scalar f2_d = Fder(sqrt(2*k*r)*sin(t/2 + M_PI/8)); scalar f1_d2 = Fder2(sqrt(2*k*r)*sin(t/2 - M_PI/8)); scalar f2_d2 = Fder2(sqrt(2*k*r)*sin(t/2 + M_PI/8)); scalar b1 = (sqrt(k)/sqrt(2*r)*sin(t/2 - M_PI/8)); scalar b2 = (sqrt(k)/sqrt(2*r)*sin(t/2 + M_PI/8)); scalar c1 = (sqrt(k*r)/sqrt(2.0)*cos(t/2 - M_PI/8)); scalar c2 = (sqrt(k*r)/sqrt(2.0)*cos(t/2 + M_PI/8)); return -i*k*der_Ht(x,y) + 1/sqrt(M_PI) * exp(a) * ((f1_d2*b1*c1 + f2_d2*b2*c2) + f1_d*(0.5*sqrt(k/(2*r))*cos(t/2 - M_PI/8)) + f2_d*(0.5*sqrt(k/(2*r))*cos(t/2 + M_PI/8))); }
t_cplx cplx_cos(t_cplx z) { double a; double b; a = exp(z.i); b = 1.0 / a; a = a / 2.0; b = b/2.0; return (cplx(cos(z.r) * (a + b), - sin(z.r ) * (a - b))); }
void burningship(t_env *env, int x, int y) { size_t i; t_cplx z; t_cplx c; t_cplx a; c.r = env->ptx + ((x - (SCREEN_W / 2)) / env->zoom); c.i = env->pty + ((y - (SCREEN_H / 2)) / env->zoom); z = cplx(0.0, 0.0); a = cplx(c.r, c.i); i = -1; while (++i < env->max_i && mod(z) < 2) { z = cplx(fabs(z.r), fabs(z.i)); z = cplx_add(cplx_mult(z, z), c); } plotpixel(env, x, y, get_color(env, z, a, i)); }
Scalar matrix_form(int n, double *wt, Func<Scalar> *u_ext[], Func<Real> *u, Func<Real> *v, Geom<Real> *e, ExtData<Scalar> *ext) const { cplx ikappa = cplx(0.0, kappa); Scalar result1 = 0; Scalar result2 = 0; for (int i = 0; i < n; i++) result1 += wt[i] * gamma(e->elem_marker, e->x[i], e->y[i]) * (u->val0[i] * conj(v->val0[i]) + u->val1[i] * conj(v->val1[i])); for (int i = 0; i < n; i++) result2 += wt[i] * er(e->elem_marker, e->x[i], e->y[i]) * (u->val0[i] * conj(v->val0[i]) + u->val1[i] * conj(v->val1[i])); return 1.0/mu_r * int_curl_e_curl_f<Real, Scalar>(n, wt, u, v) - ikappa * sqrt(mu_0 / e_0) * result1 - sqr(kappa) * result2; }
void init_from_periods(int n, cplx tau1, cplx tau2) { process_periods(tau1, tau2); cplx q(std::exp(tau_*cplx(0, M_PI))); cplx q2(q*q); /* e1 */ cplx e1(0.0); for (cplx q2k(q2); ; q2k *= q2) { cplx t1(1.0 + q2k); cplx t2(1.0 - q2k); cplx term(q2k*(1.0/(t1*t1) + 1.0/(t2*t2))); if (std::abs(term) <= 1e-10) break; e1 += term; } e1 = (e1*8.0 + 2.0/3)*M_PI*M_PI; /* e2 */ cplx e2(0.0); cplx qk; double s = -1; for (qk = q, s = -1; ; qk *= q, s = -s) { cplx t1(1.0 - qk); cplx term(s*qk/(t1*t1)); if (std::abs(term) <= 1e-10) break; e2 += term; } e2 = (e2*8.0 - 1.0/3)*M_PI*M_PI; /* e3 */ cplx e3(0.0); for (qk = q, s = -1; ; qk *= q, s = -s) { cplx t1(1.0 - s*qk); cplx term(qk/(t1*t1)); if (std::abs(term) <= 1e-10) break; e3 += term; } e3 = (e3*8.0 - 1.0/3)*M_PI*M_PI; init(n, e1, e2, e3); }
t_cplx cplx_pow(t_cplx z, double n) { double h; double lr; double li; double a; h = mod(z); lr = pow(h, n); a = atan2(z.i, z.r); li = n * a; return (cplx(lr * cos(li), lr * sin(li))); }
Scalar J_cranic(int n, double *wt, Func<Scalar> *u_ext[], Func<Real> *u, Func<Real> *v, Geom<Real> *e, ExtData<Scalar> *ext) { scalar ii = cplx(0.0, 1.0); // imaginary unit, ii^2 = -1 Scalar result = 0; Func<Scalar>* psi_prev_newton = u_ext[0]; for (int i = 0; i < n; i++) result += wt[i] * (ii * H * u->val[i] * v->val[i] / time_step - 0.5*H*H/(2*M) * (u->dx[i] * v->dx[i] + u->dy[i] * v->dy[i]) - 0.5*2* G * u->val[i] * psi_prev_newton->val[i] * conj(psi_prev_newton->val[i]) * v->val[i] - 0.5*G * psi_prev_newton->val[i] * psi_prev_newton->val[i] * u->val[i] * v->val[i] - 0.5*.5*M*OMEGA*OMEGA * (e->x[i] * e->x[i] + e->y[i] * e->y[i]) * u->val[i] * v->val[i]); return result; }
CustomWeakFormAcoustics(std::string bdy_newton, double rho, double sound_speed, double omega) : WeakForm(1) { scalar ii = cplx(0.0, 1.0); // Jacobian. add_matrix_form(new WeakFormsH1::DefaultJacobianDiffusion(0, 0, HERMES_ANY, new HermesFunction(1.0/rho), HERMES_SYM)); add_matrix_form(new WeakFormsH1::DefaultMatrixFormVol(0, 0, HERMES_ANY, new HermesFunction(-sqr(omega) / rho / sqr(sound_speed)), HERMES_SYM)); add_matrix_form_surf(new WeakFormsH1::DefaultMatrixFormSurf(0, 0, bdy_newton, new HermesFunction(-ii * omega / rho / sound_speed))); // Residual. add_vector_form(new WeakFormsH1::DefaultResidualDiffusion(0, HERMES_ANY, new HermesFunction(1.0/rho))); add_vector_form(new WeakFormsH1::DefaultResidualVol(0, HERMES_ANY, new HermesFunction(-sqr(omega) / rho / sqr(sound_speed)))); add_vector_form_surf(new WeakFormsH1::DefaultResidualSurf(0, bdy_newton, new HermesFunction(-ii * omega / rho / sound_speed))); };
Scalar CustomWeakFormGPRK::CustomFormMatrixFormVol::matrix_form_rk(int n, double *wt, Func<Scalar> *u_ext[], Func<Real> *u, Func<Real> *v, Geom<Real> *e, ExtData<Scalar> *ext) const { scalar ii = cplx(0.0, 1.0); // imaginary unit, ii^2 = -1 Scalar result = 0; Func<Scalar>* psi_prev_newton = u_ext[0]; for (int i = 0; i < n; i++) result += wt[i] * (h*h/(2*m*ii*h) * (u->dx[i] * v->dx[i] + u->dy[i] * v->dy[i]) + 2*g/(ii*h)* u->val[i] * psi_prev_newton->val[i] * conj(psi_prev_newton->val[i]) * v->val[i] + (g/ii*h) * psi_prev_newton->val[i] * psi_prev_newton->val[i] * u->val[i] * v->val[i] + .5*m*omega*omega/(ii*h) * (e->x[i] * e->x[i] + e->y[i] * e->y[i]) * u->val[i] * v->val[i]); return result; }
scalar MumpsMatrix::get(int m, int n) { _F_ // Find m-th row in the n-th column. int mid = find_position(Ai + Ap[n], Ap[n + 1] - Ap[n], m); // Return 0 if the entry has not been found. if (mid < 0) return 0.0; // Otherwise, add offset to the n-th column and return the value. if (mid >= 0) mid += Ap[n]; #if !defined(H2D_COMPLEX) && !defined(H3D_COMPLEX) return Ax[mid]; #else return cplx(Ax[mid].r, Ax[mid].i); #endif }
CustomWeakForm(double mu_r, double kappa) : WeakForm(1) { cplx ii = cplx(0.0, 1.0); // Jacobian. add_matrix_form(new WeakFormsHcurl::DefaultJacobianCurlCurl(0, 0, HERMES_ANY, 1.0/mu_r)); add_matrix_form(new WeakFormsHcurl::DefaultMatrixFormVol(0, 0, HERMES_ANY, -sqr(kappa))); add_matrix_form_surf(new WeakFormsHcurl::DefaultMatrixFormSurf(0, 0, HERMES_ANY, -kappa*ii)); // Residual. add_vector_form(new WeakFormsHcurl::DefaultResidualCurlCurl(0, HERMES_ANY, 1.0/mu_r)); add_vector_form(new WeakFormsHcurl::DefaultResidualVol(0, HERMES_ANY, -sqr(kappa))); add_vector_form_surf(new WeakFormsHcurl::DefaultResidualSurf(0, HERMES_ANY, -kappa*ii)); add_vector_form_surf(new CustomVectorFormSurf()); };
scalar der_Htt(double x, double y) { double r = sqrt(x*x + y*y); double t = atan2(y,x); scalar a = cplx(0.0, M_PI/4 - k*r); scalar f1_d = Fder(sqrt(2*k*r)*sin(t/2 - M_PI/8)); scalar f2_d = Fder(sqrt(2*k*r)*sin(t/2 + M_PI/8)); scalar f1_d2 = Fder2(sqrt(2*k*r)*sin(t/2 - M_PI/8)); scalar f2_d2 = Fder2(sqrt(2*k*r)*sin(t/2 + M_PI/8)); scalar c1 = (sqrt(k*r/(2))*cos(t/2 - M_PI/8)); scalar c2 = (sqrt(k*r/(2))*cos(t/2 + M_PI/8)); return 1/sqrt(M_PI) * exp(a) * ((f1_d2*c1*c1 + f2_d2*c2*c2) + f1_d*(-0.5*sqrt(k*r/2)*sin(t/2 - M_PI/8)) + f2_d*(-0.5*sqrt(k*r/2)*sin(t/2 + M_PI/8))); }
Scalar F_euler(int n, double *wt, Func<Scalar> *u_ext[], Func<Real> *v, Geom<Real> *e, ExtData<Scalar> *ext) { scalar ii = cplx(0.0, 1.0); // imaginary unit, ii^2 = -1 Scalar result = 0; Func<Scalar>* psi_prev_newton = u_ext[0]; Func<Scalar>* psi_prev_time = ext->fn[0]; for (int i = 0; i < n; i++) result += wt[i] * (ii * H * (psi_prev_newton->val[i] - psi_prev_time->val[i]) * v->val[i] / time_step - H*H/(2*M) * (psi_prev_newton->dx[i] * v->dx[i] + psi_prev_newton->dy[i] * v->dy[i]) - G * psi_prev_newton->val[i] * psi_prev_newton->val[i] * conj(psi_prev_newton->val[i]) * v->val[i] - .5*M*OMEGA*OMEGA * (e->x[i] * e->x[i] + e->y[i] * e->y[i]) * psi_prev_newton->val[i] * v->val[i]); return result; }
t_cplx rings(t_coeff col, double x, double y) { t_cplx z; double r; double theta; double prefix; r = sqrt(x * x + y * y); theta = atan2(y, x); prefix = mod(cplx((r + col.pa2 * col.pa2) , (2.0 * col.pa2 * col.pa2))) - (col.pa2 * col.pa2) + (r * (1.0 - col.pa2 * col.pa2)); z.r = prefix * cos(theta); z.i = prefix * sin(theta); return (z); }
void fcmul(cplxfloat_t *c, const cplxfloat_t *a, const cplxfloat_t *b, unsigned int d1, unsigned int d2, unsigned int d3) { cplxfloat_t *r = c, s; unsigned int i, j, k; if (c == a || c == b) r = alloca(d1 * d3 * sizeof(r[0])); for (i = 0; i < d1; i++) for (k = 0; k < d3; k++) { cplx(s, 0, 0); for (j = 0; j < d2; j++) cmac(s, a[i*d2+j], b[j*d3+k]); r[i*d3+k] = s; } if (r != c) memcpy(c, r, d1 * d3 * sizeof(c[0])); }
scalar linear_form_surf(int n, double *wt, Func<double> *v, Geom<double> *e, ExtData<scalar> *ext) { scalar result = 0; for (int i = 0; i < n; i++) { double r = sqrt(e->x[i] * e->x[i] + e->y[i] * e->y[i]); double theta = atan2(e->y[i], e->x[i]); if (theta < 0) theta += 2.0*M_PI; double j13 = jv(-1.0/3.0, r), j23 = jv(+2.0/3.0, r); double cost = cos(theta), sint = sin(theta); double cos23t = cos(2.0/3.0*theta), sin23t = sin(2.0/3.0*theta); double Etau = e->tx[i] * (cos23t*sint*j13 - 2.0/(3.0*r)*j23*(cos23t*sint + sin23t*cost)) + e->ty[i] * (-cos23t*cost*j13 + 2.0/(3.0*r)*j23*(cos23t*cost - sin23t*sint)); result += wt[i] * cplx(cos23t*j23, -Etau) * ((v->val0[i] * e->tx[i] + v->val1[i] * e->ty[i])); } return result; }
scalar biform_surf(int n, double *wt, Func<scalar> *u_ext[], Func<double> *u, Func<double> *v, Geom<double> *e, ExtData<scalar> *ext) { // j * kappa * E_T * F_T // E_T = nu x E x nu (nu is outer normal) cplx ii = cplx(0.0, 1.0); scalar result = 0; for (int i = 0; i < n; i++) { scalar uu[3] = { u->val0[i], u->val1[i], u->val2[i] }; scalar tpu[3]; calc_tan_proj(e->nx[i], e->ny[i], e->nz[i], uu, tpu); scalar vv[3] = { v->val0[i], v->val1[i], v->val2[i] }; scalar tpv[3]; calc_tan_proj(e->nx[i], e->ny[i], e->nz[i], vv, tpv); result += wt[i] * (uu[0] * vv[0] + uu[1] * vv[1] + uu[2] * vv[2]); } return ii * (-kappa) * result; }
static void exact_sol(double x, double y, scalar& u0, scalar& u1, scalar& u1dx, scalar& u0dy) { scalar dx,dy; u0 = exact0(x,y,dx,dy); u1 = exact1(x,y,dx,dy); scalar Hr = der_Hr(x,y); scalar Ht = der_Ht(x,y); scalar Hrr = der_Hrr(x,y); scalar Hrt = der_Hrt(x,y); scalar Htr = der_Htr(x,y); scalar Htt = der_Htt(x,y); double r = sqrt(x*x + y*y); double theta = atan2(y,x); scalar i = cplx(0.0,1.0); u1dx = i * (( Hrr * x/r + Hrt * (-y/(r*r))) * x/r + Hr * (y*y)/(r*r*r) - ((Htr * x/r + Htt * (-y/(r*r))) * y/(r*r) + Ht * (-2.0*x*y/(r*r*r*r)))); u0dy = -i * (( Hrr * y/r + Hrt * x/(r*r)) * y/r + Hr * (x*x)/(r*r*r) + (Htr * y/r + Htt * x/(r*r)) * x/(r*r) + Ht * (-2.0*x*y/(r*r*r*r))); }
t_cplx fan(t_coeff col, double x, double y) { double r; double theta; double t; t_cplx z; r = sqrt(x * x + y * y); theta = atan2(y, x); t = M_PI * col.cc * col.cc; if (mod(cplx(theta, t)) > (t * 0.5)) { z.r = r * cos(theta - (t * .5)); z.i = r * sin(theta - (t * .5)); } else { z.r = r * cos(theta + (t * 0.5)); z.i = r * sin(theta + (t * 0.5)); } return (z); }