#!/usr/bin/env python
# -*- coding: utf-8 -*-

# ***************************************************************************
# *   Copyright (C) 2011-2013 by Andreas Kührmann [kuean@users.sf.net] and  *
# *   Fabian Schäffer                                                       *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   the Free Software Foundation; either version 3 of the License, or     *
# *   (at your option) any later version.                                   *
# *                                                                         *
# *   This program is distributed in the hope that it will be useful,       *
# *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
# *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
# *   GNU General Public License for more details.                          *
# *                                                                         *
# *   You should have received a copy of the GNU General Public License     *
# *   along with this program; if not, write to the                         *
# *   Free Software Foundation, Inc.,                                       *
# *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
# ***************************************************************************

import numpy as np

__all__ = ['N3DCamberLine']

coefficients_map = {0.05: (0.058, 60.233333333),
0.1: (0.126, 8.606666667),
0.15: (0.2025, 2.6595),
0.2: (0.29, 1.107166667),
0.25: (0.391, 0.538333333)}

[docs]class N3DCamberLine(bcls.AnalyticalCamberLine):
"""
The NACA 3-digit camber line is formed by two parabolic segments that
match in value and slope at variable m, which is characterized by the
max_camber_position.
"""

def __init__(self):
super(N3DCamberLine, self).__init__()
# properties (initialized by user)
self.__max_camber_position = fdn.Uninit('max_camber_position')
# add user properties to initialization summary
self._properties.append('max_camber_position')

@property
def max_camber_position(self):
"""
Type: float - only 0.05, 0.1, 0.15, 0.2 and 0.25 are allowed
"""
return self.__max_camber_position

@max_camber_position.setter
@fdn.restrict(new_max_camber_position=[.05, .1, .15, .2, .25])
[docs]    def max_camber_position(self, new_max_camber_position):
if self.__max_camber_position != new_max_camber_position:
self.__max_camber_position = new_max_camber_position
self.update()

@fdn.memoize
[docs]    def get_derivations(self):
"""
get_derivations()

Returns: ndarray

Calculates camber line derivations and returns them in an array.

.. note:: **Note**

The return value will be cached. Recalling this method returns the
cached value, if the attribues are unchanged.
"""
self._check_initialization()
self._cached = True
m, k3 = coefficients_map[self.max_camber_position]
x = self.distribution(self.sample_rate)
index = np.where(x <= m)[0]
if index.size:
x1 = x[index]
dydx_1 = k3 * (x1 * (3 * x1 - 6 * m) + m ** 2 * (3 - m))
index = np.where(x > m)[0]
if index.size:
dydx_2 = -k3 * np.power(m, 3) / 6 * np.ones(x[index].shape)
return np.append(dydx_1, dydx_2)

@fdn.memoize
[docs]    def as_array(self):
"""
as_array()

Returns: ndarray

Calculates camber line coordinates and returns them in an array.

.. note:: **Note**

The return value will be cached. Recalling this method returns the
cached value, if the attribues are unchanged.
"""
self._check_initialization()
self._cached = True
m, A = coefficients_map[self.max_camber_position]
x = self.distribution(self.sample_rate)
index = np.where(x <= m)[0]
if index.size:
z = x[index]
y1 = A * z * (np.power(z, 2) + m * (m * (3 - m) - 3 * z))
index = np.where(x > m)[0]
if index.size:
y2 = A * np.power(m, 3) * (1 - x[index])
y = np.append(y1, y2)
return np.reshape(np.append(x, y), (-1, 2), "F")