Beispiel #1
0
int main(int argc, char *argv[]) {
  int i, status;

  char file_name[__FLUID_STRLEN_MAX];

  MPI_Init(&argc, &argv);

  if(argc > 1) {
    config_file_name = argv[1];
    printf("Using config file %s\n", config_file_name);
  }

  status = init();
  error_check(&status, "error in init\n");
  if(status) return status;

  clock_t begin, end;
  double time_spent;

  begin = clock();

  for(i  = 0; i < 1000; i++) {
    if(i%1 == 0) {
      snprintf(file_name,__FLUID_STRLEN_MAX-1,"out/restart_%1.1lf.bin",time_model);
      state_write(file_name);
      snprintf(file_name,__FLUID_STRLEN_MAX-1,"out/vort_%1.1lf.bin",time_model);
      state_write_vort(file_name);
    }
    time_step();
  }

  end = clock();

  time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
  printf("Time Spent: %1.16lf\n", time_spent);


  snprintf(file_name,__FLUID_STRLEN_MAX-1,"out/restart_%1.1lf.bin",time_model);
  state_write(file_name);
  snprintf(file_name,__FLUID_STRLEN_MAX-1,"out/vort_%1.1lf.bin",time_model);
  state_write_vort(file_name);

  MPI_Barrier(MPI_COMM_WORLD);

  finalize();

  MPI_Finalize();

  return 0;
}
Beispiel #2
0
std::pair<Vect3, Velocity> KinematicAltBands::trajectory(const TrafficState& ownship, double time, bool dir) const {
  double target_alt = min_val(ownship)+j_step_*get_step();
  std::pair<Position,Velocity> posvel;
  if (instantaneous_bands()) {
    posvel = std::pair<Position,Velocity>(ownship.getPosition().mkZ(target_alt),ownship.getVelocity().mkVs(0));
  } else {
    double tsqj = ProjectedKinematics::vsLevelOutTime(ownship.getPosition(),ownship.getVelocity(),vertical_rate_,
        target_alt,vertical_accel_)+time_step(ownship);
    if (time <= tsqj) {
      posvel = ProjectedKinematics::vsLevelOut(ownship.getPosition(), ownship.getVelocity(), time, vertical_rate_, target_alt, vertical_accel_);
    } else {
      Position npo = ownship.getPosition().linear(ownship.getVelocity(),time);
      posvel = std::pair<Position,Velocity>(npo.mkZ(target_alt),ownship.getVelocity().mkVs(0));
    }
  }
  return std::pair<Vect3,Velocity>(ownship.pos_to_s(posvel.first),ownship.vel_to_v(posvel.first,posvel.second));
}
Beispiel #3
0
bool KinematicAltBands::conflict_free_traj_step(Detection3D* conflict_det, Detection3D* recovery_det,
    double B, double T, double B2, double T2,
    const TrafficState& ownship, const std::vector<TrafficState>& traffic) const {
  bool trajdir = true;
  if (instantaneous_bands()) {
    return no_conflict(conflict_det,recovery_det,B,T,B2,T2,trajdir,0,ownship,traffic);
  } else {
    double tstep = time_step(ownship);
    double target_alt = min_val(ownship)+j_step_*get_step();
    Tuple5<double,double,double,double,double> tsqj = Kinematics::vsLevelOutTimes(ownship.altitude(),ownship.verticalSpeed(),
        vertical_rate_,target_alt,vertical_accel_,-vertical_accel_,true);
    double tsqj1 = tsqj.first+0;
    double tsqj2 = tsqj.second+0;
    double tsqj3 = tsqj.third+tstep;
    for (int i=0; i<=std::floor(tsqj1/tstep);++i) {
      double tsi = i*tstep;
      if ((B<=tsi && tsi<=T && any_los_aircraft(conflict_det,trajdir,tsi,ownship,traffic)) ||
          (recovery_det != NULL && B2 <= tsi && tsi <= T2 &&
              any_los_aircraft(recovery_det,trajdir,tsi,ownship,traffic))) {
        return false;
      }
    }
    if ((tsqj2>=B &&
        any_conflict_aircraft(conflict_det,B,std::min(T,tsqj2),trajdir,std::max(tsqj1,0.0),ownship,traffic)) ||
        (recovery_det != NULL && tsqj2>=B2 &&
            any_conflict_aircraft(recovery_det,B2,std::min(T2,tsqj2),trajdir,std::max(tsqj1,0.0),ownship,traffic))) {
      return false;
    }
    for (int i=(int)std::ceil(tsqj2/tstep); i<=std::floor(tsqj3/tstep);++i) {
      double tsi = i*tstep;
      if ((B<=tsi && tsi<=T && any_los_aircraft(conflict_det,trajdir,tsi,ownship,traffic)) ||
          (recovery_det != NULL && B2 <= tsi && tsi <= T2 &&
              any_los_aircraft(recovery_det,trajdir,tsi,ownship,traffic))) {
        return false;
      }
    }
    return no_conflict(conflict_det,recovery_det,B,T,B2,T2,trajdir,std::max(tsqj3,0.0),ownship,traffic);
  }
}
int main( int argc, char **argv ) {
    num_particles = read_int( argc, argv, "-n", 1000 );
    char *savename = read_string( argc, argv, "-o", NULL );
    MPI_Init( &argc, &argv );
    MPI_Comm_size( MPI_COMM_WORLD, &num_procs );
    MPI_Comm_rank( MPI_COMM_WORLD, &rank );
    fsave = savename && rank == 0 ? fopen( savename, "w" ) : NULL;
    particles = (particle_t*) malloc( num_particles * sizeof(particle_t) );
    MPI_Type_contiguous( 7, MPI_DOUBLE, &PARTICLE );
    MPI_Type_commit( &PARTICLE );
    set_size( num_particles );

    init_vars();
    init_partitions();

    if( rank == 0 ) {
        init_particles( num_particles, particles );
    }
    MPI_Bcast(particles, num_particles, PARTICLE, 0, MPI_COMM_WORLD);
    partition_particles();
    init_grid();

    double simulation_time = read_timer();

    for( int step = 0; step < NSTEPS; step++ ) {
        if (rank == 0) {
            right_fringe();
            MPI_Isend(right_sending_buffer, num_sending_right, PARTICLE, rank+1, 0, MPI_COMM_WORLD, &send_right_req);
            MPI_Irecv(right_receiving_buffer, num_my_particles, PARTICLE, rank+1, MPI_ANY_TAG, MPI_COMM_WORLD, &recv_right_req);
            MPI_Wait(&send_right_req, &send_right_stat);
            MPI_Wait(&recv_right_req, &recv_right_stat);
            MPI_Get_count(&recv_right_stat, PARTICLE, &recvd_right_count);
            num_augmented_particles = num_my_particles + recvd_right_count;
            memcpy(my_particles + num_my_particles, right_receiving_buffer, recvd_right_count * sizeof(particle_t));
        } else if (rank == (num_procs-1)) {
            left_fringe();
            MPI_Isend(left_sending_buffer, num_sending_left, PARTICLE, rank-1, 0, MPI_COMM_WORLD, &send_left_req);
            MPI_Irecv(left_receiving_buffer, num_my_particles, PARTICLE, rank-1, MPI_ANY_TAG, MPI_COMM_WORLD, &recv_left_req);
            MPI_Wait(&send_left_req, &send_left_stat);
            MPI_Wait(&recv_left_req, &recv_left_stat);
            MPI_Get_count(&recv_left_stat, PARTICLE, &recvd_left_count);
            num_augmented_particles = num_my_particles + recvd_left_count;
            memcpy(my_particles + num_my_particles, left_receiving_buffer, recvd_left_count * sizeof(particle_t));
        } else {
            left_fringe();
            right_fringe();
            MPI_Isend(left_sending_buffer, num_sending_left, PARTICLE, rank-1, 0, MPI_COMM_WORLD, &send_left_req);
            MPI_Irecv(left_receiving_buffer, num_my_particles, PARTICLE, rank-1, MPI_ANY_TAG, MPI_COMM_WORLD, &recv_left_req);
            MPI_Isend(right_sending_buffer, num_sending_right, PARTICLE, rank+1, 0, MPI_COMM_WORLD, &send_right_req);
            MPI_Irecv(right_receiving_buffer, num_my_particles, PARTICLE, rank+1, MPI_ANY_TAG, MPI_COMM_WORLD, &recv_right_req);
            MPI_Wait(&send_left_req, &send_left_stat);
            MPI_Wait(&send_right_req, &send_right_stat);
            MPI_Wait(&recv_left_req, &recv_left_stat);
            MPI_Wait(&recv_right_req, &recv_right_stat);
            MPI_Get_count(&recv_left_stat, PARTICLE, &recvd_left_count);
            MPI_Get_count(&recv_right_stat, PARTICLE, &recvd_right_count);
            num_augmented_particles = num_my_particles + recvd_left_count + recvd_right_count;
            memcpy(my_particles + num_my_particles, left_receiving_buffer, recvd_left_count * sizeof(particle_t));
            memcpy(my_particles + num_my_particles + recvd_left_count, right_receiving_buffer, recvd_right_count * sizeof(particle_t));
        }

        populate_grid();
        time_step();

        num_sending_left = 0;
        num_sending_right = 0;
        int num_remaining_particles = 0;
        for ( int i = 0; i < num_my_particles; i++ ) {
            if (rank != 0 && my_particles[i].x <= partition_offsets[rank]) {
                left_sending_buffer[num_sending_left++] = my_particles[i];
            } else if (rank != (num_procs-1) && my_particles[i].x > partition_offsets[rank+1]) {
                right_sending_buffer[num_sending_right++] = my_particles[i];
            } else {
                remaining_particles[num_remaining_particles++] = my_particles[i];
            }
        }
        if (rank == 0) {
            MPI_Isend(right_sending_buffer, num_sending_right, PARTICLE, rank+1, 0, MPI_COMM_WORLD, &send_right_req);
            MPI_Irecv(right_receiving_buffer, num_my_particles, PARTICLE, rank+1, MPI_ANY_TAG, MPI_COMM_WORLD, &recv_right_req);
            MPI_Wait(&send_right_req, &send_right_stat);
            MPI_Wait(&recv_right_req, &recv_right_stat);
            MPI_Get_count(&recv_right_stat, PARTICLE, &recvd_right_count);
            num_augmented_particles = num_remaining_particles + recvd_right_count;
            memcpy(my_particles, remaining_particles, num_remaining_particles * sizeof(particle_t));
            memcpy(my_particles + num_remaining_particles, right_receiving_buffer, recvd_right_count * sizeof(particle_t));
            num_my_particles = num_augmented_particles;
        } else if (rank == (num_procs-1)) {
            MPI_Isend(left_sending_buffer, num_sending_left, PARTICLE, rank-1, 0, MPI_COMM_WORLD, &send_left_req);
            MPI_Irecv(left_receiving_buffer, num_my_particles, PARTICLE, rank-1, MPI_ANY_TAG, MPI_COMM_WORLD, &recv_left_req);
            MPI_Wait(&send_left_req, &send_left_stat);
            MPI_Wait(&recv_left_req, &recv_left_stat);
            MPI_Get_count(&recv_left_stat, PARTICLE, &recvd_left_count);
            num_augmented_particles = num_remaining_particles + recvd_left_count;
            memcpy(my_particles, remaining_particles, num_remaining_particles * sizeof(particle_t));
            memcpy(my_particles + num_remaining_particles, left_receiving_buffer, recvd_left_count * sizeof(particle_t));
            num_my_particles = num_augmented_particles;
        } else {
            MPI_Isend(right_sending_buffer, num_sending_right, PARTICLE, rank+1, 0, MPI_COMM_WORLD, &send_right_req);
            MPI_Irecv(right_receiving_buffer, num_my_particles, PARTICLE, rank+1, MPI_ANY_TAG, MPI_COMM_WORLD, &recv_right_req);
            MPI_Isend(left_sending_buffer, num_sending_left, PARTICLE, rank-1, 0, MPI_COMM_WORLD, &send_left_req);
            MPI_Irecv(left_receiving_buffer, num_my_particles, PARTICLE, rank-1, MPI_ANY_TAG, MPI_COMM_WORLD, &recv_left_req);
            MPI_Wait(&send_right_req, &send_right_stat);
            MPI_Wait(&recv_right_req, &recv_right_stat);
            MPI_Wait(&send_left_req, &send_left_stat);
            MPI_Wait(&recv_left_req, &recv_left_stat);
            MPI_Get_count(&recv_left_stat, PARTICLE, &recvd_left_count);
            MPI_Get_count(&recv_right_stat, PARTICLE, &recvd_right_count);
            num_augmented_particles = num_remaining_particles + recvd_left_count + recvd_right_count;
            memcpy(my_particles, remaining_particles, num_remaining_particles * sizeof(particle_t));
            memcpy(my_particles + num_remaining_particles, left_receiving_buffer, recvd_left_count * sizeof(particle_t));
            memcpy(my_particles + num_remaining_particles + recvd_left_count, right_receiving_buffer, recvd_right_count * sizeof(particle_t));
            num_my_particles = num_augmented_particles;
        }
    }

    simulation_time = read_timer() - simulation_time;
    if( rank == 0 ) {
        printf( "num_particles = %d, num_procs = %d, simulation time = %g s\n", num_particles, num_procs, simulation_time );
    }

    if (savename) {
        if (rank == 0) {
            final_partition_sizes = (int*) malloc( num_procs * sizeof(int) );
        }
        MPI_Gather(&num_my_particles, 1, MPI_INT, final_partition_sizes, 1, MPI_INT, 0, MPI_COMM_WORLD);
        if (rank == 0) {
            final_partition_offsets = (int*) malloc( num_procs * sizeof(int) );
            final_partition_offsets[0] = 0;
            for (int i = 1; i < num_procs; i++) {
                final_partition_offsets[i] = final_partition_offsets[i-1] + final_partition_sizes[i-1];
            }
        }
        MPI_Gatherv(my_particles, num_my_particles, PARTICLE, particles, final_partition_sizes, final_partition_offsets, PARTICLE, 0, MPI_COMM_WORLD);
        if (rank == 0) {
            save( fsave, num_particles, particles );
            free(final_partition_sizes);
            free(final_partition_offsets);
        }
    }

    free( partition_offsets );
    free( partition_sizes );
    free( my_particles ); // same as my_particles
    free(remaining_particles);
    free( left_sending_buffer );
    free( right_sending_buffer );
    free( left_receiving_buffer );
    free( right_receiving_buffer );
    if( fsave ) fclose( fsave );
    MPI_Finalize();
    return 0;
}
/*
 * Main function
 */
int main(int argc, char** argv)
{
	if (argc < 2)
	{
		std::cout << "specify data file name" << std::endl;
		return 0;
	}
	const char* data_file_name = argv[1];

const unsigned long long full_program_start = current_time_ns();
{
	// set far field conditions
	{
		const double angle_of_attack = double(3.1415926535897931 / 180.0) * double(deg_angle_of_attack);

		ff_variable[VAR_DENSITY] = double(1.4);

		double ff_pressure = double(1.0);
		double ff_speed_of_sound = sqrt(GAMMA*ff_pressure / ff_variable[VAR_DENSITY]);
		double ff_speed = double(ff_mach)*ff_speed_of_sound;

		cfd_double3 ff_velocity;
		ff_velocity.x = ff_speed*double(cos((double)angle_of_attack));
		ff_velocity.y = ff_speed*double(sin((double)angle_of_attack));
		ff_velocity.z = 0.0;

		ff_variable[VAR_MOMENTUM+0] = ff_variable[VAR_DENSITY] * ff_velocity.x;
		ff_variable[VAR_MOMENTUM+1] = ff_variable[VAR_DENSITY] * ff_velocity.y;
		ff_variable[VAR_MOMENTUM+2] = ff_variable[VAR_DENSITY] * ff_velocity.z;

		ff_variable[VAR_DENSITY_ENERGY] = ff_variable[VAR_DENSITY]*(double(0.5)*(ff_speed*ff_speed)) + (ff_pressure / double(GAMMA-1.0));

		cfd_double3 ff_momentum;
		ff_momentum.x = *(ff_variable+VAR_MOMENTUM+0);
		ff_momentum.y = *(ff_variable+VAR_MOMENTUM+1);
		ff_momentum.z = *(ff_variable+VAR_MOMENTUM+2);
		compute_flux_contribution(ff_variable[VAR_DENSITY], ff_momentum, ff_variable[VAR_DENSITY_ENERGY], ff_pressure, ff_velocity, ff_flux_contribution_momentum_x, ff_flux_contribution_momentum_y, ff_flux_contribution_momentum_z, ff_flux_contribution_density_energy);
	}
	int nel;
	int nelr;

	// read in domain geometry
	double* areas;
	int* elements_surrounding_elements;
	double* normals;
	{
		std::ifstream file(data_file_name);

		file >> nel;
		nelr = block_length*((nel / block_length )+ std::min(1, nel % block_length));

		areas = new double[nelr];
		elements_surrounding_elements = new int[nelr*NNB];
		normals = new double[NDIM*NNB*nelr];

		// read in data
		for(int i = 0; i < nel; i++)
		{
			file >> areas[i];
			for(int j = 0; j < NNB; j++)
			{
				file >> elements_surrounding_elements[i*NNB + j];
				if(elements_surrounding_elements[i*NNB+j] < 0) elements_surrounding_elements[i*NNB+j] = -1;
				elements_surrounding_elements[i*NNB + j]--; //it's coming in with Fortran numbering

				for(int k = 0; k < NDIM; k++)
				{
					file >>  normals[(i*NNB + j)*NDIM + k];
					normals[(i*NNB + j)*NDIM + k] = -normals[(i*NNB + j)*NDIM + k];
				}
			}
		}

		// fill in remaining data
		int last = nel-1;
		for(int i = nel; i < nelr; i++)
		{
			areas[i] = areas[last];
			for(int j = 0; j < NNB; j++)
			{
				// duplicate the last element
				elements_surrounding_elements[i*NNB + j] = elements_surrounding_elements[last*NNB + j];
				for(int k = 0; k < NDIM; k++) normals[(i*NNB + j)*NDIM + k] = normals[(last*NNB + j)*NDIM + k];
			}
		}
	}

	// Create arrays and set initial conditions
	double* variables = alloc<double>(nelr*NVAR);
	initialize_variables(nelr, variables);

	double* old_variables = alloc<double>(nelr*NVAR);
	double* fluxes = alloc<double>(nelr*NVAR);
	double* step_factors = alloc<double>(nelr);

	// these need to be computed the first time in order to compute time step
	std::cout << "Starting..." << std::endl;

	// Begin iterations
	for(int i = 0; i < iterations; i++)
	{
		copy(old_variables, variables, nelr*NVAR);

		// for the first iteration we compute the time step
		compute_step_factor(nelr, variables, areas, step_factors);

		for(int j = 0; j < RK; j++)
		{
			compute_flux(nelr, elements_surrounding_elements, normals, variables, fluxes);
			time_step(j, nelr, old_variables, variables, step_factors, fluxes);
		}
	}

	std::cout << "Saving solution..." << std::endl;
	dump(variables, nel, nelr);
	std::cout << "Saved solution..." << std::endl;


	std::cout << "Cleaning up..." << std::endl;
	dealloc<double>(areas);
	dealloc<int>(elements_surrounding_elements);
	dealloc<double>(normals);

	dealloc<double>(variables);
	dealloc<double>(old_variables);
	dealloc<double>(fluxes);
	dealloc<double>(step_factors);
    } ; 
const unsigned long long full_program_end = current_time_ns();
printf("full_program %llu ns\n", full_program_end - full_program_start);


	std::cout << "Done..." << std::endl;

	return 0;
}
Beispiel #6
0
void TIME_STEPPER::TimeAdvance() {

#ifdef USE_SWSS
    //  This is a SWSS
    
    //  First 
    TIME_STEPPER::RKStep = 0;
    //  t0: Calc FMM and get DT
#ifndef NOFMM
    
    
    if (globalSystem->useFMM)
        DoFMM();
    else {
        for (int i = 0; i < FVMCell::AllCells.size(); ++i) {
            FVMCell::AllCells[i]->Velocity = globalSystem->unscaledVinf * SYSTEM::GambitScale;
        }
    }
        
#else
#pragma omp parallel for
    for (int i = 0; i < FVMCell::AllCells.size(); ++i) {
        FVMCell::AllCells[i]->Velocity = 0.0;
        FVMCell::AllCells[i]->VelGrads[0] = FVMCell::AllCells[i]->VelGrads[1] = FVMCell::AllCells[i]->VelGrads[2] = Vect3(0.0);
        for (int j = 0; j < FVMCell::AllCells.size(); ++j) {
            Vect3 D = FVMCell::AllCells[j]->Position - FVMCell::AllCells[i]->Position;
            FVMCell::AllCells[i]->Velocity += UTIL::globalDirectVel(D, FVMCell::AllCells[j]->Omega);
            directGradMethod(D, FVMCell::AllCells[j]->Omega, FVMCell::AllCells[i]->VelGrads);
        }

    }
#endif
    //  t0: calculate face velocities due to body
    globalSystem->GetFaceVels();
    //  t0: calculate face velocities due to body
    time_step();
    //  t0: get panel FMM Vels
#ifndef NOFMM
    if (globalSystem->useBodies) {
        globalSystem->GetPanelFMMVelocities(dt);

        //  t0: get time derivatives at t0

        //  t0: advance innter (body) timestep

        if (TIME_STEPPER::RK2Mode)
            BODY::BodySubStep(dt / 2.0, globalSystem->NumSubSteps);
        else
            BODY::BodySubStep(dt, globalSystem->NumSubSteps);

    }
#endif
    
    /* Order of integrations is as follows:
     * 0) Panel  
     * 1) stretch
     * 2) diffuse
     * 3) O2x
     * 4) O2y
     * 5) O2z
     * 
     * 
     * Other sweep is reverse
     */
    
    //  Record initial vals
    for (int i = 0; i < FVMCell::AllCells.size(); ++i) {
        FVMCell::AllCells[i]->VelHold = FVMCell::AllCells[i]->Velocity; //      this is done in vCollapseVelField
        FVMCell::AllCells[i]->VelGradsHold = FVMCell::AllCells[i]->VelGrads; //      this is done in vCollapseVelField
        for (int q = 0; q < SYSTEM::NumTransVars; ++q)
            FVMCell::AllCells[i]->TransVarsHold[q] = FVMCell::AllCells[i]->TransVars[q];
        FVMCell::AllCells[i]->OmegaHold = FVMCell::AllCells[i]->Omega;
    }
    if (globalSystem->useFMM) {
        globalOctree->DiffuseZAndAdvance(dt);
        globalOctree->StretchAndAdvance(dt);
        globalOctree->DiffuseYAndAdvance(dt);
    }
    globalOctree->O2UWxAndAdvance(dt);
    globalOctree->O2UWyAndAdvance(dt);
    globalOctree->O2UWzAndAdvance(dt);
    if (globalSystem->useFMM)
        globalOctree->DiffuseXAndAdvance(dt);

    //  Second sweep
    //  Reset initial values -- VelHold is still unchanged - can be reused; same with its gradients
#ifdef _OPENMP
#pragma omp parallel for
#endif
    for (int i = 0; i < FVMCell::AllCells.size(); ++i) {
        for (int q = 0; q < SYSTEM::NumTransVars; ++q) {
            FVMCell::AllCells[i]->TransVars0[q] = FVMCell::AllCells[i]->TransVars[q];
            FVMCell::AllCells[i]->TransVars[q] = FVMCell::AllCells[i]->TransVarsHold[q];
        }
        FVMCell::AllCells[i]->Velocity = FVMCell::AllCells[i]->VelHold;
        FVMCell::AllCells[i]->VelGrads = FVMCell::AllCells[i]->VelGradsHold;
        FVMCell::AllCells[i]->Omega = FVMCell::AllCells[i]->OmegaHold;


    }
    if (globalSystem->useFMM)
        globalOctree->DiffuseXAndAdvance(dt);

    globalOctree->O2UWzAndAdvance(dt);
    globalOctree->O2UWyAndAdvance(dt);
    globalOctree->O2UWxAndAdvance(dt);
    if (globalSystem->useFMM) {
        globalOctree->DiffuseYAndAdvance(dt);
        globalOctree->StretchAndAdvance(dt);
        globalOctree->DiffuseZAndAdvance(dt);
    }


    for (int i = 0; i < FVMCell::AllCells.size(); ++i) {
        FVMCell::AllCells[i]->Omega = Vect3(0.0);
        for (int q = 0; q < SYSTEM::NumTransVars; ++q) {
            Vect3 VarTmp = FVMCell::AllCells[i]->TransVars[q];
            FVMCell::AllCells[i]->TransVars [q] = 0.5 * (FVMCell::AllCells[i]->TransVars0[q] + VarTmp);
            FVMCell::AllCells[i]->Omega += FVMCell::AllCells[i]->TransVars[q];
        }
    }
    
#else
    
    //  t0: get cell velocities/gradients etc.
    DoFMM();


    //  t0: calculate face velocities due to body

    
    globalSystem->GetFaceVels();

    //  t0: calculate timestep length (use last values of panel singularity strengths for body influence)
    time_step();
    //  t0: get panel FMM Vels
//    if (globalSystem->useBodies)
//        globalSystem->GetPanelFMMVelocities(0.0);

    //  


    globalSystem->GetFaceVels();

 
    FVMCell::CellDerivs.allocate(SYSTEM::NumTransVars);
    for (int q = 0; q < SYSTEM::NumTransVars; ++q) {
        FVMCell::CellDerivs[q] = Array <Vect3> (FVMCell::AllCells.size());
#ifdef _OPENMP
#pragma omp parallel for
#endif
        for (int i = 0; i < FVMCell::AllCells.size(); ++i) {
            FVMCell::CellDerivs[q][i] = FVMCell::AllCells[i]->O2UW(q);
            FVMCell::CellDerivs[q][i] += FVMCell::AllCells[i]->Stretch(q);
//            FVMCell::CellDerivs[q][i] += FVMCell::AllCells[i]->Diffuse(q);
        }
#ifdef _OPENMP
#pragma omp parallel for
#endif
        for (int i = 0; i < FVMCell::AllCells.size(); ++i)
            FVMCell::AllCells[i]->TransVars[q] += dt * FVMCell::CellDerivs[q][i];
    }
#ifdef _OPENMP
#pragma omp parallel for
#endif
    for (int i = 0; i < FVMCell::AllCells.size(); ++i)
        FVMCell::AllCells[i]->NormaliseObliterate();

    //      Update cell velocities to reflect new cell omegas
#ifdef _OPENMP
#pragma omp parallel for
#endif   
    for (int i = 0; i < FVMCell::AllCells.size(); ++i)
        FVMCell::AllCells[i]->GetISAVels();
    
    
    //      Now update Panel FMM Vels to reflect new velocities then advance over timestep
    if (globalSystem->useBodies) {
        globalSystem->GetPanelFMMVelocities(dt);
        BODY::BodySubStep(dt, globalSystem->NumSubSteps);
    }


    
    
    
    //    if (TIME_STEPPER::RK2Mode) {
//        TIME_STEPPER::RKStep = 1;
//        //  t*: Do the FMM again
//        DoFMM();
//        if (globalSystem->useBodies) {
//            globalSystem->GetPanelFMMVelocities(dt / 2);
//            BODY::BodySubStep(dt / 2.0, globalSystem->NumSubSteps); //  t = t0 -> t*
//            //  t*: calculate face velocities due to body
//        }
//        globalSystem->GetFaceVels();
//        //  t*: calculate gradients at t*
//        globalOctree->FVM(); //  dom_dt(t*)
//        //  t*: advance to t1
//        globalOctree->Integrate(); //  t = t0 -> t1
//    }

#endif
    //  Put the wake in the tree from the bodytimestep
    if (globalSystem->useBodies) {
        globalSystem->PutWakesInTree();
    } else
        TIME_STEPPER::SimTime += dt;
    //  Clean up
    if (!fmod((REAL) n, 25.0))
        PruneNow = true;
    

    
    globalOctree->Reset();




}
Beispiel #7
0
int main(int argc,char **args){
  PetscReal time_max,dt,*gamma_1,*gamma_2;
  PetscReal gate_time_step,theta,fidelity,t1,t2;
  PetscScalar mat_val;
  PetscInt  steps_max,num_qubits;
  Vec rho,rho_base,rho_base2,rho_base3;
  int i,j,h_dim,system,dm_place,logical_qubits,prev_qb,prev_qb2,num_qubits2;
  circuit qiskit_read;
  Mat     encoder_mat;
  char           string[10],filename[128];
  stabilizer     S1,S2,S3,S4;
  PetscReal *r_str,gam,dep,noise;
  char           encoder_str[128];
  encoder_type   encoder_type0,encoder_type1,encoder_type2,encoder_type3;
  struct quantum_gate_struct *gate_list;
  /* Initialize QuaC */
  QuaC_initialize(argc,args);

  num_qubits = 0;

  strcpy(filename,"NULL");
  PetscOptionsGetString(NULL,NULL,"-file_circ",filename,sizeof(filename),NULL);
  qiskit_qasm_read(filename,&num_qubits,&qiskit_read);

  strcpy(filename,"NULL");
  PetscOptionsGetString(NULL,NULL,"-file_ev",filename,sizeof(filename),NULL);

  qubits  = malloc(num_qubits*sizeof(struct operator));
  gamma_1 = malloc(num_qubits*sizeof(PetscReal));
  gamma_2 = malloc(num_qubits*sizeof(PetscReal));
  for (i=0;i<num_qubits;i++){
    create_op(2,&qubits[i]);
    gamma_1[i] = 0;
    gamma_2[i] = 0;
    printf("MY_OP_TYPE op %d qubits[i]->dag %d qubits[i]->dag->dag %d\n",qubits[i]->my_op_type,qubits[i]->dag->my_op_type,qubits[i]->dag->dag->my_op_type);
  }

  gam = 1.0;
  dep = 1.0;
  noise = 0;
  PetscOptionsGetReal(NULL,NULL,"-noise",&noise,NULL);

  for (i=0; i<num_qubits;i++){
    gamma_1[i] = gam*noise;
    gamma_2[i] = dep*noise;
  }

  //Add lindblad terms
  for (i=0;i<num_qubits;i++){
    if (gamma_1[i]>0){
      //Spontaneous emission
      add_lin(gamma_1[i],qubits[i]);
    }
    if (gamma_2[i]>0){
      //Dephasing
      add_lin(gamma_2[i],qubits[i]->n);
    }
  }

  time_max  = qiskit_read.num_gates + 1;

  dt        = 1;
  steps_max = 10000000;

  /* Set the ts_monitor to print results at each time step */
  set_ts_monitor(ts_monitor);
  /* Open file that we will print to in ts_monitor */
  if (nid==0){
    f_pop = fopen("pop","w");
    fprintf(f_pop,"#Time Populations\n");
  }

  create_full_dm(&rho);
  mat_val = 1.0;
  add_value_to_dm(rho,0,0,mat_val);
  assemble_dm(rho);

  if (nid==0){
    printf("num_gates: %ld \n",qiskit_read.num_gates);
  }

  start_circuit_at_time(&qiskit_read,0.0);

  time_step(rho,0.0,time_max,dt,steps_max);
  /* get_expectation_value(rho,&mat_val,2,qubits[0]->sig_z,qubits[0]->sig_z); */
  qiskit_vqe_get_expectation(filename,rho,&mat_val);

  if (nid==0) printf("Energy: %.18lf\n",PetscRealPart(mat_val));
  //print_psi(rho,16);
  for (i=0;i<num_qubits;i++){
    destroy_op(&qubits[i]);
  }
  destroy_dm(rho);
  QuaC_finalize();
  return 0;
}
void ISOP2P1::run()
{
	initialize();
	std::cout << "Initialize mesh ... " << std::endl;
	if (isMoving == 1)
	{
		double scale_step = 0.2;
		scale = scale_step;
		do {
			// PoiseuilleVx poiseuille_vx(-1.0, 1.0);
			// PoiseuilleVy poiseuille_vy;
			// Operator::L2Project(poiseuille_vx, v_h[0], Operator::LOCAL_LEAST_SQUARE, 3);
			// Operator::L2Project(poiseuille_vy, v_h[1], Operator::LOCAL_LEAST_SQUARE, 3);

			buildMatrix();
			// stepForwardEuler();
			solveStokes();
			v_h[0].scale(scale);
			v_h[1].scale(scale);
			movingMesh();
			std::cout << "\r\tscale = " << scale << std::endl;
			scale += scale_step;
		} while (scale <= 1.0);
		/// 重新设置scale 为1.0.
		scale = 1.0;
	}
	// PoiseuilleVx poiseuille_vx(-1.0, 1.0);
	// PoiseuilleVy poiseuille_vy;
	// Operator::L2Project(poiseuille_vx, v_h[0], Operator::LOCAL_LEAST_SQUARE, 3);
	// Operator::L2Project(poiseuille_vy, v_h[1], Operator::LOCAL_LEAST_SQUARE, 3);
	// outputSolution();
	// v_h[0].reinit(fem_space_v);
	// v_h[1].reinit(fem_space_v);
	// p_h.reinit(fem_space_p);
	solveStokes();
	outputTecplotP("P0");
	outputTecplot("initial_value0");
	getchar();

	int steps_before = 0;
	int steps_after = 0;
	bool isOutput = false;

	while (t < t1)
	{
		/// 准备每步输出一个 tecplot 数据, 注意别把硬盘写爆了!
		std::stringstream ss_before;
		ss_before << "NS_Euler";
		std::stringstream ss_after;
		ss_after << "NS_Euler_updateSol";

		if (scheme == 1)
		{
			if(isMoving == 1)
				buildMatrix();
			stepForwardEuler();
		}
		else if (scheme == 2)
			stepForwardLinearizedEuler();
		else
			break;
		/// 获取时间步长.
		time_step();
		t += dt;
		steps_before++;
		ss_before << steps_before;
		outputTecplot(ss_before.str());
		std::cout << "Data outputed!" << std::endl;
		
		/// 网格移动.
		if (isMoving == 1)
			movingMesh();
		/// 输出一下P网格上的压力,monitor,以及网格移动方向.
		outputTecplotP("NS_Euler");
		getchar();

		/// 输出.
		// if (isOutput)
		// {
		steps_after++;
		ss_after << steps_after;
		outputTecplot(ss_after.str());
		std::cout << "Data outputed!" << std::endl;
		// 	isOutput = false;
		// }
		// double t_stop = int((t / 0.01) + 0.5) * 0.01;
		// if (t > t_stop - dt && t < t_stop + dt)
		// {
		// 	t = t_stop;
		// 	isOutput = true;
		// }
		// std::cout << "t = " << t << std::endl;
	}

}
Beispiel #9
0
void g2_correlation(PetscScalar ***g2_values,Vec dm0,PetscInt n_tau,PetscReal tau_max,PetscInt n_st,PetscReal st_max,PetscInt number_of_ops,...){
  TSCtx tsctx;
  PetscReal st_dt,previous_start_time,this_start_time,dt;
  PetscReal tau_t_max,dt_tau;
  PetscInt i,j,dim,steps_max;
  Mat A_star_A,tmp_mat;
  Vec init_dm;
  va_list ap;
  /*Explicitly construct our jump matrix by adding up all of the operators
   * \rho = A \rho A^\dag
   * Vectorized:
   * \rho = (A* \cross A) \rho
   */
  dim = total_levels*total_levels; //Assumes Lindblad

  MatCreate(PETSC_COMM_WORLD,&tmp_mat);
  MatSetType(tmp_mat,MATMPIAIJ);
  MatSetSizes(tmp_mat,PETSC_DECIDE,PETSC_DECIDE,dim,dim);
  MatSetFromOptions(tmp_mat);
  MatMPIAIJSetPreallocation(tmp_mat,4,NULL,4,NULL);

  va_start(ap,number_of_ops);
  //Get A* \cross I
  vadd_ops_to_mat(tmp_mat,1,number_of_ops,ap);
  va_end(ap);


  MatCreate(PETSC_COMM_WORLD,&tsctx.I_cross_A);
  MatSetType(tsctx.I_cross_A,MATMPIAIJ);
  MatSetSizes(tsctx.I_cross_A,PETSC_DECIDE,PETSC_DECIDE,dim,dim);
  MatSetFromOptions(tsctx.I_cross_A);
  MatMPIAIJSetPreallocation(tsctx.I_cross_A,4,NULL,4,NULL);
  va_start(ap,number_of_ops);
  //Get I_cross_A
  vadd_ops_to_mat(tsctx.I_cross_A,-1,number_of_ops,ap);
  va_end(ap);


  //Get (A* \cross I) (I \cross A)
  MatMatMult(tmp_mat,tsctx.I_cross_A,MAT_INITIAL_MATRIX,PETSC_DEFAULT,&A_star_A);
  MatDestroy(&tmp_mat);
  VecDuplicate(dm0,&(tsctx.tmp_dm));
  VecDuplicate(dm0,&(tsctx.tmp_dm2));
  VecDuplicate(dm0,&init_dm);

  //Arbitrary, set this better
  steps_max = 51000;
  dt        = 0.025;

  //Allocate memory for g2
  (*g2_values) = (PetscScalar **)malloc((n_st+1)*sizeof(PetscScalar *));
  for (i=0;i<n_st+1;i++){
    (*g2_values)[i] = (PetscScalar *)malloc((n_tau+1)*sizeof(PetscScalar));
    for (j=0;j<n_tau+1;j++){
      (*g2_values)[i][j] = 0.0;
    }
  }
  tsctx.g2_values = (*g2_values);

  set_ts_monitor_ctx(_g2_ts_monitor,&tsctx);
  st_dt = st_max/n_st;
  previous_start_time = 0;
  tsctx.i_st = 0;
  tsctx.i_st = tsctx.i_st + 1; //Why?

  for (this_start_time=st_dt;this_start_time<=st_max;this_start_time+=st_dt){
    //Go from previous start time to this_start_time
    tsctx.tau_evolve = 0;
    dt = (this_start_time - previous_start_time)/500; //500 is arbitrary, should be picked better
    time_step(dm0,previous_start_time,this_start_time,dt,steps_max);

    //Timestep through taus
    tau_t_max = this_start_time + tau_max;
    dt_tau = (tau_t_max - this_start_time)/n_tau;

    /*
     * Force an 'emission' to get A \rho A^\dag terms
     * We already have A* \cross A - we just do the multiplication
     */
    tsctx.tau_evolve = 1;
    //Copy the timestepped dm into our init_dm for tau sweep
    VecCopy(dm0,init_dm);
    MatMult(A_star_A,dm0,init_dm); //init_dm = A * dm0
    tsctx.i_tau = 0;
    time_step(init_dm,this_start_time,tau_t_max,dt_tau,steps_max);

    previous_start_time = this_start_time;
    tsctx.i_st = tsctx.i_st + 1;
  }

  return;
}
Beispiel #10
0
int main(int argc, char ** argv)
{
  long Block_order;        /* number of columns owned by rank       */
  long Block_size;         /* size of a single block                */
  long Colblock_size;      /* size of column block                  */
  int Tile_order=32;       /* default Tile order                    */
  int tiling;              /* boolean: true if tiling is used       */
  int Num_procs;           /* number of ranks                       */
  long order;              /* order of overall matrix               */
  int send_to, recv_from;  /* ranks with which to communicate       */
#if !SYNCHRONOUS
  MPI_Request send_req;
  MPI_Request recv_req;
#endif
  long bytes;              /* combined size of matrices             */
  int my_ID;               /* rank                                  */
  int root=0;              /* rank of root                          */
  int iterations;          /* number of times to do the transpose   */
  int i, j, it, jt, istart;/* dummies                               */
  int iter, iter_init;     /* index of iteration                    */
  int phase;               /* phase inside staged communication     */
  int colstart;            /* starting column for owning rank       */
  int error;               /* error flag                            */
  double * RESTRICT A_p;   /* original matrix column block          */
  double * RESTRICT B_p;   /* transposed matrix column block        */
  double * RESTRICT Work_in_p;/* workspace for transpose function   */
  double * RESTRICT Work_out_p;/* workspace for transpose function  */
  double abserr,           /* absolute error                        */
         abserr_tot;       /* aggregate absolute error              */
  double epsilon = 1.e-8;  /* error tolerance                       */
  double transpose_time,   /* timing parameters                     */
         avgtime;
  int    spare_ranks;      /* number of ranks to keep in reserve                  */
  int    kill_ranks;       /* number of ranks that die with each failure          */
  int    *kill_set;        /* instance of set of ranks to be killed        */
  int    kill_period;      /* average number of iterations between failures       */
  int    *fail_iter;       /* list of iterations when a failure will be triggered */
  int    fail_iter_s=0;    /* latest  */
  double init_add, addit;  /* used to offset initial solutions       */
  int    checkpointing;    /* indicates if data is restored using Fenix or
                             analytically                            */
  int    num_fenix_init=1; /* number of times Fenix_Init is called   */
  int    num_fenix_init_loc;/* number of times Fenix_Init was called */
  int    fenix_status;
  random_draw_t dice;

/*********************************************************************
** Initialize the MPI environment
*********************************************************************/
  MPI_Init(&argc,&argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &my_ID);
  MPI_Comm_size(MPI_COMM_WORLD, &Num_procs);

/*********************************************************************
** process, test and broadcast input parameters
*********************************************************************/
  error = 0;
  if (my_ID == root) {
    printf("Parallel Research Kernels version %s\n", PRKVERSION);
    printf("MPI matrix transpose with Fenix fault tolerance: B = A^T\n");

    if (argc != 7 && argc != 8){
      printf("Usage: %s <# iterations> <matrix order> <spare ranks> ",
                                                               *argv);
      printf("<kill set size> <kill period> <checkpointing> [Tile size]\n",
                                                               *argv);
      error = 1; goto ENDOFTESTS;
    }

    iterations  = atoi(argv[1]);
    if(iterations < 1){
      printf("ERROR: iterations must be >= 1 : %d \n",iterations);
      error = 1; goto ENDOFTESTS;
    }

    order = atol(argv[2]);
    spare_ranks  = atoi(argv[3]);
    if (order < Num_procs-spare_ranks) {
      printf("ERROR: matrix order %ld should at least # procs %d\n",
             order, Num_procs-spare_ranks);
      error = 1; goto ENDOFTESTS;
    }
    if (order%(Num_procs-spare_ranks)) {
      printf("ERROR: matrix order %ld should be divisible by # procs %d\n",
             order, Num_procs-spare_ranks);
      error = 1; goto ENDOFTESTS;
    }

    if (spare_ranks < 0 || spare_ranks >= Num_procs){
      printf("ERROR: Illegal number of spare ranks : %d \n", spare_ranks);
      error = 1;
      goto ENDOFTESTS;     
    }

    kill_ranks = atoi(argv[4]);
    if (kill_ranks < 0 || kill_ranks > spare_ranks) {
      printf("ERROR: Number of ranks in kill set invalid: %d\n", kill_ranks);
      error = 1;
      goto ENDOFTESTS;     
    }

    kill_period = atoi(argv[5]);
    if (kill_period < 1) {
      printf("ERROR: rank kill period must be positive: %d\n", kill_period);
      error = 1;
      goto ENDOFTESTS;     
    }

    checkpointing = atoi(argv[6]);
    if (checkpointing) {
      printf("ERROR: Fenix checkpointing not yet implemented\n");
      error = 1;
      goto ENDOFTESTS;     
    }

    if (argc == 8) Tile_order = atoi(argv[7]);

    ENDOFTESTS:;
  }
  bail_out(error);

  /*  Broadcast input data to all ranks */
  MPI_Bcast(&order,         1, MPI_LONG, root, MPI_COMM_WORLD);
  MPI_Bcast(&iterations,    1, MPI_INT,  root, MPI_COMM_WORLD);
  MPI_Bcast(&Tile_order,    1, MPI_INT,  root, MPI_COMM_WORLD);
  MPI_Bcast(&spare_ranks,   1, MPI_INT, root, MPI_COMM_WORLD);
  MPI_Bcast(&kill_ranks,    1, MPI_INT, root, MPI_COMM_WORLD);
  MPI_Bcast(&kill_period,   1, MPI_INT, root, MPI_COMM_WORLD);
  MPI_Bcast(&checkpointing, 1, MPI_INT, root, MPI_COMM_WORLD);

  if (my_ID == root) {
    printf("Number of ranks       = %d\n", Num_procs);
    printf("Matrix order          = %ld\n", order);
    printf("Number of iterations  = %d\n", iterations);
    if ((Tile_order > 0) && (Tile_order < order))
          printf("Tile size             = %d\n", Tile_order);
    else  printf("Untiled\n");
#if !SYNCHRONOUS
    printf("Non-");
#endif
    printf("Blocking messages\n");
    printf("Number of spare ranks = %d\n", spare_ranks);
    printf("Kill set size         = %d\n", kill_ranks);
    printf("Fault period          = %d\n", kill_period);
    if (checkpointing)
      printf("Data recovery         = Fenix checkpointing\n");
    else
      printf("Data recovery         = analytical\n");
  }

  /* initialize the random number generator for each rank; we do that before
     starting Fenix, so that all ranks, including spares, are initialized      */
  LCG_init(&dice);
  /* compute the iterations during which errors will be incurred               */
  for (iter=0; iter<=iterations; iter++) {
    fail_iter_s += random_draw(kill_period, &dice);
    if (fail_iter_s > iterations) break;
    num_fenix_init++;
  }
  if ((num_fenix_init-1)*kill_ranks>spare_ranks) {
    if (my_ID==0) printf("ERROR: number of injected errors %d exceeds spare ranks %d\n",
                         (num_fenix_init-1)*kill_ranks, spare_ranks);
    error = 1;
  }
  else if(my_ID==root) printf("Total injected failures  = %d times %d errors\n", 
                           num_fenix_init-1, kill_ranks);
  bail_out(error);
  if ((num_fenix_init-1)*kill_ranks>=Num_procs-spare_ranks) if (my_ID==root)
  printf("WARNING: All active ranks will be replaced by recovered ranks; timings not valid\n");

  fail_iter = (int *) prk_malloc(sizeof(int)*num_fenix_init);
  if (!fail_iter) {
    printf("ERROR: Rank %d could not allocate space for array fail_iter\n", my_ID);
    error = 1;
  }
  bail_out(error);

  /* reinitialize random number generator to obtain identical error series     */
  LCG_init(&dice);
  /* now record the actual failure iterations                                  */
  for (fail_iter_s=iter=0; iter<num_fenix_init; iter++) {
    fail_iter_s += random_draw(kill_period, &dice);
    fail_iter[iter] = fail_iter_s;
  }

  /* Here is where we initialize Fenix and mark the return point after failure */
  Fenix_Init(&fenix_status, MPI_COMM_WORLD, NULL, &argc, &argv, spare_ranks, 
             0, MPI_INFO_NULL, &error);

  if (error==FENIX_WARNING_SPARE_RANKS_DEPLETED) 
    printf("ERROR: Rank %d: Cannot reconstitute original communicator\n", my_ID);
  bail_out(error);

  MPI_Comm_rank(MPI_COMM_WORLD, &my_ID);
  MPI_Comm_size(MPI_COMM_WORLD, &Num_procs);

  /* if rank is recovered, set iter to a negative number, to be increased
     to the actual value corresponding to the current iter value among
     survivor ranks; handle number of Fenix_Init calls similarly               */
  switch (fenix_status){
    case FENIX_ROLE_INITIAL_RANK:   iter_init = num_fenix_init_loc = 0;    break;
    case FENIX_ROLE_RECOVERED_RANK: iter_init = num_fenix_init_loc = iterations+1;   break;
    case FENIX_ROLE_SURVIVOR_RANK:  iter_init = iter;  num_fenix_init_loc++;
  }

  MPI_Allreduce(&iter_init, &iter, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
  MPI_Allreduce(&num_fenix_init_loc, &num_fenix_init, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);

  /* a non-positive tile size means no tiling of the local transpose */
  tiling = (Tile_order > 0) && (Tile_order < order);
  bytes = 2 * sizeof(double) * order * order;

/*********************************************************************
** The matrix is broken up into column blocks that are mapped one to a
** rank.  Each column block is made up of Num_procs smaller square
** blocks of order block_order.
*********************************************************************/

  Block_order    = order/Num_procs;
  colstart       = Block_order * my_ID;
  Colblock_size  = order * Block_order;
  Block_size     = Block_order * Block_order;

/*********************************************************************
** Create the column block of the test matrix, the row block of the
** transposed matrix, and workspace (workspace only if #procs>1)
*********************************************************************/
  if (fenix_status != FENIX_ROLE_SURVIVOR_RANK) {
    A_p = (double *)prk_malloc(Colblock_size*sizeof(double));
    if (A_p == NULL){
      printf(" Error allocating space for original matrix on node %d\n",my_ID);
      error = 1;
    }
  }
  bail_out(error);

  if (fenix_status != FENIX_ROLE_SURVIVOR_RANK) {
    B_p = (double *)prk_malloc(Colblock_size*sizeof(double));
    if (B_p == NULL){
      printf(" Error allocating space for transpose matrix on node %d\n",my_ID);
      error = 1;
    }
  }
  bail_out(error);

  if (fenix_status != FENIX_ROLE_SURVIVOR_RANK && Num_procs>1) {
    Work_in_p   = (double *)prk_malloc(2*Block_size*sizeof(double));
    if (Work_in_p == NULL){
      printf(" Error allocating space for work on node %d\n",my_ID);
      error = 1;
    }
    Work_out_p = Work_in_p + Block_size;
  }
  bail_out(error);

  /* Fill the original column matrix                                                */
  /* intialize the input and output arrays, note that if we use the analytical
     solution to initialize, one might be tempted to skip this step for survivor
     ranks, because they already have the correct (interim) values. That would
     be wrong for two reasons: It is possible for ranks to be in different time
     steps at the same time, and it is possible that error signal delivery to
     a rank is delayed                                                         */
  if (checkpointing) {
    init_add = 0.0;
    addit    = 0.0;
  }
  else {
    init_add = (double) iter;
    addit = ((double)(iter-1) * (double) (iter))/2.0;
  }
  istart = 0;
  for (j=0;j<Block_order;j++)
    for (i=0;i<order; i++)  {
      A(i,j) = (double) (order*(j+colstart) + i) + init_add;
      B(i,j) = ((double) ((j+colstart) + order*i)*iter + addit);
  }

  for (; iter<=iterations; iter++){

    /* start timer after a warmup iteration                                        */
    if (iter == 1) {
      MPI_Barrier(MPI_COMM_WORLD);
      transpose_time = wtime();
    }

    /* inject failure if appropriate                                                */
    if (iter == fail_iter[num_fenix_init]) {
      pid_t pid = getpid();
      if (my_ID < kill_ranks) {
#if VERBOSE
        printf("Rank %d, pid %d commits suicide in iter %d\n", my_ID, pid, iter);
#endif
        kill(pid, SIGKILL);
      }
#if VERBOSE
      else printf("Rank %d, pid %d is survivor rank in iter %d\n", my_ID, pid, iter);
#endif
    }

    time_step(Block_order, Block_size, Colblock_size, Tile_order, tiling,
              Num_procs, order, my_ID, colstart, A_p, B_p, Work_in_p, Work_out_p);

  } /* end of iterations */

  MPI_Barrier(MPI_COMM_WORLD);
  transpose_time = wtime() - transpose_time;;

  abserr = 0.0;
  istart = 0;
  addit = ((double)(iterations+1) * (double) (iterations))/2.0;
  for (j=0;j<Block_order;j++) for (i=0;i<order; i++) {
      abserr += ABS(B(i,j) - (double)((order*i + j+colstart)*(iterations+1)+addit));
  }

  root = Num_procs-1;
  MPI_Reduce(&abserr, &abserr_tot, 1, MPI_DOUBLE, MPI_SUM, root, MPI_COMM_WORLD);

  if (my_ID == root) {
    if (abserr_tot < epsilon) {
      printf("Solution validates\n");
      avgtime = transpose_time/(double)iterations;
      printf("Rate (MB/s): %lf Avg time (s): %lf\n",1.0E-06*bytes/avgtime, avgtime);
#if VERBOSE
      printf("Summed errors: %f \n", abserr);
#endif
    }
    else {
      printf("ERROR: Aggregate squared error %lf exceeds threshold %e\n", abserr, epsilon);
      error = 1;
    }
  }

  bail_out(error);

  Fenix_Finalize();
  MPI_Finalize();
  exit(EXIT_SUCCESS);

}  /* end of main */
Beispiel #11
0
int main(int argc,char **args){
  PetscReal time_max,dt,*gamma_1,*gamma_2;
  PetscScalar mat_val;
  PetscInt  steps_max,num_qubits;
  int i;
  Vec rho;
  circuit projectq_read;
  char           string[10],filename[128],file_dm[128];

  /* Initialize QuaC */
  QuaC_initialize(argc,args);

  num_qubits = 0;
  /* Get the encoding strategies for each qubit */

  strcpy(filename,"NULL");
  strcpy(file_dm,"dm.dat");
  PetscOptionsGetString(NULL,NULL,"-file_circ",filename,sizeof(filename),NULL);
  PetscOptionsGetString(NULL,NULL,"-file_dm",file_dm,sizeof(file_dm),NULL);
  quil_read(filename,&num_qubits,&projectq_read); // Read the file

  qubits  = malloc(num_qubits*sizeof(struct operator)); //Allocate structure to store qubits
  gamma_1 = malloc(num_qubits*sizeof(PetscReal)); //Allocate array for qubit error rates
  gamma_2 = malloc(num_qubits*sizeof(PetscReal));

  for (i=0;i<num_qubits;i++){
    create_op(2,&qubits[i]); //create qubit
    gamma_1[i] = 0;
    gamma_2[i] = 0;
  }

  for (i=0; i<num_qubits;i++){
    //Read errors from command line using -gam0, -gam1, etc
    sprintf(string, "-gam%d",i);
    PetscOptionsGetReal(NULL,NULL,string,&gamma_1[i],NULL);
    sprintf(string, "-dep%d",i);
    PetscOptionsGetReal(NULL,NULL,string,&gamma_2[i],NULL);
  }

  //Add errors to lindblad term
  for (i=0;i<num_qubits;i++){
    add_lin(gamma_1[i]/2,qubits[i]);
    add_lin(gamma_2[i]*2,qubits[i]->n);
  }

  if (nid==0){
    // Print read in variables
    printf("qubit gam dep\n");
    for (i=0;i<num_qubits;i++){
      printf("%d %f %f \n",i,gamma_1[i],gamma_2[i]);
    }
  }
  //Time step until 1 after the last gate; gates are applied every 1.0 time unit
  time_max  = projectq_read.num_gates+1;
  dt        = 0.01;
  steps_max = 100000;

  /* Set the ts_monitor to print results at each time step, if desired */
  set_ts_monitor(ts_monitor);
  /* Open file that we will print to in ts_monitor */
  if (nid==0){
    f_pop = fopen("pop","w");
    fprintf(f_pop,"#Time Populations\n");
  }

  create_full_dm(&rho); //Allocate and set our initial density matrix
  mat_val = 1.0;
  add_value_to_dm(rho,0,0,mat_val);
  assemble_dm(rho);
  if (nid==0){
    //Print number of gates
    printf("num_gates: %ld\n",projectq_read.num_gates);
  }

  //Start out circuit at time 0.0, first gate will be at 1.0
  start_circuit_at_time(&projectq_read,0.0);
  //Run the evolution, with error and with the circuit
  time_step(rho,0.0,time_max,dt,steps_max);
  print_dm_sparse_to_file(rho,pow(2,num_qubits),file_dm);
  //Clean up memory
  for (i=0;i<num_qubits;i++){
    destroy_op(&qubits[i]);
  }
  destroy_dm(rho);
  QuaC_finalize();
  return 0;
}
int main(int argc, char** argv) 
{
    ros::init(argc, argv, "base_planner_node", ros::init_options::NoSigintHandler);
    signal(SIGINT, ctrl_C_Handler);
    ros::NodeHandle nh;
    tf::TransformListener lr(ros::Duration(3));
    listener = &lr;

    ros::ServiceClient controller_srv_client = nh.serviceClient<std_srvs::SetBool>("controller_cmd");
    ros::ServiceServer set_position = nh.advertiseService("give_goal", giveGoal);

    ros::param::param<double>("~time_step" , path_t_step , 0.1); 
    ros::param::param<double>("~target_speed", target_speed, 0.01); 
    ros::param::param<double>("~target_accel", target_accel, 0.02); 
    ros::param::param<double>("~probe_offset", probe_offset, 0.525); 

    ros::Duration time_step(path_t_step);

    ros::Subscriber cmd_pose_sub = nh.subscribe("/move_base_simple/goal", 1, moveBaseSimpleCallback);
    ros::Subscriber phase_space_sub =  nh.subscribe("map_to_cepheus", 1, PhaseSpaceCallback);
       
    ros::Publisher path_pub = nh.advertise<nav_msgs::Path>("path", 1000);


    geometry_msgs::PoseStamped cmd_pos;
	geometry_msgs::TwistStamped cmd_vel;
    cmd_pos.header.frame_id = "drogue";
	cmd_vel.header.frame_id = "drogue";
	geometry_msgs::Vector3 cmd_acc;

    ros::Publisher pos_pub = nh.advertise<geometry_msgs::PoseStamped>("planner_pos", 1);
    ros::Publisher vel_pub = nh.advertise<geometry_msgs::TwistStamped>("planner_vel", 1);
    ros::Publisher acc_pub = nh.advertise<geometry_msgs::Vector3>("planner_acc", 1);

    bool path_running=false;
    int cnt;

    while(!g_request_shutdown)
    {
        if (new_path) {
        	ROS_INFO("New Path calculated");
        	new_path=false;
        	path_running=true;
        	cnt = 0;
    		//path_pub.publish(path);

    		//request to enable controller
			std_srvs::SetBool srv;
			srv.request.data = true;
			if (controller_srv_client.call(srv)) {
				ROS_INFO_STREAM("Controller response: " << srv.response.message);
			}
			else{
				ROS_ERROR("Failed to call Controller");
			}
    	}


    	if(path_running) 
    	{
			cmd_pos.pose.position.x = path_matrix(0,cnt);
			cmd_pos.pose.position.y = path_matrix(1,cnt);
			cmd_pos.pose.position.z =0.0;
			cmd_pos.pose.orientation = tf::createQuaternionMsgFromYaw(path_matrix(2,cnt));

	 		cmd_vel.twist.linear.x = path_matrix(3,cnt);
	 		cmd_vel.twist.linear.y = path_matrix(4,cnt);
	 		cmd_vel.twist.angular.z = path_matrix(5,cnt);
			cmd_acc.x = path_matrix(6,cnt);
	 		cmd_acc.y = path_matrix(7,cnt);
			cmd_acc.z = path_matrix(8,cnt);

			pos_pub.publish(cmd_pos);
			vel_pub.publish(cmd_vel);
			acc_pub.publish(cmd_acc);	

			cnt++;
			if(cnt >= path_matrix.cols()) {
				path_running = false;

				//request to disable controller
				std_srvs::SetBool srv;
				srv.request.data = false;
				if (controller_srv_client.call(srv)) {
					ROS_INFO_STREAM("Controller response: " << srv.response.message);
				}
				else{
					ROS_ERROR("Failed to call Controller");
				}
			}
		}

        ros::spinOnce(); 
        //wait one time step
        time_step.sleep();
    }
    return 0;
}
int main(int argc, char *argv[])
{
  char   input_file[ONE_LINE],
         *func = {" main(int argc, char *argv[]) :  "};
  FILE   *file_ptr;
  SHELL  model;
  TID   wallTime, cpuTime;


  MPI_Init(&argc, &argv);      
  MPI_Comm_size(MPI_COMM_WORLD,&NumProcs);
  MPI_Comm_rank(MPI_COMM_WORLD, &Rank);

  time_step(10,1);  // time controll for total shell model calc 

  // Read file names and open file containing basic input data

  if(argc >= 2) { // as program parameters
    strcpy(input_file, argv[1]); // save in_data input_file
  }
  else  { // or as separate typed in filename
    printf("\n Type in file name for basic shell model data = ");
    scanf("%s",input_file);
  } // end of data-file-name input

  // Time control for the total shell model calculation

  wallClock(1,0); // initialization 
  cpuClock(1,0);
  wallClock(1,1); // start time
  cpuClock(1,0);

  input_process(input_file, &model); // module shell-model-input.c

   /********************  test output  ********/

  if(Rank == MASTER) {
    FILE    *file_ptr;

    if((file_ptr = fopen(model.title,"a"))== NULL) {
      printf("\n\nRank(%d): Error in function main()():",Rank);
      printf("\nWrong file = %s for the output data\n", model.title);
      MPI_Abort(MPI_COMM_WORLD,Rank);
    }
    fprintf(file_ptr,"\n\n     NumProcs = %d\n\n\n",NumProcs);
    fflush(file_ptr);
    fclose(file_ptr);
  } // end MASTER
  /****************   end test outpt ********/


  strcpy(TEST_FILE_NAME, model.title);    // parameter for test output 


  // all files identifies with rank number

  if(  (!strcmp(model.type_calc,"random-start"))
     ||(!strcmp(model.type_calc,"dimension"))) {

       /*
       ** A Lanczos iteration shell model calculation
       ** based on random initial state.
       */

    id_LancIterateCalc(&model);

  }
  else {
    printf("\n\n The calculation process: %s not implemented\n\n",
                                                model.type_calc);
   MPI_Abort(MPI_COMM_WORLD,Rank);
  }

  // Stop and read total process time 

  wallClock(1,2); // stop tile
  cpuClock(1,2);
  wallTime = wallClock(1,3);  // read time
  cpuTime  = cpuClock(1,3);

  if(Rank == MASTER) {
    if((file_ptr = fopen(model.title,"a"))== NULL) {
      printf("\n\nError in function lanc-main.c():");
      printf("\nWrong file = %s for the output data\n", model.title);
     MPI_Abort(MPI_COMM_WORLD,Rank);
    }
    fprintf(file_ptr, "\n\nTotal shell model process wall time used");  
    fprintf(file_ptr, " %llu hour %llu min %llu sec",
	  wallTime.hour, wallTime.min, wallTime.sec);
    fprintf(file_ptr, "\nTotal shell model process cpu time used");  
    fprintf(file_ptr, " %llu hour %llu min %llu sec\n",
	  cpuTime.hour, cpuTime.min, cpuTime.sec);
    fclose(file_ptr);
  }  // end MASTER

  //   MPI_Finalize();

  MPI_Finalize();




  return 0; // sucsessfull termination

} // End: function main()