#!/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__ = ['N3DRCamberLine']

coefficients_map = {0.1: (0.13, 8.665, 0.000763993),
0.15: (0.217, 2.632166667, 0.006768822),
0.2: (0.318, 1.086666667, 0.030306748),
0.25: (0.441, 0.531833333, 0.135506111)}

[docs]class N3DRCamberLine(bcls.AnalyticalCamberLine):
"""
The NACA 3-digit-reflexed camber line was designed to have a theoretical
zero pitching moment and is formed by two parabolic segments that match
in value and slope at the variable :math:p.
"""

def __init__(self):
super(N3DRCamberLine, 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.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=[.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, A, B = coefficients_map[self.max_camber_position]
x = self.distribution(self.sample_rate)
index = np.where(x <= m)[0]
if index.size:
z = x[index]
dydx_1 = A * (3 * (z - m) ** 2 - B * (1 - m) ** 3 - m ** 3)
index = np.where(x > m)[0]
if index.size:
z = x[index]
dydx_2 = A * (B * 3 * (z - m) ** 2 - B * (1 - m) ** 3 - m ** 3)
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, B = 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 - m) ** 3 - B * (1 - m) ** 3 * z + (1 - z) * m ** 3)
index = np.where(x > m)[0]
if index.size:
z = x[index]
y2 = A * (B * ((z - m) ** 3 - (1 - m) ** 3 * z) + (1 - z) * m ** 3)
y = np.append(y1, y2)
return np.reshape(np.append(x, y), (-1, 2), "F")