def create_fragment(self, parent=None, flow=None, direction=None, comment=None, value=None, balance=False, **kwargs): """ :param parent: :param flow: :param direction: :param comment: :param value: :param balance: :param kwargs: :return: """ if flow is None: ch = cyoa('(N)ew flow or (S)earch for flow? ', 'ns') if ch == 'n': flow = self.new_flow() elif ch == 's': index = self.current_archive or select_archive(self) elem = {'i': False, 'e': True, 'a': None}[cyoa('(I)ntermediate, (E)lementary, or (A)ll flows? ', 'aei', 'I').lower()] flow = self.find_flow(None, index=index, elementary=elem) if flow is None: return None flow = flow.entity() else: raise ValueError print('Creating fragment with flow %s' % flow) direction = direction or {'i': 'Input', 'o': 'Output'}[cyoa('flow is (I)nput or (O)utput?', 'IO').lower()] comment = comment or ifinput('Enter FragmentFlow comment: ', '') if parent is None: # direction reversed for UX! user inputs direction w.r.t. fragment, not w.r.t. parent if value is None: value = 1.0 frag = self.new_fragment(flow, comp_dir(direction), Comment=comment, exchange_value=value, **kwargs) else: if parent.term.is_null: self.terminate_to_foreground(parent) if value is None: val = ifinput('Exchange value (%s per %s): ' % (flow.unit(), parent.unit), '1.0') if val == '1.0': value = 1.0 else: value = parse_math(val) frag = self[0].add_child_fragment_flow(parent, flow, direction, Comment=comment, exchange_value=value, **kwargs) if balance: frag.set_balance_flow() self.traverse(parent) if self.db.is_elementary(frag.flow): self.terminate_to_foreground(frag) return frag
def edit_flow(self): flow = pick_one(self._catalog[0].flows()) ch = cyoa('Edit (P)roperties or (C)haracterizations? ', 'pc').lower() if ch == 'p': self._edit_entity(flow) elif ch == 'c': self.edit_characterizations(flow)
def new_flow(self, flow=None, name=None, cas=None, quantity=None, comment=None, compartment=None): if flow is None: name = name or input('Enter flow name: ') cas = cas or ifinput('Enter CAS number (or none): ', '') print('Choose reference quantity or none to create new: ') if quantity is None: q = pick_one(self.flow_properties) if q is None: q = self.new_quantity() quantity = q comment = comment or input('Enter flow comment: ') if compartment is None: print('Choose compartment:') compartment = pick_compartment(self.db.compartments).to_list() flow = LcFlow.new(name, quantity, CasNumber=cas, Compartment=compartment, Comment=comment) # flow.add_characterization(q, reference=True) else: quantity = flow.reference_entity self._catalog[0].add(flow) flow.profile() while ifinput('Add characterizations for this flow? y/n', 'n') != 'n': ch = cyoa('[n]ew or [e]xisting quantity? ', 'en', 'e') if ch == 'n': cq = self.new_quantity() else: cq = pick_one(self[0].quantities()) if cq is None: cq = self.new_quantity() val = parse_math(input('Value (1 %s = x %s): ' % (quantity.unit(), cq.unit()))) flow.add_characterization(cq, value=val) return flow
def _continue_search(self, result_set): """ :param result_set: set of catalog refs :return: """ while 1: if len(result_set) == 0: return 'No results.' if len(result_set) > 20: group(result_set) i = cyoa('\n(B) Browse results, (A) select all, (N)arrow search, or (X) abandon search / finished?', 'BANX', 'N') else: show_res(result_set) i = cyoa('\n(S) Select one, (A) select all, (N)arrow search, or (X) abandon search / finished?', 'SANX', 'S') if i.lower() == 'x': return True elif i.lower() == 'a': self.selected = self.selected.union(result_set) return True elif i.lower() == 'n': result_set = self._narrow_search(result_set) elif i.lower() == 'b': pick = pick_one(result_set) e = self._prompt_add(pick) if e in result_set: result_set.remove(e) elif i.lower() == 'b' or i.lower() == 's': # 'b' ad 's' are the same- browse and pick pick = pick_one(result_set) if pick is not None: self.selected.add(pick) result_set.remove(pick) else: try: if int(i) < len(result_set): pick = result_set[int(i)] self.selected.add(pick) result_set.remove(pick) except ValueError: pass
def background_scenario(self, scenario, index=None): if index is None: index = self.current_archive or select_archive(self) for bg in self[0].fragments(background=True): print('\nfragment %s' % bg) if scenario in bg.terminations(): print('Terminated to %s' % bg.termination(scenario).term_node) ch = cyoa('(K)eep current or (S)earch for termination? ', 'ks', 'k') if ch == 's': p_ref = self.terminate_by_search(bg, index=index) if p_ref is not None: bg.terminate(p_ref, scenario=scenario)
def add_child_fragment(self): parent = self._selected_fragment k = cyoa('use (N)ew or (E)xisting flow?', 'NE', 'N') if k.lower() == 'e': print('Select Reference flow:') flow = pick_one(self._catalog[0].flows()) if flow is None: print('Canceling child fragment flow') return None else: flow = self.create_flow() direction = menu_list('Input', 'Output') self._catalog[0].add_child_fragment_flow(parent, flow, direction)
def create_fragment(self): print('Create fragment.') name = input('Name: ') k = cyoa('Reference flow: Use (F)oreground flow or (S)earch for flow?', 'FS', 'F') if k.lower() == 'f': print('Select Reference flow:') flow = pick_one(self._catalog[0].flows()) else: self.isearch('flow')() flow = pick_one([f for f in self.selected if f.entity_type == 'flow']) self._m.add_to_foreground(flow) flow = flow.entity() print('Direction w.r.t. upstream:') direction = menu_list('Input', 'Output') print('interface\nname: %s\nflow: %s\ndirn: %s' % (name, flow, direction)) self._catalog[0].create_fragment(flow, direction, name=name)
def new_flow(self, flow=None, name=None, cas=None, quantity=None, comment=None, compartment=None, local_unit=None, **kwargs): if flow is None: name = name or self.input('Enter flow name: ', 'New flow') cas = cas or self.ifinput('Enter CAS number (or none): ', '') if quantity is None: if self._interactive: print('Choose reference quantity or none to create new: ') q = pick_one([fp for fp in self._qdb.flow_properties]) if q is None: q = self.new_quantity() quantity = q else: print('Using mass as reference quantity') quantity = self._qdb.get_canonical('mass') comment = comment or self.input('Enter flow comment: ', '') if compartment is None: if self._interactive: print('Choose compartment:') compartment = pick_compartment( self._qdb.c_mgr.compartments).to_list() else: print('Designating Intermediate flow') compartment = self._qdb.c_mgr.find_matching( 'Intermediate Flows').to_list() else: compartment = self._qdb.c_mgr.find_matching( compartment).to_list() if local_unit is not None: local_conv = quantity.convert(to=local_unit) if local_conv is None: print('Falling back to default unit: %s' % quantity.unit()) local_unit = None flow = LcFlow.new(name, quantity, CasNumber=cas, Compartment=compartment, Comment=comment, local_unit=local_unit, **kwargs) else: quantity = flow.reference_entity if self._interactive: flow.profile() while ifinput('Add characterizations for this flow? y/n', 'n') != 'n': ch = cyoa('[n]ew or [e]xisting quantity? ', 'en', 'e') if ch == 'n': cq = self.new_quantity() else: cq = pick_one(self._qdb.quantities()) if cq is None: cq = self.new_quantity() val = parse_math( input('Value (1 %s = x %s): ' % (quantity.unit(), cq.unit()))) flow.add_characterization(cq, value=val) return flow
def edit_flow(self, flow): ch = cyoa('Edit (P)roperties or (C)haracterizations? ', 'pc').lower() if ch == 'p': self._edit_entity(flow) elif ch == 'c': self.edit_characterizations(flow)