From 1d76798e0aad25fe183980f8cf94e4542a4fef5c Mon Sep 17 00:00:00 2001 From: Anthony Scopatz Date: Thu, 9 Feb 2017 11:41:09 -0500 Subject: [PATCH] Pyagent Documentation (#230) LGTM! thx @scopatz --- source/arche/cli.rst | 44 +- source/arche/custom_tables.rst | 60 +- source/arche/dbtypes.rst | 22 +- source/arche/decay.rst | 24 +- source/arche/dre.rst | 579 ++++++++++++------ source/arche/dynamic_loading.rst | 8 +- source/arche/errors.rst | 33 +- .../{hello_world.rst => hello_world_cpp.rst} | 68 +- source/arche/hello_world_py.rst | 130 ++++ source/arche/index.rst | 24 +- source/arche/logger.rst | 24 +- source/arche/resources.rst | 161 ++++- source/arche/sim_init.rst | 2 - source/arche/timestep.rst | 22 +- source/arche/toolkit.rst | 101 ++- source/arche/{tour.rst => tour_cpp.rst} | 0 .../{tutorial => tutorial_cpp}/cyclist.rst | 0 .../arche/{tutorial => tutorial_cpp}/dre.rst | 0 .../{tutorial => tutorial_cpp}/github_new.png | Bin .../{tutorial => tutorial_cpp}/index.rst | 7 +- .../input_files.rst | 0 .../{tutorial => tutorial_cpp}/setup.rst | 58 +- .../simple_cycle.svg | 0 .../{tutorial => tutorial_cpp}/state_var.rst | 0 .../storage_capacity.svg | 0 .../storage_diagram.svg | 0 .../{tutorial => tutorial_cpp}/testing.rst | 0 .../{tutorial => tutorial_cpp}/toolkit.rst | 0 source/arche/tutorial_py/github_new.png | Bin 0 -> 132458 bytes source/arche/tutorial_py/index.rst | 28 + source/arche/tutorial_py/input_files.rst | 5 + source/arche/tutorial_py/setup.rst | 170 +++++ source/arche/tutorial_py/simple_cycle.svg | 4 + source/arche/tutorial_py/state_var.rst | 228 +++++++ source/arche/tutorial_py/storage_capacity.svg | 4 + source/arche/tutorial_py/storage_diagram.svg | 4 + source/arche/tutorial_py/testing.rst | 4 + source/arche/tutorial_py/toolkit.rst | 329 ++++++++++ source/cyclusagent.py | 163 ++++- source/user/input_specs/archetypes.rst | 21 +- source/user/input_specs/commodity.rst | 26 +- source/user/input_specs/control.rst | 24 +- source/user/input_specs/facility.rst | 75 +-- source/user/input_specs/inst.rst | 94 ++- source/user/input_specs/recipe.rst | 61 +- source/user/input_specs/region.rst | 63 +- source/user/writing_input.rst | 78 ++- 47 files changed, 2155 insertions(+), 593 deletions(-) rename source/arche/{hello_world.rst => hello_world_cpp.rst} (69%) create mode 100644 source/arche/hello_world_py.rst rename source/arche/{tour.rst => tour_cpp.rst} (100%) rename source/arche/{tutorial => tutorial_cpp}/cyclist.rst (100%) rename source/arche/{tutorial => tutorial_cpp}/dre.rst (100%) rename source/arche/{tutorial => tutorial_cpp}/github_new.png (100%) rename source/arche/{tutorial => tutorial_cpp}/index.rst (90%) rename source/arche/{tutorial => tutorial_cpp}/input_files.rst (100%) rename source/arche/{tutorial => tutorial_cpp}/setup.rst (67%) rename source/arche/{tutorial => tutorial_cpp}/simple_cycle.svg (100%) rename source/arche/{tutorial => tutorial_cpp}/state_var.rst (100%) rename source/arche/{tutorial => tutorial_cpp}/storage_capacity.svg (100%) rename source/arche/{tutorial => tutorial_cpp}/storage_diagram.svg (100%) rename source/arche/{tutorial => tutorial_cpp}/testing.rst (100%) rename source/arche/{tutorial => tutorial_cpp}/toolkit.rst (100%) create mode 100644 source/arche/tutorial_py/github_new.png create mode 100644 source/arche/tutorial_py/index.rst create mode 100644 source/arche/tutorial_py/input_files.rst create mode 100644 source/arche/tutorial_py/setup.rst create mode 100644 source/arche/tutorial_py/simple_cycle.svg create mode 100644 source/arche/tutorial_py/state_var.rst create mode 100644 source/arche/tutorial_py/storage_capacity.svg create mode 100644 source/arche/tutorial_py/storage_diagram.svg create mode 100644 source/arche/tutorial_py/testing.rst create mode 100644 source/arche/tutorial_py/toolkit.rst diff --git a/source/arche/cli.rst b/source/arche/cli.rst index 58bf08255..c7edb670b 100644 --- a/source/arche/cli.rst +++ b/source/arche/cli.rst @@ -2,7 +2,6 @@ Archetype Command Line Interface ======================================= - There are a few archetype-specific |Cyclus| CLI commands that allow introspecting details of archetypes that |Cyclus| can find. Most expose data that is automatically generated by the Cyclus preprocessor. For example, @@ -39,15 +38,15 @@ To view a JSON structured output of all the data generated and collected from $ cyclus --agent-annotations :cycamore:Source { "all_parents": [ - "cyclus::Agent", - "cyclus::Facility", - "cyclus::Ider", - "cyclus::StateWrangler", - "cyclus::TimeListener", - "cyclus::Trader", - "cyclus::toolkit::AgentManaged", + "cyclus::Agent", + "cyclus::Facility", + "cyclus::Ider", + "cyclus::StateWrangler", + "cyclus::TimeListener", + "cyclus::Trader", + "cyclus::toolkit::AgentManaged", "cyclus::toolkit::CommodityProducer" - ], + ], "doc": "This facility acts as a source of material with a fixed... ... @@ -69,26 +68,33 @@ And to list all archetypes inside a particular library: Archetype Versioning -------------------- - The ``cyclus::Agent`` class exposes a ``version()`` member function which can be -queried with the |Cyclus| CLI. For example, +queried with the |Cyclus| CLI. For example, - .. code-block:: bash +.. code-block:: bash - $ cyclus --agent-version :agents:Source - 1.3.1-7-g9a2c9c9 + $ cyclus --agent-version :agents:Source + 1.3.1-7-g9a2c9c9 This is generated from some ``git`` version control information. You can make your own version tag information for some archetype like +**C++:** + +.. code-block:: c++ - .. code-block:: c++ + virtual std::string version() { return "My Version"; } - virtual std::string version() { return "My Version"; } +**Python:** + +.. code-block:: Python + + def version(self): + return "My Version" and then access the version with - .. code-block:: bash +.. code-block:: bash - $ cyclus --agent-version my/path:my_library:MyArchetype - My Version + $ cyclus --agent-version my/path:my_library:MyArchetype + My Version diff --git a/source/arche/custom_tables.rst b/source/arche/custom_tables.rst index 5063f70c1..473530b69 100644 --- a/source/arche/custom_tables.rst +++ b/source/arche/custom_tables.rst @@ -1,4 +1,3 @@ - Custom Database Tables ======================= @@ -6,6 +5,8 @@ Agents are allowed to write their own custom output into the database during simulations. The interface for recording this data is accessible through the agent's :term:`context`. Data may be logged via the Context::NewDatum function: +**C++:** + .. code-block:: c++ MyReactor::Tick() { @@ -22,10 +23,29 @@ agent's :term:`context`. Data may be logged via the Context::NewDatum function: ... } +**Python:** + +.. code-block:: python + + from cyclus.agents import Facility + import cyclus.typesystem as ts + + class MyReactor(Facility): + + def tick(self): + ... + d = self.context.new_datum("MyReactorData") + d.add_val("AgentID", self.id, type=ts.INT) + d.add_val("Time", self.context.time, type=ts.INT) + d.add_val("WaterUsage", self.monthl_water, type=ts.DOUBLE) + d.add_val("OperatingCost", self.monthly_op_cost, type=ts.DOUBLE) + d.record() + ... + This would create a table in the output database named "MyReactorData". The table would get a new row entry every time step with four columns named -"AgentID", "Time", "WaterUsage" and "OperatingCost". ``AddVal`` calls can be chained -any number of times for an arbitrary number of columns. ``Record`` must be +"AgentID", "Time", "WaterUsage" and "OperatingCost". "Add value" calls can be chained +any number of times for an arbitrary number of columns. Record must be called once for each datum after all values have been added. Any custom tables created in this manner will appear in the output database alongside the |cyclus| core tables. Because there may be several agent instances of a @@ -33,6 +53,8 @@ single agent class, tables should generally include a column that adds their ID; and for similar reasons, it is often desirable to include the simulation time as a column in these tables: +**C++:** + .. code-block:: c++ context()->NewDatum("MyReactorData") @@ -41,6 +63,16 @@ time as a column in these tables: ->AddVal(... ... +**Python:** + +.. code-block:: python + + d = self.context.new_datum("MyReactorData") + d.add_val("AgentID", self.id, type=ts.INT) + d.add_val("Time", self.context.time, type=ts.INT) + d.add_val(... + ... + Datums with the same table name must have the same schema (e.g. same field names and value types). It is the responsibility of the developer to enforce this in their code. @@ -53,8 +85,8 @@ enforce this in their code. use. For information on which c++ types the backends support, you can check :doc:`here `. -.. note:: If you require a datatype that isn't currently supported, please - ask the kernel developers and they will help as soon as possible. +.. note:: If you require a datatype that isn't currently supported, please + ask the kernel developers and they will help as soon as possible. Table Data Shapes ------------------ @@ -70,6 +102,8 @@ vector indicates variable length also. It is an error to pass in a shape vector with the wrong rank (number of elements) for that type. An example of using the shape vector follows: +**C++:** + .. code-block:: c++ std::vector colors; @@ -84,15 +118,27 @@ using the shape vector follows: context()->NewDatum("DecorPreferences") ->AddVal("AgentID", id()) ->AddVal("Time", context()->time()) - ->AddVal("FavoritColors", colors, shape) + ->AddVal("FavoriteColors", colors, shape) ->Record(); +**Python:** + +.. code-block:: c++ + + colors = ["green", "blue", "chartreuse"] + shape = [5, 8] + d = self.context.new_datum("DecorPreferences") + d.add_val("AgentID", self.id, type=ts.INT) + d.add_val("Time", self.context.time, type=ts.INT) + d.add_val("FavoriteColors", colors, shape, ts.VECTOR_STRING) + d.record() + In the example above, the "chartreuse" color is longer than the 8 characters specified in the shape. So it will be truncated to "chartreu" in the database. Shape vectors should generally be stored as class member variables to avoid excessive memory [de]allocation and should be set correctly from construction to destruction of your agent. - + Reserved Table Names --------------------- diff --git a/source/arche/dbtypes.rst b/source/arche/dbtypes.rst index 25a86cb69..c69d55872 100644 --- a/source/arche/dbtypes.rst +++ b/source/arche/dbtypes.rst @@ -3,8 +3,8 @@ Database Types ============== -Here is a listing of all supported data types that the various backends have -implemented, by |cyclus| version number. If your agents need a type that is not +Here is a listing of all supported data types that the various backends have +implemented, by |cyclus| version number. If your agents need a type that is not yet supported, please let us know and we'll get to it as soon as possible! **Description of fields:** @@ -29,7 +29,7 @@ yet supported, please let us know and we'll get to it as soon as possible!