~njansson/dolfin/hpc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/perl

# Convert an inp mesh file to an xml file that can be read by DOLFIN
#
# Usage: inp2xml mesh.inp mesh.xml
#
# Copyright (C) 2002 Anders Logg.
# Licensed under the GNU LGPL Version 2.1.
#
# First added:  2002-12-06
# Last changed: 2002

use Text::ParseWords;

# Open files
open(INFILE,"<$ARGV[0]");
open(OUTFILE,">$ARGV[1]");

# State
$state = "header";

# Variables
$nodes = 0;

# Parse file
while ($line=<INFILE>) {

  # Remove leading and trailing space
  $line =~ s/^\s+//;
  $line =~ s/\s+$//;

  # Convert line into list of words
  @words = &shellwords($line);

  # Convert

  if ( "$state" eq "header" ) {

	 $nodes = @words[0];
	 $cells = @words[1];

	 print ( OUTFILE "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" );
	 print ( OUTFILE "\n" );
	 print ( OUTFILE "<dolfin xmlns:dolfin=\"http://www.phi.chalmers.se/dolfin/\">\n" );
	 print ( OUTFILE "  <mesh>\n");
	 print ( OUTFILE "    <nodes size=\"$nodes\">\n" );
	 
	 $state = "nodes";
		
  }
  elsif ( "$state" eq "nodes" ) {

	 $number = @words[0] - 1;
	 $x = @words[1];
	 $y = @words[2];
	 $z = @words[3];
	 
	 print ( OUTFILE "      <node name=\"$number\" x=\"$x\" y=\"$y\" z=\"$z\"/>\n" );

	 if ( $number == ( $nodes - 1 ) ){

		print ( OUTFILE "    </nodes>\n" );
		print ( OUTFILE "    <cells size=\"$cells\">\n" );
				
		$state = "cells";
	 }
		
  }
  elsif ( "$state" eq "cells" ) {

	 $number = @words[0] - 1;
	 $dummy  = @words[1];
	 $type   = @words[2];

	 if ( "$type" eq "tet" ) {
		
		$n0 = @words[3] - 1;
		$n1 = @words[4] - 1;
		$n2 = @words[5] - 1;
		$n3 = @words[6] - 1;

		print ( OUTFILE "      <tetrahedron name=\"$number\" n0=\"$n0\" n1=\"$n1\" n2=\"$n2\" n3=\"$n3\"/>\n" );

		if ( $number == ($cells - 1) ){

		  print ( OUTFILE "    </cells>\n");
		  print ( OUTFILE "  </mesh>\n");
		  print ( OUTFILE "</dolfin>");
		
		}
		
	 }
	 elsif ( "$type" eq "tri" ) {
		
		$n0 = @words[3] - 1;
		$n1 = @words[4] - 1;
		$n2 = @words[5] - 1;

		print ( OUTFILE "      <triangle name=\"$number\" n0=\"$n0\" n1=\"$n1\" n2=\"$n2\"/>\n" );

		if ( $number == ($cells - 1) ) {

		  print ( OUTFILE "    </cells>\n");
		  print ( OUTFILE "  </mesh>\n");
		  print ( OUTFILE "</dolfin>");
		
		}
		
	 }

  }

}