Ejemplo n.º 1
0
int  main (int argc, char **argv){
  int	numbers[MAX_ARRAY+1];	/* An array big enough */
  int	i, index, searchval;
  float average;

  printf ("Enter a series of integer values.\n");
  printf ("Separate them by spaces or newlines.\n");
  printf ("Terminate the series by entering a negative number: ");

  i = 0;			/* First entry goes here */

  scanf ("%d", &numbers[i]);
  while (numbers[i] >= 0){
    i++;
    printf ("Enter a number, < 0 to quit: ");
    scanf ("%d", &numbers[i]);
  }

  printf ("Calling print_array\n");

  print_array (numbers);

  printf ("The sum of the numbers you entered is %d\n", sum_array (numbers));

  printf ("The average of the numbers you entered is %f\n", 
	  average_array (numbers));

  printf ("Now enter an integer value to search for: ");
  scanf ("%d", &searchval);

  while (searchval >= 0){
    index = search_array (numbers, searchval);
    if (index < 0)
      printf ("Value %d not found in array\n", searchval);
    else
      printf ("First occurrence of %d found at index %d\n", searchval, index);

    printf ("Enter a new integer search value, or a negative value to quit: ");
    scanf ("%d", &searchval);
  }

}
Ejemplo n.º 2
0
void ts_fc_determine_potential (double *Grid, int Nx, int Ny, int Nz, double dx, double dy, double dz, int xmin, int ymin, double t, QCADLayer *clocking_layer, ts_fc_OP *options)
{
	int i;
	int j;
	int k;
	int w;
	double dx2, dy2; 
	double dz2;
	int cmp;
	int iter, iter_count;
	double er_elec, er_cell;
	double *er_array;
	double dist_to_ground;
	double cell_layer;
	GList *llItr = NULL;
	QCADRectangleElectrode *rc_electrode;
	QCADElectrode *electrode;
	
	llItr = clocking_layer->lstObjs;			
	electrode = (QCADElectrode *)(llItr->data);
	er_elec = electrode->electrode_options.relative_permittivity;
	er_cell = options->epsilonR;
	
	cell_layer = options->cell_elevation;
	
	er_array = (double*)malloc(Nz*sizeof(double));
	for (i = 0; i < Nz; i++) {
		if (i*dz < cell_layer) {
			er_array[i] = er_elec;
		}
		else {
			er_array[i] = er_cell;
		}
	}
	
	
	int elec_flag = 0;
	
	double thresh = options->convergence_tolerance;
	//double thresh = 1e-3;
	
	int loc_x1, loc_x2, loc_y1, loc_y2, loc_z;
	
	dx2 = dx*dx;
	dy2 = dy*dy;
	dz2 = dz*dz;	
	
	double one_over_dx2 = 1/dx2;
	double one_over_dy2 = 1/dy2;
	double one_over_dz2 = 1/dz2;
	
	double *Grid_old;
	double *Grid_temp;
	double *Diff;
	
	Grid_old = (double*)malloc(Nx*Ny*Nz*sizeof(double));
	array_copy(Grid,Grid_old,Nx*Ny*Nz);
	
	iter = 1;
	iter_count = 0;
	while (iter) {		
		iter_count = iter_count+1;
		w = 0;
		for (i = 0; i < Nx; i++) {
			for (j = 0; j < Ny; j++) {
				for (k = 0; k < Nz; k++) {
					if (k == 0) {
						for(llItr = clocking_layer->lstObjs; llItr != NULL; llItr = llItr->next){
							if (!elec_flag) {
								if(llItr->data != NULL){
									rc_electrode = (QCADRectangleElectrode *)(llItr->data);
									if ( (xmin+dx*i >= rc_electrode->precompute_params.pt[0].xWorld) && (xmin+dx*i <= rc_electrode->precompute_params.pt[1].xWorld) ) {
										if ( (ymin+dy*j >= rc_electrode->precompute_params.pt[0].yWorld) && (ymin+dy*j <= rc_electrode->precompute_params.pt[2].yWorld) ) {
											Grid[w] = qcad_electrode_get_voltage ((QCADElectrode *)rc_electrode, t);
											elec_flag = 1;
										}
									}
								}
							}
						}
					}
					if (elec_flag == 0) {
						loc_x1 = (i == 0) ? 0 : (i-1)*Ny*Nz; 
						loc_x2 = (i == Nx-1) ? (Nx-1)*Ny*Nz : (i+1)*Ny*Nz;
						loc_y1 = (i == 0) ? 0 : (j-1)*Nz; 
						loc_y2 = (i == Ny-1) ? (Ny-1)*Nz : (j+1)*Nz;
						loc_z = (k == 0) ? k : k-1;
						
						if (k == Nz-1) {
							Grid[w] = 0;
						}
						else {
							Grid[w] = (er_array[k]*one_over_dx2*(Grid[loc_x1 + j*Nz + k] + Grid[loc_x2 + j*Nz + k]) + er_array[k]*one_over_dy2*(Grid[i*Ny*Nz + loc_y1 + k] + Grid[i*Ny*Nz + loc_y2 + k]) + one_over_dz2*(er_array[k]*Grid[i*Ny*Nz + j*Nz + k+1] + (2*er_array[k-1]-er_array[k])*Grid[i*Ny*Nz + j*Nz + loc_z]))/(2*er_array[k]*(one_over_dx2 + one_over_dy2) + 2*er_array[k-1]*one_over_dz2);
						}
					}
					w=w+1;
					elec_flag = 0;
				}
			}
		}
		
		Diff = compare_arr(Grid, Grid_old, Nx*Ny*Nz);
		cmp = search_array(Diff, thresh, Nx*Ny*Nz);
		
		if (cmp == -1) {
			iter = 0;
		}
		else {
			array_copy(Grid,Grid_old,Nx*Ny*Nz);
		}
		free(Diff);
}
//printf("It took %d iterations to converge @ time %e\n", iter_count, t);
free(Grid_old);
free(er_array);
array_copy(Grid,pot_grid,Nx*Ny*Nz);
}// ts_fc_determine_potential