<div>Is this what you had in mind (more or less)? ---John</div><div><br></div><div>//-----------------------------------CONVERTER---------------------------------------------------------------------------------------------------------</div>
<div><br></div><div>static char help[] = "Reads matrix: \n -f <input_file> : file to load \n\n";<br></div><br>#define SIZE 4002<br><br>#include <stdio.h><br>#include <stdlib.h><br>#include "petscmat.h"<br>
<br>int main(int argc,char **args)<br>{<br> Mat A;<br> char file[PETSC_MAX_PATH_LEN];<br> PetscErrorCode ierr;<br> PetscScalar *a;<br> PetscTruth flg;<br>
PetscInt n=SIZE;<br> int i, j;<br><br> PetscInitialize(&argc,&args,(char *)0,help);<br><br> ierr = PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN-1,&flg);CHKERRQ(ierr);<br>
if (!flg) SETERRQ(1,"Must indicate binary file with the -f option");<br><br> ierr = PetscMalloc(SIZE*SIZE*sizeof(PetscScalar),&a);CHKERRQ(ierr);<br><br> FILE *fpK;<br><br> double *K = (double *)calloc( sizeof(double), SIZE*SIZE );<br>
<br> if((fpK = fopen(file, "rb")) == NULL) <br> {<br> printf("Cannot open joe_DOF4002_k_double.bin\n");<br> exit(1);<br> }<br><br> fprintf(stderr, "Doubles read for K = %d\n", fread( (void *)K, sizeof(double), SIZE*SIZE, fpK ));<br>
<br> for ( i = 0; i < SIZE; i++ )<br> for ( j = i; j < SIZE; j++ ) a[i+j*SIZE]= *(K+i*SIZE+j);<br><br> MatCreateSeqDense(PETSC_COMM_SELF, n, n, a, &A); <br> MatView(A,PETSC_VIEWER_BINARY_(PETSC_COMM_WORLD));<br>
<br> ierr = PetscFinalize();CHKERRQ(ierr);<br> return 0;<br>}<br><br><br><br><div class="gmail_quote">On Wed, Jul 13, 2011 at 1:02 AM, Barry Smith <span dir="ltr"><<a href="mailto:bsmith@mcs.anl.gov">bsmith@mcs.anl.gov</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><br>
If it is truly a dense matrix then it isn't clear that you want to store it as a PETSc binary matrix but you can using the following code<br>
<br>
PetscInt n;<br>
PetscScalar *values;<br>
<br>
allocate values the correct size; read the entries into values then call MatCreateSeqDense(PETSC_COMM_SELF,n,n,values,&A); the call MatView(A,PETSC_VIEWER_BINARY_(PETSC_COMM_WORLD));<br>
<font color="#888888"><br>
Barry<br>
</font><div><div class="h5"><br>
On Jul 12, 2011, at 11:41 PM, John Chludzinski wrote:<br>
<br>
> I have a matrix stored as a sequential list of binary values (not PETSc binary form) that I read in with a single block read:<br>
><br>
> read( (void *)K, sizeof(double), SIZE*SIZE, fpK ).<br>
><br>
> How do I convert this simple binary file of (sequentially stored) doubles into a "PETSc binary matrix file"?<br>
><br>
> ---John<br>
<br>
</div></div></blockquote></div><br>