Skip to content

Commit

Permalink
Added Su2ToCfduConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
heySourabh committed Aug 11, 2023
1 parent 4038a68 commit b551123
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/main/util/Su2ToCfduConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package main.util;

import main.geom.Point;
import main.geom.VTKType;
import main.io.DataFileReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import static java.util.Arrays.copyOfRange;

public class Su2ToCfduConverter {

private final File s2uFile;
private final File cfduFile;

public Su2ToCfduConverter(File s2uFile, File cfduFile) {
this.s2uFile = s2uFile;
this.cfduFile = cfduFile;
}

public void convert() throws FileNotFoundException {
try (DataFileReader su2Reader = new DataFileReader(this.s2uFile, "%");
PrintWriter cfduWriter = new PrintWriter(cfduFile)) {
int numDimensions = su2Reader.readIntParameter("NDIME");

int numElements = su2Reader.readIntParameter("NELEM");
Su2Element[] elements = new Su2Element[numElements];
for (int ei = 0; ei < numElements; ei++) {
int[] conn = su2Reader.readIntArray();
VTKType vtkType = VTKType.get(conn[0]);
elements[ei] = new Su2Element(vtkType, copyOfRange(conn, 1, vtkType.numPoints() + 1));
}

int numPoints = su2Reader.readIntParameter("NPOIN");
Point[] points = new Point[numPoints];
for (int i = 0; i < numPoints; i++) {
points[i] = createPoint(su2Reader.readDoubleArray(), numDimensions);
}

int numBndMarkers = su2Reader.readIntParameter("NMARK");
Su2Boundary[] boundaries = new Su2Boundary[numBndMarkers];
for (int bnd = 0; bnd < numBndMarkers; bnd++) {
String markerTag = su2Reader.readParameter("MARKER_TAG");
int numBndElements = su2Reader.readIntParameter("MARKER_ELEMS");
Su2Element[] bndElements = new Su2Element[numBndElements];
for (int ei = 0; ei < numBndElements; ei++) {
int[] conn = su2Reader.readIntArray();
VTKType vtkType = VTKType.get(conn[0]);
bndElements[ei] = new Su2Element(vtkType, copyOfRange(conn, 1, vtkType.numPoints() + 1));
}
boundaries[bnd] = new Su2Boundary(markerTag, bndElements);
}

// ------ Finished reading su2 file, now writing to cfdu file

cfduWriter.println("dimension = " + numDimensions);
cfduWriter.println("mode = ASCII");

cfduWriter.println("points = " + points.length);
for (Point point : points) {
cfduWriter.printf("%1.8g %1.8g %1.8g%n", point.x, point.y, point.z);
}

cfduWriter.println("elements = " + elements.length);
for (Su2Element element : elements) {
cfduWriter.printf("%d", element.vtkType.ID);
for (int i = 0; i < element.connectivity.length; i++) {
cfduWriter.printf(" %d", element.connectivity[i]);
}
cfduWriter.println();
}

cfduWriter.println("boundaries = " + boundaries.length);
for (Su2Boundary boundary : boundaries) {
cfduWriter.println("bname = " + boundary.tag);
cfduWriter.println("bfaces = " + boundary.bndElements.length);
for (Su2Element bndElement : boundary.bndElements) {
cfduWriter.printf("%d", bndElement.vtkType.ID);
for (int i = 0; i < bndElement.connectivity.length; i++) {
cfduWriter.printf(" %d", bndElement.connectivity[i]);
}
cfduWriter.println();
}
}
}
}

private Point createPoint(double[] pointData, int dimension) {
return switch (dimension) {
case 2 -> new Point(pointData[0], pointData[1], 0.0);
case 3 -> new Point(pointData[0], pointData[1], pointData[2]);
default -> throw new IllegalArgumentException("Unable to create point of dimension " + dimension);
};
}

private record Su2Element(VTKType vtkType, int[] connectivity) {
}

private record Su2Boundary(String tag, Su2Element[] bndElements) {
}
}
153 changes: 153 additions & 0 deletions test/main/util/Su2ToCfduConverterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package main.util;

import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

import static org.junit.Assert.assertEquals;

public class Su2ToCfduConverterTest {

@Test
public void convert2DMeshFile() throws IOException {
File inputFile = new File("test/test_data/su2_to_cfdu/square.su2");
File outputFile = new File("test/test_data/su2_to_cfdu/square.cfdu");
Su2ToCfduConverter converter = new Su2ToCfduConverter(inputFile, outputFile);
converter.convert();

String expectedOutput = """
dimension = 2
mode = ASCII
points = 9
0.0000000 0.0000000 0.0000000
0.50000000 0.0000000 0.0000000
1.0000000 0.0000000 0.0000000
0.0000000 0.50000000 0.0000000
0.50000000 0.50000000 0.0000000
1.0000000 0.50000000 0.0000000
0.0000000 1.0000000 0.0000000
0.50000000 1.0000000 0.0000000
1.0000000 1.0000000 0.0000000
elements = 8
5 0 1 3
5 1 4 3
5 1 2 4
5 2 5 4
5 3 4 6
5 4 7 6
5 4 5 7
5 5 8 7
boundaries = 4
bname = lower
bfaces = 2
3 0 1
3 1 2
bname = right
bfaces = 2
3 2 5
3 5 8
bname = upper
bfaces = 2
3 8 7
3 7 6
bname = left
bfaces = 2
3 6 3
3 3 0
""";
assertEquals(Files.readString(outputFile.toPath()), expectedOutput);
}

@Test
public void convert3DMeshFile() throws IOException {
File inputFile = new File("test/test_data/su2_to_cfdu/cube.su2");
File outputFile = new File("test/test_data/su2_to_cfdu/cube.cfdu");
Su2ToCfduConverter converter = new Su2ToCfduConverter(inputFile, outputFile);
converter.convert();

String expectedOutput = """
dimension = 3
mode = ASCII
points = 14
-1.0000000 -1.0000000 -1.0000000
1.0000000 -1.0000000 -1.0000000
1.0000000 1.0000000 -1.0000000
-1.0000000 1.0000000 -1.0000000
-1.0000000 1.0000000 1.0000000
-1.0000000 -1.0000000 1.0000000
1.0000000 -1.0000000 1.0000000
1.0000000 1.0000000 1.0000000
0.0000000 0.0000000 -1.0000000
-1.0000000 0.0000000 0.0000000
0.0000000 -1.0000000 0.0000000
1.0000000 0.0000000 0.0000000
0.0000000 1.0000000 0.0000000
0.0000000 0.0000000 1.0000000
elements = 24
10 12 13 10 11
10 9 13 10 12
10 10 8 9 12
10 12 10 8 11
10 9 8 0 3
10 5 0 9 10
10 0 8 10 1
10 9 3 4 12
10 9 4 5 13
10 11 6 10 1
10 10 13 5 6
10 7 6 13 11
10 7 13 4 12
10 2 3 8 12
10 12 2 7 11
10 11 8 2 1
10 6 10 13 11
10 0 8 9 10
10 13 5 9 10
10 13 9 4 12
10 12 7 13 11
10 3 9 8 12
10 8 2 12 11
10 11 10 8 1
boundaries = 6
bname = left
bfaces = 4
5 0 9 3
5 5 9 0
5 3 9 4
5 4 9 5
bname = right
bfaces = 4
5 2 11 1
5 1 11 6
5 7 11 2
5 6 11 7
bname = upper
bfaces = 4
5 3 12 2
5 2 12 7
5 4 12 3
5 7 12 4
bname = lower
bfaces = 4
5 1 10 0
5 0 10 5
5 6 10 1
5 5 10 6
bname = front
bfaces = 4
5 4 5 13
5 7 4 13
5 5 6 13
5 6 7 13
bname = back
bfaces = 4
5 0 1 8
5 3 0 8
5 1 2 8
5 2 3 8
""";
assertEquals(Files.readString(outputFile.toPath()), expectedOutput);
}
}
49 changes: 49 additions & 0 deletions test/test_data/su2_to_cfdu/square.su2
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
%
% Problem dimension
%
NDIME= 2
%
% Inner element connectivity
%
NELEM= 8
5 0 1 3 0
5 1 4 3 1
5 1 2 4 2
5 2 5 4 3
5 3 4 6 4
5 4 7 6 5
5 4 5 7 6
5 5 8 7 7
%
% Node coordinates
%
NPOIN= 9
0.00000000000000 0.00000000000000 0
0.50000000000000 0.00000000000000 1
1.00000000000000 0.00000000000000 2
0.00000000000000 0.50000000000000 3
0.50000000000000 0.50000000000000 4
1.00000000000000 0.50000000000000 5
0.00000000000000 1.00000000000000 6
0.50000000000000 1.00000000000000 7
1.00000000000000 1.00000000000000 8
%
% Boundary elements
%
NMARK= 4
MARKER_TAG= lower
MARKER_ELEMS= 2
3 0 1
3 1 2
MARKER_TAG= right
MARKER_ELEMS= 2
3 2 5
3 5 8
MARKER_TAG= upper
MARKER_ELEMS= 2
3 8 7
3 7 6
MARKER_TAG= left
MARKER_ELEMS= 2
3 6 3
3 3 0

0 comments on commit b551123

Please sign in to comment.