Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Project 1: Stellar Populations and Object-Oriented Design

San Diego State University

Course: ASTR 596: Modeling the Universe
Instructor: Dr. Anna Rosen
Duration: 1.5 weeks
Due: Monday, September 8, 2025

Learning Objectives: Object-oriented programming, vectorization, modular code design, matplotlib mastery

AI Policy: Phase 1 - Struggle first, AI only after 30 minutes of independent work

Textbook Reference: This assignment covers the online course Scientific Computing with Python “textbook”, The Python Fundamentals Module, Chapters 1-6

Note: This project does not require extensions. Instead, you’re expected to become proficient with matplotlib - study the documentation, explore examples, and create publication-quality visualizations. Your plotting module is an investment for the entire course!

Note on Implementation Order: Parts are labeled A-F for reference, but complete them in whatever order makes sense to you. Consider starting with constants.py, then validating zams.py before building the rest.

Background

Stars on the Zero-Age Main Sequence (ZAMS) represent stars at the moment they begin stable hydrogen fusion in their cores - essentially their “birth” as true stars. The ZAMS is a theoretical starting point where stars have just achieved hydrostatic equilibrium between gravity and radiation pressure, with a homogeneous composition throughout.

Important: Stars don’t stay at their ZAMS properties! They evolve continuously on the main sequence, becoming gradually larger, hotter, and more luminous as they convert hydrogen to helium in their cores. The Sun, for example, was about 30% less luminous when it formed 4.6 billion years ago (L☉,ZAMS ≈ 0.7 L☉) compared to today. This is why your ZAMS calculations won’t match present-day solar values - that’s expected and correct!

How the Tout formulae were derived: The authors ran detailed stellar evolution models for hundreds of combinations of mass and metallicity, then used nonlinear least-squares regression to fit polynomial expressions to the model outputs. The coefficients (α, β, γ, etc.) are the fitted parameters that minimize the squared differences between the formula and the models. We’ll explore fitting methods like this in detail in Project 4.

Quick Start: Validation First!

Before diving into implementation, validate your setup:

  1. Download Tout et al. (1996): https://articles.adsabs.harvard.edu/pdf/1996MNRAS.281..257T

  2. Create constants.py with CGS units

  3. Implement the luminosity function in zams.py

  4. Test immediately: import zams; print(zams.luminosity(1.0))

  5. Should get L ≈ 0.698 L☉ for 1 M☉

Starting with validation ensures you’re on the right track before building everything else.

TODOs Summary

ModuleClass?Required Implementation
constants.pyDefine physical constants in CGS units
zams.py• Validate Z range (0.0001-0.03)
• Check Z=0.02 if solar_Z_only
• Complete coefficients dictionary
• Implement Eq. 1 for luminosity
• Implement Eq. 2 for radius
star.py• Mass validation
• Calculate properties
• Methods for T_eff, t_MS, t_KH, λ_peak
• f-string representation with units
stellar_population.py• Handle numpy arrays only
• Vectorized calculations
• Performance comparison methods
astro_plot.py• Style setup
• HR diagram with inverted T-axis
• Multi-panel support
• Return figure AND axis objects

Part A: Constants Module

Create constants.py with physical constants in CGS units. No starter code provided - this is straightforward. Include at minimum: LSUN, RSUN, MSUN, TSUN, G, SIGMA_SB, WIEN_B, CSOL, YEAR, GYR, MYR.

Part B: ZAMS Functions Module

Question to consider: Why use a module with functions instead of a class for ZAMS calculations?

Create zams.py implementing Tout et al. (1996) relations. Starter code provided separately.

Key points:

Part C: Star Class

Create star.py for individual star objects. Starter code provided separately.

This is the ONLY project with starter code. Study it carefully for future projects!

Key requirements:

Part D: StellarPopulation Class

Create stellar_population.py to handle collections of stars.

Design requirement: Your __init__ must work two ways:

  1. Pass in a numpy array of masses directly: StellarPopulation(masses=my_array)

  2. Pass N and sampling method: StellarPopulation(n_stars=1000, sampling='logspace')

Required sampling methods (look at NumPy Docs):

Important: This class works with numpy arrays ONLY, not lists. Vectorization requires arrays!

Part E: Performance Testing

Write timing tests in your analysis script or notebook to compare:

  1. Building lists with append vs pre-allocated arrays

  2. Looping vs vectorized calculations

  3. Time and report speedup factors

Do NOT put timing code in the StellarPopulation class itself - test it from outside!

Part F: Plotting Module

Create astro_plot.py with reusable plotting functions. Starter code provided separately.

Essential Documentation:

Browse MANY examples to learn different techniques!

Design notes:

Required Plots

Plot TypePurposeKey Requirements
HR DiagramShow main sequenceInverted T-axis, log scales, color by mass
Mass-LuminosityValidate Tout fitsLog-log scale, overplot L ∝ M³
PerformanceShow speedupMulti-panel: times and speedup
Physical PropertiesMass dependenciesMulti-panel: timescales, Wien’s peak
Mass DistributionsCompare sampling2x2 grid of histograms

Validation Requirements

ZAMS values for 1 MM_☉:

Performance:

Analysis Requirements

Create project1_analysis.ipynb OR project1_analysis.py containing:

  1. Validation: Test solar values match Tout et al.

  2. Population Analysis: All required plots

  3. Performance Study: Timing comparisons with clear output

  4. Physical Insights: Discussion of results

If using .py, print all output clearly and include screenshots in report appendix.

Deliverables

Code Files:

Report (3-4 pages total):

Grading Rubric

Core Functionality (40%)

Code Quality & Design (30%)

Visualization & Scientific Communication (30%)

Development tips:

Stellar Astrophysics Equations Reference

EquationFormulaDescriptionNotes
Effective
Temperature
TeffT=(LL)0.25×(RR)0.5\frac{T_{\rm{eff}}}{T_☉} = \left(\frac{L}{L_☉}\right)^{0.25} × \left(\frac{R}{R_☉} \right)^{-0.5}Stefan-
Boltzmann
law
T=5777KT_☉ = 5777 K
(present-day);
use ZAMS LL and RR
Main Sequence
Lifetime
tMS=10 Gyr×MM×(LL)t_{\rm MS} = 10 \text{ Gyr} × \frac{M}{M_☉} × \left(\frac{L}{L_☉}\right)Fuel consumption
rate
Normalized to Sun’s
present-day values;
use L=LZAMSL = L_{\rm ZAMS}
Kelvin-Helmholtz
Timescale
tKH=GM2RLt_{\rm KH} = \frac{GM²}{RL}Gravitational
contraction
timescale
Convert to CGS;
G=6.674×108 cm³/g/s²G = 6.674×10^{-8}~\text{cm³/g/s²}
Wien’s Peak Wavelengthλmax=b/Tλ_{\rm max} = b/TWien’s
displacement
law
b=0.2898 cm Kb = 0.2898~\text{cm K};
convert result to nm

Variable Definitions: