csr.c

Go to the documentation of this file.
00001 /*
00002 %M Esse módulo contem as funções para montagem da matriz de rigidez
00003  * no formato compressed sparse row
00004  *     06-07 André Luis Muller
00005  *
00006  */
00007 
00008 /*
00009 ** ------------------------------------------------------------------------
00010 ** Global variables and symbols:
00011 */
00012 #include <stdio.h>
00013 #include <stdlib.h>
00014 #include <math.h>
00015 
00016 #include "elm.h"
00017 #include "node.h"
00018 #include "alg.h"
00019 #include "fem.h"
00020 #include "csr.h"
00021 
00022 /*
00023 ** ------------------------------------------------------------------------
00024 ** Local variables and symbols:
00025 */
00026 
00027 
00028 /*
00029 ** ------------------------------------------------------------------------
00030 ** Local functions:
00031 */
00032 
00033 
00034 /*
00035 %F
00036 */
00037 void BuildIaVec(CsrNode *node_list, int *ia, int nnu, int *nna)
00038 {
00039  /* esta função controi o vetor ia do formato CSR
00040  */
00041 
00042  /* inicia nna
00043  */
00044  *nna = 0;
00045  
00046  /* constroi o vetor ia
00047  */
00048  BuildIaVector(node_list, nnu, nna, ia);
00049 
00050 }
00051 /* fim da função BuildIaVector */
00052 
00053 /*
00054 %F
00055 */
00056 void BuildJaVec(CsrNode *node_list, int *ja)
00057 {
00058  /* esta função controi o vetor ja do formato CSR
00059  */
00060 
00061  /* constroi o vetor ja
00062  */
00063  BuildJaVector(node_list, ja);
00064 
00065  
00066 }
00067 /* fim da função BuildJaVector */
00068 
00069 /*=====================  BuildNodeList ======================*/
00070 
00071 void BuildNodeList( CsrNode *node_list )
00072 {
00073  int       elem;                  /* elemento corrente             */
00074  int       node;                  /* nó corrente                   */
00075  int       next;                  /* next                          */
00076  int       j,k;                   /* índice                        */
00077  int       inc[8];                /* incidência                    */
00078  int       index;                 /* número de nós do elemento     */
00079  
00080  /* loop para todos os elementos
00081  */
00082  for( elem = 0; elem < NumElements; elem++ ){  
00083   /* pega o num. de nós e os nós do elemento 
00084   */
00085   ElmGetInc(ElmList[elem], inc, &index);
00086   for( j = 0; j < index; j++ ){
00087    node = inc[j];
00088    for( k = 0; k < index; k++ ){
00089     if(k != j){
00090      next = inc[k];
00091      /* coloca o nó na lista
00092      */
00093      mshAddAdjNode( &node_list[node], next );
00094     }
00095    }
00096   }
00097  }
00098 }
00099 
00100 /* fim da função BuildNodeList */
00101 
00102 /*=====================  BuildIaVector  ======================*/
00103 
00104 void BuildIaVector(CsrNode *node_list, int nnu, int *nna, int *ia )
00105 {
00106  /* essa função cria o vetor ia do formato CSR
00107  */
00108  int       i,j,k;                     
00109  AdjNode   *adjnode;
00110  AdjNode   *adjnodehead;
00111  int       nn;
00112 
00113  /* inicia variáveis
00114  */
00115  k = 1;
00116  
00117  /* seta a possição 0 do vetor ia
00118  */
00119  ia[0] = 0;
00120 
00121  /* percorre a lista de nós
00122  */
00123  if( node_list != NULL ){
00124   for ( j = 0; j < NumNodes; j++) {
00125    nn = 0;
00126    adjnodehead = node_list[j].adj_head;
00127    while ( adjnodehead != NULL ){
00128     adjnode = adjnodehead;
00129     adjnodehead = adjnode->next; 
00130     nn ++;
00131    }
00132    /* adiciona os termos de todos os
00133    graus de liberdade
00134    */
00135    for ( i = 0; i < NDof; i++) {
00136     ia[k] = ia[k-1] + (nn + 1) * NDof;
00137     k ++;
00138    }
00139    /* incrementa nna
00140    */
00141    *nna += ((nn + 1) * NDof) * NDof;
00142   }
00143  }
00144 
00145  /* seta o valor para o último termo do
00146     vetor ia
00147     */
00148  ia[nnu] = *nna;
00149 }
00150 
00151 /* fim da função BuildIaVector */
00152 
00153 /*=====================  BuildJaVector  ======================*/
00154 
00155 void BuildJaVector(CsrNode *node_list, int *ja )
00156 {
00157  /* essa função cria o vetor ja do formato CSR
00158  */
00159  int       i,j,l,m;                     
00160  AdjNode   *adjnode;
00161  AdjNode   *adjnodehead;
00162  int       nv, cja[200];
00163 
00164  /* inicia l
00165  */
00166  l = 0;
00167 
00168  /* percorre a lista de nós
00169  */
00170  if( node_list != NULL ){
00171   for ( j = 0; j < NumNodes; j++) {
00172    nv = 0;
00173    for ( i = 0; i < NDof; i++) {
00174     ja[l] = cja[nv] = NDof*j+i;
00175     l ++;
00176     nv ++;
00177    }
00178    adjnodehead = node_list[j].adj_head;
00179    while ( adjnodehead != NULL ){
00180     adjnode = adjnodehead;
00181     for ( i = 0; i < NDof; i++) {
00182      ja[l] = cja[nv] = NDof*adjnodehead->id+i;
00183      l ++;
00184      nv ++;
00185     }
00186     adjnodehead = adjnode->next; 
00187    }
00188    /* adiciona os termos de todos os 
00189       graus de liberdade
00190    */
00191    for ( i = 0; i < (NDof - 1); i++) {
00192     for ( m = 0; m < nv; m++) {
00193      /* faz a correção do primeiro termo de cada linha
00194      */
00195      if(m == 0){
00196       if(i == 0){
00197        ja[l] = cja[1];
00198        l ++;
00199        ja[l] = cja[0];      
00200        m++;
00201       }
00202       /* se NDof maior que 2
00203       */
00204       else{
00205        ja[l] = cja[2];
00206        l ++;
00207        ja[l] = cja[1]; 
00208        l ++;
00209        ja[l] = cja[0];     
00210        m = 2;
00211       } 
00212      }
00213      else{
00214       ja[l] = cja[m];
00215      }
00216      /* incrementa l
00217      */
00218      l ++;
00219     }
00220    }
00221   }
00222  }
00223 }
00224 
00225 /* fim da função BuildJaVector */
00226 
00227 /*========================  mshAddAdjNode  ========================*/
00228 
00229 void mshAddAdjNode( CsrNode *node, int adj_id )
00230 {
00231  /* essa função cria a lista de adjacências de todos
00232     os nós da malha de elementos finitos
00233  */
00234  AdjNode   *newadj;             
00235  AdjNode   *adjnode;            
00236  int       found = 0;           
00237  
00238  adjnode = node->adj_head;                    /* inicializa adjnode*/
00239  while( adjnode != NULL)                      /* loop enquanto adjnode diferente de NULL(zero),ou seja, percorre toda a lista*/
00240  {                                             
00241   if( adjnode->id == adj_id )                 /* testa índice*/
00242   {
00243    found=1;
00244    break;                                      
00245   }
00246   adjnode = adjnode->next;                    /* incrementa adjnode apontando para o seguinte*/
00247  }
00248  if(!found){
00249   newadj=(AdjNode *)malloc(sizeof(AdjNode));  /* Aloca memória dinâmicamente*/                                              
00250   newadj->next=node->adj_head;                /* nova adj aponta para cabeçário*/
00251   newadj->id=adj_id;                          /* indexa nó à lista*/ 
00252   node->adj_head = newadj;
00253  } 
00254 }
00255 
00256 /* fim da função mshAddAdjNode */
00257 
00258 /*========================  FreeNodeList  =========================*/
00259 
00260 void FreeNodeList( CsrNode *node_list )
00261 {
00262  AdjNode   *adjnode;    /* auxiliar adjacent node pointer   */
00263  int       i;           /* counter                          */
00264 
00265  if( node_list != NULL ){
00266   for ( i = 0; i < NumNodes; i++)        /* loop para todos os nós */
00267    while ( node_list[i].adj_head != NULL ){
00268     adjnode = node_list[i].adj_head;
00269     node_list[i].adj_head = adjnode->next;
00270     free (adjnode);                      /* libera memoria da lista de nós adjacentes */
00271    }
00272   free (node_list);             
00273  }
00274  node_list = NULL;
00275 }
00276 
00277 /* fim da função FreeNodeList */
00278 
00279 /*=====================  BuildaVector  ======================*/
00280 
00281 int BuildaVector(int i, int j, double coef, int *ia, int *ja, double *avector )
00282 {
00283  /* essa função coloca os termos da matriz global
00284     no vetor avector segundo o formato CSR
00285  */
00286  int k;
00287  /* percorre cada linha da matriz global*/
00288  for( k = 0; k < (ia[i + 1] - ia[i]); k++ ){
00289   if(ja[ia[i]+k] == j){
00290    /* monta o vetor de coeficientes*/
00291    avector[ia[i]+k] += coef;
00292    return(1);
00293   }
00294  }
00295  return (0);
00296 }
00297 
00298 /* fim da função BuildaVector */
00299 
00300 /* =======================================================  End of File  */
00301 

Generated on Tue Oct 23 11:23:29 2007 for Relax by  doxygen 1.5.3