From f8420e0a46e1069220dcd642bca30c3fcb8c1b91 Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Sat, 2 Apr 2016 13:00:29 +0200 Subject: Don't update xsections if unnecessary. --- src/physics.c | 57 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 21 deletions(-) (limited to 'src/physics.c') diff --git a/src/physics.c b/src/physics.c index 3981258..04c655c 100644 --- a/src/physics.c +++ b/src/physics.c @@ -34,6 +34,8 @@ void phy_sim_reset(phy_sim *sim) sim->time = 0; sim->running = false; + + sim->frame_index = get_time(); } int phy_sim_create(phy_sim *sim) @@ -45,8 +47,8 @@ int phy_sim_create(phy_sim *sim) fi = &sim->field_info; - fi->width = 100; - fi->height = 100; + fi->width = 50; + fi->height = 50; fi->depth = 50; fi->spacing = 1.5e10f / max3(fi->width, fi->height, fi->depth); sim->time_delta = 0.1; @@ -171,45 +173,58 @@ void phy_sim_step_H(phy_field_info *fi, float *mul_const, float dt, } } -void phy_sim_add_sources(phy_sim *sim) +void phy_sim_add_sources_E(phy_sim *sim) { phy_field_info *fi = &sim->field_info; float *E; size_t x, y, z; - for (z = 1; z < fi->depth - 1; z++) - for (y = 15; y < fi->height - 15; y++) - for (x = 1; x < fi->width - 1; x++) { - float s; + x = fi->width / 2; + y = fi->height / 2; + z = fi->depth / 2; - E = sim->fields[2].E + z * fi->zstr + y * fi->ystr + x * fi->xstr; + E = sim->fields[2].E + z * fi->zstr + y * fi->ystr + x * fi->xstr; + E[0] += exp(-0.1f * pow(sim->time - 5.0f, 2)) * 5; +} - s = exp(-600.0f * pow((float)x / fi->width - 0.15f, 2)); - s *= sin(sim->time); - //s *= exp(-2.0f * pow(sim->time - 2.0f, 2)) * 1000; - E[0] += s; - } +void phy_sim_add_sources_H(phy_sim *sim) +{ + phy_field_info *fi = &sim->field_info; + float *H; + size_t x, y, z; + + x = fi->width / 2; + y = fi->height / 2; + z = fi->depth / 2; + + H = sim->fields[2].H + z * fi->zstr + y * fi->ystr + x * fi->xstr; + H[1] -= exp(-0.1f * pow(sim->time - 4.5f, 2)) * 5; } void phy_sim_step(phy_sim *sim) { phy_field_info *fi = &sim->field_info; phy_field_em *fields = sim->fields, tmp; - // note: only pointers are rotated, not the entire fields + + phy_sim_step_E(fi, sim->field_mu, sim->time_delta, + fields[2].E, fields[0].E, fields[1].H); + phy_sim_add_sources_E(sim); + phy_sim_step_H(fi, sim->field_eps, sim->time_delta, + fields[2].H, fields[0].H, fields[1].E); + phy_sim_add_sources_H(sim); + SDL_mutexP(sim->rotate_lock); + + // note: only pointers are rotated, not the entire fields memcpy(&tmp, fields, sizeof(phy_field_em)); memcpy(fields, fields + 1, sizeof(phy_field_em)); memcpy(fields + 1, fields + 2, sizeof(phy_field_em)); memcpy(fields + 2, &tmp, sizeof(phy_field_em)); - SDL_mutexV(sim->rotate_lock); + sim->time += sim->time_delta; - phy_sim_step_H(fi, sim->field_eps, sim->time_delta, - fields[2].H, fields[0].H, fields[1].E); - phy_sim_step_E(fi, sim->field_mu, sim->time_delta, - fields[2].E, fields[0].E, fields[1].H); - phy_sim_add_sources(sim); + sim->frame_index = get_time(); - sim->time += sim->time_delta; + SDL_mutexV(sim->rotate_lock); } void phy_run_command(phy_sim *sim, int number, void *data) -- cgit