heatsink module

This is a file, named "heatsink.py". It can be included in other files as a module, providing the Heatsink class.

# -*- coding: utf-8 -*-
"""
Created on Thu Apr 15 09:35:33 2021

This file creates a MODULE containing the Heatsink class.
For inclusion by other programs.

@author: rmontant
"""
import numpy as np

class Heatsink:
    '''
    This class represents a "heatsink" as a cube with gaps
    carved out of it, resulting in a finned structure.
    '''
    # Set each object's individual component values,
    # including the basic cubical array of points:
    def __init__(self, nfins=4, finwidth=4, gapwidth=6, basedepth=5):

        # Adjustable parameters of the finned heatsink:
        self.nfins = nfins
        self.finwidth = finwidth
        self.gapwidth = gapwidth
        self.basedepth = basedepth
        self.ngaps = self.nfins - 1

        # The cube's dimensions:
        self.width = nfins * finwidth + self.ngaps * gapwidth
        self.length = self.width
        self.depth = int( 1.2 * self.width ) # 1.2 seems like a good number

        # Build a basic cube, initialized with values proportional to the depth:
        self.cube = np.empty( (self.width, self.length, self.depth) )
        for z in range(self.depth):
            self.cube[ : , : , z] = z / self.depth

        # Carve out the fins, and build an array of the heatsink's points:
        self.carve_gaps()
        self.build_pointarray()
    #--------

    # Create the gaps between fins in the heatsink-cube:
    def carve_gaps(self):
        for g in range(self.ngaps):
            #
            # In class:  "gstart" is incorrectly defined,
            # so all gaps are adjacent.  The result looks wrong.
            # Here is the correct definition of "gstart", giving the
            # proper distribution of gaps (and therefore of fins).
            #
            #gstart = self.finwidth + g * self.gapwidth
            gstart = self.finwidth + (g * (self.gapwidth + self.finwidth))

            gend = gstart + self.gapwidth
            gap = np.full( \
                (self.gapwidth, self.length, self.depth-self.basedepth),
                -np.inf
            )
            self.cube[ gstart:gend, : , self.basedepth: ] = gap
    #--------

    # Build an array of those points from the cube that are
    # part of the finned heatsink shape:
    def build_pointarray(self):
        coords = []
        for x in range(self.width):
            for y in range(self.length):
                for z in range(self.depth):
                    if self.cube[x,y,z] > -np.inf:
                        coords.append( (x,y,z, self.cube[x,y,z]) )
        self.coords = np.array( coords )
    #--------

#--------