Solar Simulation Source Code

This is the source code that uses the Sun, TMY weather, and Collector classes to build a very simple solar collector simulation.  Even  though it is a very simple simulation, it does  incorporate actual hour by hour weather, and accurate modeling of a solar collector based on its efficiency curve.
.

This is just to take a look at before you download -- if you want to download it, then download all the solar classes here:  Solar Tools Source

 

An slightly more complex example simulation that includes storage and report writing is included in the download package.

 


'Solar Analysis Tools -
 'Copyright (C) 2005 Gary Reysa  www.BuildItSolar.com  (gary@BuildItSolar.com)
 ' This program is free software; you can redistribute it and/or modify it
 ' under the terms of the GNU General Public License as published by the Free
 ' Software Foundation; either version 2 of the License, or (at your option)
 ' any later version.
 ' This program is distributed in the hope that it will be useful and encourage
 ' further development of solar tools, 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.
'Really Simple Simulation Example:
'This is a bare minimum simulation that still makes use of 
'hourly weather data, and models a solar collector faithfully.
' Its to show how simple a simulation that uses actual weather
' and a collector defined by an efficiency curve can be.
' Note how the collector efficiency varies greatly with the 
' weather (cloudyness).  
' The next step would be to include realistic variation of 
' the collector inlet temperature (Ti).  This will also have
' an effect on collector efficiency, as higher inlet temperatures
' reduce efficiency by increasing collector losses.
' The other (slightly more complex) example includes a storage
' tank in the simulation, and provides realistice collector inlet temperature.
Imports System.io
Imports System.Math
Module OutHouse
Const DegPerRad As Single = 57.3
' Main simulation loop
Sub Runsimulation()
    Dim SV As New VectorCls          ' vector to store current direction to sun
    Dim Idn, Idif As Single          ' solar direct and diffuse radiation
    Dim Tix As Integer
    Dim Efic, Qout, Incidence, IColTot As Single
    ' Set the values in ths Sun object that do not change during simulation
    ' Make a TMY object to fetch and store the hr by hr weather
    Dim TMY As TMYcls = New TMYcls()   ' to store hour by hour weather 
    Dim T As TMYstruc                 ' stores current hour of weather
    'read in a TMY weather data set 
    TMY.ReadTMYFile("Billings.TM2")
       ' this TMY weather file must be on your current dir -- 
       ' in Visual Studio this seems to be the bin dir for the project you are in.
       ' This mehtod opens the file, and reads the full years worth of weather into
       ' memory. 
    ' Make a Sun 
    Dim Sun As New SunCls()
        'Set the values in the Sun object that remain constant during simulation
        Sun.Latitude = 46 / DegPerRad    ' Local latitude 
        Sun.Longitude = 110 / DegPerRad  ' Local longitude
        Sun.Altitude = 0                 ' Altitude above sea level
        Sun.Year = 2001                  ' just use 2001 
    ' Make a Collector
    Dim OHCol As New CollectorEcurveCls   ' collector with efficiency curve defined
        ' Set the values in the Collector object that do not change during simulation
        OHCol.Area = 150                ' collector area
        OHCol.Azimuth = 0 / DegPerRad   ' Collector azimuth (bearing)
        OHCol.Tilt = 90 / DegPerRad     ' Collector tilt
        OHCol.Slope = -0.805            ' Slope of the efficiency curve 
        OHCol.YIntercept = 0.725        ' Y intercept of efficiency curves
    
    ' Main simulation loop:
    ' works like this:
    '     For each hour in simulation
    '         Get weather and sun intensity for this hour from TMY object
    '         Get sun direction for this hour from Sun object
    '         Get Collector heat output (if any) for this hour from Collector object
    '         Report on how collector is doing
    '     Next Hour
    ' 
    ' Write a header for the simulation display output
         Console.WriteLine("Mon   Day   Hr   Tamb SunIdn SunElev  SunAz IncidAng Icoltot     Qout   Efic")
    ' Do a simulation for a few days in November
    For Tix = TMY.TMYrecIndexForDate(11, 6, 8) To TMY.TMYrecIndexForDate(11, 11, 18)
                'WEATHER:
                ' Get the weather for the hour 
                T = TMY.TMYrec(Tix)
                ' Get the sun intensities from the weather data for this hour
                Idn = T.Idn
                ' Estimate the diffuse on collector from the horizontal diffuse reported in TMY
                Idif = T.Ihorzdif * ((1 + Cos(OHCol.Tilt))) / 2
                'SUN: 
                ' Update the sun to the current hour
                ' This allows the Sun object to figure out where the Sun is in the
                ' sky (information the collector needs to know)
                Sun.DayOfMonth = T.Day
                Sun.Month = T.Month
                Sun.ASThr = T.Hour
                ' Get the direction to the sun
                SV = Sun.ToSunVector()
                ' COLLECTOR:
                ' Update the Collector object to this hour
                OHCol.SunVector = SV      ' tell it sun direction
                OHCol.Idn = Idn           ' tell it sun intensity -- direct and diffuse
                OHCol.Idif = Idif
                OHCol.Ta = T.Tdb          ' tell it outside temperature
                OHCol.Ti = 100            ' tell it what its inlet temperature will be
                'Get some collector results
                IColTot = OHCol.Icoltot
                Qout = OHCol.QOut
                Efic = OHCol.Efic
                Incidence = OHCol.IncidAngle
                'Display some results
                Console.WriteLine("{0,4} {1,4} {2,4} {3,5:F0}  {4,5:F0} {5,5:F0}    {6,6:F0}  {7,6:F0}   {8,6:F0} {9,8:F0} {10,6:F2}", _
                      T.Month, T.Day, T.Hour, _
                      T.Tdb, T.Idn, Sun.AltAng * DegPerRad, Sun.AzimuthAng * DegPerRad, Incidence * DegPerRad, IColTot, Qout, Efic)
    Next
End Sub 'RunSimulation
    Sub Main()
        Console.BufferWidth = 120
        Console.BufferHeight = 300
        Runsimulation()
        Console.WriteLine("Done")
        Console.WriteLine("Hit <enter> to continue")
        Console.ReadLine()
    End Sub
End Module