⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readgeo.c

📁 有限元学习研究用源代码(老外的),供科研人员参考
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "igridgeo.h"


struct igridgeo geo = { 1.0, 0, 0, 0,
			NULL, NULL, NULL };

static int  sidetable[5] = {0, 0, 4, 3, 4};

/* This program read in the geometry file generated by IGRID.
   Return 0 if the geometry is read correctly,
   else return error codes which are defined in igridgeo.h
*/
			
void main(){
    char filename[32];
    int status;

    printf("Input filename\n");
    scanf("%s", filename);
    status = readgeometry(filename, &geo);
    if(status == OK)
      printgeometry(&geo);
    else
      printf("status = %d\n", status);
}

int readgeometry(char *filename, struct igridgeo *geo){

    char signature[64], keyword[64];
    int status = OK;
    FILE *stream;

    if((stream = fopen(filename, "r")) == NULL) return FILE_ERROR;
    fscanf(stream, "%60s", signature);
    if(checksignature(signature) != OK){
      fclose(stream);
      return WRONGSIGNATURE;
    }

    while(fscanf(stream, "%60s", keyword) != EOF){
      if(strcmp(keyword, KW_UNIT) == 0){
        status = readunit(stream, geo);
        if (status != OK) break;
      }
      else if(strcmp(keyword, KW_NCELL) == 0){
        status = readncell(stream, geo);
        if (status != OK) break;
      }
      else if(strcmp(keyword, KW_NSIDE) == 0){
        status = readnside(stream, geo);
        if (status != OK) break;
      }
      else if(strcmp(keyword, KW_NNODE) == 0){
        status = readnnode(stream, geo);
        if (status != OK) break;
      }
      else if(strcmp(keyword, KW_CELL) == 0){
        status = readcell(stream, geo);
        if (status != OK) break;
      }
      else if(strcmp(keyword, KW_SIDE) == 0){
        status = readside(stream, geo);
        if (status != OK) break;
      }
      else if(strcmp(keyword, KW_NODE) == 0){
        status = readnode(stream, geo);
        if (status != OK) break;
      }
      else{
        printf("Keyword not recognized.\n");
        status = gotodelimitor(stream);
      }
    }
    fclose(stream);
    return status;
}

int checksignature(char *signature){

    if(strcmp(GEOSIGNATURE, signature) == 0) return OK;
    else return WRONGSIGNATURE;
}


int readunit(FILE *stream, struct igridgeo *geo){

    fscanf(stream, "%lf }", &(geo->unit));
    return OK;
}

int readncell(FILE *stream, struct igridgeo *geo){

    int i, j;

    fscanf(stream, "%d }", &(geo->ncell));

    if((geo->cell = malloc((geo->ncell+1) * sizeof(struct cell))) == NULL)
      return OUTOFMEM;

    // init cells
    for(i = 0; i <= geo->ncell; i++){
      geo->cell[i].type = 0;
      geo->cell[i].property1 = geo->cell[i].property2 =
      geo->cell[i].property3 = geo->cell[i].property4 = 0;
      for(j = 0; j < 4; j++) geo->cell[i].side[j] = 0;
    }
    return OK;
}


int readnside(FILE *stream, struct igridgeo *geo){

    int i, j;
    fscanf(stream, "%d }", &(geo->nside));

    if((geo->side = malloc((geo->nside+1) * sizeof(struct side)))==NULL)
      return OUTOFMEM;

    // init sides
    for(i = 0; i <= geo->nside; i++){
      geo->side[i].property = 0;
      geo->side[i].node[0] = geo->side[i].node[1] = 0;
      geo->side[i].cell[0] = geo->side[i].cell[1] = 0;
    }
    return OK;
}

int readnnode(FILE *stream, struct igridgeo *geo){

    int i, j;

    fscanf(stream, "%d }", &(geo->nnode));

    if((geo->node = malloc((geo->nnode+1) * sizeof(struct node))) == NULL)
      return OUTOFMEM;

    // init nodes
    for(i = 0; i <= geo->nside; i++){
      geo->node->property = 0;
      // geo->node.x = geo->node.y = 0.0;
    }
    return OK;
}

int readcell(FILE *stream, struct igridgeo *geo){

    int i, j;
    int status = OK;
    struct cell *pcell;


    if(geo->ncell <= 0) return NCELLUNKNOWN;
    if(geo->nside <= 0) return NSIDEUNKNOWN;

    for(i = 1; i <= geo->ncell; i++){
      pcell = geo->cell;
      fscanf(stream, "%*u %u %lf %lf %lf %lf",
        &pcell[i].type, &pcell[i].property1, &pcell[i].property2,
        &pcell[i].property3, &pcell[i].property4);

      for(j = 0; j < sidetable[pcell[i].type]; j++){
        fscanf(stream, "%u", &pcell[i].side[j]);
        status = updateside(geo, i, pcell[i].side[j]);
      }
    }
    fscanf(stream, " }");
    return status;
}


int readside(FILE *stream, struct igridgeo *geo){

    int i;
    int status = OK;

    if(geo->nside <= 0) return NSIDEUNKNOWN;

    for(i = 1; i <= geo->nside; i++){
      fscanf(stream, "%*u %u %u %u",
        &geo->side[i].property, &geo->side[i].node[0],
        &geo->side[i].node[1]);
    }
    fscanf(stream, " }");
    return status;
}


int readnode(FILE *stream, struct igridgeo *geo){

    int i;
    int status = OK;

    if(geo->nnode <= 0) return NNODEUNKNOWN;

    for(i = 1; i <= geo->nnode; i++){
      fscanf(stream, "%*u %u %lf %lf",
        &geo->node[i].property, &geo->node[i].x,
        &geo->node[i].y);
    }
    fscanf(stream, " }");
    return status;
}

int updateside(struct igridgeo *geo, int c, int s){

    int status = OK;

    if(geo->side[s].cell[0] == 0){
      geo->side[s].cell[0] = c;
    }
    else if (geo->side[s].cell[1] == 0){
      geo->side[s].cell[1] = c;
    }
    else{
      // printf("cell = %u, side = %u  ", c, s);
      status = GEO_ERROR;
    }
    return status;
}

int gotodelimitor(FILE *stream){
    char c;
    int  status;
    while((fscanf(stream, "%c", &c) != EOF) && (c != '}'));
    if(c == '}') status = OK;
    else{
      fclose(stream);
      status = NO_DELIMITOR;
    }
    return status;
}


int printgeometry(struct igridgeo *geo){

    int i;

    printf("Unit = %lf\n", geo->unit);
    printf("nCell = %u\n", geo->ncell);
    printf("nSide = %u\n", geo->nside);
    printf("nNode = %u\n", geo->nnode);

    for(i = 1; i <= geo->ncell; i++)
      printf("c %u, p1 = %lf, p2 = %lf, s1 = %u, s2 = %u, s3 = %u, s4 = %u\n",
        i, geo->cell[i].property1, geo->cell[i].property2,
        geo->cell[i].side[0], geo->cell[i].side[1],
        geo->cell[i].side[2], geo->cell[i].side[3]);

    for(i = 1; i <= geo->nside; i++)
      printf("s %u, n1 = %u, n2 = %u, c1 = %u, c2 = %u\n", 
        i, geo->side[i].node[0], geo->side[i].node[1], 
           geo->side[i].cell[0], geo->side[i].cell[1]);

    for(i = 1; i <= geo->nnode; i++)
      printf("n %u, x = %lf, y = %lf\n", i, geo->node[i].x,
        geo->node[i].y);
    return OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -