void MultiscaleModelFSI1D::solve ( bc_Type& bc, solution_Type& solution, const std::string& solverType ) { #ifdef HAVE_LIFEV_DEBUG debugStream ( 8130 ) << "MultiscaleModelFSI1D::solve() \n"; #endif // Re-initialize solution copySolution ( *M_solution_tn, solution ); // Subiterate to respect CFL UInt subiterationNumber (1); Real timeStep = M_data->dataTime()->timeStep(); Real CFL = M_solver->computeCFL ( solution, M_data->dataTime()->timeStep() ); if ( CFL > M_data->CFLmax() ) { subiterationNumber = std::ceil ( CFL / M_data->CFLmax() ); timeStep /= subiterationNumber; } if ( M_comm->MyPID() == 0 ) std::cout << solverType << " Number of subiterations " << subiterationNumber << " ( CFL = " << CFL* timeStep / M_data->dataTime()->timeStep() << " )" << std::endl; for ( UInt i (1) ; i <= subiterationNumber ; ++i ) { updateBCPhysicalSolverVariables(); M_physics->setArea_tn ( *solution["A"] ); M_solver->updateRHS ( solution, timeStep ); M_solver->iterate ( bc, solution, M_data->dataTime()->previousTime() + i * timeStep, timeStep ); } }
// reject the last asynchronous step and restore the history // of solutions to the last major step void e_trsolver::rejectstep_async(void) { // restore the solution (node voltages and branch currents) from // the previously stored solution copySolution (lastsolution, solution); // Restore the circuit histories to their previous states truncateHistory (lastasynctime); // Restore the time deltas inputState (dState, lastdeltas); for (int i = 0; i < 8; i++) { deltas[i] = lastdeltas[i]; } delta = lastdelta; // copy the deltas to all the circuit elements setDelta (); // reset the corrector and predictor coefficients calcCorrectorCoeff (corrType, corrOrder, corrCoeff, deltas); calcPredictorCoeff (predType, predOrder, predCoeff, deltas); }
void MultiscaleModelFSI1D::saveSolution() { #ifdef HAVE_LIFEV_DEBUG debugStream ( 8130 ) << "MultiscaleModelFSI1D::saveSolution() \n"; #endif // Update exporter solution removing ghost nodes copySolution ( *M_solution, *M_exporterSolution ); #ifdef HAVE_HDF5 M_exporter->postProcess ( M_data->dataTime()->time() ); if ( M_data->dataTime()->isLastTimeStep() ) { M_exporter->closeFile(); } #endif #ifdef HAVE_MATLAB_POSTPROCESSING //Matlab post-processing M_solver->postProcess ( *M_exporterSolution, M_data->dataTime()->time() ); #endif }
void MultiscaleModelFSI1D::buildModel() { #ifdef HAVE_LIFEV_DEBUG debugStream ( 8130 ) << "MultiscaleModelFSI1D::buildModel() \n"; #endif // Display data // if ( M_comm->MyPID() == 0 ) // M_data->showMe(); M_solver->buildConstantMatrices(); // Update previous solution copySolution ( *M_solution, *M_solution_tn ); #ifdef JACOBIAN_WITH_FINITEDIFFERENCE if ( M_couplings.size() > 0 ) { updateLinearModel(); } #endif }
void MultiscaleModelFSI1D::initializeSolution() { #ifdef HAVE_LIFEV_DEBUG debugStream ( 8130 ) << "MultiscaleModelFSI1D::initializeSolution() \n"; #endif if ( multiscaleProblemStep > 0 ) { #ifdef HAVE_HDF5 M_importer->setMeshProcId ( M_exporterMesh, M_comm->MyPID() ); M_importer->addVariable ( IOData_Type::ScalarField, "Area ratio (fluid)", M_feSpace, (*M_exporterSolution) ["AoverA0minus1"], static_cast <UInt> ( 0 ) ); M_importer->addVariable ( IOData_Type::ScalarField, "Flow rate (fluid)", M_feSpace, (*M_exporterSolution) ["Q"], static_cast <UInt> ( 0 ) ); // M_importer->addVariable( IOData_Type::ScalarField, "W1", M_feSpace, (*M_exporterSolution)["W1"], static_cast <UInt> ( 0 ), M_feSpace->dof().numTotalDof() ); // M_importer->addVariable( IOData_Type::ScalarField, "W2", M_feSpace, (*M_exporterSolution)["W2"], static_cast <UInt> ( 0 ), M_feSpace->dof().numTotalDof() ); M_importer->addVariable ( IOData_Type::ScalarField, "Pressure (fluid)", M_feSpace, (*M_exporterSolution) ["P"], static_cast <UInt> ( 0 ) ); // Import M_exporter->setTimeIndex ( M_importer->importFromTime ( M_data->dataTime()->initialTime() ) + 1 ); M_importer->closeFile(); #else std::cout << "!!! ERROR: Importer not implemented for this filter !!!" << std::endl; #endif // Copy the imported solution to the problem solution container copySolution ( *M_exporterSolution, *M_solution ); // Compute A from AreaRatio M_solver->computeArea ( *M_solution ); // Compute W1 and W2 from A and Q M_solver->computeW1W2 ( *M_solution ); // Initialize area in physics for viscoelastic computation M_physics->setArea_tn ( * (*M_solution) ["A"] ); } else { M_solver->initialize ( *M_solution ); copySolution ( *M_solution, *M_exporterSolution ); } }
void MultiscaleModelFSI1D::setupModel() { #ifdef HAVE_LIFEV_DEBUG debugStream ( 8130 ) << "MultiscaleModelFSI1D::setupModel() \n"; #endif //FEspace setupFESpace(); //Setup solution M_solver->setupSolution ( *M_solution ); M_solver->setupSolution ( *M_solution_tn ); //Set default BC (has to be called after setting other BC) M_bc->handler()->setDefaultBC(); M_bc->setPhysicalSolver ( M_solver ); M_bc->setSolution ( M_solution ); M_bc->setFluxSource ( M_flux, M_source ); //Post-processing #ifdef HAVE_HDF5 M_exporter->setMeshProcId ( M_exporterMesh, M_comm->MyPID() ); DOF tmpDof ( *M_exporterMesh, M_feSpace->refFE() ); std::vector<Int> myGlobalElements ( tmpDof.globalElements ( *M_exporterMesh ) ); MapEpetra map ( -1, myGlobalElements.size(), &myGlobalElements[0], M_comm ); M_solver->setupSolution ( *M_exporterSolution, map, true ); M_exporter->addVariable ( IOData_Type::ScalarField, "Area ratio (fluid)", M_feSpace, (*M_exporterSolution) ["AoverA0minus1"], static_cast <UInt> ( 0 ) ); M_exporter->addVariable ( IOData_Type::ScalarField, "Flow rate (fluid)", M_feSpace, (*M_exporterSolution) ["Q"], static_cast <UInt> ( 0 ) ); //M_exporter->addVariable( IOData_Type::ScalarField, "W1", M_feSpace, (*M_exporterSolution)["W1"], static_cast <UInt> ( 0 ), M_feSpace->dof().numTotalDof() ); //M_exporter->addVariable( IOData_Type::ScalarField, "W2", M_feSpace, (*M_exporterSolution)["W2"], static_cast <UInt> ( 0 ), M_feSpace->dof().numTotalDof() ); M_exporter->addVariable ( IOData_Type::ScalarField, "Pressure (fluid)", M_feSpace, (*M_exporterSolution) ["P"], static_cast <UInt> ( 0 ) ); #endif #ifdef HAVE_MATLAB_POSTPROCESSING M_solver->resetOutput ( *M_exporterSolution ); #endif //Setup solution initializeSolution(); #ifdef JACOBIAN_WITH_FINITEDIFFERENCE if ( M_couplings.size() > 0 ) { createLinearBC(); updateLinearBC ( *M_solution ); setupLinearModel(); // Initialize the linear solution copySolution ( *M_solution, *M_linearSolution ); } #endif }
// accept an asynchronous step into the solution history void e_trsolver::acceptstep_async(void) { // copy the solution in case we wish to step back to this // point later copySolution (solution, lastsolution); // Store the time lastasynctime = time; // Store the deltas history saveState (dState, lastdeltas); // Store the current delta lastdelta = delta; }
void MultiscaleModelFSI1D::updateModel() { #ifdef HAVE_LIFEV_DEBUG debugStream ( 8130 ) << "MultiscaleModelFSI1D::updateModel() \n"; #endif // Update previous solution copySolution ( *M_solution, *M_solution_tn ); #ifdef JACOBIAN_WITH_FINITEDIFFERENCE if ( M_couplings.size() > 0 ) { updateLinearModel(); } #endif }
bool copyToSolutions(solution_list_t *solutions, int *numberOfSolutions, solution_t newSolution, int maxSolutionSize) { const size_t CHUNK_SIZE = 10000; /* how many to allocate each time we realloc */ if( (*numberOfSolutions % CHUNK_SIZE) == 0) { size_t numChunks = (*numberOfSolutions) / CHUNK_SIZE + 1; *solutions = (solution_list_t)realloc(*solutions, numChunks * CHUNK_SIZE * sizeof(solution_t)); } if(solutions == NULL) { return false; } solution_t copy = NULL; copySolution(newSolution, ©, maxSolutionSize); (*solutions)[(*numberOfSolutions)] = copy; (*numberOfSolutions)+=1; return true; }
/* Swap two cities. It creates SolutionCandidate, where solution with swapped cities is saved with modifications that were made (that's why I put it here, locally) */ SolutionCandidate swapCitiesIn(const Solution& solution, City city1, City city2) { SolutionCandidate newSolution; newSolution.solution = copySolution(solution); int routeID1, routeID2; //find each city in original solution and swap it in new one; remember id of changed route for (int i = 0; i<solution.num_routes; i++) { for (int j= 0 ; j<solution.routes[i].num_cities; j++) { if (solution.routes[i].cities[j].id == city1.id) { newSolution.solution.routes[i].cities[j] = city2; routeID1 = i; } else if (solution.routes[i].cities[j].id == city2.id) { newSolution.solution.routes[i].cities[j] = city1; routeID2 = i; } } } //check how many routes were changed (1 or 2) and save it in new solution candidate if (routeID1 == routeID2) { newSolution.numberOfModifications = 1; newSolution.modifiedRoutes = (Route*) malloc(sizeof(Route)); newSolution.modifiedRoutes[0] = newSolution.solution.routes[routeID1]; } else { newSolution.numberOfModifications = 2; newSolution.modifiedRoutes = (Route*) malloc(sizeof(Route)*2); newSolution.modifiedRoutes[0] = newSolution.solution.routes[routeID1]; newSolution.modifiedRoutes[1] = newSolution.solution.routes[routeID2]; } return newSolution; }
int main() { int timer = 0,step,solution=0, useNew, accepted; float temp = INIT_TEMP; memberType current,working,best; FILE *fp; fp=fopen("stats.txt", "w"); srand(time(NULL)); initSolution(¤t); computeEnergy(¤t); best.energy=100.0; copySolution(&working, ¤t); while( temp > FIN_TEMP ) { printf("\n Temperature = %f", temp); accepted = 0; /* Monte Carlo step*/ for( step = 0; step < STEPS; step++); { useNew=0; tweakSolution(&working); computeEnergy(&working); if(working.energy <= current.energy) { useNew = 1; } else { float test = rand() % 1; float delta = working.energy - current.energy; float calc = exp(-delta/temp); if(calc > test) { accepted++; useNew = 1; } } } if(useNew) { useNew = 0; copySolution(¤t, &working); if(current.energy < best.energy) { copySolution(&best, ¤t); solution = 1; } else { copySolution(&working, ¤t); } } fprintf(fp, "%d %f %f %d \n", timer++, temp, best.energy, accepted); printf("Best Energy = %f\n", best.energy); temp *= ALPHA; } fclose(fp); if(solution) { emitSolution(&best); } return 0; }
int optimization(struct GA *ga, struct CGoL *cgol){ printf("\nIn optimization\n"); printf("\ncurrentGeneration: %d\n", (ga -> currentGeneration) + 1); printf("\nmaxGeneration: %d\n", ga -> maxGeneration); int again = 0; //Mutate initial population if((ga -> currentGeneration) == 0){ printf("\nBest Solution at start\n"); printSolution(&(ga -> bestSolution), (ga -> solutionHeight), (ga -> solutionWidth), 0); printf("\nWith a fitness of: (%d)\n",(ga -> bestSolution.fitness)); sleep(1); mutatePopulation((ga -> population), (ga -> maxSolution), (ga -> solutionWidth), (ga -> solutionHeight), INITIAL_MUTATION_RATE); } int p; //Run each solution through CGoL to determine their fitness //Don't show the board... int seedTooLarge; int i; for(i = 0; i < (ga -> maxSolution); i++){ seedTooLarge = runCGoL(&(ga -> population[i]), (ga -> solutionWidth), (ga -> solutionHeight), (ga -> fitnessSpecifier), cgol, 0); if(seedTooLarge == 0) return seedTooLarge; //Temp fix } //Sort population by fitness sortPopulationByFitness((ga -> population), (ga -> maxSolution)); //Allocate memory for a new blank population for the next generation struct Solution *newPopulation = ga_initializePopulation((ga -> maxSolution), (ga -> solutionWidth), (ga -> solutionHeight)); //For each new solution perform the following by working with the last population //Selection and Crossover for(i = 0; i < (ga -> maxSolution); i++){ int parentA, parentB; //Selection to find each parent parentA = selection_Roulette((ga -> population), (ga -> maxSolution)); parentB = selection_Roulette((ga -> population), (ga -> maxSolution)); //perform crossover at different parts for each line (newPopulation)[i] = *crossover(&(ga -> population[parentA]), &(ga -> population[parentB]), (ga -> solutionWidth), (ga -> solutionHeight)); } //For each solution in the new population //Mutation mutatePopulation(newPopulation, (ga -> maxSolution), (ga -> solutionWidth), (ga -> solutionHeight), (ga -> mutationRate)); //Determine to continue or set the bestSeed and finish if((ga -> currentGeneration) < ((ga -> maxGeneration)-1)){ again = 1; }else{ //Testing past bestSolution vs current leading solution if(ga -> bestSolution.fitness < ga -> population[(ga -> maxSolution) - 1].fitness){ printf("\nga -> bestSolution.fitness < ga -> population[0].fitness\n"); //free the old bestSolution ga_freeSolution(&(ga -> bestSolution), (ga -> solutionHeight)); //Copy values and return a new solution (ga -> bestSolution) = *copySolution(&((ga -> population)[(ga -> maxSolution) - 1]), (ga -> solutionWidth), (ga -> solutionHeight)); } printf("\nBest Solution at finish\n"); printSolution(&(ga -> bestSolution), (ga -> solutionHeight), (ga -> solutionWidth), 0); printf("\nWith a fitness of: (%d)\n",(ga -> bestSolution.fitness)); sleep(1); } //Free memory from the old population ga_freePopulation((ga -> population), (ga -> maxSolution), (ga -> solutionHeight)); //Set ga.population to point to the new population (ga -> population) = newPopulation; //Advance currentGeneration (ga -> currentGeneration)++; return again; }