/releases/felt-3.07/src/Patchwork/graph.c

# · C · 152 lines · 102 code · 24 blank · 26 comment · 33 complexity · fd6e0a7bbf5e95f2b9199a493edf9379 MD5 · raw file

  1. /*
  2. This file is part of the FElt finite element analysis package.
  3. Copyright (C) 1993-2000 Jason I. Gobat and Darren C. Atkinson
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. /*****************************************************************************
  17. *
  18. * File: graph.c
  19. *
  20. * Description: contains code to read and write basic ASCII data files that
  21. * are formatted for input into graphing packages like gnuplot
  22. *
  23. ****************************************************************************/
  24. # include <stdio.h>
  25. # include <string.h>
  26. # include "problem.h"
  27. # include "objects.h"
  28. # include "mesh.h"
  29. # include "patchwork.h"
  30. # include "definition.h"
  31. # include "error.h"
  32. # undef atof
  33. extern double atof ();
  34. extern double strtod ();
  35. extern int InitializeProblem ( );
  36. int ReadGraphFile (filename)
  37. char *filename;
  38. {
  39. FILE *fp;
  40. unsigned i;
  41. Node node [20];
  42. double x, y, z;
  43. Material material;
  44. Constraint constraint;
  45. char buffer [80];
  46. char *ptr1, *ptr2;
  47. Definition truss, cst, htk;
  48. if (strcmp (filename, "-") == 0)
  49. fp = stdin;
  50. else {
  51. fp = fopen (filename, "r");
  52. if (fp == NULL) {
  53. error ("graph: could not open %s for reading.", filename);
  54. return 1;
  55. }
  56. }
  57. InitializeProblem ( );
  58. constraint = CreateConstraint ("default");
  59. material = CreateMaterial ("default");
  60. truss = LookupDefinition ("truss");
  61. cst = LookupDefinition ("CSTPlaneStress");
  62. htk = LookupDefinition ("htk");
  63. i = 0;
  64. while (fgets (buffer, 80, fp) != NULL) {
  65. if (strcmp (buffer, "\n") != 0) {
  66. x = strtod (buffer, &ptr1);
  67. ptr1 = strchr (buffer, ' ');
  68. y = strtod (ptr1+1, &ptr2);
  69. ptr2 = strchr (ptr1+1, ' ');
  70. z = atof (ptr2+1);
  71. node [i] = AddNode (x, y, z, constraint, NULL);
  72. i++;
  73. }
  74. else {
  75. if (i == 2)
  76. AddElement (truss, node, material, NULL, 0);
  77. else if (i == 3)
  78. AddElement (cst, node, material, NULL, 0);
  79. else if (i == 4)
  80. AddElement (htk, node, material, NULL, 0);
  81. else {
  82. error ("graph: can only handle 2, 3, and 4 node elements.\n");
  83. return 1;
  84. }
  85. i = 0;
  86. }
  87. }
  88. problem.nodes = CoalesceNodes (problem.nodes, problem.elements,
  89. &(problem.num_nodes), problem.num_elements);
  90. if (fp != stdin)
  91. fclose (fp);
  92. return 0;
  93. }
  94. int WriteGraphFile (filename)
  95. char *filename;
  96. {
  97. FILE *output;
  98. unsigned i,j;
  99. Element e;
  100. if (strcmp (filename, "-") == 0)
  101. output = stdout;
  102. else {
  103. output = fopen (filename, "w");
  104. if (output == NULL) {
  105. error ("graph: could not open %s for writing.", filename);
  106. return 1;
  107. }
  108. }
  109. for (i = 1 ; i <= problem.num_elements ; i++) {
  110. e = problem.elements [i];
  111. for (j = 1 ; j <= e -> definition -> shapenodes ; j++) {
  112. if (e -> node[j] == NULL)
  113. break;
  114. fprintf (output,"%g %g %g\n", e -> node [j] -> x,
  115. e -> node [j] -> y, e -> node [j] -> z);
  116. }
  117. if (e -> definition -> shapenodes > 2)
  118. fprintf (output,"%g %g %g\n", e -> node [1] -> x,
  119. e -> node [1] -> y, e -> node [1] -> z);
  120. fprintf (output,"\n");
  121. }
  122. if (output != stdout)
  123. fclose (output);
  124. return 0;
  125. }