Ejemplo n.º 1
0
void eval_A_times_u(int N, const double u[], double Au[])
{
  int i,j,n2;
  double t0,t1;

  n2=N&~1;
  for(i=0;i<n2;i+=2)
    {
      t0=0;
      t1=0;
      for(j=0;j<N;j++)
	{
	  t0+=get_A(i,j)*u[j];
	  t1+=get_A(i+1,j)*u[j];
	}
      Au[i]=t0;
      Au[i+1]=t1;
    }

  if(i!=N)
    {
      t0=0;
      for(j=0;j<N;j++)
        {
	  t0+=get_A(i,j)*u[j];
	}
      Au[i]=t0;
    }
}
Ejemplo n.º 2
0
void eval_At_times_u(int N, const double u[], double Au[])
{
  int i,j,n4;
  double t0,t1,t2,t3;

  n4=N&~3;
  for(i=0;i<n4;i+=4)
    {
      t0=0;
      t1=0;
      t2=0;
      t3=0;
      for(j=0;j<N;j++)
        {
	  t0+=get_A(j,i)*u[j];
	  t1+=get_A(j,i+1)*u[j];
	  t2+=get_A(j,i+2)*u[j];
	  t3+=get_A(j,i+3)*u[j];
        }
      Au[i]=t0;
      Au[i+1]=t1;
      Au[i+2]=t2;
      Au[i+3]=t3;
    }

  for(;i<N;i++)
    {
      t0=0;
      for(j=0;j<N;j++)
        {
	  t0+=get_A(j,i)*u[j];
	}
      Au[i]=t0;
    }
}
 virtual void initialize() 
 {
    if(get_x()!=Teuchos::null) get_x()->putScalar(0.0);
    if(get_dxdt()!=Teuchos::null) get_dxdt()->putScalar(0.0);
    if(get_f()!=Teuchos::null) get_f()->putScalar(0.0);
    if(get_A()!=Teuchos::null) {
      Teuchos::RCP<CrsMatrixType> mat = get_A(); 
      mat->resumeFill();
      mat->setAllToScalar(0.0);
      mat->fillComplete();
    }
 }
Ejemplo n.º 4
0
/******************************************************************************
 * MAIN
 ******************************************************************************/
int main(int argc, char * argv[])
{
    int A[MAX_NUM_NR];
    char * filename_unsorted = get_filename(argc, argv);
    char * filename_sorted = get_sorted_filename(filename_unsorted);

    int n = get_A(A, MAX_NUM_NR, filename_unsorted);

    print_A(A, n, "original:");
    heap_sort(A, n);
    print_A(A, n, "sorted:");

    assert(verify_A(A, n, filename_sorted));

    /* priority queue */
    int heapsize = n;
    build_max_heap(A, heapsize);
    ______________________________(A, n, heapsize, "===============\\nafter build_max_heap\\n===============");

    printf("heap_maximum: %d\n", heap_maximum(A, heapsize));

    printf("heap_extract_max: %d\n", heap_extract_max(A, &heapsize));
    print_A(A, heapsize, "after heap_extract_max");
    ______________________________(A, n, heapsize, "===============\\nafter heap_extract_max\\n===============");

    heap_increase_key(A, heapsize, 3, 88);
    print_A(A, heapsize, "after heap_increase_key");
    ______________________________(A, n, heapsize, "===============\\nafter heap_increase_key\\n===============");

    max_heap_insert(A, n, &heapsize, 97);
    print_A(A, heapsize, "after max_heap_insert");
    ______________________________(A, n, heapsize, "===============\\nafter max_heap_insert\\n===============");

    return 0;
}
Ejemplo n.º 5
0
bool ButcherTable::is_fully_implicit()
{
  bool result = false;
  for (unsigned int i = 0; i < size; i++) {
    for (unsigned int j = 0; j < size; j++) {
      double val_ij = get_A(i, j);
      if (j > i && fabs(val_ij) > 1e-12) result = true;
    }
  }

  return result;
} 
Ejemplo n.º 6
0
/******************************************************************************
 * main
 ******************************************************************************/
int main(int argc, char * argv[])
{
    int A[MAX_NUM_NR];
    char * filename_unsorted = get_filename(argc, argv);
    char * filename_sorted = get_sorted_filename(filename_unsorted);

    int n = get_A(A, MAX_NUM_NR, filename_unsorted);

    print_A(A, n, "original:");
    insertion_sort(A, n);
    print_A(A, n, "sorted:");

    assert(verify_A(A, n, filename_sorted));

    return 0;
}
Ejemplo n.º 7
0
/******************************************************************************
 * main
 ******************************************************************************/
int main(int argc, char * argv[])
{
	int n;
	int A[MAX_NUM_NR];
	char * filename_unsorted = get_filename(argc, argv);
	char * filename_sorted = get_sorted_filename(filename_unsorted);

	int i;
	for (i = 0; i < 2; i++) {
		n = get_A(A, MAX_NUM_NR, filename_unsorted);
		print_A(A, n, "original:");
		merge_sort(A, n, i == 0 ? normal : sentinel);
		print_A(A, n, "sorted (merge sort, %s merge):",
			i == 0 ? "normal" : "sentinel");
		assert(verify_A(A, n, filename_sorted));
	}

	return 0;
}
Ejemplo n.º 8
0
int main(int argc, char * argv[])
{
	int A[MAX_NUM_NR];
	char * filename_unsorted = 0;
	int r = 0;

	opterr = 0;
	while (1) {
		int c;
		static struct option long_options[] = {
			{"verbose",      required_argument, 0, 'v'},
			{"radix",        required_argument, 0, 'r'},
			{"file",         required_argument, 0, 'f'},
			{0, 0, 0, 0}
		};
		/* getopt_long stores the option index here. */
		int option_index = 0;
     
		c = getopt_long(argc, argv, "v:r:f:",
				long_options, &option_index);
     
		/* Detect the end of the options. */
		if (c == -1)
			break;
     
		switch (c) {
		case 0:
			/* If this option set a flag, do nothing else now. */
			if(long_options[option_index].flag != 0)
				break;
			printf("option %s", long_options[option_index].name);
			if(optarg)
				printf(" with arg %s", optarg);
			printf("\n");
			break;
     
		case 'v':
			verbose = atoi_or_abort(optarg);
			printf("%sverbose:%d%s\n", GREEN_B, verbose, NOCOLOR);
			break;
     
		case 'r':
			r = atoi_or_abort(optarg);
			printf("%sr:%d%s\n", GREEN_B, r, NOCOLOR);
			break;
     
		case 'f':
			filename_unsorted = optarg;
			break;
     
		case '?':
			/* getopt_long already printed an error message. */
			printf("%sbad arg. %s%s\n", RED_B, argv[optind-1], NOCOLOR);
			break;
     
		default:
			abort();
		}
	}
	/* Print any remaining command line arguments (not options). */
	if (optind < argc)
	{
		printf("these ARGV-elements will be ignored: %s", RED);
		while (optind < argc)
			printf ("%s ", argv[optind++]);
		printf("%s\n", NOCOLOR);
	}

	if (!filename_unsorted) {
		printf("%sNo test case file given, abort.%s\n", RED_B, NOCOLOR);
		print_usage(argv[0]);
		abort();
	}

	if (!r) {
		printf("%sNo radix is given, abort.%s\n", RED_B, NOCOLOR);
		print_usage(argv[0]);
		abort();
	}

	char * filename_sorted = get_sorted_filename(filename_unsorted);

	int n = get_A(A, MAX_NUM_NR, filename_unsorted);

	print_A(A, n, "original:");
	radix_sort(A, n, 32, r);
	/* counting_sort(A, n, r, 0); */
	print_A(A, n, "sorted:");

	assert(verify_A(A, n, filename_sorted));

	return 0;
}
Ejemplo n.º 9
0
/*************************************
 * Jovi: update for multiple purpose *
 * BEST FIRST SEARCH IMPLEMENTATION  *
 *************************************/
Bool do_best_first_search_for_multiple_purpose ( void ) {
    
	static Bool fc_for_multiple_purpose = TRUE; /*first round*/
	static State S;
    
	BfsNode *first;
	int i, min = INFINITY;
	Bool start = TRUE;
    
	if ( fc_for_multiple_purpose ) {
		make_state( &S, gnum_ft_conn );
		S.max_F = gnum_ft_conn;
		fc_for_multiple_purpose = FALSE;
	}
    
	lbfs_space_head = new_BfsNode();
	lbfs_space_had = NULL;
    
	for ( i = 0; i < BFS_HASH_SIZE; i++ ) {
		lbfs_hash_entry[i] = NULL;
	}
    
	add_to_bfs_space_for_multiple_purpose( &ginitial_state, -1, NULL );
    
	while ( TRUE ) {
		if ( (first = lbfs_space_head->next) == NULL ) {
			printf("\n\nbest first search space empty! problem proven unsolvable.\n\n");
			return FALSE;
		}
        
		lbfs_space_head->next = first->next;
		if ( first->next ) {
			first->next->prev = lbfs_space_head;
		}
        
		if ( LESS( first->h, min ) ) {
			min = first->h;
			if ( start ) {
				printf("\nadvancing to distance : %4d", min);
				start = FALSE;
			} else {
				printf("\n                        %4d", min);
			}
            
			/*
             * modified by JC: return ealier
             */
			if(is_solved_state( &first->S )){
				printf("\nstate have seen. return!\n");
				break;
			}
		}
        
		if ( first->h == 0 ) {
			break;
		}
        
		get_A( &(first->S) );
		for ( i = 0; i < gnum_A; i++ ) {
            
			/*if(g_is_strong){*/
			/*JC: dismiss invalid actions*/
            int k;
            Bool found = FALSE;
            for(k = 0; k < gnum_IV; k++){
                if(gA[i] == gInvActs[k].act){
                    found = TRUE;
                    break;
                }
            }
			/*}*/
            
			if(found) continue;
            
			result_to_dest( &S, &(first->S), gA[i] );
			add_to_bfs_space_for_multiple_purpose( &S, gA[i], first );
		}
        
		first->next = lbfs_space_had;
		lbfs_space_had = first;
	}
    
	extract_plan( first );
	return TRUE;
}
Ejemplo n.º 10
0
/* dD_nac = a * D_nac * (A'/A + B'/B - C'/C) */
static void get_derivative_nac(double *ddnac,
                               double *dnac,
                               const int num_patom,
                               const double *lattice,
                               const double *mass,
                               const double *q,
                               const double *born,
                               const double *dielectric,
                               const double *q_direction,
                               const double factor)
{
  int i, j, k, l, m;
  double a, b, c, da, db, dc, volume, mass_sqrt;
  double q_cart[3], rec_lat[9];

  volume =
    lattice[0] * (lattice[4] * lattice[8] - lattice[5] * lattice[7]) +
    lattice[1] * (lattice[5] * lattice[6] - lattice[3] * lattice[8]) +
    lattice[2] * (lattice[3] * lattice[7] - lattice[4] * lattice[6]);

  rec_lat[0] = (lattice[4] * lattice[8] - lattice[5] * lattice[7]) / volume;
  rec_lat[1] = (lattice[5] * lattice[6] - lattice[3] * lattice[8]) / volume;
  rec_lat[2] = (lattice[3] * lattice[7] - lattice[4] * lattice[6]) / volume;
  rec_lat[3] = (lattice[7] * lattice[2] - lattice[8] * lattice[1]) / volume;
  rec_lat[4] = (lattice[8] * lattice[0] - lattice[6] * lattice[2]) / volume;
  rec_lat[5] = (lattice[6] * lattice[1] - lattice[7] * lattice[0]) / volume;
  rec_lat[6] = (lattice[1] * lattice[5] - lattice[2] * lattice[4]) / volume;
  rec_lat[7] = (lattice[2] * lattice[3] - lattice[0] * lattice[5]) / volume;
  rec_lat[8] = (lattice[0] * lattice[4] - lattice[1] * lattice[3]) / volume;

  for (i = 0; i < 3; i++) {
    q_cart[i] = 0;
    for (j = 0; j < 3; j++) {
      if (q_direction) {
        q_cart[i] += rec_lat[i * 3 + j] * q_direction[j];
      } else {
        q_cart[i] += rec_lat[i * 3 + j] * q[j];
      }
    }
  }

  c = get_C(q_cart, dielectric);

  for (i = 0; i < num_patom; i++) { /* atom_i */
    for (j = 0; j < num_patom; j++) { /* atom_j */
      mass_sqrt = sqrt(mass[i] * mass[j]);
      for (k = 0; k < 3; k++) { /* derivative direction */
        for (l = 0; l < 3; l++) { /* alpha */
          a = get_A(i, l, q_cart, born);
          da = get_dA(i, l, k, born);
          for (m = 0; m < 3; m++) { /* beta */
            b = get_A(j, m, q_cart, born);
            db = get_dA(j, m, k, born);
            dc = get_dC(l, m, k, q_cart, dielectric);
            ddnac[k * num_patom * num_patom * 9 +
                  i * 9 * num_patom + j * 9 + l * 3 + m] =
              (da * b + db * a - a * b * dc / c) / (c * mass_sqrt) * factor;
            if (k == 0) {
              dnac[i * 9 * num_patom + j * 9 + l * 3 + m] =
                a * b / (c * mass_sqrt) * factor;
            }
          }
        }
      }
    }
  }
}
Ejemplo n.º 11
0
void manual_control( void )

{

  static Bool fc = TRUE;
  static State S;

  int i, j, h, choice;
  BfsNode *tmp, *curr;

  if ( fc ) {
    make_state( &S, gnum_ft_conn );
    fc = FALSE;
  }

  tmp = new_BfsNode();
  copy_source_to_dest( &(tmp->S), &ginitial_state );
  tmp->op = -1;
  curr = tmp;
  if ( gcmd_line.dominating ) {
    hash_bfs_node( curr );
  }

  while ( TRUE ) {
    if ( !curr ) break;
    h = get_1P_and_H( &(curr->S), &ggoal_state, NULL, curr->father, curr->op );
    get_A( &(curr->S) );

    while ( TRUE ) {
      printf("\n\n\n-------------state h = %d", h);
      if ( h > 0 ) printf(" (resp. %d actions)", h - 1);
      printf(", info level %d, %d applicable actions", gcmd_line.debug, gnum_A);
      if ( gcmd_line.debug >= 1 ) {
	print_state( curr->S );
      }
      if ( 0 ) {
	printf("\nH:"); 
	for ( i = 0; i < gnum_H; i++ ) {
	  printf(" ");
	  print_op_name( gH[i] );
	}
      }
      printf("\n"); 
      for ( i = 0; i < gnum_A; i++ ) {
	printf("\n%3d ", i); 
	for ( j = 0; j < gnum_H; j++ ) {
	  if ( gA[i] == gH[j] ) break;
	}
	if ( j < gnum_H ) {
	  printf("H: ");
	} else {
	  printf(" : ");
	}
	print_op_name( gA[i] );
      }
      printf("\n\n -1: retract last choice");
      printf("\n -2: set info level");
      printf("\n\nchoice: "); scanf("%d", &choice);
      if ( choice >= -2 && choice < gnum_A ) break;
    }
    
    if ( choice >= 0 ) {
      if ( !result_to_dest( &S, NULL, curr, gA[choice] ) ) {
	printf("\naction not applicable!");
	continue;
      }
      
      if ( gcmd_line.dominating && bfs_state_hashed( &S, curr, gA[choice] ) ) {
	printf("\nthis state is dominated!\n\n");
      }
      
      tmp = new_BfsNode();
      copy_source_to_dest( &(tmp->S), &S );
      tmp->father = curr;
      tmp->op = gA[choice];
      curr = tmp;
      
      if ( gcmd_line.dominating ) {
	hash_bfs_node( curr );
      }
      
      continue;
    }
    if ( choice == -1 ) {
      curr = curr->father;
      continue;
    }
    printf("\nlevel : "); scanf("%d", &gcmd_line.debug);
  }

}
Ejemplo n.º 12
0
Bool do_best_first_search( void )

{

  static Bool fc = TRUE;
  static State S;

  BfsNode *first;
  int i, min = INFINITY;
  Bool start = TRUE;

  if ( fc ) {
    make_state( &S, gnum_ft_conn );
    fc = FALSE;
  }

  lbfs_space_head = new_BfsNode();
  lbfs_space_had = NULL;

  add_to_bfs_space( &ginitial_state, -1, NULL );

  while ( TRUE ) {
    if ( (first = lbfs_space_head->next) == NULL ) {
      printf("\n\nbest first search space empty! problem proven unsolvable.\n\n");
      return FALSE;
    }

    lbfs_space_head->next = first->next;
    if ( first->next ) {
      first->next->prev = lbfs_space_head;
    }

    if ( LESS( first->h, min ) ) {
      min = first->h;
      if ( start ) {
	printf("\nadvancing to distance : %4d", min);
	start = FALSE;
	fflush(stdout);
      } else {
	printf("\n                        %4d", min);
	fflush(stdout);
      }
    }

    if ( first->h == 0 ) {
      break;
    }

    get_A( &(first->S) );
    for ( i = 0; i < gnum_A; i++ ) {
      if ( !result_to_dest( &S, NULL, first, gA[i] ) ) continue;
      add_to_bfs_space( &S, gA[i], first );
    }

    first->next = lbfs_space_had;
    lbfs_space_had = first;
  }

  extract_plan( first );
  return TRUE;

}