WarpTwin
Documentation for WarpTwin models and classes.
Loading...
Searching...
No Matches
TabularAtmosphereModel.h
/******************************************************************************
* Copyright (c) ATTX INC 2025. All Rights Reserved.
*
* This software and associated documentation (the "Software") are the 
* proprietary and confidential information of ATTX, INC. The Software is 
* furnished under a license agreement between ATTX and the user organization 
* and may be used or copied only in accordance with the terms of the agreement.
* Refer to 'license/attx_license.adoc' for standard license terms.
*
* EXPORT CONTROL NOTICE: THIS SOFTWARE MAY INCLUDE CONTENT CONTROLLED UNDER THE
* INTERNATIONAL TRAFFIC IN ARMS REGULATIONS (ITAR) OR THE EXPORT ADMINISTRATION 
* REGULATIONS (EAR99). No part of the Software may be used, reproduced, or 
* transmitted in any form or by any means, for any purpose, without the express 
* written permission of ATTX, INC.
******************************************************************************/
/*
Tabular atmosphere model header file

Author: Alex Reynolds
*/
/*
Metadata for MS GUI:
imdata = {"exclude" : True}
*/

#ifndef MODELS_ENVIRONMENT_TABULAR_ATMOSPHERE_MODEL_H
#define MODELS_ENVIRONMENT_TABULAR_ATMOSPHERE_MODEL_H

#include "simulation/Model.h"
#include "utils/Interpolate2D.h"
#include "locations.h"

namespace warptwin {

    /**
     * @brief   Tabular atmosphere model
     * 
     * This model reads and returns atmospheric parameters pressure, temperature, 
     * and density on the basis of altitude in meters above ground level. 
     * 
     * The model reads from files in the format defined in data/atmosphere, which
     * is a txt format input.
     * 
     * This is a highly simplified implementation -- for ultra high fidelity 
     * propagation consider using a different model.
     * 
     * For values above the tabulated range, the model returns the values at the
     * end of the table for temperature and zero for pressure and density. For 
     * values below the end of the table the model returns the lowest altitude
     * value in the table.
     * 
     * Speed of sound calculation based on Introduction to Flight by Anderson, 
     * p. 177 eq 4.54
     * 
     * Text file must be in the format 
     * H(km) T(K) p(Pa) ρ(kg/m^3)
     * 
     * Author: Alex Reynolds <alex.reynolds@attxengineering.com>
    */
    MODEL(TabularAtmosphereModel)
    public:
        // TODO: Replace need for mass with direct acceleration calculation when 
        // body accel methods are implemented
        // Model params
        //         NAME                     TYPE                    DEFAULT VALUE
        START_PARAMS
            /** The atmosphere data file which should be loaded. Default is 1976 std atmosphere. */
            SIGNAL(atmos_file,              std::string,            warptwinDir()+"data"+slash()+"atmosphere"+slash()+"1976stdatmos.txt")
            /** Our ideal gas constant for speed of sound calculation. Default is for air */
            SIGNAL(R_idealgas_kJ_kg_K,      double,                 286.0)
            /** The specific heat ratio for air -- used in calculation of the speed of sound */
            SIGNAL(gamma,                   double,                 1.4)
        END_PARAMS

        // Model inputs
        //         NAME                     TYPE                    DEFAULT VALUE
        START_INPUTS
            /** The reference altitude in meters above sea level */
            SIGNAL(altitude_m,              double,                 0.0)
        END_INPUTS

        // Model outputs
        //         NAME                     TYPE                    DEFAULT VALUE
        START_OUTPUTS
            /** The temperature of the atmosphere in degrees kelvin */
            SIGNAL(temperature_K,           double,                 0.0)
            /** The pressure of the atmosphere in Pascals (N/m^2) */
            SIGNAL(pressure_Pa,             double,                 0.0)
            /** The density of the atmosphere in kg/m^3 */
            SIGNAL(density_kg_m3,           double,                 0.0)
            /** The speed of sound in meters per second */
            SIGNAL(speed_of_sound,          double,                 0.0)
        END_OUTPUTS

    protected:
        int16 start() override;
        int16 execute() override; 

        // Internal variables for atmosphere calculations.
        double _alt_km;
        double _min_altitude_km;
        double _max_altitude_km;
        std::vector<std::vector<double>> _atmos_table;
        Interpolate2D _interp_pressure;
        Interpolate2D _interp_temperature;
        Interpolate2D _interp_density;

        // Variables to prevent repeated warning calls when above/below the table
        bool _min_warned = false;
        bool _max_warned = false;
    };
}

#endif