SECompartmentNodes.hxx
1 /* Distributed under the Apache License, Version 2.0.
2  See accompanying NOTICE file for details.*/
3 
4 template<COMPARTMENT_NODE_TEMPLATE>
6 {
7  m_Quantity = nullptr;
8  m_Potential = nullptr;
9 }
10 
11 template<COMPARTMENT_NODE_TEMPLATE>
13 {
14  Clear();
15 }
16 
17 template<COMPARTMENT_NODE_TEMPLATE>
19 {
20  m_QuantityNodes.clear();
21  m_AllNodes.clear();
22  SAFE_DELETE(m_Quantity);
23  SAFE_DELETE(m_Potential);
24 }
25 
26 template<COMPARTMENT_NODE_TEMPLATE>
28 {
29  if (std::find(m_AllNodes.begin(), m_AllNodes.end(), &node) == m_AllNodes.end())
30  m_AllNodes.push_back(&node);
31  SortNode(node);
32 }
33 template<COMPARTMENT_NODE_TEMPLATE>
35 {
36  Remove(m_AllNodes, &node);
37  StateChange();
38 }
39 
40 template<COMPARTMENT_NODE_TEMPLATE>
42 {
43  if (m_QuantityNodes.size() == 1)
44  return m_QuantityNodes[0]->GetNextQuantity();
45  if (m_Quantity == nullptr)
46  m_Quantity = new QuantityScalar();
47  m_Quantity->SetReadOnly(false);
48  m_Quantity->Invalidate();
49  for (NodeType* n : m_QuantityNodes)
50  m_Quantity->Increment(n->GetNextQuantity());
51  m_Quantity->SetReadOnly(true);
52  return *m_Quantity;
53 }
54 template<COMPARTMENT_NODE_TEMPLATE>
55 double SECompartmentNodes<COMPARTMENT_NODE_TYPES>::GetQuantity(const QuantityUnit& unit) const
56 {
57  if (m_QuantityNodes.empty())
58  return SEScalar::dNaN();
59  if (m_QuantityNodes.size() == 1)
60  return m_QuantityNodes[0]->GetNextQuantity().GetValue(unit);
61  double d = 0;
62  for (NodeType* n : m_QuantityNodes)
63  d += n->GetNextQuantity().GetValue(unit);
64  return d;
65 }
66 
67 template<COMPARTMENT_NODE_TEMPLATE>
69 {
70  for (NodeType* n : m_AllNodes)
71  if (n->HasNextPotential())
72  return true;
73  return false;
74 }
75 template<COMPARTMENT_NODE_TEMPLATE>
77 {
78  if (m_AllNodes.size() == 1)
79  return m_AllNodes[0]->GetNextPotential();
80 
81  const PotentialUnit* punit = m_AllNodes[0]->GetNextPotential().GetUnit();
82 
83  if (m_Potential == nullptr)
84  m_Potential = new PotentialScalar();
85  m_Potential->SetReadOnly(false);
86  m_Potential->Invalidate();
87  if (punit != nullptr)
88  m_Potential->SetValue(GetPotential(*punit), *punit);
89  m_Potential->SetReadOnly(true);
90  return *m_Potential;
91 }
92 template<COMPARTMENT_NODE_TEMPLATE>
93 double SECompartmentNodes<COMPARTMENT_NODE_TYPES>::GetPotential(const PotentialUnit& punit) const
94 {
95  if (!HasPotential())
96  return SEScalar::dNaN();
97  if (m_AllNodes.size() == 1)
98  return m_AllNodes[0]->GetNextPotential().GetValue(punit);
99 
100  // Every node will have a potential after its calculated
101  // But in case your nodes are not calculated, i need to see if you have a pressure
102 
103  double t = 0;
104  if (!m_QuantityNodes.empty())
105  {
106  // Volume weight by only the nodes with volume and pressure
107  const QuantityUnit* qunit = m_QuantityNodes[0]->GetNextQuantity().GetUnit();
108  double totalQuantity = 0;
109  for (NodeType* n : m_QuantityNodes)
110  if(n->HasNextPotential())
111  totalQuantity += n->GetNextQuantity().GetValue(*qunit);
112  for (NodeType* n : m_QuantityNodes)
113  if (n->HasNextPotential())
114  t += n->GetNextPotential().GetValue(punit) * (n->GetNextQuantity().GetValue(*qunit) / totalQuantity);
115  }
116  else
117  {
118  int num = 0;
119  for (NodeType* n : m_AllNodes)
120  if (n->HasNextPotential())
121  {
122  num++;
123  t += n->GetNextPotential().GetValue(punit);
124  }
125  t = t / num;
126  }
127  return t;
128 }
129 
130 template<COMPARTMENT_NODE_TEMPLATE>
132 {
133  if (!Contains(m_QuantityNodes, node))
134  {
135  if (node.HasQuantityBaseline())
136  m_QuantityNodes.push_back(&node);
137  }
138  else
139  {
140  if (!node.HasQuantityBaseline())
141  Remove(m_QuantityNodes,&node);
142  }
143 }
144 template<COMPARTMENT_NODE_TEMPLATE>
146 {
147  m_QuantityNodes.clear();
148  for (NodeType* node : m_AllNodes)
149  SortNode(*node);
150 }
151 
152 
153 #include "cdm/compartment/electrical/SEElectricalCompartment.h"
155 #include "cdm/compartment/fluid/SEFluidCompartment.h"
157 #include "cdm/compartment/thermal/SEThermalCompartment.h"
Definition: Logger.h:23
Definition: Logger.h:71
virtual bool HasPotential() const
Definition: SECompartmentNodes.cpp:72
virtual QuantityScalar & GetQuantity()
Definition: SECompartmentNodes.cpp:45
virtual void StateChange()
Definition: SECompartmentNodes.cpp:149
virtual void RemoveNode(NodeType &node)
Definition: SECompartmentNodes.cpp:38
SECompartmentNodes(Logger *logger)
Definition: SECompartmentNodes.cpp:9
virtual void Clear()
Definition: SECompartmentNodes.cpp:22
virtual PotentialScalar & GetPotential()
Definition: SECompartmentNodes.cpp:80
void SortNode(NodeType &node)
Definition: SECompartmentNodes.cpp:135
virtual ~SECompartmentNodes()
Definition: SECompartmentNodes.cpp:16
virtual void MapNode(NodeType &node)
Definition: SECompartmentNodes.cpp:31
static double dNaN()
Definition: SEScalar.cpp:10

Distributed under the Apache License, Version 2.0.

See accompanying NOTICE file for details.