kusano 7d535a
kusano 7d535a
/*! @file creadhb.c
kusano 7d535a
 * \brief Read a matrix stored in Harwell-Boeing format
kusano 7d535a
 *
kusano 7d535a
 * 
kusano 7d535a
 * -- SuperLU routine (version 2.0) --
kusano 7d535a
 * Univ. of California Berkeley, Xerox Palo Alto Research Center,
kusano 7d535a
 * and Lawrence Berkeley National Lab.
kusano 7d535a
 * November 15, 1997
kusano 7d535a
 *
kusano 7d535a
 * Purpose
kusano 7d535a
 * =======
kusano 7d535a
 * 
kusano 7d535a
 * Read a COMPLEX PRECISION matrix stored in Harwell-Boeing format 
kusano 7d535a
 * as described below.
kusano 7d535a
 * 
kusano 7d535a
 * Line 1 (A72,A8) 
kusano 7d535a
 *  	Col. 1 - 72   Title (TITLE) 
kusano 7d535a
 *	Col. 73 - 80  Key (KEY) 
kusano 7d535a
 * 
kusano 7d535a
 * Line 2 (5I14) 
kusano 7d535a
 * 	Col. 1 - 14   Total number of lines excluding header (TOTCRD) 
kusano 7d535a
 * 	Col. 15 - 28  Number of lines for pointers (PTRCRD) 
kusano 7d535a
 * 	Col. 29 - 42  Number of lines for row (or variable) indices (INDCRD) 
kusano 7d535a
 * 	Col. 43 - 56  Number of lines for numerical values (VALCRD) 
kusano 7d535a
 *	Col. 57 - 70  Number of lines for right-hand sides (RHSCRD) 
kusano 7d535a
 *                    (including starting guesses and solution vectors 
kusano 7d535a
 *		       if present) 
kusano 7d535a
 *           	      (zero indicates no right-hand side data is present) 
kusano 7d535a
 *
kusano 7d535a
 * Line 3 (A3, 11X, 4I14) 
kusano 7d535a
 *   	Col. 1 - 3    Matrix type (see below) (MXTYPE) 
kusano 7d535a
 * 	Col. 15 - 28  Number of rows (or variables) (NROW) 
kusano 7d535a
 * 	Col. 29 - 42  Number of columns (or elements) (NCOL) 
kusano 7d535a
 *	Col. 43 - 56  Number of row (or variable) indices (NNZERO) 
kusano 7d535a
 *	              (equal to number of entries for assembled matrices) 
kusano 7d535a
 * 	Col. 57 - 70  Number of elemental matrix entries (NELTVL) 
kusano 7d535a
 *	              (zero in the case of assembled matrices) 
kusano 7d535a
 * Line 4 (2A16, 2A20) 
kusano 7d535a
 * 	Col. 1 - 16   Format for pointers (PTRFMT) 
kusano 7d535a
 *	Col. 17 - 32  Format for row (or variable) indices (INDFMT) 
kusano 7d535a
 *	Col. 33 - 52  Format for numerical values of coefficient matrix (VALFMT) 
kusano 7d535a
 * 	Col. 53 - 72 Format for numerical values of right-hand sides (RHSFMT) 
kusano 7d535a
 *
kusano 7d535a
 * Line 5 (A3, 11X, 2I14) Only present if there are right-hand sides present 
kusano 7d535a
 *    	Col. 1 	      Right-hand side type: 
kusano 7d535a
 *	         	  F for full storage or M for same format as matrix 
kusano 7d535a
 *    	Col. 2        G if a starting vector(s) (Guess) is supplied. (RHSTYP) 
kusano 7d535a
 *    	Col. 3        X if an exact solution vector(s) is supplied. 
kusano 7d535a
 *	Col. 15 - 28  Number of right-hand sides (NRHS) 
kusano 7d535a
 *	Col. 29 - 42  Number of row indices (NRHSIX) 
kusano 7d535a
 *          	      (ignored in case of unassembled matrices) 
kusano 7d535a
 *
kusano 7d535a
 * The three character type field on line 3 describes the matrix type. 
kusano 7d535a
 * The following table lists the permitted values for each of the three 
kusano 7d535a
 * characters. As an example of the type field, RSA denotes that the matrix 
kusano 7d535a
 * is real, symmetric, and assembled. 
kusano 7d535a
 *
kusano 7d535a
 * First Character: 
kusano 7d535a
 *	R Real matrix 
kusano 7d535a
 *	C Complex matrix 
kusano 7d535a
 *	P Pattern only (no numerical values supplied) 
kusano 7d535a
 *
kusano 7d535a
 * Second Character: 
kusano 7d535a
 *	S Symmetric 
kusano 7d535a
 *	U Unsymmetric 
kusano 7d535a
 *	H Hermitian 
kusano 7d535a
 *	Z Skew symmetric 
kusano 7d535a
 *	R Rectangular 
kusano 7d535a
 *
kusano 7d535a
 * Third Character: 
kusano 7d535a
 *	A Assembled 
kusano 7d535a
 *	E Elemental matrices (unassembled) 
kusano 7d535a
 *
kusano 7d535a
 * 
kusano 7d535a
 */
kusano 7d535a
#include <stdio.h></stdio.h>
kusano 7d535a
#include <stdlib.h></stdlib.h>
kusano 7d535a
#include "slu_cdefs.h"
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*! \brief Eat up the rest of the current line */
kusano 7d535a
int cDumpLine(FILE *fp)
kusano 7d535a
{
kusano 7d535a
    register int c;
kusano 7d535a
    while ((c = fgetc(fp)) != '\n') ;
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
int cParseIntFormat(char *buf, int *num, int *size)
kusano 7d535a
{
kusano 7d535a
    char *tmp;
kusano 7d535a
kusano 7d535a
    tmp = buf;
kusano 7d535a
    while (*tmp++ != '(') ;
kusano 7d535a
    sscanf(tmp, "%d", num);
kusano 7d535a
    while (*tmp != 'I' && *tmp != 'i') ++tmp;
kusano 7d535a
    ++tmp;
kusano 7d535a
    sscanf(tmp, "%d", size);
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
int cParseFloatFormat(char *buf, int *num, int *size)
kusano 7d535a
{
kusano 7d535a
    char *tmp, *period;
kusano 7d535a
    
kusano 7d535a
    tmp = buf;
kusano 7d535a
    while (*tmp++ != '(') ;
kusano 7d535a
    *num = atoi(tmp); /*sscanf(tmp, "%d", num);*/
kusano 7d535a
    while (*tmp != 'E' && *tmp != 'e' && *tmp != 'D' && *tmp != 'd'
kusano 7d535a
	   && *tmp != 'F' && *tmp != 'f') {
kusano 7d535a
        /* May find kP before nE/nD/nF, like (1P6F13.6). In this case the
kusano 7d535a
           num picked up refers to P, which should be skipped. */
kusano 7d535a
        if (*tmp=='p' || *tmp=='P') {
kusano 7d535a
           ++tmp;
kusano 7d535a
           *num = atoi(tmp); /*sscanf(tmp, "%d", num);*/
kusano 7d535a
        } else {
kusano 7d535a
           ++tmp;
kusano 7d535a
        }
kusano 7d535a
    }
kusano 7d535a
    ++tmp;
kusano 7d535a
    period = tmp;
kusano 7d535a
    while (*period != '.' && *period != ')') ++period ;
kusano 7d535a
    *period = '\0';
kusano 7d535a
    *size = atoi(tmp); /*sscanf(tmp, "%2d", size);*/
kusano 7d535a
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
static int ReadVector(FILE *fp, int n, int *where, int perline, int persize)
kusano 7d535a
{
kusano 7d535a
    register int i, j, item;
kusano 7d535a
    char tmp, buf[100];
kusano 7d535a
    
kusano 7d535a
    i = 0;
kusano 7d535a
    while (i < n) {
kusano 7d535a
	fgets(buf, 100, fp);    /* read a line at a time */
kusano 7d535a
	for (j=0; j
kusano 7d535a
	    tmp = buf[(j+1)*persize];     /* save the char at that place */
kusano 7d535a
	    buf[(j+1)*persize] = 0;       /* null terminate */
kusano 7d535a
	    item = atoi(&buf[j*persize]); 
kusano 7d535a
	    buf[(j+1)*persize] = tmp;     /* recover the char at that place */
kusano 7d535a
	    where[i++] = item - 1;
kusano 7d535a
	}
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
/*! \brief Read complex numbers as pairs of (real, imaginary) */
kusano 7d535a
int cReadValues(FILE *fp, int n, complex *destination, int perline, int persize)
kusano 7d535a
{
kusano 7d535a
    register int i, j, k, s, pair;
kusano 7d535a
    register float realpart;
kusano 7d535a
    char tmp, buf[100];
kusano 7d535a
    
kusano 7d535a
    i = pair = 0;
kusano 7d535a
    while (i < n) {
kusano 7d535a
	fgets(buf, 100, fp);    /* read a line at a time */
kusano 7d535a
	for (j=0; j
kusano 7d535a
	    tmp = buf[(j+1)*persize];     /* save the char at that place */
kusano 7d535a
	    buf[(j+1)*persize] = 0;       /* null terminate */
kusano 7d535a
	    s = j*persize;
kusano 7d535a
	    for (k = 0; k < persize; ++k) /* No D_ format in C */
kusano 7d535a
		if ( buf[s+k] == 'D' || buf[s+k] == 'd' ) buf[s+k] = 'E';
kusano 7d535a
	    if ( pair == 0 ) {
kusano 7d535a
	  	/* The value is real part */
kusano 7d535a
		realpart = atof(&buf[s]);
kusano 7d535a
		pair = 1;
kusano 7d535a
	    } else {
kusano 7d535a
		/* The value is imaginary part */
kusano 7d535a
	        destination[i].r = realpart;
kusano 7d535a
		destination[i++].i = atof(&buf[s]);
kusano 7d535a
		pair = 0;
kusano 7d535a
	    }
kusano 7d535a
	    buf[(j+1)*persize] = tmp;     /* recover the char at that place */
kusano 7d535a
	}
kusano 7d535a
    }
kusano 7d535a
kusano 7d535a
    return 0;
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
kusano 7d535a
void
kusano 7d535a
creadhb(int *nrow, int *ncol, int *nonz,
kusano 7d535a
	complex **nzval, int **rowind, int **colptr)
kusano 7d535a
{
kusano 7d535a
kusano 7d535a
    register int i, numer_lines = 0, rhscrd = 0;
kusano 7d535a
    int tmp, colnum, colsize, rownum, rowsize, valnum, valsize;
kusano 7d535a
    char buf[100], type[4], key[10];
kusano 7d535a
    FILE *fp;
kusano 7d535a
kusano 7d535a
    fp = stdin;
kusano 7d535a
kusano 7d535a
    /* Line 1 */
kusano 7d535a
    fgets(buf, 100, fp);
kusano 7d535a
    fputs(buf, stdout);
kusano 7d535a
#if 0
kusano 7d535a
    fscanf(fp, "%72c", buf); buf[72] = 0;
kusano 7d535a
    printf("Title: %s", buf);
kusano 7d535a
    fscanf(fp, "%8c", key);  key[8] = 0;
kusano 7d535a
    printf("Key: %s\n", key);
kusano 7d535a
    cDumpLine(fp);
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
    /* Line 2 */
kusano 7d535a
    for (i=0; i<5; i++) {
kusano 7d535a
	fscanf(fp, "%14c", buf); buf[14] = 0;
kusano 7d535a
	sscanf(buf, "%d", &tmp);
kusano 7d535a
	if (i == 3) numer_lines = tmp;
kusano 7d535a
	if (i == 4 && tmp) rhscrd = tmp;
kusano 7d535a
    }
kusano 7d535a
    cDumpLine(fp);
kusano 7d535a
kusano 7d535a
    /* Line 3 */
kusano 7d535a
    fscanf(fp, "%3c", type);
kusano 7d535a
    fscanf(fp, "%11c", buf); /* pad */
kusano 7d535a
    type[3] = 0;
kusano 7d535a
#ifdef DEBUG
kusano 7d535a
    printf("Matrix type %s\n", type);
kusano 7d535a
#endif
kusano 7d535a
    
kusano 7d535a
    fscanf(fp, "%14c", buf); sscanf(buf, "%d", nrow);
kusano 7d535a
    fscanf(fp, "%14c", buf); sscanf(buf, "%d", ncol);
kusano 7d535a
    fscanf(fp, "%14c", buf); sscanf(buf, "%d", nonz);
kusano 7d535a
    fscanf(fp, "%14c", buf); sscanf(buf, "%d", &tmp);
kusano 7d535a
    
kusano 7d535a
    if (tmp != 0)
kusano 7d535a
	  printf("This is not an assembled matrix!\n");
kusano 7d535a
    if (*nrow != *ncol)
kusano 7d535a
	printf("Matrix is not square.\n");
kusano 7d535a
    cDumpLine(fp);
kusano 7d535a
kusano 7d535a
    /* Allocate storage for the three arrays ( nzval, rowind, colptr ) */
kusano 7d535a
    callocateA(*ncol, *nonz, nzval, rowind, colptr);
kusano 7d535a
kusano 7d535a
    /* Line 4: format statement */
kusano 7d535a
    fscanf(fp, "%16c", buf);
kusano 7d535a
    cParseIntFormat(buf, &colnum, &colsize);
kusano 7d535a
    fscanf(fp, "%16c", buf);
kusano 7d535a
    cParseIntFormat(buf, &rownum, &rowsize);
kusano 7d535a
    fscanf(fp, "%20c", buf);
kusano 7d535a
    cParseFloatFormat(buf, &valnum, &valsize);
kusano 7d535a
    fscanf(fp, "%20c", buf);
kusano 7d535a
    cDumpLine(fp);
kusano 7d535a
kusano 7d535a
    /* Line 5: right-hand side */    
kusano 7d535a
    if ( rhscrd ) cDumpLine(fp); /* skip RHSFMT */
kusano 7d535a
    
kusano 7d535a
#ifdef DEBUG
kusano 7d535a
    printf("%d rows, %d nonzeros\n", *nrow, *nonz);
kusano 7d535a
    printf("colnum %d, colsize %d\n", colnum, colsize);
kusano 7d535a
    printf("rownum %d, rowsize %d\n", rownum, rowsize);
kusano 7d535a
    printf("valnum %d, valsize %d\n", valnum, valsize);
kusano 7d535a
#endif
kusano 7d535a
    
kusano 7d535a
    ReadVector(fp, *ncol+1, *colptr, colnum, colsize);
kusano 7d535a
    ReadVector(fp, *nonz, *rowind, rownum, rowsize);
kusano 7d535a
    if ( numer_lines ) {
kusano 7d535a
        cReadValues(fp, *nonz, *nzval, valnum, valsize);
kusano 7d535a
    }
kusano 7d535a
    
kusano 7d535a
    fclose(fp);
kusano 7d535a
kusano 7d535a
}
kusano 7d535a