public static bool cnf_header_read(string cnf_file_name, ref int v_num, ref int c_num, ref int l_num) //****************************************************************************80 // // Purpose: // // CNF_HEADER_READ reads the header of a CNF file. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 06 June 2008 // // Author: // // John Burkardt // // Parameters: // // Input, string CNF_FILE_NAME, the name of the CNF file. // // Output, int *V_NUM, the number of variables. // // Output, int *C_NUM, the number of clauses. // // Output, int *L_NUM, the number of signed literals. // // Output, bool CNF_HEADER_READ, is TRUE if there was an error during // the read. // { string[] input; string line = ""; try { input = File.ReadAllLines(cnf_file_name); } catch (Exception) { Console.WriteLine(""); Console.WriteLine("CNF_HEADER_READ - Fatal error!"); Console.WriteLine(" Could not open file."); return(true); } // // Read lines until you find one that is not blank and does not begin // with a "c". This should be the header line. // int index = 0; foreach (string tmp in input) { index++; switch (tmp[0]) { case 'c': case 'C': continue; } if (0 >= typeMethods.s_len_trim(tmp)) { continue; } line = tmp; break; } switch (line) { case "": Console.WriteLine(""); Console.WriteLine("CNF_HEADER_READ - Fatal error!"); Console.WriteLine(" Error3 while reading the file."); return(true); } switch (line.StartsWith("p cnf ")) { // // We expect to be reading the line "p cnf V_NUM C_NUM" // case false: Console.WriteLine(""); Console.WriteLine("CNF_DATA_READ - Fatal error!"); Console.WriteLine(" First non-comment non-blank line does not start"); Console.WriteLine(" with 'p cnf' marker."); return(true); } // // Remove the first four characters and shift left. // line = line.Replace("p cnf ", ""); // // Extract the next word, which is the number of variables. // try { i4vec ti = typeMethods.s_to_i4vec(line, 2); v_num = ti.ivec[0]; c_num = ti.ivec[1]; } catch (Exception) { Console.WriteLine(""); Console.WriteLine("CNF_HEADER_READ - Fatal error!"); Console.WriteLine(" Unexpected End of input."); return(true); } // // Read remaining lines, counting the literals while ignoring occurrences of '0'. // l_num = 0; foreach (string tmp in input.Skip(index)) { line = tmp; switch (line[0]) { case 'c': case 'C': continue; } if (typeMethods.s_len_trim(line) < 0) { break; } if (typeMethods.s_len_trim(line) == 0) { continue; } while (true) { string tmp2 = tmp.Replace(" ", " "); string[] tokens = tmp2.Split(' '); string word = tokens[0]; Join(" ", tokens.Skip(1)); if (typeMethods.s_len_trim(word) <= 0) { break; } int l_val = typeMethods.s_to_i4(word).val; if (l_val != 0) { l_num += 1; } } } return(false); }
public static void spy_file(string header, string data_filename) //****************************************************************************80 // // Purpose: // // SPY_FILE plots a sparsity pattern stored in a file. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 16 September 2014 // // Author: // // John Burkardt // // Parameters: // // Input, string HEADER, the name to be used for the // title of the plot, and as part of the names of the command // and plot files. // // Input, string DATA_FILENAME, the name of the file // containing the indices of nonzero matrix entries. // { int n0 = +typeMethods.i4_huge(); int n1 = -typeMethods.i4_huge(); int m0 = +typeMethods.i4_huge(); int m1 = -typeMethods.i4_huge(); int nz_num = 0; string[] data_unit = File.ReadAllLines(data_filename); foreach (string line in data_unit) { i4vec vals = typeMethods.s_to_i4vec(line, 2); int i = vals.ivec[0]; int j = vals.ivec[1]; nz_num += 1; m0 = Math.Min(m0, i); m1 = Math.Max(m1, i); n0 = Math.Min(n0, j); n1 = Math.Max(n1, j); } // // Create command file. // string command_filename = header + "_commands.txt"; List <string> command_unit = new() { "# " + command_filename, "#", "# Usage:", "# gnuplot < " + command_filename + "", "#", "unset key", "set term png" }; string png_filename = header + ".png"; command_unit.Add("set output '" + png_filename + "'"); command_unit.Add("set size ratio -1"); command_unit.Add("set xlabel '<--- J --->'"); command_unit.Add("set ylabel '<--- I --->'"); command_unit.Add("set title '" + nz_num + " nonzeros for \"" + header + "\"'"); command_unit.Add("set timestamp"); command_unit.Add("plot [y=" + m0 + ":" + m1 + "] [x=" + n0 + ":" + n1 + "] '" + data_filename + "' with points pt 5"); File.WriteAllLines(command_filename, command_unit); Console.WriteLine(" Created graphics command file '" + command_filename + "'"); }
public static void xyf_data_read(string input_filename, int face_num, int face_data_num, ref int[] face_pointer, ref int[] face_data) //****************************************************************************80 // // Purpose: // // XYF_DATA_READ reads the data in an XYF file. // // Discussion: // // This routine assumes that the file contains exactly three kinds of // records: // // COMMENTS which begin with a '#' character in column 1; // BLANKS which contain nothing but 'whitespace'; // FACE ITEMS, which are indices of points on a face. // // The routine ignores comments and blank faces and returns // the number of face items. // // Licensing: // // This code is distributed under the GNU LGPL license. // // Modified: // // 25 October 2010 // // Author: // // John Burkardt // // Parameters: // // Input, string INPUT_FILENAME, the name of the input file. // // Input, int FACE_NUM, the number of faces. // // Input, int FACE_DATA_NUM, the number of face items. // // Output, int FACE_POINTER[FACE_NUM+1], pointers to the // first face item for each face. // // Output, int FACE_DATA[FACE_DATA_NUM], the face items. // { string[] input; try { input = File.ReadAllLines(input_filename); } catch (Exception) { Console.WriteLine(""); Console.WriteLine("XYF_DATA_READ - Fatal error!"); Console.WriteLine(" Cannot open the input file \"" + input_filename + "\"."); return; } int face = 0; face_pointer[0] = 0; int index = 0; while (face < face_num) { string text; try { text = input[index]; index++; } catch (Exception) { Console.WriteLine(""); Console.WriteLine("XYF_DATA_READ - Fatal error!"); Console.WriteLine(" Unexpected end of information."); return; } if (text[0] == '#' || typeMethods.s_len_trim(text) == 0) { continue; } int n = typeMethods.s_word_count(text); face_pointer[face + 1] = face_pointer[face] + n; int ilo = face_pointer[face]; i4vec r = typeMethods.s_to_i4vec(text, n); switch (r.error) { case true: Console.WriteLine(""); Console.WriteLine("XYF_DATA_READ - Fatal error!"); Console.WriteLine(" Error from S_TO_I4VEC."); return; } for (int ix = 0; ix < r.ivec.Length; ix++) { face_data[ilo + ix] = r.ivec[ix]; } face += 1; } }