~njansson/dolfin/hpc

« back to all changes in this revision

Viewing changes to misc/utils/xml/inp2xml

  • Committer: Johannes Ring
  • Date: 2008-03-05 22:43:06 UTC
  • Revision ID: johannr@simula.no-20080305224306-2npsdyhfdpl2esji
The BIG commit!

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
# Convert an inp mesh file to an xml file that can be read by DOLFIN
 
4
#
 
5
# Usage: inp2xml mesh.inp mesh.xml
 
6
#
 
7
# Copyright (C) 2002 Anders Logg.
 
8
# Licensed under the GNU LGPL Version 2.1.
 
9
#
 
10
# First added:  2002-12-06
 
11
# Last changed: 2002
 
12
 
 
13
use Text::ParseWords;
 
14
 
 
15
# Open files
 
16
open(INFILE,"<$ARGV[0]");
 
17
open(OUTFILE,">$ARGV[1]");
 
18
 
 
19
# State
 
20
$state = "header";
 
21
 
 
22
# Variables
 
23
$nodes = 0;
 
24
 
 
25
# Parse file
 
26
while ($line=<INFILE>) {
 
27
 
 
28
  # Remove leading and trailing space
 
29
  $line =~ s/^\s+//;
 
30
  $line =~ s/\s+$//;
 
31
 
 
32
  # Convert line into list of words
 
33
  @words = &shellwords($line);
 
34
 
 
35
  # Convert
 
36
 
 
37
  if ( "$state" eq "header" ) {
 
38
 
 
39
         $nodes = @words[0];
 
40
         $cells = @words[1];
 
41
 
 
42
         print ( OUTFILE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
 
43
         print ( OUTFILE "\n" );
 
44
         print ( OUTFILE "<dolfin xmlns:dolfin=\"http://www.phi.chalmers.se/dolfin/\">\n" );
 
45
         print ( OUTFILE "  <mesh>\n");
 
46
         print ( OUTFILE "    <nodes size=\"$nodes\">\n" );
 
47
         
 
48
         $state = "nodes";
 
49
                
 
50
  }
 
51
  elsif ( "$state" eq "nodes" ) {
 
52
 
 
53
         $number = @words[0] - 1;
 
54
         $x = @words[1];
 
55
         $y = @words[2];
 
56
         $z = @words[3];
 
57
         
 
58
         print ( OUTFILE "      <node name=\"$number\" x=\"$x\" y=\"$y\" z=\"$z\"/>\n" );
 
59
 
 
60
         if ( $number == ( $nodes - 1 ) ){
 
61
 
 
62
                print ( OUTFILE "    </nodes>\n" );
 
63
                print ( OUTFILE "    <cells size=\"$cells\">\n" );
 
64
                                
 
65
                $state = "cells";
 
66
         }
 
67
                
 
68
  }
 
69
  elsif ( "$state" eq "cells" ) {
 
70
 
 
71
         $number = @words[0] - 1;
 
72
         $dummy  = @words[1];
 
73
         $type   = @words[2];
 
74
 
 
75
         if ( "$type" eq "tet" ) {
 
76
                
 
77
                $n0 = @words[3] - 1;
 
78
                $n1 = @words[4] - 1;
 
79
                $n2 = @words[5] - 1;
 
80
                $n3 = @words[6] - 1;
 
81
 
 
82
                print ( OUTFILE "      <tetrahedron name=\"$number\" n0=\"$n0\" n1=\"$n1\" n2=\"$n2\" n3=\"$n3\"/>\n" );
 
83
 
 
84
                if ( $number == ($cells - 1) ){
 
85
 
 
86
                  print ( OUTFILE "    </cells>\n");
 
87
                  print ( OUTFILE "  </mesh>\n");
 
88
                  print ( OUTFILE "</dolfin>");
 
89
                
 
90
                }
 
91
                
 
92
         }
 
93
         elsif ( "$type" eq "tri" ) {
 
94
                
 
95
                $n0 = @words[3] - 1;
 
96
                $n1 = @words[4] - 1;
 
97
                $n2 = @words[5] - 1;
 
98
 
 
99
                print ( OUTFILE "      <triangle name=\"$number\" n0=\"$n0\" n1=\"$n1\" n2=\"$n2\"/>\n" );
 
100
 
 
101
                if ( $number == ($cells - 1) ) {
 
102
 
 
103
                  print ( OUTFILE "    </cells>\n");
 
104
                  print ( OUTFILE "  </mesh>\n");
 
105
                  print ( OUTFILE "</dolfin>");
 
106
                
 
107
                }
 
108
                
 
109
         }
 
110
 
 
111
  }
 
112
 
 
113
}