#
# ISC License
#
# Copyright (c) 2023, Autonomous Vehicle Systems Lab, University of Colorado at Boulder
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
#
# Unit Test Script
# Module Name: sensorThermal
# Author: Adam Herrmann
# Creation Date: January 12th, 2023
#
import pytest
import os, inspect
import numpy as np
filename = inspect.getframeinfo(inspect.currentframe()).filename
path = os.path.dirname(os.path.abspath(filename))
bskName = 'Basilisk'
splitPath = path.split(bskName)
# Import all of the modules that we are going to be called in this simulation
from Basilisk.utilities import SimulationBaseClass
from Basilisk.simulation import sensorThermal # import the module that is to be tested
from Basilisk.architecture import messaging # import the message definitions
from Basilisk.utilities import macros
from Basilisk.architecture import astroConstants
# Uncomment this line is this test is to be skipped in the global unit test run, adjust message as needed.
# @pytest.mark.skipif(conditionstring)
# Uncomment this line if this test has an expected failure, adjust message as needed.
# @pytest.mark.xfail(conditionstring)
# Provide a unique test method name, starting with 'test_'.
# The following 'parametrize' function decorator provides the parameters and expected results for each
# of the multiple test runs for this test. Note that the order in that you add the parametrize method
# matters for the documentation in that it impacts the order in which the test arguments are shown.
# The first parametrize arguments are shown last in the pytest argument list
# Accuracy parametrization for pytest-html generation
[docs]
@pytest.mark.parametrize("accuracy", [1e-6])
def test_sensorThermal(show_plots, accuracy):
r"""
**Validation Test Description**
This test checks the change in temperature from electrical heating and thermal radiation for a sensor with an
insulated back. The sensor normal is perpendicular to the incoming sunlight, so there is no solar absorption.
The test uses three starting temperatures:
- Zero: The starting temperature is zero degrees Celsius.
- Hot: The starting temperature is approximately 98 degrees Celsius.
- Cold: The starting temperature is approximately -75 degrees Celsius.
The thermal sensor is set to match the parameters in `scenarioSensorThermal.py`, which assumes a 2 kg aluminum
sensor with the emissivity and absorptivity coefficients of Kapton tape.
**Test Parameters**
Args:
accuracy (float): absolute accuracy value used in the validation tests
**Description of Variables Being Tested**
In this file we are checking the values of the variables
- ``sensorTemp``
which represents the temperature of the sensor. This array is compared to the analytical ``trueTemp`` array.
"""
# Create simulation variable names
unitTaskName = "unitTask" # arbitrary name (don't change)
unitProcessName = "TestProcess" # arbitrary name (don't change)
# Create a sim module as an empty container
unitTestSim = SimulationBaseClass.SimBaseClass()
# set the simulation time variable used later on
simulationTime = macros.sec2nano(1.)
#
# create the simulation process
#
testProcessRate = macros.sec2nano(1.0) # update process rate update time
testProc = unitTestSim.CreateNewProcess(unitProcessName)
testProc.addTask(unitTestSim.CreateNewTask(unitTaskName, testProcessRate))
#
# setup the simulation tasks/objects
#
# set device status message
sensorStatusMsgPayload = messaging.DeviceStatusMsgPayload()
sensorStatusMsgPayload.deviceStatus = 1
sensorStatusMsg = messaging.DeviceStatusMsg().write(sensorStatusMsgPayload)
# set the spacecraft message
scStateMsgPayload = messaging.SCStatesMsgPayload()
scStateMsgPayload.r_BN_N = [6378*1000., 0., 0.]
scStateMsgPayload.sigma_BN = [0., 0., 0.]
scStateMsg = messaging.SCStatesMsg().write(scStateMsgPayload)
# set the sun message
sunMsgPayload = messaging.SpicePlanetStateMsgPayload()
sunMsgPayload.PositionVector = [astroConstants.AU*1000., 0., 0.]
sunMsg = messaging.SpicePlanetStateMsg().write(sunMsgPayload)
#
# Setup the temperature modeling
#
sensorThermalModel = sensorThermal.SensorThermal()
sensorThermalModel.ModelTag = 'sensorThermalModel'
sensorThermalModel.nHat_B = [0, 0, 1]
sensorThermalModel.sensorArea = 1.0 # m^2
sensorThermalModel.sensorAbsorptivity = 0.25
sensorThermalModel.sensorEmissivity = 0.34
sensorThermalModel.sensorMass = 2.0 # kg
sensorThermalModel.sensorSpecificHeat = 890
sensorThermalModel.sensorPowerDraw = 30.0 # W
initialTemperatures = np.array([0.0, 97.961, -74.8505]) # [deg C]
sensorThermalModel.T_0 = initialTemperatures[0]
sensorThermalModel.sunInMsg.subscribeTo(sunMsg)
sensorThermalModel.stateInMsg.subscribeTo(scStateMsg)
sensorThermalModel.sensorStatusInMsg.subscribeTo(sensorStatusMsg)
unitTestSim.AddModelToTask(unitTaskName, sensorThermalModel)
#
# log data
#
# log the RW temperature
tempLog = sensorThermalModel.temperatureOutMsg.recorder()
unitTestSim.AddModelToTask(unitTaskName, tempLog)
#
# initialize Simulation
#
unitTestSim.InitializeSimulation()
# run the first test (nominal)
unitTestSim.ConfigureStopTime(simulationTime)
unitTestSim.ExecuteSimulation()
# Reset to the hot initial temperature.
sensorThermalModel.T_0 = initialTemperatures[1]
sensorThermalModel.Reset(simulationTime)
# run the second test (hot case)
unitTestSim.ConfigureStopTime(2*simulationTime)
unitTestSim.ExecuteSimulation()
# change the test conditions
sensorThermalModel.T_0 = initialTemperatures[2]
sensorThermalModel.Reset(2*simulationTime)
# Run the cold case.
unitTestSim.ConfigureStopTime(3*simulationTime)
unitTestSim.ExecuteSimulation()
#
# retrieve the logged data
#
sensorTemp = np.array(tempLog.temperature)
#
# set the truth vectors
#
# Calculate one Euler step for each initial temperature, using the 2022 CODATA value.
stefanBoltzmann = 5.670374419e-8 # [W/(m^2 K^4)]
temperaturesKelvin = initialTemperatures + 273.15 # [K]
radiatedPower = (
sensorThermalModel.sensorArea * sensorThermalModel.sensorEmissivity * stefanBoltzmann * temperaturesKelvin**4
)
heatCapacity = sensorThermalModel.sensorMass * sensorThermalModel.sensorSpecificHeat
stepSeconds = simulationTime * macros.NANO2SEC
temperatureChange = (sensorThermalModel.sensorPowerDraw - radiatedPower) * stepSeconds / heatCapacity
trueTemp = np.concatenate(([initialTemperatures[0]], initialTemperatures + temperatureChange))
assert sensorTemp.shape == trueTemp.shape
np.testing.assert_allclose(sensorTemp, trueTemp, rtol=0.0, atol=accuracy)
#
# Run this unitTest as a stand-along python script
#
if __name__ == "__main__":
test_sensorThermal(
False, # show_plots
1e-6 # accuracy
)