[Avida-SVN] r1103 - in development/source: main tools

baerb at myxo.css.msu.edu baerb at myxo.css.msu.edu
Tue Nov 28 11:57:20 PST 2006


Author: baerb
Date: 2006-11-28 14:57:14 -0500 (Tue, 28 Nov 2006)
New Revision: 1103

Modified:
   development/source/main/cEnvironment.cc
   development/source/main/cEnvironment.h
   development/source/main/cResource.cc
   development/source/main/cResource.h
   development/source/main/cResourceLib.cc
   development/source/main/cResourceLib.h
   development/source/main/cSpatialResCount.h
   development/source/tools/cStringUtil.cc
   development/source/tools/cStringUtil.h
Log:

Added code to read spatial resource flow values for individual pixels.  There
is no "backend" code to actually use these resources yet.



Modified: development/source/main/cEnvironment.cc
===================================================================
--- development/source/main/cEnvironment.cc	2006-11-28 19:27:46 UTC (rev 1102)
+++ development/source/main/cEnvironment.cc	2006-11-28 19:57:14 UTC (rev 1103)
@@ -26,6 +26,11 @@
 #include "cTools.h"
 #include "cWorld.h"
 #include <iostream>
+
+#ifndef tArray_h
+#include "tArray.h"
+#endif
+
 using namespace std;
 
 
@@ -261,7 +266,17 @@
   while (desc.GetSize() > 0) {
     cString cur_resource = desc.PopWord();
     const cString name = cur_resource.Pop(':');
-    cResource* new_resource = resource_lib.AddResource(name);
+
+    /* If resource does not already exist create it, however if it already
+       exists (for instance was created as a cell resource) pull it out of
+       the library and modify the existing values */
+
+    cResource* new_resource;
+    if (! resource_lib.DoesResourceExist(name)) {
+      new_resource = resource_lib.AddResource(name);
+    } else {
+      new_resource = resource_lib.GetResource(name);
+    }
     
     while (cur_resource.GetSize() != 0) {
       cString var_entry = cur_resource.Pop(':');
@@ -350,7 +365,7 @@
     
     // If there are valid values for X/Y1's but not for X/Y2's assume that 
     // the user is interested only in one point and set the X/Y2's to the
-    // same value as 
+    // same value as X/Y1's
     
     if (new_resource->GetInflowX1()>-99 && new_resource->GetInflowX2()==-99){
       new_resource->SetInflowX2(new_resource->GetInflowX1());
@@ -369,6 +384,92 @@
   return true;
 }
 
+bool cEnvironment::LoadCell(cString desc)
+
+/* Routine to read in spatial resources loaded in one cell at a time */
+
+{
+  if (desc.GetSize() == 0) {
+    cerr << "Warning: Resource line with no resources listed." << endl;
+    return false;
+  }
+  
+  cResource* this_resource;
+  while (desc.GetSize() > 0) {
+    cString cur_resource = desc.PopWord();
+    const cString name = cur_resource.Pop(':');
+
+    /* if this resource has not been already created go ahead and create it and
+       set some default global values */
+
+    if (! resource_lib.DoesResourceExist(name)) {
+      this_resource = resource_lib.AddResource(name);
+      this_resource->SetInitial(0.0);
+      this_resource->SetInflow(0.0);
+      this_resource->SetOutflow(0.0);
+      this_resource->SetGeometry("GRID");
+      this_resource->SetInflowX1(-99);
+      this_resource->SetInflowX2(-99);
+      this_resource->SetInflowY1(-99);
+      this_resource->SetInflowY2(-99);
+      this_resource->SetOutflowX1(-99);
+      this_resource->SetOutflowX2(-99);
+      this_resource->SetOutflowY1(-99);
+      this_resource->SetOutflowY2(-99);
+      this_resource->SetXDiffuse(0.0);
+      this_resource->SetXGravity(0.0);
+      this_resource->SetYDiffuse(0.0);
+      this_resource->SetYGravity(0.0);
+    } else {
+      this_resource = resource_lib.GetResource(name);
+    }
+    cString cell_list_str = cur_resource.Pop(':'); 
+    tArray<int> cell_list = cStringUtil::ReturnArray(cell_list_str);
+    double tmp_initial = 0.0;
+    double tmp_inflow = 0.0;
+    double tmp_outflow = 0.0;
+    while (cur_resource.GetSize() != 0) {
+      cString var_entry = cur_resource.Pop(':');
+      cString var_name;
+      cString var_value;
+      const cString var_type = 
+        cStringUtil::Stringf("resource '%s'", static_cast<const char*>(name));
+      
+      // Parse this entry.
+      if (!ParseSetting(var_entry, var_name, var_value, var_type)) {
+        return false;
+      }
+      
+      if (var_name == "inflow") {
+        if (!AssertInputDouble(var_value, "inflow", var_type)) return false;
+        tmp_inflow = var_value.AsDouble();
+      }
+      else if (var_name == "outflow") {
+        if (!AssertInputDouble(var_value, "outflow", var_type)) return false;
+        tmp_outflow = var_value.AsDouble();
+      }
+      else if (var_name == "initial") {
+        if (!AssertInputDouble(var_value, "initial", var_type)) return false;
+        tmp_initial = var_value.AsDouble();
+      }
+      else {
+        cerr << "Error: Unknown variable '" << var_name
+        << "' in resource '" << name << "'" << endl;
+        return false;
+      }
+    }
+    for (int i=0; i < cell_list.GetSize(); i++) {
+      cCellResource tmp_cell_resource(cell_list[i],tmp_initial,
+                                      tmp_inflow, tmp_outflow);
+      this_resource->AddCellResource(tmp_cell_resource);
+      cout << "cell_list[" << i << "] = " << cell_list[i] << endl;
+    }
+    
+  }
+  
+  return true;
+}
+
 bool cEnvironment::LoadReaction(cString desc)
 {
   // Make sure this reaction has a description...
@@ -562,6 +663,9 @@
 }
 
 bool cEnvironment::LoadLine(cString line) 
+
+/* Routine to read in a line from the enviroment file and hand that line
+   line to the approprate routine to process it.                         */ 
 {
   cString type = line.PopWord();      // Determine type of this entry.
   type.ToUpper();                     // Make type case insensitive.
@@ -571,6 +675,7 @@
   else if (type == "REACTION") load_ok = LoadReaction(line);
   else if (type == "MUTATION") load_ok = LoadMutation(line);
   else if (type == "SET_ACTIVE") load_ok = LoadSetActive(line);
+  else if (type == "CELL") load_ok = LoadCell(line);
   else {
     cerr << "Error: Unknown environment keyword '" << type << "." << endl;
     return false;

Modified: development/source/main/cEnvironment.h
===================================================================
--- development/source/main/cEnvironment.h	2006-11-28 19:27:46 UTC (rev 1102)
+++ development/source/main/cEnvironment.h	2006-11-28 19:57:14 UTC (rev 1103)
@@ -67,6 +67,7 @@
   bool LoadReactionProcess(cReaction * reaction, cString desc);
   bool LoadReactionRequisite(cReaction * reaction, cString desc);
   bool LoadResource(cString desc);
+  bool LoadCell(cString desc);
   bool LoadReaction(cString desc);
   bool LoadMutation(cString desc);
 

Modified: development/source/main/cResource.cc
===================================================================
--- development/source/main/cResource.cc	2006-11-28 19:27:46 UTC (rev 1102)
+++ development/source/main/cResource.cc	2006-11-28 19:57:14 UTC (rev 1103)
@@ -14,7 +14,23 @@
 
 using namespace std;
 
+cCellResource::cCellResource()
+  : cell_id(-99)
+  , initial(0.0)
+  , inflow(0.0)
+  , outflow(0.0)
+{
+}
 
+cCellResource::cCellResource(int _cell_id, double _initial, double _inflow, 
+                             double _outflow)
+  : cell_id(_cell_id)
+  , initial(_initial)
+  , inflow(_inflow)
+  , outflow(_outflow)
+{
+}
+  
 cResource::cResource(const cString & _name, int _id)
   : name(_name)
   , id(_id)

Modified: development/source/main/cResource.h
===================================================================
--- development/source/main/cResource.h	2006-11-28 19:27:46 UTC (rev 1102)
+++ development/source/main/cResource.h	2006-11-28 19:57:14 UTC (rev 1103)
@@ -14,7 +14,28 @@
 #ifndef cString_h
 #include "cString.h"
 #endif
+#ifndef tArray_h
+#include "tArray.h"
+#endif
 
+
+/* class to hold resource informations for individual cells (mini-chemostats) */
+
+class cCellResource
+{
+private:
+  int cell_id;
+  double initial;
+  double inflow;
+  double outflow;
+
+public:
+  cCellResource();
+  cCellResource(int _cell_id, double _initial, double _inflow, double _outflow);
+};
+
+/* class to hold all infromation for a single resource */
+
 class cResource
 {
 private:
@@ -36,6 +57,7 @@
   double xgravity;
   double ydiffuse;
   double ygravity;
+  tArray<cCellResource> cell_list;
  
   
   cResource(); // @not_implemented
@@ -80,6 +102,7 @@
   void SetXGravity(double _xgravity) { xgravity = _xgravity; }
   void SetYDiffuse(double _ydiffuse) { ydiffuse = _ydiffuse; }
   void SetYGravity(double _ygravity) { ygravity = _ygravity; }
+  void AddCellResource(cCellResource new_cell) { cell_list.Push(new_cell); }
 };
 
 

Modified: development/source/main/cResourceLib.cc
===================================================================
--- development/source/main/cResourceLib.cc	2006-11-28 19:27:46 UTC (rev 1102)
+++ development/source/main/cResourceLib.cc	2006-11-28 19:57:14 UTC (rev 1103)
@@ -44,3 +44,15 @@
 {
   return resource_array[id];
 }
+
+bool cResourceLib::DoesResourceExist(const cString & res_name) 
+
+/* Return boolean for if the named resource exists in the resource library */
+
+{
+  for (int i = 0; i < resource_array.GetSize(); i++) {
+    if (resource_array[i]->GetName() == res_name) return true;
+  }
+  return false;
+}
+

Modified: development/source/main/cResourceLib.h
===================================================================
--- development/source/main/cResourceLib.h	2006-11-28 19:27:46 UTC (rev 1102)
+++ development/source/main/cResourceLib.h	2006-11-28 19:57:14 UTC (rev 1103)
@@ -35,6 +35,7 @@
   cResource* AddResource(const cString& res_name);
   cResource* GetResource(const cString& res_name) const;
   cResource* GetResource(int id) const;
+  bool DoesResourceExist(const cString & res_name);
 };
 
 

Modified: development/source/main/cSpatialResCount.h
===================================================================
--- development/source/main/cSpatialResCount.h	2006-11-28 19:27:46 UTC (rev 1102)
+++ development/source/main/cSpatialResCount.h	2006-11-28 19:57:14 UTC (rev 1103)
@@ -18,6 +18,10 @@
 #include "tArray.h"
 #endif
 
+#ifndef cResource_h
+#include "cResource.h"
+#endif
+
 class cSpatialResCount
 {
 private:
@@ -31,7 +35,8 @@
 public:
   cSpatialResCount();
   cSpatialResCount(int inworld_x, int inworld_y, int ingeometry);
-  cSpatialResCount(int inworld_x, int inworld_y, int ingeometry, double inxdiffuse, double inydiffuse,
+  cSpatialResCount(int inworld_x, int inworld_y, int ingeometry, 
+                   double inxdiffuse, double inydiffuse,
                    double inxgravity, double inygravity);
   
   void ResizeClear(int inworld_x, int inworld_y, int ingeometry);
@@ -52,7 +57,9 @@
   void FlowAll();
   const double SumAll() const;
   void Source(double amount) const;
+  // void Source(tArray<cCellReource> cell_list) const;
   void Sink(double percent) const;
+  // void Sink(tArray<cCellReource> cell_list) const;
   void SetGeometry(int in_geometry) { geometry = in_geometry; }
   void SetXdiffuse(double in_xdiffuse) { xdiffuse = in_xdiffuse; }
   void SetXgravity(double in_xgravity) { xgravity = in_xgravity; }

Modified: development/source/tools/cStringUtil.cc
===================================================================
--- development/source/tools/cStringUtil.cc	2006-11-28 19:27:46 UTC (rev 1102)
+++ development/source/tools/cStringUtil.cc	2006-11-28 19:57:14 UTC (rev 1103)
@@ -308,3 +308,28 @@
 {
   return Stringf("%f", in_double);
 }
+
+/* Return an array of integers from a string with format x,y..z,a */
+
+tArray<int> cStringUtil::ReturnArray(cString& in_string)
+{
+  tArray<int> out_list;
+  while (in_string.GetSize() != 0) {
+    cString chunk = in_string.Pop(',');
+
+    /* if the string has a .. in it find the two numbers on either side of it */
+
+    if (chunk.IsSubstring(".",1)) {
+      cString start_str = chunk.Pop('.');
+      chunk.RemoveChar('.');
+      cString stop_str = chunk.PopWord();
+      int start_int = start_str.AsInt();
+      int stop_int = stop_str.AsInt();
+      for (int i = start_int; i <= stop_int; i++) out_list.Push(i);
+    } else {
+      out_list.Push(chunk.AsInt());
+    }
+  }
+  return(out_list);
+}
+

Modified: development/source/tools/cStringUtil.h
===================================================================
--- development/source/tools/cStringUtil.h	2006-11-28 19:27:46 UTC (rev 1102)
+++ development/source/tools/cStringUtil.h	2006-11-28 19:57:14 UTC (rev 1103)
@@ -15,6 +15,10 @@
 #include "cString.h"
 #endif
 
+#ifndef tArray_h
+#include "tArray.h"
+#endif
+
 class cStringUtil
 {
 private:
@@ -59,6 +63,10 @@
   static cString Convert(bool in_bool);
   static cString Convert(int in_int);
   static cString Convert(double in_double);
+
+  /* Return an array of integers from a string with format x,y..z,a */
+
+  static tArray<int> ReturnArray(cString& in_string);
 };
 
 




More information about the Avida-cvs mailing list