@@ -45,7 +45,7 @@ def define_arguments(argparser):
4545
4646def write_table (instance , * indexes , ** kwargs ):
4747 # there must be a way to accept specific named keyword arguments and
48- # also an open-ended list of positional arguments (*indexes), but I
48+ # also an open-ended list of positional arguments (*indexes), but I
4949 # don't know what that is.
5050 output_file = kwargs ["output_file" ]
5151 headings = kwargs ["headings" ]
@@ -78,7 +78,6 @@ def format_row(row):
7878 w .writerows (
7979 # TODO: flatten x (unpack tuples) like Pyomo before calling values()
8080 # That may cause problems elsewhere though...
81-
8281 format_row (row = values (instance , * x ))
8382 for x in itertools .product (* indexes )
8483 )
@@ -109,6 +108,7 @@ def post_solve(instance, outdir):
109108 """
110109 save_generic_results (instance , outdir , instance .options .sorted_output )
111110 save_total_cost_value (instance , outdir )
111+ save_cost_components (instance , outdir )
112112 save_results (instance , outdir )
113113
114114
@@ -140,6 +140,36 @@ def save_total_cost_value(instance, outdir):
140140 fh .write ('{}\n ' .format (value (instance .SystemCost )))
141141
142142
143+ def save_cost_components (m , outdir ):
144+ """
145+ Save values for all individual components of total system cost on NPV basis.
146+ """
147+ cost_dict = dict ()
148+ for annual_cost in m .Cost_Components_Per_Period :
149+ cost = getattr (m , annual_cost )
150+ # note: storing value() instead of the expression may save
151+ # some memory while this function runs
152+ cost_dict [annual_cost ] = value (sum (
153+ cost [p ] * m .bring_annual_costs_to_base_year [p ]
154+ for p in m .PERIODS
155+ ))
156+ for tp_cost in m .Cost_Components_Per_TP :
157+ cost = getattr (m , tp_cost )
158+ cost_dict [tp_cost ] = value (sum (
159+ cost [t ] * m .tp_weight_in_year [t ]
160+ * m .bring_annual_costs_to_base_year [m .tp_period [t ]]
161+ for t in m .TIMEPOINTS
162+ ))
163+ write_table (
164+ m ,
165+ cost_dict .keys (),
166+ output_file = os .path .join (outdir , "cost_components.tab" ),
167+ headings = ('component' , 'npv_cost' ),
168+ values = lambda m , c : (c , cost_dict [c ]),
169+ digits = 16
170+ )
171+
172+
143173def save_results (instance , outdir ):
144174 """
145175 Save model solution for later reuse.
0 commit comments