-1

I need help with a script that involves manipulating .dat files, that contains airfoil points. The airfoil data is received in column forms containing the x,y coordinates of the point in normalized form, with a 1m chord. I would like to make a script that returns the airfoil points in a .dat file in which I:

  • Multiply each point by the desired chord value (Since the points are normalized to 1m, each point must be multiplied by a chosen string value in order to scale the airfoil);

  • Rotate these points to a given angle of attack, described in degrees. In the script below is my idea of how to describe this rotation for the application I want to realize;

  • Move the points so that the origin (0,0) becomes the center of pressure of the airfoil (so for each point x,y, a math addition will be made to move the origin to the center of pressure).

The airfoil .dat file, chord value, angle of attack, and translation are inputs to be put into the code. My question is how to interpret the .dat file and generate another .dat file as output with the data modified by the math operations listed above.

Attached is a .dat file of the airfoil and the math operations to implement in the script

airfoil NACA2414.dat from AirfoilTools

def scale(self):    
    xs = []
    ys = []
    for point in self.data:
        x, y = point
        xs.append(float(x)*self.chord)
        ys.append(float(y)*self.chord)
    
    self.data = tuple(zip(xs, ys))
    return self.data 


def rotate(self):  
    ox, oy = (0, 0)
    
    xs = []
    ys = []
    for point in self.data:
        x, y = point
        xs.append(ox + math.cos(math.radians(-self.alpha)) * (float(x) - ox) - math.sin(math.radians(-self.alpha)) * (float(y) - oy))
        ys.append(oy + math.sin(math.radians(-self.alpha)) * (float(x) - ox) + math.cos(math.radians(-self.alpha)) * (float(y) - oy))
        
    self.data = tuple(zip(zs, ys))
    return self.data

def translate(self):
    xs = []
    ys = []
    for point in self.data:
        x, y = point
        xs.append(float(x) + self.x_trans)
        ys.append(float(y) + self.y_trans)
        
    self.data = tuple(zip(xs, ys))
    return self.data
awgomes
  • 25
  • 4
  • 2
    What exactly is your question? – mkrieger1 Aug 03 '22 at 14:38
  • Hi mkrieger1, my question is how do I interpret the .dat file and generate another .dat file as output with the changed data? I don't have much experience interpreting .dat files in python. – awgomes Aug 03 '22 at 16:14
  • Check this answer if it is suitable for you: https://stackoverflow.com/a/50659478/18667225 ! If not, please add some more information of your actual problem to your question above! See [ask]. – Markus Aug 03 '22 at 18:00
  • 1
    You really need to use `numpy`. After reading the data into a numpy array, most of these operations become one-liners, because you can treat the whole array as a unit. – Tim Roberts Aug 03 '22 at 19:59
  • 1. The fact that this file represents an airfoil is irrelevant to your question. 2. Please include a snippet of the file in your question, so that it doesn't become useless if/when Prof. Selig changes his site. Please read [ask] and the [question checklist](//meta.stackoverflow.com/q/260648/843953) – Pranav Hosangadi Aug 03 '22 at 20:04
  • Does this answer your question? [How to read space-separated data into a numpy array?](https://stackoverflow.com/questions/64353814/how-to-read-space-separated-data-into-a-numpy-array) Since you also want to ignore the first line in that file, read the [documentation of `np.loadtxt`](https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html) to see if there's an argument that will allow you to do this. – Pranav Hosangadi Aug 03 '22 at 20:05

1 Answers1

0

Here are some examples of how numpy can help you.

import numpy as np
import math

data = np.loadtxt('x.dat', skiprows=1)
print(data)

# Rotation.  There are other packages that have rotation like this builtin.
theta = np.deg2rad(45)
rotate= np.array([
        [math.cos(theta), -math.sin(theta)],
        [math.sin(theta), math.cos(theta)]
    ])
ndata = []
for row in data:
    ndata.append( np.dot( rotate, row ) )
data = np.array(ndata)
print(data)

# Scaling and transform.
data = data * 5 - 100
print(data)
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30