Commits

Matthew Turk committed 9c0dd04

Moving to SetupKernels instead of SetupFields, and solidifying a bit the idea
of kernels and field containers and their utility. Some memory will get leaked
in this method as it stands, but not much, and I intend to clean that up.

Comments (0)

Files changed (8)

src/enzo/FieldContainer.C

+/***********************************************************************
+/
+/  FIELD CONTAINER CLASS
+/
+/  written by: Matthew Turk, Devin Silvia
+/  date:       January 2012
+/
+/  PURPOSE:
+/
+************************************************************************/
+
+#ifdef NEW_PROBLEM_TYPES
+#include <string.h>
+#include <vector>
+#include <stdio.h>
+#include <math.h>
+#include <iostream>
+#include "ErrorExceptions.h"
+#include "macros_and_parameters.h"
+#include "typedefs.h"
+#include "global_data.h"
+#include "Fluxes.h"
+#include "GridList.h"
+#include "ExternalBoundary.h"
+#include "Grid.h"
+#include "Hierarchy.h"
+#include "TopGridData.h"
+#include "ProblemType.h"
+#include "EventHooks.h"
+
+FieldContainer::FieldContainer()
+{
+    return;
+}
+
+FieldContainer::FieldContainer(FieldContainer &fields)
+{
+    std::map<std::string, float>::iterator it;
+    std::string field_name;
+    for(it = fields.start(); it != fields.end(); it++)
+    {
+        field_name = (*it).first;
+        this->add_field(field_name, fields.field_numbers[field_name],
+                        fields[field_name]);
+    }
+}
+
+void FieldContainer::add_field(std::string field_name, int FieldNumber,
+                               float value)
+{
+    if (this->field_values.count(field_name) != 0)
+        ENZO_VFAIL("Tried to reset default for %s", field_name.c_str())
+    this->field_values[field_name] = value;
+    this->field_numbers[field_name] = FieldNumber;
+    return;
+}
+
+void FieldContainer::clear()
+{
+    for (std::map<std::string, float>::iterator it =
+            this->field_values.begin();
+         it != this->field_values.end();
+         it++)
+    {
+        this->field_values[it->first] = 0.0;
+    }
+    return;
+}
+
+int FieldContainer::needs(std::string field_name)
+{
+    if (this->field_values.count(field_name) == 0) return FALSE;
+    else return TRUE;
+}
+
+float& FieldContainer::operator[](std::string field_name)
+{
+    if (this->field_values.count(field_name) == 0)
+        ENZO_VFAIL("Asked for %s but it did not exist as a field.", field_name.c_str())
+    return this->field_values[field_name];
+}
+
+std::map<std::string, float>::iterator FieldContainer::start()
+{
+    return this->field_values.begin();
+}
+
+std::map<std::string, float>::iterator FieldContainer::end()
+{
+    return this->field_values.end();
+}
+
+
+#endif

src/enzo/FieldContainer.h

+/***********************************************************************
+/
+/  FIELD CONTAINER CLASS
+/
+/  written by: Matthew Turk, Devin Silvia
+/  date:       January 2012
+/
+/  PURPOSE:
+/
+************************************************************************/
+#ifndef __FIELD_CONTAINER__
+#define __FIELD_CONTAINER__
+
+class FieldContainer
+{
+    public:
+        FieldContainer();
+        FieldContainer(FieldContainer &orig);
+        float &operator[](std::string);
+        void add_field(std::string, int FieldNumber,
+                       float value = FLOAT_UNDEFINED);
+        void clear();
+        int needs(std::string field_name);
+        std::map<std::string, float>::iterator start();
+        std::map<std::string, float>::iterator end();
+    protected:
+        std::map<std::string, float> field_values;
+        std::map<std::string, field_type> field_numbers;
+};
+
+#endif

src/enzo/InitializationKernel.C

 #include "EventHooks.h"
 
 InitializationKernelConstant::InitializationKernelConstant(
-    FieldContainer *constants, int additive = FALSE)
+    FieldContainer *constants, int additive)
 {
     this->constants = constants;
     this->additive = additive;
                 FLOAT dx, FLOAT dy, FLOAT dz)
 {
     std::map<std::string, float>::iterator it;
-    for(it = this->constants->defaults_start();
-        it != this->constants->defaults_end();
+    for(it = this->constants->start();
+        it != this->constants->end();
         it++)
     {
       if (this->additive == TRUE) {

src/enzo/InitializationKernel.h

 {
     
     public:
-        InitializationKernelConstant(FieldContainer *constants, int additive);
+        InitializationKernelConstant(FieldContainer *constants,
+                    int additive = FALSE);
         void apply(FieldContainer &fields,
                    FLOAT x, FLOAT y, FLOAT z,
                    FLOAT dx, FLOAT dy, FLOAT dz);

src/enzo/Make.config.objects

         ffte4X.o \
         ffte_st1.o \
         fft_utils.o \
+        FieldContainer.o \
         FinalizeFluxes.o \
         FindCube.o \
         FindField.o \

src/enzo/ProblemType.C

 EnzoProblemType::EnzoProblemType()
 {
     this->DataLabelCount = 0;
+    this->NumberOfKernels = 0;
 }
 
 int EnzoProblemType::AddDataLabel(const char *FieldName) {
 
 }
 
-void FieldContainer::add_field(std::string field_name, int FieldNumber,
-                               float default_value)
+void ApplyKernels(grid *thisgrid)
 {
-    if (this->default_values.count(field_name) != 0)
-        ENZO_VFAIL("Tried to reset default for %s", field_name.c_str())
-    this->default_values[field_name] = default_value;
-    this->field_numbers[field_name] = FieldNumber;
-    return;
-}
-
-void FieldContainer::reset()
-{
-    for (std::map<std::string, float>::iterator it =
-            this->default_values.begin();
-         it != this->default_values.end();
-         it++)
-    {
-        this->field_values[it->first] = it->second;
-    }
-    return;
-}
-
-int FieldContainer::needs(std::string field_name)
-{
-    if (this->default_values.count(field_name) == 0) return FALSE;
-    else return TRUE;
-}
-
-float& FieldContainer::operator[](std::string field_name)
-{
-    if (this->field_values.count(field_name) == 0)
-        ENZO_VFAIL("Asked for %s but it did not exist as a field.", field_name.c_str())
-    return this->field_values[field_name];
-}
-
-std::map<std::string, float>::iterator FieldContainer::defaults_start()
-{
-    return this->default_values.begin();
-}
-
-std::map<std::string, float>::iterator FieldContainer::defaults_end()
-{
-    return this->default_values.end();
+    /* Now we iterate over every cell */
 }
 
 #endif

src/enzo/ProblemType.h

 #define __PROBLEM_TYPES__
 
 #include "InitializationKernel.h"
+#include "FieldContainer.h"
 #include <string>
 
-class FieldContainer
-{
-    public:
-        float &operator[](std::string);
-        void add_field(std::string, int FieldNumber, float default_value);
-        void reset();
-        int needs(std::string field_name);
-        std::map<std::string, float>::iterator defaults_start();
-        std::map<std::string, float>::iterator defaults_end();
-    private:
-        std::map<std::string, float> field_values;
-        std::map<std::string, float> default_values;
-        std::map<std::string, field_type> field_numbers;
-};
-
 class EnzoProblemType
 {
 /* These will be overridden in the public namespace by implementations */
             HierarchyEntry &TopGrid, TopGridData &MetaData)
     {return SUCCESS;}
     int AddDataLabel(const char *FieldName);
-    virtual void SetupFields(FieldContainer &Fields)
-    {return;}
+    virtual void SetupKernels()=0;
+    void ApplyKernels(grid *thisgrid) {return;}
 
 protected:
     //.. constructor
     EnzoProblemType();
     virtual ~EnzoProblemType()
-    {}
+    {
+        this->ClearKernels();
+    }
     int DataLabelCount;
     void ReadConfig(FILE *fptr);
 
 
     protected:
 
+        int AddKernel(InitializationKernel *kernel)
+        {
+            this->kernels[this->NumberOfKernels++] = kernel;
+            return this->NumberOfKernels;
+        }
+        void ClearKernels()
+        {
+            for (int i = 0; i < this->NumberOfKernels; i++)
+                delete this->kernels[i];
+            this->NumberOfKernels = 0;
+        }
+        int NumberOfKernels;
+        InitializationKernel *kernels[100];
+
     private:
-
-        std::vector<InitializationKernel> kernels;
 };
 
 /*!

src/enzo/ProblemType_RotatingCylinder.C

 
       this->ReadConfig(fptr);
 
-      FieldContainer background;
-      this->SetupFields(background);
+      this->SetupKernels();
 
       this->InitializeUniformGrid(TopGrid.GridData,
             RotatingCylinderDensity,
 
     } 
 
+    void SetupKernels()
+    {
+        FieldContainer *background_fields = new FieldContainer;
+        background_fields->add_field("Density", Density, RotatingCylinderDensity);
+        background_fields->add_field("TotalEnergy", TotalEnergy, RotatingCylinderTotalEnergy);
+        background_fields->add_field("GasEnergy", InternalEnergy,
+                          RotatingCylinderTotalEnergy);
+        background_fields->add_field("x-velocity", Velocity1, 0.0);
+        background_fields->add_field("y-velocity", Velocity2, 0.0);
+        background_fields->add_field("z-velocity", Velocity3, 0.0);
+        background_fields->add_field("Metal_Density", Metallicity, 0.0);
 
-    void SetupFields(FieldContainer &Fields)
-    {
-        Fields.add_field("Density", Density, RotatingCylinderDensity);
-        Fields.add_field("TotalEnergy", TotalEnergy, RotatingCylinderTotalEnergy);
-        Fields.add_field("GasEnergy", InternalEnergy,
-                          RotatingCylinderTotalEnergy);
-        Fields.add_field("x-velocity", Velocity1, 0.0);
-        Fields.add_field("y-velocity", Velocity2, 0.0);
-        Fields.add_field("z-velocity", Velocity3, 0.0);
-        Fields.add_field("Metal_Density", Metallicity, 0.0);
+        InitializationKernelConstant *background = 
+            new InitializationKernelConstant(background_fields);
+
+        this->AddKernel(static_cast<InitializationKernel*>(background));
     }
 
     void ReadConfig(FILE *fptr)