Misc_Weights.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# Applied Python PRISM
# (PRISM) PaRametrIc System Model
#
# Written by Charlie Taylor <cet@appliedpython.com> 
# Oct,21 2005
from prism.Summary import Summary
from prism.MassItem import MassItem

def compareItemNames(tup1, tup2):
    return cmp(tup1[0].upper(), tup2[0].upper())
        
def compareItemMass(tup1, tup2):
    return cmp(tup2[1], tup1[1])

class Misc_Weights( MassItem ):
    '''Mass Item in PRISM'''
    
    def __init__(self, name="Misc. Mass Items", type="inert", mass_lbm=0.0):
        
        MassItem.__init__(self, name, type=type)
        
        self.name = name
        self.type = type # type can be "fluid", "inert", "propellant", etc.
        self.itemsList = []
        self.mass_lbm = mass_lbm # if add_item never called, this mass remains
        
    def reCalc(self):
        '''Override this routine for non-constant mass items'''
        pass
        
    def add_item(self, name="misc item", mass_lbm=0.0 ):
        self.itemsList.append( (name, mass_lbm) )
        self.mass_lbm = 0.0
        self.itemsList.sort( compareItemNames )

        for n,m in self.itemsList:
            self.mass_lbm += m
        
    def getMassStr(self):
        return '%12.3f lbm %s'%(self.mass_lbm,self.name)
        
    def buildSummary(self):
        summ = Summary(  summName='Miscellaneous Mass Items',
          componentName=self.name, mass_lbm=self.mass_lbm, type=self.type)
          
        summ.addAssumption( 'All items are of type: ' + self.type )
        
        if len(self.itemsList)>1:
            summ.addInput(' ===SORT ORDER===','Alphabetical', '', '%s')
        for n,m in self.itemsList:        
            # add inputs
            summ.addInput(n, m, 'lbm', '%7.3f')

        dummyList = self.itemsList[:]
        dummyList.sort(compareItemMass)
        if len(self.itemsList)>1:
            summ.addOutput(' ===SORT ORDER===','Mass', '', '%s')
        for n,m in dummyList:        
            # add inputs
            summ.addOutput(n, m, 'lbm', '%7.3f')
        return summ

    
    def getItemTextSummary(self):
        summ = self.buildSummary()
        return summ.getItemTextSummary()
        
        
    def getSummary(self):
        return self.getItemTextSummary()
            
    def getItemHTMLSummary(self):
        summ = self.buildSummary()
        return summ.getItemHTMLSummary()
        

if __name__ == "__main__":  #self test

    h = Misc_Weights(name="Test Articles", mass_lbm=1.12345)
    print h.getMassStr()
    h.add_item("item #1", 1.111)
    h.add_item("item #6", 6.666)
    h.add_item("item #2", 2.222)
    h.add_item("item #4", 4.444)
    h.add_item("item #5", 5.555)
    h.add_item("item #3", 3.333)
    print
    print h.getSummary()
    print
    #print h.getItemHTMLSummary()