# include # include # include # include # include # include using namespace std; int main ( int argc, char *argv[] ); char ch_cap ( char ch ); bool ch_eqi ( char ch1, char ch2 ); int ch_to_digit ( char ch ); int file_column_count ( char *input_filename ); void file_name_ext_get ( char *file_name, int *i, int *j ); char *file_name_ext_swap ( char *file_name, char *ext ); int file_row_count ( char *input_filename ); int i4_max ( int i1, int i2 ); int i4_min ( int i1, int i2 ); int i4col_compare ( int m, int n, int a[], int i, int j ); void i4col_sort_a ( int m, int n, int a[] ); void i4col_swap ( int m, int n, int a[], int icol1, int icol2 ); void i4i4i4_sort_a ( int i1, int i2, int i3, int *j1, int *j2, int *j3 ); void i4mat_transpose_print ( int m, int n, int a[], char *title ); void i4mat_transpose_print_some ( int m, int n, int a[], int ilo, int jlo, int ihi, int jhi, char *title ); int *itable_data_read ( char *input_filename, int m, int n ); void itable_header_read ( char *input_filename, int *m, int *n ); void metis_graph_write ( char *output_file_name, int m, int n, int neighbor[] ); int s_index_last_c ( char *s, char c ); int s_len_trim ( char *s ); int s_to_i4 ( char *s, int *last, bool *error ); bool s_to_i4vec ( char *s, int n, int ivec[] ); int s_word_count ( char *s ); void sort_heap_external ( int n, int *indx, int *i, int *j, int isgn ); void timestamp ( void ); //****************************************************************************80 int main ( int argc, char *argv[] ) //****************************************************************************80 // // Purpose: // // MAIN is the main program for NEIGHBORS_TO_METIS_GRAPH. // // Discussion: // // A neighbor file (from e.g. triangulation_triangle_neighbor or // tet_mesh_neighbors ) is read, and an output graph file is written in the // format used by METIS. // // From this point, one may call either // // pmetis graph_file NPARTS // // or // // kmetis graph_file NPARTS // // Here, NPARTS is the number of parts in the partition. // // PMETIS is recommended for NPARTS < 8, and KMETIS for 8 <= NPARTS. // // Usage: // // neighbors_to_metis_graph file.neighbors // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 22 October 2008 // // Authors: // // Jeff Borggaard, John Burkardt // { int dim_num; int *element_neighbor; int element_num; int element_faces; int i; char input_neighbor_file_name[255]; int node_num; char *output_metis_graph_file_name; cout << "\n"; timestamp ( ); cout << "\n"; cout << "NEIGHBORS_TO_METIS_GRAPH:\n"; cout << " C++ version:\n"; cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n"; cout << "\n"; cout << " Write triangle or tet mesh neighbors in MeTiS graph file format.\n"; // // If at least one command line argument, it's the neighbor file name. // if ( 1 < argc ) { strcpy ( input_neighbor_file_name, argv[1] ); } else { cout << "\n"; cout << "NEIGHBORS_TO_METIS_GRAPH:\n"; cout << " Please enter the name of the neighbor file.\n"; cin.getline ( input_neighbor_file_name, sizeof ( input_neighbor_file_name ) ); } // // Read the data. // itable_header_read ( input_neighbor_file_name, &element_faces, &element_num ); cout << "\n"; cout << " Read the header of \"" << input_neighbor_file_name << "\".\n"; cout << "\n"; cout << " Element faces = " << element_faces << "\n"; cout << " Number of elements = " << element_num << "\n"; if ( element_faces != 3 && element_faces != 4 ) { cerr << "\n"; cerr << "NEIGHBORS_TO_METIS_GRAPH - Fatal error!\n"; cerr << " Each element must have 3 or 4 faces.\n"; exit ( 1 ); } element_neighbor = itable_data_read ( input_neighbor_file_name, element_faces, element_num ); cout << "\n"; cout << " Read the data in \"" << input_neighbor_file_name << "\".\n"; i4mat_transpose_print_some ( element_faces, element_num, element_neighbor, 1, 1, element_faces, 10, " Portion of ELEMENT_NEIGHBOR:" ); // // Write the neighbor information to a file. // Create the output file name from the input file name. // output_metis_graph_file_name = file_name_ext_swap ( input_neighbor_file_name, "metis_graph" ); metis_graph_write ( output_metis_graph_file_name, element_faces, element_num, element_neighbor ); if ( element_faces == 3 ) { cout << " Wrote the triangle neighbor information to \"" << output_metis_graph_file_name << "\".\n"; } else if ( element_faces == 4 ) { cout << " Wrote the tetrahedron neighbor information to \"" << output_metis_graph_file_name << "\".\n"; } delete [] element_neighbor; delete [] output_metis_graph_file_name; cout << "\n"; cout << "NEIGHBORS_TO_METIS_GRAPH:\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); return 0; } //****************************************************************************80 char ch_cap ( char ch ) //****************************************************************************80 // // Purpose: // // CH_CAP capitalizes a single character. // // Discussion: // // This routine should be equivalent to the library "toupper" function. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 19 July 1998 // // Author: // // John Burkardt // // Parameters: // // Input, char CH, the character to capitalize. // // Output, char CH_CAP, the capitalized character. // { if ( 97 <= ch && ch <= 122 ) { ch = ch - 32; } return ch; } //****************************************************************************80 bool ch_eqi ( char ch1, char ch2 ) //****************************************************************************80 // // Purpose: // // CH_EQI is true if two characters are equal, disregarding case. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char CH1, CH2, the characters to compare. // // Output, bool CH_EQI, is true if the two characters are equal, // disregarding case. // { if ( 97 <= ch1 && ch1 <= 122 ) { ch1 = ch1 - 32; } if ( 97 <= ch2 && ch2 <= 122 ) { ch2 = ch2 - 32; } return ( ch1 == ch2 ); } //****************************************************************************80 int ch_to_digit ( char ch ) //****************************************************************************80 // // Purpose: // // CH_TO_DIGIT returns the integer value of a base 10 digit. // // Example: // // CH DIGIT // --- ----- // '0' 0 // '1' 1 // ... ... // '9' 9 // ' ' 0 // 'X' -1 // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char CH, the decimal digit, '0' through '9' or blank are legal. // // Output, int CH_TO_DIGIT, the corresponding integer value. If the character was // 'illegal', then DIGIT is -1. // { int digit; if ( '0' <= ch && ch <= '9' ) { digit = ch - '0'; } else if ( ch == ' ' ) { digit = 0; } else { digit = -1; } return digit; } //****************************************************************************80 int file_column_count ( char *input_filename ) //****************************************************************************80 // // Purpose: // // FILE_COLUMN_COUNT counts the number of columns in the first line of a file. // // Discussion: // // The file is assumed to be a simple text file. // // Most lines of the file is presumed to consist of COLUMN_NUM words, separated // by spaces. There may also be some blank lines, and some comment lines, // which have a "#" in column 1. // // The routine tries to find the first non-comment non-blank line and // counts the number of words in that line. // // If all lines are blanks or comments, it goes back and tries to analyze // a comment line. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *INPUT_FILENAME, the name of the file. // // Output, int FILE_COLUMN_COUNT, the number of columns assumed // to be in the file. // { int column_num; ifstream input; bool got_one; char line[256]; // // Open the file. // input.open ( input_filename ); if ( !input ) { column_num = -1; cerr << "\n"; cerr << "FILE_COLUMN_COUNT - Fatal error!\n"; cerr << " Could not open the file:\n"; cerr << " \"" << input_filename << "\"\n"; return column_num; } // // Read one line, but skip blank lines and comment lines. // got_one = false; for ( ; ; ) { input.getline ( line, sizeof ( line ) ); if ( input.eof ( ) ) { break; } if ( s_len_trim ( line ) == 0 ) { continue; } if ( line[0] == '#' ) { continue; } got_one = true; break; } if ( !got_one ) { input.close ( ); input.open ( input_filename ); for ( ; ; ) { input.getline ( line, sizeof ( line ) ); if ( input.eof ( ) ) { break; } if ( s_len_trim ( line ) == 0 ) { continue; } got_one = true; break; } } input.close ( ); if ( !got_one ) { cerr << "\n"; cerr << "FILE_COLUMN_COUNT - Warning!\n"; cerr << " The file does not seem to contain any data.\n"; return -1; } column_num = s_word_count ( line ); return column_num; } //****************************************************************************80 void file_name_ext_get ( char *file_name, int *i, int *j ) //****************************************************************************80 // // Purpose: // // FILE_NAME_EXT_GET determines the "extension" of a file name. // // Discussion: // // The "extension" of a file name is the string of characters // that appears after the LAST period in the name. A file // with no period, or with a period as the last character // in the name, has a "null" extension. // // Blanks are unusual in file names. This routine ignores all // trailing blanks, but will treat initial or internal blanks // as regular characters acceptable in a file name. // // Example: // // FILE_NAME I J // // bob.for 3 6 // N.B.C.D 5 6 // Naomi. 5 5 // Arthur -1 -1 // .com 0 0 // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 17 July 1998 // // Author: // // John Burkardt // // Parameters: // // Input, char *FILE_NAME, a file name to be examined. // // Output, int *I, *J, the indices of the first and last characters // in the file extension. // // If no period occurs in FILE_NAME, then // I = J = -1; // Otherwise, // I is the position of the LAST period in FILE_NAME, and J is the // position of the last nonblank character following the period. // { *i = s_index_last_c ( file_name, '.' ); if ( *i == -1 ) { *j = -1; } else { *j = s_len_trim ( file_name ) - 1; } return; } //****************************************************************************80 char *file_name_ext_swap ( char *file_name, char *ext ) //****************************************************************************80 // // Purpose: // // FILE_NAME_EXT_SWAP replaces the current "extension" of a file name. // // Discussion: // // The "extension" of a file name is the string of characters // that appears after the LAST period in the name. A file // with no period, or with a period as the last character // in the name, has a "null" extension. // // Example: // // Input Output // ================ ================== // FILE_NAME EXT FILE_NAME_EXT_SWAP // // bob.for obj bob.obj // bob.bob.bob txt bob.bob.txt // bob yak bob.yak // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 06 December 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *FILE_NAME, a file name. // // Input, char *EXT, the extension to be used on the output // copy of FILE_NAME, replacing the current extension if any. // // Output, char *FILE_NAME_EXT_SWAP, the file name with the new extension. // { int ext_len; int file_name_len; char *file_name2; int i; int j; int period; char *s; char *t; ext_len = s_len_trim ( ext ); file_name_len = s_len_trim ( file_name ); // // Call FILE_NAME_EXT_GET to get I, the index of the period character // beginning the file extension. // file_name_ext_get ( file_name, &period, &j ); // // If there is no extension, point to where the "." should be. // if ( period == -1 ) { period = file_name_len; } file_name2 = new char[ period + 1 + ext_len + 1]; s = file_name2; t = file_name; for ( i = 0; i < period; i++ ) { *s = *t; s++; t++; } *s = '.'; s++; t = ext; for ( i = 0; i < ext_len; i++ ) { *s = *t; s++; t++; } *s = '\0'; return file_name2; } //****************************************************************************80 int file_row_count ( char *input_filename ) //****************************************************************************80 // // Purpose: // // FILE_ROW_COUNT counts the number of row records in a file. // // Discussion: // // It does not count lines that are blank, or that begin with a // comment symbol '#'. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *INPUT_FILENAME, the name of the input file. // // Output, int FILE_ROW_COUNT, the number of rows found. // { int bad_num; int comment_num; ifstream input; int i; char line[256]; int record_num; int row_num; row_num = 0; comment_num = 0; record_num = 0; bad_num = 0; input.open ( input_filename ); if ( !input ) { cerr << "\n"; cerr << "FILE_ROW_COUNT - Fatal error!\n"; cerr << " Could not open the input file: \"" << input_filename << "\"\n"; return (-1); } for ( ; ; ) { input.getline ( line, sizeof ( line ) ); if ( input.eof ( ) ) { break; } record_num = record_num + 1; if ( line[0] == '#' ) { comment_num = comment_num + 1; continue; } if ( s_len_trim ( line ) == 0 ) { comment_num = comment_num + 1; continue; } row_num = row_num + 1; } input.close ( ); return row_num; } //****************************************************************************80 int i4_max ( int i1, int i2 ) //****************************************************************************80 // // Purpose: // // I4_MAX returns the maximum of two I4's. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 October 1998 // // Author: // // John Burkardt // // Parameters: // // Input, int I1, I2, are two integers to be compared. // // Output, int I4_MAX, the larger of I1 and I2. // { int value; if ( i2 < i1 ) { value = i1; } else { value = i2; } return value; } //****************************************************************************80 int i4_min ( int i1, int i2 ) //****************************************************************************80 // // Purpose: // // I4_MIN returns the minimum of two I4's. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 October 1998 // // Author: // // John Burkardt // // Parameters: // // Input, int I1, I2, two integers to be compared. // // Output, int I4_MIN, the smaller of I1 and I2. // { int value; if ( i1 < i2 ) { value = i1; } else { value = i2; } return value; } //****************************************************************************80 int i4col_compare ( int m, int n, int a[], int i, int j ) //****************************************************************************80 // // Purpose: // // I4COL_COMPARE compares columns I and J of an I4COL. // // Example: // // Input: // // M = 3, N = 4, I = 2, J = 4 // // A = ( // 1 2 3 4 // 5 6 7 8 // 9 10 11 12 ) // // Output: // // I4COL_COMPARE = -1 // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 12 June 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int M, N, the number of rows and columns. // // Input, int A[M*N], an array of N columns of vectors of length M. // // Input, int I, J, the columns to be compared. // I and J must be between 1 and N. // // Output, int I4COL_COMPARE, the results of the comparison: // -1, column I < column J, // 0, column I = column J, // +1, column J < column I. // { int k; // // Check. // if ( i < 1 ) { cerr << "\n"; cerr << "I4COL_COMPARE - Fatal error!\n"; cerr << " Column index I = " << i << " is less than 1.\n"; exit ( 1 ); } if ( n < i ) { cerr << "\n"; cerr << "I4COL_COMPARE - Fatal error!\n"; cerr << " N = " << n << " is less than column index I = " << i << ".\n"; exit ( 1 ); } if ( j < 1 ) { cerr << "\n"; cerr << "I4COL_COMPARE - Fatal error!\n"; cerr << " Column index J = " << j << " is less than 1.\n"; exit ( 1 ); } if ( n < j ) { cerr << "\n"; cerr << "I4COL_COMPARE - Fatal error!\n"; cerr << " N = " << n << " is less than column index J = " << j << ".\n"; exit ( 1 ); } if ( i == j ) { return 0; } k = 1; while ( k <= m ) { if ( a[k-1+(i-1)*m] < a[k-1+(j-1)*m] ) { return (-1); } else if ( a[k-1+(j-1)*m] < a[k-1+(i-1)*m] ) { return 1; } k = k + 1; } return 0; } //****************************************************************************80 void i4col_sort_a ( int m, int n, int a[] ) //****************************************************************************80 // // Purpose: // // I4COL_SORT_A ascending sorts the columns of an I4COL. // // Discussion: // // In lexicographic order, the statement "X < Y", applied to two // vectors X and Y of length M, means that there is some index I, with // 1 <= I <= M, with the property that // // X(J) = Y(J) for J < I, // and // X(I) < Y(I). // // In other words, X is less than Y if, at the first index where they // differ, the X value is less than the Y value. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 12 June 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int M, the number of rows of A. // // Input, int N, the number of columns of A. // // Input/output, int A[M*N]. // On input, the array of N columns of M vectors; // On output, the columns of A have been sorted in ascending // lexicographic order. // { int i; int indx; int isgn; int j; // // Initialize. // i = 0; indx = 0; isgn = 0; j = 0; // // Call the external heap sorter. // for ( ; ; ) { sort_heap_external ( n, &indx, &i, &j, isgn ); // // Interchange the I and J objects. // if ( 0 < indx ) { i4col_swap ( m, n, a, i, j ); } // // Compare the I and J objects. // else if ( indx < 0 ) { isgn = i4col_compare ( m, n, a, i, j ); } else if ( indx == 0 ) { break; } } return; } //****************************************************************************80 void i4col_swap ( int m, int n, int a[], int icol1, int icol2 ) //****************************************************************************80 // // Purpose: // // I4COL_SWAP swaps two columns of an I4COL. // // Discussion: // // The two dimensional information is stored as a one dimensional // array, by columns. // // The row indices are 1 based, NOT 0 based// However, a preprocessor // variable, called OFFSET, can be reset from 1 to 0 if you wish to // use 0-based indices. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 03 April 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int M, N, the number of rows and columns. // // Input/output, int A[M*N], an array of data. // // Input, int ICOL1, ICOL2, the two columns to swap. // These indices should be between 1 and N. // { # define OFFSET 1 int i; int t; // // Check. // if ( icol1 - OFFSET < 0 || n-1 < icol1 - OFFSET ) { cerr << "\n"; cerr << "I4COL_SWAP - Fatal error!\n"; cerr << " ICOL1 is out of range.\n"; exit ( 1 ); } if ( icol2 - OFFSET < 0 || n-1 < icol2 - OFFSET ) { cerr << "\n"; cerr << "I4COL_SWAP - Fatal error!\n"; cerr << " ICOL2 is out of range.\n"; exit ( 1 ); } if ( icol1 == icol2 ) { return; } for ( i = 0; i < m; i++ ) { t = a[i+(icol1-OFFSET)*m]; a[i+(icol1-OFFSET)*m] = a[i+(icol2-OFFSET)*m]; a[i+(icol2-OFFSET)*m] = t; } return; # undef OFFSET } //****************************************************************************80 void i4i4i4_sort_a ( int i1, int i2, int i3, int *j1, int *j2, int *j3 ) //****************************************************************************80 // // Purpose: // // I4I4I4_SORT_A ascending sorts a triple of I4's. // // Discussion: // // The program allows the reasonable call: // // i4i4i4_sort_a ( i1, i2, i3, &i1, &i2, &i3 ); // // and this will return the reasonable result. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 12 October 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int I1, I2, I3, the values to sort. // // Output, int *J1, *J2, *J3, the sorted values. // { int k1; int k2; int k3; // // Copy arguments, so that the user can make "reasonable" calls like: // // i4i4i4_sort_a ( i1, i2, i3, &i1, &i2, &i3 ); // k1 = i1; k2 = i2; k3 = i3; *j1 = i4_min ( i4_min ( k1, k2 ), i4_min ( k2, k3 ) ); *j2 = i4_min ( i4_max ( k1, k2 ), i4_min ( i4_max ( k2, k3 ), i4_max ( k3, k1 ) ) ); *j3 = i4_max ( i4_max ( k1, k2 ), i4_max ( k2, k3 ) ); return; } //****************************************************************************80 void i4mat_transpose_print ( int m, int n, int a[], char *title ) //****************************************************************************80 // // Purpose: // // I4MAT_TRANSPOSE_PRINT prints an I4MAT, transposed. // // Discussion: // // An I4MAT is an MxN array of I4's, stored by (I,J) -> [I+J*M]. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 31 January 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int M, the number of rows in A. // // Input, int N, the number of columns in A. // // Input, int A[M*N], the M by N matrix. // // Input, char *TITLE, a title to be printed. // { int i; int j; int jhi; int jlo; i4mat_transpose_print_some ( m, n, a, 1, 1, m, n, title ); return; } //****************************************************************************80 void i4mat_transpose_print_some ( int m, int n, int a[], int ilo, int jlo, int ihi, int jhi, char *title ) //****************************************************************************80 // // Purpose: // // I4MAT_TRANSPOSE_PRINT_SOME prints some of an I4MAT, transposed. // // Discussion: // // An I4MAT is an MxN array of I4's, stored by (I,J) -> [I+J*M]. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 14 June 2005 // // Author: // // John Burkardt // // Parameters: // // Input, int M, the number of rows of the matrix. // M must be positive. // // Input, int N, the number of columns of the matrix. // N must be positive. // // Input, int A[M*N], the matrix. // // Input, int ILO, JLO, IHI, JHI, designate the first row and // column, and the last row and column to be printed. // // Input, char *TITLE, a title for the matrix. // { # define INCX 10 int i; int i2hi; int i2lo; int j; int j2hi; int j2lo; if ( 0 < s_len_trim ( title ) ) { cout << "\n"; cout << title << "\n"; } // // Print the columns of the matrix, in strips of INCX. // for ( i2lo = ilo; i2lo <= ihi; i2lo = i2lo + INCX ) { i2hi = i2lo + INCX - 1; i2hi = i4_min ( i2hi, m ); i2hi = i4_min ( i2hi, ihi ); cout << "\n"; // // For each row I in the current range... // // Write the header. // cout << " Row: "; for ( i = i2lo; i <= i2hi; i++ ) { cout << setw(6) << i << " "; } cout << "\n"; cout << " Col\n"; cout << "\n"; // // Determine the range of the rows in this strip. // j2lo = i4_max ( jlo, 1 ); j2hi = i4_min ( jhi, n ); for ( j = j2lo; j <= j2hi; j++ ) { // // Print out (up to INCX) entries in column J, that lie in the current strip. // cout << setw(5) << j << " "; for ( i = i2lo; i <= i2hi; i++ ) { cout << setw(6) << a[i-1+(j-1)*m] << " "; } cout << "\n"; } } cout << "\n"; return; # undef INCX } //****************************************************************************80 int *itable_data_read ( char *input_filename, int m, int n ) //****************************************************************************80 // // Purpose: // // ITABLE_DATA_READ reads data from an ITABLE file. // // Discussion: // // The file is assumed to contain one record per line. // // Records beginning with the '#' character are comments, and are ignored. // Blank lines are also ignored. // // Each line that is not ignored is assumed to contain exactly (or at least) // M real numbers, representing the coordinates of a point. // // There are assumed to be exactly (or at least) N such records. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 11 October 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *INPUT_FILENAME, the name of the input file. // // Input, int M, the number of spatial dimensions. // // Input, int N, the number of points. The program // will stop reading data once N values have been read. // // Output, int ITABLE_DATA_READ[M*N], the table data. // { bool error; ifstream input; int i; int j; char line[255]; int *table; int *x; input.open ( input_filename ); if ( !input ) { cerr << "\n"; cerr << "ITABLE_DATA_READ - Fatal error!\n"; cerr << " Could not open the input file: \"" << input_filename << "\"\n"; return NULL; } table = new int[m*n]; x = new int[m]; j = 0; while ( j < n ) { input.getline ( line, sizeof ( line ) ); if ( input.eof ( ) ) { break; } if ( line[0] == '#' || s_len_trim ( line ) == 0 ) { continue; } error = s_to_i4vec ( line, m, x ); if ( error ) { continue; } for ( i = 0; i < m; i++ ) { table[i+j*m] = x[i]; } j = j + 1; } input.close ( ); delete [] x; return table; } //****************************************************************************80 void itable_header_read ( char *input_filename, int *m, int *n ) //****************************************************************************80 // // Purpose: // // ITABLE_HEADER_READ reads the header from an ITABLE file. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 04 June 2004 // // Author: // // John Burkardt // // Parameters: // // Input, char *INPUT_FILENAME, the name of the input file. // // Output, int *M, the number of spatial dimensions. // // Output, int *N, the number of points // { *m = file_column_count ( input_filename ); if ( *m <= 0 ) { cerr << "\n"; cerr << "ITABLE_HEADER_READ - Fatal error!\n"; cerr << " FILE_COLUMN_COUNT failed.\n"; *n = -1; return; } *n = file_row_count ( input_filename ); if ( *n <= 0 ) { cerr << "\n"; cerr << "ITABLE_HEADER_READ - Fatal error!\n"; cerr << " FILE_ROW_COUNT failed.\n"; return; } return; } //****************************************************************************80 void metis_graph_write ( char *output_file_name, int m, int n, int neighbor[] ) //****************************************************************************80 // // Purpose: // // METIS_GRAPH_WRITE writes the METIS graph file. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 22 October 2008 // // Author: // // Jeff Borggaard, John Burkardt // // Parameters: // // Input, char *OUTPUT_FILE_NAME, the output file name. // // Input, int M, the number of graph edges // // Input, int N, the number of graph vertices. // // Input, int NEIGHBOR[M*N], the neighbor_file data. // { ofstream output_unit; int i; int i_positive; int j; int n_elements; int n_faces; output_unit.open ( output_file_name ); if ( !output_unit ) { cerr << "\n"; cerr << "METIS_GRAPH_WRITE - Fatal error!\n"; cerr << " Could not open the output file \"" << output_file_name << "\".\n"; exit ( 1 ); } output_unit << "% MeTiS GraphFile \"" << output_file_name << "\".\n"; output_unit << "%\n"; output_unit << "% Each tetrahedron has four neighbors.\n"; output_unit << "% For each element, the elements that \n"; output_unit << "% share faces with it are listed.\n"; i_positive = 0; for ( j = 0; j < n; j++ ) { for ( i = 0; i < m; i++ ) { if ( 0 < neighbor[i+j*m] ) { i_positive = i_positive + 1; } } } n_elements = n; n_faces = i_positive / 2; output_unit << "%\n"; output_unit << "% Output the number of elements and faces.\n"; output_unit << "%\n"; output_unit << " " << n_elements << " " << n_faces << "\n"; output_unit << "%\n"; output_unit << "% For each element, list neighbors\n"; output_unit << "% that share faces with it.\n"; output_unit << "%\n"; for ( j = 0; j < n; j++ ) { i_positive = 0; for ( i = 0; i < m; i++ ) { if ( 0 < neighbor[i+j*m] ) { output_unit << " " << neighbor[i+j*m]; i_positive = i_positive + 1; } } if ( 0 < i_positive ) { output_unit << "\n"; } } output_unit << "%\n"; output_unit << "% End of file.\n"; output_unit.close ( ); return; } //****************************************************************************80 int s_index_last_c ( char *s, char c ) //****************************************************************************80 // // Purpose: // // S_INDEX_LAST_C points to the last occurrence of a given character. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 10 October 2004 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, a pointer to a string. // // Input, char C, the character to search for. // // Output, int S_INDEX_LAST_C, the index in S of the last occurrence // of the character, or -1 if it does not occur. // { int n; char *t; n = strlen ( s ) - 1; t = s + strlen ( s ) - 1; while ( 0 <= n ) { if ( *t == c ) { return n; } t--; n--; } return (-1); } //****************************************************************************80 int s_len_trim ( char *s ) //****************************************************************************80 // // Purpose: // // S_LEN_TRIM returns the length of a string to the last nonblank. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 26 April 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, a pointer to a string. // // Output, int S_LEN_TRIM, the length of the string to the last nonblank. // If S_LEN_TRIM is 0, then the string is entirely blank. // { int n; char *t; n = strlen ( s ); t = s + strlen ( s ) - 1; while ( 0 < n ) { if ( *t != ' ' ) { return n; } t--; n--; } return n; } //****************************************************************************80 int s_to_i4 ( char *s, int *last, bool *error ) //****************************************************************************80 // // Purpose: // // S_TO_I4 reads an I4 from a string. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 13 June 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, a string to be examined. // // Output, int *LAST, the last character of S used to make IVAL. // // Output, bool *ERROR is TRUE if an error occurred. // // Output, int *S_TO_I4, the integer value read from the string. // If the string is blank, then IVAL will be returned 0. // { char c; int i; int isgn; int istate; int ival; *error = false; istate = 0; isgn = 1; i = 0; ival = 0; while ( *s ) { c = s[i]; i = i + 1; // // Haven't read anything. // if ( istate == 0 ) { if ( c == ' ' ) { } else if ( c == '-' ) { istate = 1; isgn = -1; } else if ( c == '+' ) { istate = 1; isgn = + 1; } else if ( '0' <= c && c <= '9' ) { istate = 2; ival = c - '0'; } else { *error = true; return ival; } } // // Have read the sign, expecting digits. // else if ( istate == 1 ) { if ( c == ' ' ) { } else if ( '0' <= c && c <= '9' ) { istate = 2; ival = c - '0'; } else { *error = true; return ival; } } // // Have read at least one digit, expecting more. // else if ( istate == 2 ) { if ( '0' <= c && c <= '9' ) { ival = 10 * (ival) + c - '0'; } else { ival = isgn * ival; *last = i - 1; return ival; } } } // // If we read all the characters in the string, see if we're OK. // if ( istate == 2 ) { ival = isgn * ival; *last = s_len_trim ( s ); } else { *error = true; *last = 0; } return ival; } //****************************************************************************80 bool s_to_i4vec ( char *s, int n, int ivec[] ) //****************************************************************************80 // // Purpose: // // S_TO_I4VEC reads an I4VEC from a string. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 September 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, the string to be read. // // Input, int N, the number of values expected. // // Output, int IVEC[N], the values read from the string. // // Output, bool S_TO_I4VEC, is TRUE if an error occurred. // { bool error; int i; int lchar; error = false; for ( i = 0; i < n; i++ ) { ivec[i] = s_to_i4 ( s, &lchar, &error ); if ( error ) { cerr << "\n"; cerr << "S_TO_I4VEC - Fatal error!\n"; cerr << " S_TO_I4 returned error while reading item " << i << "\n"; return error; } s = s + lchar; } return error; } //****************************************************************************80 int s_word_count ( char *s ) //****************************************************************************80 // // Purpose: // // S_WORD_COUNT counts the number of "words" in a string. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 08 February 2005 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, the string to be examined. // // Output, int S_WORD_COUNT, the number of "words" in the string. // Words are presumed to be separated by one or more blanks. // { bool blank; int i; int word_num; word_num = 0; blank = true; while ( *s ) { if ( *s == ' ' ) { blank = true; } else if ( blank ) { word_num = word_num + 1; blank = false; } *s++; } return word_num; } //****************************************************************************80 void sort_heap_external ( int n, int *indx, int *i, int *j, int isgn ) //****************************************************************************80 // // Purpose: // // SORT_HEAP_EXTERNAL externally sorts a list of items into ascending order. // // Discussion: // // The actual list is not passed to the routine. Hence it may // consist of integers, reals, numbers, names, etc. The user, // after each return from the routine, will be asked to compare or // interchange two items. // // The current version of this code mimics the FORTRAN version, // so the values of I and J, in particular, are FORTRAN indices. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 05 February 2004 // // Author: // // Original FORTRAN77 version by Albert Nijenhuis, Herbert Wilf. // C++ version by John Burkardt // // Reference: // // Albert Nijenhuis, Herbert Wilf, // Combinatorial Algorithms, // Academic Press, 1978, second edition, // ISBN 0-12-519260-6. // // Parameters: // // Input, int N, the length of the input list. // // Input/output, int *INDX. // The user must set INDX to 0 before the first call. // On return, // if INDX is greater than 0, the user must interchange // items I and J and recall the routine. // If INDX is less than 0, the user is to compare items I // and J and return in ISGN a negative value if I is to // precede J, and a positive value otherwise. // If INDX is 0, the sorting is done. // // Output, int *I, *J. On return with INDX positive, // elements I and J of the user's list should be // interchanged. On return with INDX negative, elements I // and J are to be compared by the user. // // Input, int ISGN. On return with INDX negative, the // user should compare elements I and J of the list. If // item I is to precede item J, set ISGN negative, // otherwise set ISGN positive. // { static int i_save = 0; static int j_save = 0; static int k = 0; static int k1 = 0; static int n1 = 0; // // INDX = 0: This is the first call. // if ( *indx == 0 ) { i_save = 0; j_save = 0; k = n / 2; k1 = k; n1 = n; } // // INDX < 0: The user is returning the results of a comparison. // else if ( *indx < 0 ) { if ( *indx == -2 ) { if ( isgn < 0 ) { i_save = i_save + 1; } j_save = k1; k1 = i_save; *indx = -1; *i = i_save; *j = j_save; return; } if ( 0 < isgn ) { *indx = 2; *i = i_save; *j = j_save; return; } if ( k <= 1 ) { if ( n1 == 1 ) { i_save = 0; j_save = 0; *indx = 0; } else { i_save = n1; j_save = 1; n1 = n1 - 1; *indx = 1; } *i = i_save; *j = j_save; return; } k = k - 1; k1 = k; } // // 0 < INDX: the user was asked to make an interchange. // else if ( *indx == 1 ) { k1 = k; } for ( ; ; ) { i_save = 2 * k1; if ( i_save == n1 ) { j_save = k1; k1 = i_save; *indx = -1; *i = i_save; *j = j_save; return; } else if ( i_save <= n1 ) { j_save = i_save + 1; *indx = -2; *i = i_save; *j = j_save; return; } if ( k <= 1 ) { break; } k = k - 1; k1 = k; } if ( n1 == 1 ) { i_save = 0; j_save = 0; *indx = 0; *i = i_save; *j = j_save; } else { i_save = n1; j_save = 1; n1 = n1 - 1; *indx = 1; *i = i_save; *j = j_save; } return; } //****************************************************************************80 void timestamp ( void ) //****************************************************************************80 // // Purpose: // // TIMESTAMP prints the current YMDHMS date as a time stamp. // // Example: // // May 31 2001 09:45:54 AM // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 02 October 2003 // // Author: // // John Burkardt // // Parameters: // // None // { # define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; size_t len; time_t now; now = time ( NULL ); tm = localtime ( &now ); len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); cout << time_buffer << "\n"; return; # undef TIME_SIZE }