You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
3.0 KiB

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
library(shiny)
library(photoec)
library(common) # using: roundup()
library(dplyr)
library(ggplot2)
TemperatureSlideLimits <- c(1000, 9000)
TemperaturePreset <- 5778
wavelength <-
# all values below in meters
seq(from = 1E-9, # 1 nm
to = 5E-6, # 5000 nm
by = 1E-9)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Black body radiation"),
withMathJax(),
markdown("
Spectral radiance of a black body as defined by Planck's law:
$$I\\left(\\lambda, T\\right) = \\frac{2{\\pi}hc^2}{\\lambda^5}\\frac{1}{\\exp\\left(\\displaystyle\\frac{hc}{{\\lambda}kT}\\right) - 1}$$
where \\(h\\) is the Planck constant, \\(c\\) is the speed of light, and
\\(k\\) is the Boltzmann constant.
**Explore the behaviour of the spectral radiance by manipulating
the black body temperature using the slider below!**
"),
# Sidebar with a slider input for black body temperature
sidebarLayout(
sidebarPanel(
sliderInput("TemperatureVar",
"Black body temperature, T/K:",
min = min(TemperatureSlideLimits),
max = max(TemperatureSlideLimits),
value = TemperaturePreset)
),
# Show a plot of the spectral irradiance curve
mainPanel(
plotOutput("PlanckPlot")
)
),
markdown("
The wavelength of the irradiance maximum is labelled inside the plot.
[Source code repository and issue tracker](https://codeberg.org/solarchemist/black-body-by-temperature),
please use it to report bugs or suggestions!
")
)
# Define server logic required to draw the Planck distribution
server <- function(input, output) {
output$PlanckPlot <- renderPlot({
blackbody <-
cbind(
photoec::sunlight.Planck(wavelength = wavelength, temperature = input$TemperatureVar),
Temperature = input$TemperatureVar)
# find the wavelength of the maximum irradiance at this temperature
blackbody$WavelengthOfMaxIrradiance <-
blackbody$wavelength[which.max(blackbody$Sun.spectralradiance)]
p <- ggplot(blackbody) +
geom_path(aes(wavelength, Sun.spectralradiance)) +
geom_text(
vjust = 0, hjust = 0.5,
aes(
label = paste(1E9 * unique(WavelengthOfMaxIrradiance), "nm"),
y = max(Sun.spectralradiance),
x = unique(WavelengthOfMaxIrradiance))) +
# scale_x_log10() +
# scale_y_log10() +
# scale_y_sqrt() +
# coord_cartesian(ylim = c(0, 8E14),
# xlim = c(0, 5E-6)) +
scale_x_continuous(labels = c(0, 1000, 2000, 3000, 4000, 5000)) +
labs(
x = "Wavelength/nm",
y = "Spectral irradiance/(W/m2/m)")
print(p)
})
}
# Run the application
shinyApp(ui = ui, server = server)