CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Main CFD Forum

Elemtary matrix to CSR global matrix

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   September 19, 2002, 12:42
Default Elemtary matrix to CSR global matrix
  #1
xueying
Guest
 
Posts: n/a
How can I assemble elemtary matrix to global matrix in CSR format? If anybody has this subroutine, can you share it with me?
  Reply With Quote

Old   September 22, 2002, 14:50
Default Re: Elemtary matrix to CSR global matrix
  #2
Wen Long
Guest
 
Posts: n/a
Here is my C progam, can you try it?

//--------------

/*Using Compressed Row Storage method to represente a sparce matrix to do A*Vin=Vout (matrix vector multiplying) */

#include <stdio.h> #include <stdlib.h>

/*#define N 100; */ /*The maximum size of the matrix is N*N */

void main(void) { float E[10000];

long int C[10000],R[101]; /* E[]----Element value vector

C[]----Column index vector

R[]----Row position vector */

float Vin[100], Vout[100]; /* Vin[N]---Input vector */

/* Vout[N]---Output vector, Vout[]=A*Vin[] */

long int n; /*actual matrix size */ long int num; long int i; void multiply(float*,long int *, long int *, long int ,float *,float * );

Again:

printf("Input size of matrix A, n=:\n"); scanf("%d",&n); if(n>100) {printf("n must < 100!, try again\n"); goto Again; }

printf("Input total number of non-zero elements in A\n"); scanf("%d",&num);

for(i=0;i<num; i++) /* input E[] and C[] */

{

printf("E[%d]=\n",i);

scanf("%f",&E[i]);

printf("C[%d]=\n",i);

scanf("%d",&C[i]);

}

for(i=0; i<=n; i++) /*input R[] */

{

printf("R[%d]=\n",i);

scanf("%d",&R[i]);

}

for(i=0; i<n; i++) /*input Vin[] */

{

printf("Vin[%d]=\n",i);

scanf("%f",&Vin[i]);

}

multiply(E,C,R,n,Vin,Vout); /*do the calculation */

printf("Vout[%d=",n); /* print Vout[n] */ for(i=0; i<n; i++)

{

printf("%10f\n",Vout[i]);

}

}

void multiply(float *pE,

long int *pC,

long int *pR,

long int n,

float *pVin,

float *pVout)

{

/* pE----pointer to array E */

/* pC----pointer to array C */

/* pR----pointer to array R */

/* pVin---pointer to array Vin */

/* pVout---pointer to array Vout */

int i,j;

for(i=0; i<n; i++) /* row i of A */

{

pVout[i]=0;

for(j=pR[i];j<pR[i+1];j++) /*j is the range of index of row i in E[] */

{

pVout[i]=pVout[i]+pE[j]*pVin[pC[j]]; /* sum up */

}

}

}

  Reply With Quote

Old   September 24, 2002, 09:44
Default Re: Elemtary matrix to CSR global matrix
  #3
Wen Long
Guest
 
Posts: n/a
Sorry, some chars missing when I was copying it (seems funny).

Here is the right one:

/*Using Compressed Row Storage method to represente a sparce matrix to do A*Vin (matrix vector multiplying) */

#include <stdio.h> #include <stdlib.h>

/*#define N 100; */ /*The maximum size of the matrix is N*N */

void main(void) { float E[10000];

long int C[10000],R[101]; /* E[]----Element value vector

C[]----Column index vector

R[]----Row position vector */

float Vin[100], Vout[100]; /* Vin[N]---Input vector */

/* Vout[N]---Output vector, Vout[]=A*Vin[] */

long int n; /*actual matrix size */ long int num; long int i; void multiply(float*,long int *, long int *, long int ,float *,float * );

Again:

printf("Input size of matrix A, n=:\n"); scanf("%d",&n); if(n>100) {printf("n must < 100!, try again\n"); goto Again; }

printf("Input total number of non-zero elements in A\n"); scanf("%d",&num);

for(i=0; i < num ; i++)

/* input E[] and C[] */

{

printf("E[%d]=\n",i);

scanf("%f",&E[i]);

printf("C[%d]=\n",i);

scanf("%d",&C[i]);

}

for(i=0; i < = n; i++) /*input R[] */

{

printf("R[%d]=\n",i);

scanf("%d",&R[i]);

}

for(i=0; i < n; i++)

/*input Vin[] */

{

printf("Vin[%d]=\n",i);

scanf("%f",&Vin[i]);

}

multiply(E,C,R,n,Vin,Vout); /*do the calculation */

printf("Vout[%d=",n); /* print Vout[n] */

for(i=0 ;

i < n ;

i++)

{

printf("%10f\n",Vout[i]);

}

}

void multiply(float *pE,

long int *pC,

long int *pR,

long int n,

float *pVin,

float *pVout)

{

/* pE----pointer to array E */

/* pC----pointer to array C */

/* pR----pointer to array R */

/* pVin---pointer to array Vin */

/* pVout---pointer to array Vout */

int i,j;

for(i=0;

i < n;

i++) /* row i of A */

{

pVout[i]=0;

for(j=pR[i];j<pR[i+1];j++) /*j is the range of index of row i in E[] */

{

pVout[i]=pVout[i]+pE[j]*pVin[pC[j]]; /* sum up */

}

}

}
  Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write k and epsilon before the abnormal end xiuying OpenFOAM Running, Solving & CFD 8 August 27, 2013 15:33
Upgraded from Karmic Koala 9.10 to Lucid Lynx10.04.3 bookie56 OpenFOAM Installation 8 August 13, 2011 04:03
Convergence moving mesh lr103476 OpenFOAM Running, Solving & CFD 30 November 19, 2007 14:09
IcoFoam parallel woes msrinath80 OpenFOAM Running, Solving & CFD 9 July 22, 2007 02:58
Could anybody help me see this error and give help liugx212 OpenFOAM Running, Solving & CFD 3 January 4, 2006 18:07


All times are GMT -4. The time now is 09:04.