package uk.co.flyingsquirrels.aero;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import uk.co.flyingsquirrels.utils.FileUtils;
import uk.co.flyingsquirrels.utils.MathUtils;
/**
* Reads and stores polar data from JavaFoil at a fixed (single) Reynold's number
*/
public class AirfoilTable {
private static final String NUMBER = "-?\\d+\\.\\d+";
private static final Pattern PATTERN = Pattern.compile(String.format("^(%s)\\s+(%s)\\s+(%s)\\s+(%s)", NUMBER, NUMBER, NUMBER, NUMBER));
private static final int ALPHA = 0;
private static final int CL = 1;
private static final int CD = 2;
private static final int CM = 3;
private final String name;
private final long reynolds;
private final double[][] coeffs;
private final int dataLength;
public AirfoilTable(String airfoil) {
List<String> lines = FileUtils.readLines("airfoils/" + airfoil + ".txt");
name = lines.get(0);
reynolds = Long.valueOf(lines.get(2).split("=")[1].trim());
List<Coeff> coeffs = new ArrayList<Coeff>();
for (String line: lines) {
readData(line, coeffs);
}
this.dataLength = coeffs.size();
this.coeffs = new double[4][dataLength];
for (int i = 0; i < dataLength; i++) {
Coeff coeff = coeffs.get(i);
this.coeffs[ALPHA][i] = MathUtils.toRadians(coeff.getAlpha());
this.coeffs[CL][i] = coeff.getCl();
this.coeffs[CD][i] = coeff.getCd();
this.coeffs[CM][i] = coeff.getCm();
}
}
public String getName() {
return name;
}
public long getReynolds() {
return reynolds;
}
public int getDataLength() {
return dataLength;
}
public double[] getAlpha() {
return coeffs[ALPHA];
}
public double[] getCl() {
return coeffs[CL];
}
public double[] getCd() {
return coeffs[CD];
}
public double[] getCm() {
return coeffs[CM];
}
private void readData(String line, Collection<Coeff> coeffs) {
Matcher m = PATTERN.matcher(line);
if (m.find()) {
double alpha = Double.parseDouble(m.group(1));
double cl = Double.parseDouble(m.group(2));
double cd = Double.parseDouble(m.group(3));
double cm = Double.parseDouble(m.group(4));
coeffs.add(new Coeff(alpha, cl, cd, cm));
}
}
private class Coeff {
private final double alpha;
private final double cl;
private final double cd;
private final double cm;
Coeff(double alpha, double cl, double cd, double cm) {
this.alpha = alpha;
this.cl = cl;
this.cd = cd;
this.cm = cm;
}
double getAlpha() {
return alpha;
}
double getCl() {
return cl;
}
double getCd() {
return cd;
}
double getCm() {
return cm;
}
}
}