📄 iohb.c
字号:
/*
Fri Aug 15 16:29:47 EDT 1997
Harwell-Boeing File I/O in C
V. 1.0
National Institute of Standards and Technology, MD.
K.A. Remington
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
NOTICE
Permission to use, copy, modify, and distribute this software and
its documentation for any purpose and without fee is hereby granted
provided that the above copyright notice appear in all copies and
that both the copyright notice and this permission notice appear in
supporting documentation.
Neither the Author nor the Institution (National Institute of Standards
and Technology) make any representations about the suitability of this
software for any purpose. This software is provided "as is" without
expressed or implied warranty.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
---------------------
INTERFACE DESCRIPTION
---------------------
---------------
QUERY FUNCTIONS
---------------
FUNCTION:
int readHB_info(const char *filename, int *M, int *N, int *nz,
char **Type, int *Nrhs)
DESCRIPTION:
The readHB_info function opens and reads the header information from
the specified Harwell-Boeing file, and reports back the number of rows
and columns in the stored matrix (M and N), the number of nonzeros in
the matrix (nz), the 3-character matrix type(Type), and the number of
right-hand-sides stored along with the matrix (Nrhs). This function
is designed to retrieve basic size information which can be used to
allocate arrays.
FUNCTION:
int readHB_header(FILE* in_file, char* Title, char* Key, char* Type,
int* Nrow, int* Ncol, int* Nnzero, int* Nrhs,
char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
int* Ptrcrd, int* Indcrd, int* Valcrd, int* Rhscrd,
char *Rhstype)
DESCRIPTION:
More detailed than the readHB_info function, readHB_header() reads from
the specified Harwell-Boeing file all of the header information.
------------------------------
DOUBLE PRECISION I/O FUNCTIONS
------------------------------
FUNCTION:
int readHB_newmat_double(const char *filename, int *M, int *N, *int nz,
int **colptr, int **rowind, double**val)
int readHB_mat_double(const char *filename, int *colptr, int *rowind,
double*val)
DESCRIPTION:
This function opens and reads the specified file, interpreting its
contents as a sparse matrix stored in the Harwell/Boeing standard
format. (See readHB_aux_double to read auxillary vectors.)
-- Values are interpreted as double precision numbers. --
The "mat" function uses _pre-allocated_ vectors to hold the index and
nonzero value information.
The "newmat" function allocates vectors to hold the index and nonzero
value information, and returns pointers to these vectors along with
matrix dimension and number of nonzeros.
FUNCTION:
int readHB_aux_double(const char* filename, const char AuxType, double b[])
int readHB_newaux_double(const char* filename, const char AuxType, double** b)
DESCRIPTION:
This function opens and reads from the specified file auxillary vector(s).
The char argument Auxtype determines which type of auxillary vector(s)
will be read (if present in the file).
AuxType = 'F' right-hand-side
AuxType = 'G' initial estimate (Guess)
AuxType = 'X' eXact solution
If Nrhs > 1, all of the Nrhs vectors of the given type are read and
stored in column-major order in the vector b.
The "newaux" function allocates a vector to hold the values retrieved.
The "mat" function uses a _pre-allocated_ vector to hold the values.
FUNCTION:
int writeHB_mat_double(const char* filename, int M, int N,
int nz, const int colptr[], const int rowind[],
const double val[], int Nrhs, const double rhs[],
const double guess[], const double exact[],
const char* Title, const char* Key, const char* Type,
char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
const char* Rhstype)
DESCRIPTION:
The writeHB_mat_double function opens the named file and writes the specified
matrix and optional auxillary vector(s) to that file in Harwell-Boeing
format. The format arguments (Ptrfmt,Indfmt,Valfmt, and Rhsfmt) are
character strings specifying "Fortran-style" output formats -- as they
would appear in a Harwell-Boeing file. They are used to produce output
which is as close as possible to what would be produced by Fortran code,
but note that "D" and "P" edit descriptors are not supported.
If NULL, the following defaults will be used:
Ptrfmt = Indfmt = "(8I10)"
Valfmt = Rhsfmt = "(4E20.13)"
-----------------------
CHARACTER I/O FUNCTIONS
-----------------------
FUNCTION:
int readHB_mat_char(const char* filename, int colptr[], int rowind[],
char val[], char* Valfmt)
int readHB_newmat_char(const char* filename, int* M, int* N, int* nonzeros,
int** colptr, int** rowind, char** val, char** Valfmt)
DESCRIPTION:
This function opens and reads the specified file, interpreting its
contents as a sparse matrix stored in the Harwell/Boeing standard
format. (See readHB_aux_char to read auxillary vectors.)
-- Values are interpreted as char strings. --
(Used to translate exact values from the file into a new storage format.)
The "mat" function uses _pre-allocated_ arrays to hold the index and
nonzero value information.
The "newmat" function allocates char arrays to hold the index
and nonzero value information, and returns pointers to these arrays
along with matrix dimension and number of nonzeros.
FUNCTION:
int readHB_aux_char(const char* filename, const char AuxType, char b[])
int readHB_newaux_char(const char* filename, const char AuxType, char** b,
char** Rhsfmt)
DESCRIPTION:
This function opens and reads from the specified file auxillary vector(s).
The char argument Auxtype determines which type of auxillary vector(s)
will be read (if present in the file).
AuxType = 'F' right-hand-side
AuxType = 'G' initial estimate (Guess)
AuxType = 'X' eXact solution
If Nrhs > 1, all of the Nrhs vectors of the given type are read and
stored in column-major order in the vector b.
The "newaux" function allocates a character array to hold the values
retrieved.
The "mat" function uses a _pre-allocated_ array to hold the values.
FUNCTION:
int writeHB_mat_char(const char* filename, int M, int N,
int nz, const int colptr[], const int rowind[],
const char val[], int Nrhs, const char rhs[],
const char guess[], const char exact[],
const char* Title, const char* Key, const char* Type,
char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
const char* Rhstype)
DESCRIPTION:
The writeHB_mat_char function opens the named file and writes the specified
matrix and optional auxillary vector(s) to that file in Harwell-Boeing
format. The format arguments (Ptrfmt,Indfmt,Valfmt, and Rhsfmt) are
character strings specifying "Fortran-style" output formats -- as they
would appear in a Harwell-Boeing file. Valfmt and Rhsfmt must accurately
represent the character representation of the values stored in val[]
and rhs[].
If NULL, the following defaults will be used for the integer vectors:
Ptrfmt = Indfmt = "(8I10)"
Valfmt = Rhsfmt = "(4E20.13)"
*/
/*---------------------------------------------------------------------*/
/* If zero-based indexing is desired, _SP_base should be set to 0 */
/* This will cause indices read from H-B files to be decremented by 1 */
/* and indices written to H-B files to be incremented by 1 */
/* <<< Standard usage is _SP_base = 1 >>> */
#ifndef _SP_base
#define _SP_base 1
#endif
/*---------------------------------------------------------------------*/
#include "iohb.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
char* substr(const char* S, const int pos, const int len);
void upcase(char* S);
void IOHBTerminate(char* message);
int readHB_info(const char* filename, int* M, int* N, int* nz, char** Type,
int* Nrhs)
{
/****************************************************************************/
/* The readHB_info function opens and reads the header information from */
/* the specified Harwell-Boeing file, and reports back the number of rows */
/* and columns in the stored matrix (M and N), the number of nonzeros in */
/* the matrix (nz), and the number of right-hand-sides stored along with */
/* the matrix (Nrhs). */
/* */
/* For a description of the Harwell Boeing standard, see: */
/* Duff, et al., ACM TOMS Vol.15, No.1, March 1989 */
/* */
/* ---------- */
/* **CAVEAT** */
/* ---------- */
/* ** If the input file does not adhere to the H/B format, the ** */
/* ** results will be unpredictable. ** */
/* */
/****************************************************************************/
FILE *in_file;
int Ptrcrd, Indcrd, Valcrd, Rhscrd;
int Nrow, Ncol, Nnzero;
char* mat_type;
char Title[73], Key[9], Rhstype[4];
char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
mat_type = *Type;
if ( mat_type == NULL ) IOHBTerminate("Insufficient memory for mat_typen");
if ( (in_file = fopen( filename, "r")) == NULL ) {
fprintf(stderr,"Error: Cannot open file: %s\n",filename);
return 0;
}
readHB_header(in_file, Title, Key, mat_type, &Nrow, &Ncol, &Nnzero, Nrhs,
Ptrfmt, Indfmt, Valfmt, Rhsfmt,
&Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
fclose(in_file);
*Type = mat_type;
*(*Type+3) = (char) NULL;
*M = Nrow;
*N = Ncol;
*nz = Nnzero;
if (Rhscrd == 0) {*Nrhs = 0;}
/* In verbose mode, print some of the header information: */
/*
if (verbose == 1)
{
printf("Reading from Harwell-Boeing file %s (verbose on)...\n",filename);
printf(" Title: %s\n",Title);
printf(" Key: %s\n",Key);
printf(" The stored matrix is %i by %i with %i nonzeros.\n",
*M, *N, *nz );
printf(" %i right-hand--side(s) stored.\n",*Nrhs);
}
*/
return 1;
}
int readHB_header(FILE* in_file, char* Title, char* Key, char* Type,
int* Nrow, int* Ncol, int* Nnzero, int* Nrhs,
char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
int* Ptrcrd, int* Indcrd, int* Valcrd, int* Rhscrd,
char *Rhstype)
{
/*************************************************************************/
/* Read header information from the named H/B file... */
/*************************************************************************/
int Totcrd,Neltvl,Nrhsix;
char line[BUFSIZ];
/* First line: */
fgets(line, BUFSIZ, in_file);
if ( sscanf(line,"%*s") < 0 )
IOHBTerminate("iohb.c: Null (or blank) first line of HB file.\n");
(void) sscanf(line, "%72c%8[^\n]", Title, Key);
*(Key+8) = (char) NULL;
*(Title+72) = (char) NULL;
/* Second line: */
fgets(line, BUFSIZ, in_file);
if ( sscanf(line,"%*s") < 0 )
IOHBTerminate("iohb.c: Null (or blank) second line of HB file.\n");
if ( sscanf(line,"%i",&Totcrd) != 1) Totcrd = 0;
if ( sscanf(line,"%*i%i",Ptrcrd) != 1) *Ptrcrd = 0;
if ( sscanf(line,"%*i%*i%i",Indcrd) != 1) *Indcrd = 0;
if ( sscanf(line,"%*i%*i%*i%i",Valcrd) != 1) *Valcrd = 0;
if ( sscanf(line,"%*i%*i%*i%*i%i",Rhscrd) != 1) *Rhscrd = 0;
/* Third line: */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -