// 2 dimensional functions for E / H movement void Hupdate2d(Field &EM, Loss &lass, int t){ // update magnetic field, x direction #pragma omp parallel for for (size_t dx = 0; dx < spacex; dx++){ for (size_t dy = 0; dy < spacey - 1; dy++){ EM.Hx(dx,dy) = lass.HxH(dx,dy) * EM.Hx(dx, dy) - lass.HxE(dx,dy) * (EM.Ez(dx,dy + 1) - EM.Ez(dx,dy)); } } // update magnetic field, y direction #pragma omp parallel for for (size_t dx = 0; dx < spacex - 1; dx++){ for (size_t dy = 0; dy < spacey; dy++){ EM.Hy(dx,dy) = lass.HyH(dx,dy) * EM.Hy(dx,dy) + lass.HyE(dx,dy) * (EM.Ez(dx + 1,dy) - EM.Ez(dx,dy)); } } //return EM; }
// This is the function we writs the bulk of the code in void FDTD(Field EM, const int final_time, const double eps, const int space, Loss lass, std::ofstream& output){ // For magnetic field: // double offset = 0.00005; // for electric field: double offset = 0.05; double loss = 0.0; double Cour = 1.0 / sqrt(2.0); // Relative permittivity for (int dx = 0; dx < space; dx++){ for (int dy = 0; dy < space; dy++){ if (dx > 100 && dx < 150){ lass.EzH(dx, dy) = Cour * eps; lass.EzE(dx, dy) = 1.0; lass.HyH(dx, dy) = 1.0; lass.HyE(dx, dy) = Cour / eps; lass.HxE(dx, dy) = Cour / eps; lass.HxH(dx, dy) = 1.0; /* lass.EzH(dx, dy) = eps / 9.0 /(1.0 - loss); lass.EzE(dx, dy) = (1.0 - loss) / (1.0 + loss); lass.HyH(dx, dy) = (1.0 - loss) / (1.0 + loss); lass.HyE(dx, dy) = (1.0 / eps) / (1.0 + loss); lass.HxE(dx, dy) = (1.0 / eps) / (1.0 + loss); lass.HxH(dx, dy) = (1.0 - loss) / (1.0 + loss); */ } else{ lass.EzH(dx, dy) = Cour * eps; lass.EzE(dx, dy) = 1.0; lass.HyH(dx, dy) = 1.0; lass.HyE(dx, dy) = Cour / eps; lass.HxE(dx, dy) = Cour / eps; lass.HxH(dx, dy) = 1.0; /* lass.EzH(dx, dy) = eps; lass.EzE(dx, dy) = 1.0; lass.HyH(dx, dy) = 1.0; lass.HyE(dx, dy) = (1.0 / eps); lass.HxE(dx, dy) = (1.0 / eps); lass.HxH(dx, dy) = 1.0; */ } } } // Time looping for (int t = 0; t < final_time; t++){ // Linking the final two elements for an ABC for (int da = 0; da < space; da++){ EM.Hy(da,space - 1) = EM.Hy(da,space - 2); EM.Hx(space - 1,da) = EM.Hx(space - 2,da); } // update magnetic field, y direction for (int dx = 0; dx < space - 1; dx++){ for (int dy = 0; dy < space; dy++){ EM.Hy(dx,dy) = lass.HyH(dx,dy) * EM.Hy(dx,dy) + lass.HyE(dx,dy) * (EM.Ez(dx + 1,dy) - EM.Ez(dx,dy)); } } // update magnetic field, x direction for (int dx = 0; dx < space; dx++){ for (int dy = 0; dy < space - 1; dy++){ EM.Hx(dx,dy) = lass.HxH(dx,dy) * EM.Hx(dx, dy) + lass.HxE(dx,dy) * (EM.Ez(dx,dy + 1) - EM.Ez(dx,dy)); } } // Correction to the H field for the TFSF boundary // Hy[49] -= exp(-(t - 40.) * (t - 40.)/100.0) / eps; // Hy[49] -= sin((t-10.0)*0.2)*0.0005; // Linking the first two elements in the electric field for (int dy = 0; dy < space; dy++){ EM.Ez(0,dy) = EM.Ez(1,dy); EM.Ez(space - 1,dy) = EM.Ez(space - 2,dy); } // update electric field for (int dx = 1; dx < space - 1; dx++){ for (int dy = 1; dy < space - 1; dy++){ EM.Ez(dx,dy) = lass.EzE(dx,dy) * EM.Ez(dx,dy) + lass.EzH[dx] * (EM.Hy(dx, dy) - EM.Hy(dx - 1, dy) - EM.Hx(dx,dy) - EM.Hx(dx, dy - 1)); } } // set src for next step for (int dy = 0; dy < space; dy++){ EM.Ez(50,dy) += exp(-((t + 1 - 40.) * (t + 1 - 40.))/100.0); } // EM.Ez[0] = 0; // Ez[50] += sin((t - 10.0 + 1)*0.2)*0.0005; /* if (t > 0){ Ez[50] = sin(0.1 * t) / (0.1 * t); } else{ Ez[50] = 1; } */ if (t % 50 == 0){ for (int dx = 0; dx < space; dx = dx + 50){ for (int dy = 0; dy < space; dy = dy + 50){ output << t << '\t' << dx <<'\t' << dy << '\t' << EM.Ez(dx, dy) << '\n'; //output << Ez(dx,dy) + (t * offset) << '\n'; //output << Hy[dx] + (t * offset) << '\n'; } } output << '\n' << '\n'; } } }
// Creating loss void createloss2d(Loss &lass, double eps, double Cour, double loss){ double radius = 400; int sourcex = 450, sourcex2 = 250; int sourcey = 750, sourcey2 = 100; double dist, var, Q, epsp, mup, dist2, var_old; double cutoff = 1.5; for (size_t dx = 0; dx < spacex; dx++){ for (size_t dy = 0; dy < spacey; dy++){ dist = sqrt((dx - sourcex)*(dx - sourcex) + (dy - sourcey)*(dy - sourcey)); dist2 = sqrt((dx - sourcex2)*(dx - sourcex2) + (dy - sourcey2)*(dy - sourcey2)); // if (dx > 100 && dx < 150 && dy > 75 && dy < 125){ if (dist < radius){ // if (dist > 100000000){ Q = cbrt(-(radius / dist) + sqrt((radius/dist) * (radius/dist) + (1.0/27.0))); var = (Q - (1.0 / (3.0 * Q))) * (Q - (1.0 / (3.0 * Q))); // var = radius / dist; // var = 1.1; if (abs(var - var_old) > cutoff){ var = var_old; } if (var > 10){ var = 10; } if (isnan(var)){ var = var_old; } epsp = eps / (var * var); mup = 1 / (var * var); /* lass.EzH(dx, dy) = Cour * eps; lass.EzE(dx, dy) = 1.0; lass.HyH(dx, dy) = 1.0; lass.HyE(dx, dy) = Cour / eps; lass.HxE(dx, dy) = Cour / eps; lass.HxH(dx, dy) = 1.0; */ lass.EzH(dx, dy) = Cour * epsp /(1.0 - loss); lass.EzE(dx, dy) = (1.0 - loss) / (1.0 + loss); lass.HyH(dx, dy) = (1.0 - loss) / (1.0 + loss); lass.HyE(dx, dy) = Cour * (mup / eps) / (1.0 + loss); lass.HxE(dx, dy) = Cour * (mup / eps) / (1.0 + loss); lass.HxH(dx, dy) = (1.0 - loss) / (1.0 + loss); /* // PEC stuff lass.EzH(dx, dy) = 0; lass.EzE(dx, dy) = 0; lass.HyH(dx, dy) = 0; lass.HyE(dx, dy) = 0; lass.HxE(dx, dy) = 0; lass.HxH(dx, dy) = 0; */ var_old = var; } else{ /* lass.EzH(dx, dy) = Cour * eps; lass.EzE(dx, dy) = 1.0; lass.HyH(dx, dy) = 1.0; lass.HyE(dx, dy) = Cour / eps; lass.HxE(dx, dy) = Cour / eps; lass.HxH(dx, dy) = 1.0; lass.EzH(dx, dy) = Cour * eps /(1.0 - loss); lass.EzE(dx, dy) = (1.0 - loss) / (1.0 + loss); lass.HyH(dx, dy) = (1.0 - loss) / (1.0 + loss); lass.HyE(dx, dy) = Cour * (1.0 / eps) / (1.0 + loss); lass.HxE(dx, dy) = Cour * (1.0 / eps) / (1.0 + loss); lass.HxH(dx, dy) = (1.0 - loss) / (1.0 + loss); */ lass.EzH(dx, dy) = Cour * eps; lass.EzE(dx, dy) = 1.0; lass.HyH(dx, dy) = 1.0; lass.HyE(dx, dy) = Cour * (1.0 / eps); lass.HxE(dx, dy) = Cour * (1.0 / eps); lass.HxH(dx, dy) = 1.0; } } } //return lass; }