static void idle_func ( void ) { get_from_UI ( dens_prev, u_prev, v_prev ); vel_step ( N, u, v, u_prev, v_prev, visc, dt ); dens_step ( N, dens, dens_prev, u, v, diff, dt ); glutSetWindow ( win_id ); glutPostRedisplay (); }
static void idle_func ( void ) { get_from_UI ( dens_prev, u_prev, v_prev ); int idxX = N / 2; int idxY = 1; v[IX(idxX, idxY)] = force * 0.09f; vel_step ( N, u, v, u_prev, v_prev, visc, dt ); dens_step ( N, dens, dens_prev, u, v, diff, dt ); glutSetWindow ( win_id ); glutPostRedisplay (); }
//----------------------------------------------------------------------------- void StamFluidSolver::updatePhysics ( ) { /* //way to track FPS without having to modify main.cc so that extern float fpsReal is not necessary. const char* buf = Con::getVariable("fps::real"); float fps = dAtof(buf); Con::warnf("FPS: %f", fps); */ //get_from_UI ( dens_prev, u_prev, v_prev ); // NOTE that this used to be called before for(int i = 0; i < 5; i++) { int idx = _touchLocations[i]; if(idx > -1 && idx < _gridCellsCount) { dens[idx] = 1.0f; } } // This code is a loop that was copied from get_from_UI /* for (int i=0 ; i<_gridCellsCount ; i++ ) { u[i] = v[i] = 0.0f; //r_prev[i] = g_prev[i] = b_prev[i] = 0.0f; } */ vel_step ( u, v, u_prev, v_prev, visc, dt ); if(!mTextureMorph) { // This code is a loop that was copied from get_from_UI for (int i=0 ; i<_gridCellsCount ; i++ ) { dens[i] *= 0.9f; } for (int i=0 ; i<_gridCellsCount ; i++ ) { dens_prev[i] *= 0.9f; } dens_step ( dens, dens_prev, u, v, diff, dt ); } /*if(mSimulateTemperature) texture_step ( N, temp, temp_prev, u, v, diff, dt );*/ if(mTextureMorph) { texture_step ( texU, texU_prev, u, v, diff, dt ); texture_step (texV, texV_prev, u, v, 0, dt ); } }
int main(int argc, const char * argv[]) { int i; int Nx = 128; int Ny = 256; int center_x = 64; int center_y = 128; int frames = 8; float dt = 0.1f; float visc = 0.001f; float *u, *v; // allocate data int size = (Nx+2)*(Ny+2); u = calloc(size, sizeof(float)); v = calloc(size, sizeof(float)); if (!u || !v) printf("Cannot allocate data\n"); file = fopen("output.txt","w"); fprintf(file, "%d %d %d %d %d\n", frames, Nx, Ny, center_x, center_y); start_solver((Nx+2)*(Ny+2)); v[IX(center_x, center_y)] = 100.0; vel_step(Nx, Ny, u, v, visc, dt); vel_step(Nx, Ny, u, v, visc, dt); vel_step(Nx, Ny, u, v, visc, dt); vel_step(Nx, Ny, u, v, visc, dt); for (i=0;i<frames;i++) { vel_step(Nx, Ny, u, v, visc, dt); vel_step(Nx, Ny, u, v, visc, dt); vel_step(Nx, Ny, u, v, visc, dt); vel_step(Nx, Ny, u, v, visc, dt); print_vel(u, v, Nx, Ny); } end_solver(); fclose(file); return 0; }
/////////////////////////////////////////////////////////////////////////////// /// FFD routine for GLUT idle callback /// ///\param para Pointer to FFD parameters ///\param var Pointer to all variables ///\param BINDEX Pointer to bounary index /// ///\return No return needed /////////////////////////////////////////////////////////////////////////////// void ffd_idle_func(PARA_DATA *para, REAL **var, int **BINDEX) { // Get the display in XY plane get_xy_UI(para, var, (int)para->geom->kmax/2); vel_step(para, var, BINDEX); den_step(para, var, BINDEX); temp_step(para, var, BINDEX); if(para->outp->cal_mean == 1) average_time(para, var); // Update the visualization results after a few tiem steps // to save the time for visualization if(para->mytime->step_current%para->outp->tstep_display==0) { glutSetWindow(para->outp->win_id); glutPostRedisplay( ); } timing(para); } // End of ffd_idle_func()
/////////////////////////////////////////////////////////////////////////////// /// FFD solver /// ///\param para Pointer to FFD parameters ///\param var Pointer to FFD simulation variables ///\param BINDEX Pointer to boundary index /// ///\return 0 if no error occurred /////////////////////////////////////////////////////////////////////////////// int FFD_solver(PARA_DATA *para, REAL **var, int **BINDEX) { int step_total = para->mytime->step_total; REAL t_steady = para->mytime->t_steady; double t_cosim; int flag, next; if(para->solv->cosimulation == 1) t_cosim = para->mytime->t + para->cosim->modelica->dt; /*************************************************************************** | Solver Loop ***************************************************************************/ next = 1; while(next==1) { //------------------------------------------------------------------------- // Integration //------------------------------------------------------------------------- flag = vel_step(para, var, BINDEX); if(flag != 0) { ffd_log("FFD_solver(): Could not solve velocity.", FFD_ERROR); return flag; } else if(para->outp->version==DEBUG) ffd_log("FFD_solver(): solved velocity step.", FFD_NORMAL); flag = temp_step(para, var, BINDEX); if(flag != 0) { ffd_log("FFD_solver(): Could not solve temperature.", FFD_ERROR); return flag; } else if(para->outp->version==DEBUG) ffd_log("FFD_solver(): solved temperature step.", FFD_NORMAL); flag = den_step(para, var, BINDEX); if(flag != 0) { ffd_log("FFD_solver(): Could not solve trace substance.", FFD_ERROR); return flag; } else if(para->outp->version==DEBUG) ffd_log("FFD_solver(): solved density step.", FFD_NORMAL); timing(para); //------------------------------------------------------------------------- // Process for Cosimulation //------------------------------------------------------------------------- if(para->solv->cosimulation == 1) { /*....................................................................... | Conditon 1: If synchronization point is reached, | Action: Do data exchange .......................................................................*/ if(fabs(para->mytime->t - t_cosim)<SMALL) { if(para->outp->version==DEBUG) ffd_log("FFD_solver(): Cosimulation, reached synchronization point", FFD_NORMAL); // Average the FFD simulation data flag = average_time(para, var); if(flag != 0) { ffd_log("FFD_solver(): Could not average the data over time.", FFD_ERROR); return flag; } // the data for cosimulation flag = read_cosim_data(para, var, BINDEX); if(flag != 0) { ffd_log("FFD_solver(): Could not read cosimulation data.", FFD_ERROR); return flag; } flag = write_cosim_data(para, var); if(flag != 0) { ffd_log("FFD_solver(): Could not write cosimulation data.", FFD_ERROR); return flag; } sprintf(msg, "ffd_solver(): Synchronized data at t=%f[s]\n", para->mytime->t); ffd_log(msg, FFD_NORMAL); // Set the next synchronization time t_cosim += para->cosim->modelica->dt; // Reset all the averaged data to 0 flag = reset_time_averaged_data(para, var); if(flag != 0) { ffd_log("FFD_solver(): Could not reset averaged data.", FFD_ERROR); return flag; } /*....................................................................... | Check if Modelica asks to stop the simulation .......................................................................*/ if(para->cosim->para->flag==0) { // Stop the solver next = 0; sprintf(msg, "ffd_solver(): Received stop command from Modelica at " "FFD time: %f[s], Modelica Time: %f[s].", para->mytime->t, para->cosim->modelica->t); if(para->mytime->t==para->cosim->modelica->t) ffd_log(msg, FFD_NORMAL); else ffd_log(msg, FFD_WARNING); } continue; } // End of Conditon 1 /*....................................................................... | Conditon 2: synchronization point is not reached , | but already miss the synchronization point | Action: Stop simulation .......................................................................*/ else if(para->mytime->t-t_cosim>SMALL) { sprintf(msg, "ffd_solver(): Mis-matched synchronization step with " "t_ffd=%f[s], t_cosim=%f[s], dt_syn=%f[s], dt_ffd=%f[s].", para->mytime->t, t_cosim, para->cosim->modelica->dt, para->mytime->dt); ffd_log(msg, FFD_ERROR); sprintf(msg, "para->mytime->t - t_cosim=%lf", para->mytime->t - t_cosim); ffd_log(msg, FFD_ERROR); return 1; } // end of Conditon 2 /*....................................................................... | Conditon 3: synchronization point is not reached | and not miss the synchronization point | Action: Do FFD internal simulation and add data for future average .......................................................................*/ else { if(para->outp->version==DEBUG) ffd_log("FFD_solver(): Cosimulation, prepare next step for FFD", FFD_NORMAL); // Integrate the data on the boundary surface flag = surface_integrate(para, var, BINDEX); if(flag != 0) { ffd_log("FFD_solver(): " "Could not average the data on boundary.", FFD_ERROR); return flag; } else if (para->outp->version==DEBUG) ffd_log("FFD_solver(): completed surface integration", FFD_NORMAL); flag = add_time_averaged_data(para, var); if(flag != 0) { ffd_log("FFD_solver(): " "Could not add the averaged data.", FFD_ERROR); return flag; } else if (para->outp->version==DEBUG) ffd_log("FFD_solver(): completed time average", FFD_NORMAL); } // End of Condition 3 } // End of cosimulation //------------------------------------------------------------------------- // Process for single simulation //------------------------------------------------------------------------- else { if(para->outp->version==DEBUG) ffd_log("FFD_solver(): Single Simulation, prepare for next time step", FFD_NORMAL); // Start to record data for calculating mean velocity if needed if(para->mytime->t>t_steady && para->outp->cal_mean==0) { para->outp->cal_mean = 1; flag = reset_time_averaged_data(para, var); if(flag != 0) { ffd_log("FFD_solver(): Could not reset averaged data.", FFD_ERROR); return flag; } else ffd_log("FFD_solver(): Start to calculate mean properties.", FFD_NORMAL); } if(para->outp->cal_mean==1) { flag = add_time_averaged_data(para, var); if(flag != 0) { ffd_log("FFD_solver(): Could not add the averaged data.", FFD_ERROR); return 1; } } next = para->mytime->step_current < step_total ? 1 : 0; } } // End of While loop return flag; } // End of FFD_solver( )
void Fluid::step(float dt) { vel_step(dt); dens_temp_step(dt); }
/////////////////////////////////////////////////////////////////////////////// /// Set default initial values for simulation variables /// ///\param para Pointer to FFD parameters ///\param var Pointer to FFD simulation variables ///\param BINDEX Pointer to boundary index /// ///\return 0 if no error occurred /////////////////////////////////////////////////////////////////////////////// int set_initial_data(PARA_DATA *para, REAL **var, int **BINDEX) { int i, j; int size = (para->geom->imax+2)*(para->geom->jmax+2)*(para->geom->kmax+2); int flag = 0; para->mytime->t = 0.0; para->mytime->step_current = 0; para->outp->cal_mean = 0; /**************************************************************************** | Set inital value for FFD variables ****************************************************************************/ for(i=0; i<size; i++) { var[GX][i] = 0.0; var[GY][i] = 0.0; var[GZ][i] = 0.0; var[VX][i] = para->init->u; var[VY][i] = para->init->v; var[VZ][i] = para->init->w; var[VXM][i] = 0.0; var[VYM][i] = 0.0; var[VZM][i] = 0.0; var[VXS][i] = 0.0; var[VYS][i] = 0.0; var[VZS][i] = 0.0; var[TEMP][i] = para->init->T; var[TEMPM][i] = 0.0; var[TEMPS][i] = 0.0; // Source of temperature var[IP][i] = 0.0; var[AP][i] = 0.0; var[AW][i] = 0.0; var[AE][i] = 0.0; var[AS][i] = 0.0; var[AN][i] = 0.0; var[AB][i] = 0.0; var[AF][i] = 0.0; var[B][i] = 0.0; var[AP0][i] = 0.0; var[TMP1][i] = 0.0; var[TMP2][i] = 0.0; var[TMP3][i] = 0.0; var[PP][i] = 0.0; var[FLAGP][i] = -1.0; var[FLAGU][i] = -1.0; var[FLAGV][i] = -1.0; var[FLAGW][i] = -1.0; var[VXBC][i] = 0.0; var[VYBC][i] = 0.0; var[VZBC][i] = 0.0; var[TEMPBC][i] = 0.0;; var[QFLUXBC][i] = 0.0; var[QFLUX][i] = 0.0; var[Xi1][i] = 0.0; var[Xi2][i] = 0.0; var[Xi1S][i] = 0.0; var[Xi2S][i] = 0.0; var[Xi1BC][i] = 0.0; var[Xi2BC][i] = 0.0; var[C1][i] = 0.0; var[C2][i] = 0.0; var[C1S][i] = 0.0; var[C2S][i] = 0.0; var[C1BC][i] = 0.0; var[C2BC][i] = 0.0; } // Calculate the thermal diffusivity para->prob->alpha = para->prob->cond / (para->prob->rho*para->prob->Cp); /**************************************************************************** | Read the configurations defined by SCI ****************************************************************************/ if(para->inpu->parameter_file_format == SCI) { flag = read_sci_input(para, var, BINDEX); if(flag != 0) { sprintf(msg, "set_inital_data(): Could not read file %s", para->inpu->parameter_file_name); ffd_log(msg, FFD_ERROR); return flag; } flag = read_sci_zeroone(para, var, BINDEX); if(flag != 0) { ffd_log("set_inital_data(): Could not read block information file", FFD_ERROR); return flag; } mark_cell(para, var); } /**************************************************************************** | Allocate memory for sensor data if there is at least one sensor ****************************************************************************/ if(para->sens->nb_sensor>0) { para->sens->senVal = (REAL *) malloc(para->sens->nb_sensor*sizeof(REAL)); if(para->sens->senVal==NULL) { ffd_log("set_initial_data(): Could not allocate memory for " "para->sens->senVal", FFD_ERROR); return -1; } para->sens->senValMean = (REAL *) malloc(para->sens->nb_sensor*sizeof(REAL)); if(para->sens->senValMean==NULL) { ffd_log("set_initial_data(): Could not allocate memory for " "para->sens->senValMean", FFD_ERROR); return 1; } } /**************************************************************************** | Allocate memory for Species ****************************************************************************/ if(para->bc->nb_port>0&¶->bc->nb_Xi>0) { para->bc->XiPort = (REAL **) malloc(sizeof(REAL*)*para->bc->nb_port); para->bc->XiPortAve = (REAL **) malloc(sizeof(REAL*)*para->bc->nb_port); para->bc->XiPortMean = (REAL **) malloc(sizeof(REAL*)*para->bc->nb_port); if(para->bc->XiPort==NULL || para->bc->XiPortAve==NULL || para->bc->XiPortMean==NULL) { ffd_log("set_initial_data(): Could not allocate memory for XiPort.", FFD_ERROR); return 1; } for(i=0; i<para->bc->nb_port; i++) { para->bc->XiPort[i] = (REAL *) malloc(sizeof(REAL)*para->bc->nb_Xi); para->bc->XiPortAve[i] = (REAL *) malloc(sizeof(REAL)*para->bc->nb_Xi); para->bc->XiPortMean[i] = (REAL *) malloc(sizeof(REAL)*para->bc->nb_Xi); if(para->bc->XiPort[i]==NULL || para->bc->XiPortAve[i]==NULL || para->bc->XiPortMean[i]==NULL) { sprintf(msg, "set_initial_data(): Could not allocate memory for " "Xi at Port[%d].", i); ffd_log(msg, FFD_ERROR); return 1; } for(j=0; j<para->bc->nb_Xi; j++) { para->bc->XiPort[i][j] = 0.0; para->bc->XiPortAve[i][j] = 0.0; para->bc->XiPortMean[i][j] = 0.0; } } if(para->outp->version==DEBUG) { ffd_log("Allocated memory for Xi", FFD_NORMAL); } } else if(para->outp->version==DEBUG) { ffd_log("No Xi in the simulation", FFD_NORMAL); } /**************************************************************************** | Allocate memory for Substances ****************************************************************************/ if(para->bc->nb_port>0&¶->bc->nb_C>0) { para->bc->CPort = (REAL **) malloc(sizeof(REAL *)*para->bc->nb_port); para->bc->CPortAve = (REAL **) malloc(sizeof(REAL *)*para->bc->nb_port); para->bc->CPortMean = (REAL **) malloc(sizeof(REAL *)*para->bc->nb_port); if(para->bc->CPort==NULL || para->bc->CPortAve==NULL || para->bc->CPortMean) { ffd_log("set_initial_data(): Could not allocate memory for CPort.", FFD_ERROR); return 1; } for(i=0; i<para->bc->nb_port; i++) { para->bc->CPort[i] = (REAL *) malloc(sizeof(REAL)*para->bc->nb_C); para->bc->CPortAve[i] = (REAL *) malloc(sizeof(REAL)*para->bc->nb_C); para->bc->CPortMean[i] = (REAL *) malloc(sizeof(REAL)*para->bc->nb_C); if(para->bc->CPort[i]==NULL || para->bc->CPortAve[i]==NULL || para->bc->CPortMean[i]) { ffd_log("set_initial_data(): " "Could not allocate memory for C at Port[i].", FFD_ERROR); return 1; } for(j=0; j<para->bc->nb_C; j++) { para->bc->CPort[i][j] = 0.0; para->bc->CPortAve[i][j] = 0.0; para->bc->CPortMean[i][j] = 0.0; } } if(para->outp->version==DEBUG) { ffd_log("Allocated memory for C", FFD_NORMAL); } } else if(para->outp->version==DEBUG) { ffd_log("No C in the simulation", FFD_NORMAL); } /**************************************************************************** | Pre-calculate data needed but not change in the simulation ****************************************************************************/ para->geom->volFlu = fluid_volume(para, var); para->geom->pindex = (int) para->geom->jmax/2; /**************************************************************************** | Set all the averaged data to 0 ****************************************************************************/ flag = reset_time_averaged_data(para, var); if(flag != 0) { ffd_log("FFD_solver(): Could not reset averaged data.", FFD_ERROR); return flag; } /**************************************************************************** | Conduct the data exchange at the inital state of cosimulation ****************************************************************************/ if(para->solv->cosimulation==1) { /*------------------------------------------------------------------------ | Calculate the area of boundary ------------------------------------------------------------------------*/ flag = bounary_area(para, var, BINDEX); if(flag != 0) { ffd_log("set_initial_data(): Could not get the boundary area.", FFD_ERROR); return flag; } /*------------------------------------------------------------------------ | Read the cosimulation parameter data (Only need once) ------------------------------------------------------------------------*/ flag = read_cosim_parameter(para, var, BINDEX); if(flag != 0) { ffd_log("set_initial_data(): Could not read cosimulation parameters.", FFD_ERROR); return 1; } /*------------------------------------------------------------------------ | Read the cosimulation data ------------------------------------------------------------------------*/ flag = read_cosim_data(para, var, BINDEX); if(flag != 0) { ffd_log("set_initial_data(): Could not read initial data for " "cosimulaiton.", FFD_ERROR); return flag; } /*------------------------------------------------------------------------ | Perform the simulation for one step to update the FFD initial condition ------------------------------------------------------------------------*/ flag = vel_step(para, var, BINDEX); if(flag != 0) { ffd_log("FFD_solver(): Could not solve velocity.", FFD_ERROR); return flag; } else if(para->outp->version==DEBUG) ffd_log("FFD_solver(): solved velocity step.", FFD_NORMAL); flag = temp_step(para, var, BINDEX); if(flag != 0) { ffd_log("FFD_solver(): Could not solve temperature.", FFD_ERROR); return flag; } else if(para->outp->version==DEBUG) ffd_log("FFD_solver(): solved temperature step.", FFD_NORMAL); flag = den_step(para, var, BINDEX); if(flag != 0) { ffd_log("FFD_solver(): Could not solve trace substance.", FFD_ERROR); return flag; } else if(para->outp->version==DEBUG) ffd_log("FFD_solver(): solved density step.", FFD_NORMAL); // Integrate the data on the boundary surface flag = surface_integrate(para, var, BINDEX); if(flag != 0) { ffd_log("FFD_solver(): " "Could not average the data on boundary.", FFD_ERROR); return flag; } else if (para->outp->version==DEBUG) ffd_log("FFD_solver(): completed surface integration", FFD_NORMAL); flag = add_time_averaged_data(para, var); if(flag != 0) { ffd_log("FFD_solver(): " "Could not add the averaged data.", FFD_ERROR); return flag; } else if (para->outp->version==DEBUG) ffd_log("FFD_solver(): completed time average", FFD_NORMAL); // Average the FFD simulation data flag = average_time(para, var); if(flag != 0) { ffd_log("FFD_solver(): Could not average the data over time.", FFD_ERROR); return flag; } /*------------------------------------------------------------------------ | Write the cosimulation data ------------------------------------------------------------------------*/ flag = write_cosim_data(para, var); if(flag != 0) { ffd_log("set_initial_data(): Could not write initial data for " "cosimulaiton.", FFD_ERROR); return flag; } } return flag; } // set_initial_data()
void update_fluid() { get_force_source( dens_prev, u_prev, v_prev, w_prev ); vel_step ( M,N,O, u, v, w, u_prev, v_prev,w_prev, visc, dt ); dens_step ( M,N,O, dens, dens_prev, u, v, w, diff, dt ); }