Locations

Contents

Overview | Fields | Types | Constructors | Methods | Examples

Overview

Locations are compound components in Macro that represent geographic or conceptual places in an energy system. They serve as containers for collections of Nodes, where each Node has a different Commodity or sub-Commodity type. Locations are one of the core organizational concepts in Macro, alongside Assets.

The primary purpose of Locations is to simplify the construction and management of complex energy system models. Rather than requiring users to manually connect individual Edges of Assets to specific Nodes for each commodity type, the Asset as a whole can be connected to a Location and Macro will automatically handle the routing and connection of the appropriate Edges and Nodes within that Location. This abstraction makes model building more intuitive and less error-prone.

Location Structure and Node Management

Each Location maintains a collection of Nodes organized by commodity type, with each Node handling flows of a specific Commodity. The Location uses a Set to track the commodity types of all Nodes it contains, ensuring that only one Node per commodity type exists within any given Location. This constraint maintains clarity about which Node handles each commodity type and how Edges should be routed.

When a user attempts to add a Node to a Location that already contains a Node of the same commodity type, the system will throw an error unless explicitly instructed to replace the existing Node. This safeguard helps maintain model consistency and prevents accidental overwrites of important network components. Macro can also be configured to automatically create new Nodes for commodity types that do not already exist in the Location, streamlining the process of adding new components to the model.

Simplified Model Construction

The Location abstraction makes building energy systems much more intuitive because it mirrors how people naturally think about energy infrastructure. Physical equipment like power plants, storage facilities, and transmission lines are typically associated with specific geographic locations. By organizing the model around Locations, users can construct systems that directly correspond to real-world geography and infrastructure layout.

When Assets are connected to Locations rather than individual Nodes, Macro automatically determines the appropriate connections based on the commodity types involved in the Asset's operation. For example, a natural gas power plant Asset connected to a Location will automatically have its gas input connected to the Location's natural gas Node and its electricity output connected to the Location's electricity Node. This automatic routing eliminates much of the manual wiring that would otherwise be required.

Physical and Logical Modeling

Locations can represent both physical geographic areas (such as cities, regions, or specific sites) and logical groupings (such as market zones or administrative areas). This flexibility allows users to organize their models at the appropriate level of detail for their analysis needs. A national energy system model might use Locations to represent states or provinces, while a local energy system model might use Locations to represent individual buildings or districts.

The Location concept also supports hierarchical thinking about energy systems, where larger geographic areas contain multiple smaller areas, each with their own local energy infrastructure and commodity flows. While Macro doesn't enforce hierarchical relationships between Locations, users can organize their models to reflect these real-world structures.

Key Concepts

  • Node Collection: Locations contain multiple Nodes, each handling a specific commodity type at that location
  • Automatic Routing: Assets connected to Locations automatically route to appropriate Nodes based on commodity types
  • Commodity Uniqueness: Each Location enforces one Node per commodity or sub-commodity type using a tracking Set
  • Simplified Modeling: Locations make model construction intuitive by mirroring real-world geographic organization
  • Flexible Abstraction: Can represent physical locations or logical groupings depending on modeling needs
  • Error Prevention: Automatic checking prevents duplicate commodity Nodes within the same Location

Location Fields

Locations have the following fields. When running a model, the fields are set by the input files or programmatically when building the system.

Units in Macro

We have assumed that your System is using units of MWh for energy, tonnes for mass, and hour-long time steps. You can use any set of units as long as they are consistent across your operations and investment inputs.

Identification and System Reference

FieldTypeDescriptionDefault
idSymbolUnique identifier for the location-
systemAbstractSystemReference to the system containing this location-

Node Management

FieldTypeDescriptionDefault
nodesDict{Symbol,Node}Dictionary mapping commodity symbols to nodesDict{Symbol,Node}()
commoditiesSet{Symbol}Set of commodity symbols for nodes in this locationSet{Symbol}()
constraintsVector{AbstractTypeConstraint}Parsed location-level constraintsAbstractTypeConstraint[]

Types

Type Hierarchy

Location is a subtype ofthe abstract MacroObject type:

MacroObject
├── Location
├── AbstractAsset
├── AbstractVertex
└── AbstractEdge

Location

The Location type represents a geographic or conceptual place in an energy system that contains multiple nodes for different commodities.

Constructors

Keyword Constructors

Location(; id::Symbol, system::AbstractSystem, nodes::Dict{Symbol,Node}=Dict{Symbol,Node}(), 
         commodities::Set{Symbol}=Set{Symbol}(),
         constraints::Vector{AbstractTypeConstraint}=AbstractTypeConstraint[])

Direct constructor using keyword arguments for all fields.

ParameterTypeDescriptionRequired
idSymbolUnique identifier for the locationYes
systemAbstractSystemReference to the containing systemYes
nodesDict{Symbol,Node}Dictionary mapping commodity symbols to nodesNo
commoditiesSet{Symbol}Set of commodity symbols for contained nodesNo
constraintsVector{AbstractTypeConstraint}Parsed location-level constraintsNo

Factory Constructors

Location loading parses each raw entry into a validated LocationInput, then constructs the runtime Location with its system reference:

LocationInput fieldTypeDefault
idStringRequired; nonempty
constraintsDict{Symbol,Any}Empty dictionary
commoditiesDict{Symbol,Dict{Symbol,Any}}Empty dictionary

parse_location_input(entry) normalizes a location-name string or dictionary and extracts the input fields. Location(input::LocationInput, system) converts the constraint dictionary into the existing constraint types and configs and constructs the runtime location. Additional raw input fields remain available to builders for explicit use; they are not automatically stored on LocationInput or Location.

The parser checks raw entry structure and required keys. The typed LocationInput constructor accepts an AbstractString ID and AbstractDict constraint and commodity inputs, converts them for storage, and validates their structure. A fallback constructor reports unsupported field types, including for programmatically constructed inputs. Required constraint configurations are validated when building the runtime Location.

Programmatic input dictionaries must use symbol keys for the location entry, constraint names, commodity names, and node fields. This is the format produced by the JSON reader; no JSON3-specific container type is required. For example, use Dict(:demand => Dict(:timeseries => Dict(:path => "demand.csv", :header => "demand"))). LocationInput checks the dictionaries it directly consumes and rejects string keys rather than partially converting them. Node-field values, including demand, supply and constraint payloads, are passed through to their existing loaders without recursive inspection or conversion here. Those consumers expect their usual parsed-compatible inputs; malformed nested dictionaries may fail later during loading. String values, including IDs and file paths, remain supported, and opaque extension fields or custom constraint payloads retain their own key conventions.

MacroEnergy.LocationInput — Type
LocationInput(; id, constraints=Dict{Symbol,Any}(), commodities=Dict())

Validated inputs consumed when constructing a Location. id is a required, nonempty string; constraints contains raw constraint input dictionaries/flags. Constraint instances and typed configs are created when building the Location. commodities maps commodity names to node input dictionaries. Empty dictionaries use node defaults and an ID of <location>_<commodity>. Input dictionaries use symbol keys, as produced by the JSON reader. String values such as IDs and paths are unchanged; opaque extension payloads keep their own keys. Raw input dictionaries remain extensible: additional fields may be consumed by builders without becoming fields of this type.

source
load_locations!(system::AbstractSystem, rel_or_abs_path::String, data::AbstractString)
load_locations!(system::AbstractSystem, rel_or_abs_path::String, data::AbstractVector)
load_locations!(system::AbstractSystem, rel_or_abs_path::String, data::AbstractDict{Symbol, Any})

Factory methods for loading locations from input data and adding them to a system.

ParameterTypeDescription
systemAbstractSystemSystem to add the locations to
rel_or_abs_pathStringRelative or absolute path for data files
dataString/Vector/DictLocation data (ID strings, vector, or dictionary)

Methods

Accessor Methods

Methods for accessing location data and properties.

MethodDescriptionReturn Type
id(location)Get location identifierSymbol

Node Management Methods

Methods for managing nodes within locations.

MethodDescriptionReturn Type
add_node!(location, node, replace=false)Add a node to the location for its commodity typeNothing
refresh_commodities_list!(location)Update the commodities set based on current nodesNothing

System Loading Methods

Methods for loading locations into systems from input data.

MethodDescriptionReturn Type
load_locations!(system, path, data::AbstractString)Load single location from a Location IDNothing
load_locations!(system, path, data::AbstractVector)Load locations and their commodity nodes from strings or objectsNothing
load_locations!(system, path, data::AbstractDict{Symbol,Any})Load locations from dictionary dataNothing

Location Template Internals

The public template_location methods use add_location_template! to insert one entry into an in-memory document. update_location_templates! handles the file lifecycle, and write_json(io, document) handles serialization.

Batch calls build the ID lookup once, use dictionary lookups for duplicate checks, and read and write the file once. Validation and copying scale with input size. Comparing repeated detailed definitions also serializes and parses their JSON; name-only repeats skip that comparison. When using the insertion helper directly in a loop, pass the shared ID lookup to avoid rescanning existing entries.

MacroEnergy.location_templates_by_id — Function
location_templates_by_id(document)

Validate existing location IDs and return an ID-to-entry lookup. Replace the document's location array with a mutable vector that accepts strings and objects.

source
MacroEnergy.location_template_definition — Function
location_template_definition(entry)

Return parsed JSON for comparing definitions across file round-trips, accounting for parsed key types and Symbol values serialized as strings.

source
MacroEnergy.add_location_template! — Function
add_location_template!(document, entry[, locations_by_id])

Validate and copy one location string or dictionary into the document. Preserve existing entries for repeated names or identical definitions; reject conflicts. Pass a shared ID lookup when adding several entries to avoid rebuilding it.

source
MacroEnergy.update_location_templates! — Function
update_location_templates!(update!, loc_file, make_file)

Read or initialize a location document, call update!(document, locations_by_id), and write once after the callback returns. Require an existing file if make_file is false; otherwise allow a new JSON file or directory.

source
MacroEnergy.write_json — Method
write_json(io::IO, data::AbstractDict)

Write formatted JSON to a caller-owned stream, allowing infinite numeric values. Leave the stream open and return nothing.

source

Model Building Methods (Inherited from MacroObject)

Methods used internally during model construction.

MethodDescriptionReturn Type
add_linking_variables!(location, model)Add linking variables to JuMP modelNothing
define_available_capacity!(location, model)Define available capacity constraintsNothing
planning_model!(location, model)Add planning model constraintsNothing
operation_model!(location, model)Add operational model constraintsNothing

Examples

Locations can define their commodity nodes directly in locations.json, or receive independent nodes through each node's location field. Both forms can be used in one system, provided they do not define the same commodity twice at a location. AutoCreateLocations can create a missing location while loading an independent node. AutoCreateNodes can create a missing commodity node when resolving an asset endpoint at an existing location.

Location File

The Location file is a JSON file containing a list of location objects. Each object requires a unique id containing a nonempty string and may include commodities and constraints objects. Location constraints use the existing grouped capacity constraint format. A location name string is shorthand for an object containing only id; both forms are normalized and validated the same way. Its default location is system/locations.json.

For example, these entries mix shorthand names and location objects:

{
  "locations": [
    "boston",
    {"id": "princeton"},
    "new york"
  ]
}

This is not a required file, but it is a convenient way to define locations, their commodity nodes, and location-level constraints.

Defining Commodity Nodes Within a Location

The optional commodities object maps each commodity name to a dictionary of node inputs. An empty dictionary creates a node using the existing node defaults, including BalanceConstraint. Commodity names must already be defined in the system, with time data available; subcommodities are supported in the same way as other node inputs.

{
  "locations": [
    {
      "id": "SE",
      "commodities": {
        "Hydrogen": {},
        "Electricity": {
          "id": "elec_SE",
          "demand": [100.0],
          "supply": {
            "grid": {"price": [30.0], "min": [0.0], "max": [1000.0]}
          },
          "constraints": {"BalanceConstraint": true}
        }
      }
    },
    "MIDAT"
  ]
}

Without an explicit node id, Macro generates <location>_<commodity>, so the Hydrogen node above is SE_Hydrogen. The containing location supplies the node's location field. An explicitly supplied location must match the parent. Node demand, supply, constraints, time series and additional inputs use the existing node-loading pipeline; relative time-series paths are resolved from the system data directory.

System loading creates commodities and time data first, then each location and its commodity nodes, then independent nodes from nodes.json, and finally assets. Assets can connect using either the location ID and commodity or an explicit node ID. Existing location-name strings and nodes.json inputs remain supported and can be mixed with this form.

Time-domain reduction also recognizes each commodity key as node context. Commodity-specific feature weights and exclusions work for these nodes without an explicit type field, using the same series-discovery methods as independent nodes. Both inline series and CSV references retain their original paths when reduced.

Node and location IDs share one namespace within a system: a node ID cannot match another node or any location, and a location ID cannot match another location or any node. A node can belong to only one location, and each location can contain only one node of each commodity. Duplicate definitions across commodities and nodes.json produce errors during creation/registration. Location-level constraints remain separate from the constraints of the individual commodity nodes. Asset-defined node construction is outside this feature.

Explicit nodes in commodities are created regardless of AutoCreateNodes; that setting only controls creation of missing nodes during endpoint lookup.

Programmatic Loading

load_locations! replaces the system's location/node list, then processes entries in order. Each location is registered and its commodity nodes are created before processing the next entry. Errors stop loading immediately; earlier entries are not rolled back. If loading fails, correct the input and reconstruct the system rather than reuse its partial state.

add!(system, node) and add!(system, location) both call validate_network_id before registration. Programmatic builders should register nodes and locations through these methods. add_node! assigns location membership and checks commodity/membership conflicts; it does not register the node in the system or check network ID uniqueness. These checks currently cover nodes and locations, rather than asset or component IDs.

Adding a Node to a Location

Nodes are added to a Location by including the location field in the Node input file. For example, to add Node{Electricity} to Boston and Princeton, you would define it as follows:

{
    "nodes": [
        {
            "type": "Electricity",
            "global_data": {
                "time_interval": "Electricity",
                "max_nsd": [1.0],
                "price_nsd": [5000.0],
                "constraints": {
                    "BalanceConstraint": true,
                    "MaxNonServedDemandConstraint": true,
                    "MaxNonServedDemandPerSegmentConstraint": true
                }
            },
            "instance_data": [
                {
                    "id": "boston_electricity",
                    "location": "boston",
                    "demand": {
                        "timeseries": {
                            "path": "system/demand.csv",
                            "header": "Demand_Boston"
                        }
                    }
                },
                {
                    "id": "princeton_electricity",
                    "location": "princeton",
                    "demand": {
                        "timeseries": {
                            "path": "system/demand.csv",
                            "header": "Demand_Princeton"
                        }
                    }
                }
            ]
        }
    ]
}

During the System generation process, Macro will add these Nodes to the Locations specified in the location field using the add_node! method. If both Boston and Princeton already have Node{Electricity} defined, Macro will throw an error unless the replace parameter of the add_node! is set to true.

Automatically Creating Locations and Nodes

Automatic creating of Locations and Nodes can be enabled by setting the AutoCreateLocations and AutoCreateNodes flags in the system configuration (settings/macro_settings by default).

{
  "AutoCreateNodes":false,
  "AutoCreateLocations":true
}

AutoCreateLocations

Using the example input from the previous section, if we did not have a locations.json input file but AutoCreateLocations is set to true, then Macro will create the Location IDs "boston" and "princeton" automatically when the corresponding Node instances are created.

AutoCreateNodes

Let's consider a second example where a ThermalPower{NaturalGas} Asset has a location field set to "boston" and that Location exists in the locations.json file. Macro will attempt to automatically connect the UnidirectionalEdge{NaturalGas} and UnidirectionalEdge{Electricity} of the ThermalPower{NaturalGas} Asset to the corresponding Node instances in the "boston" location. If one or both of these Node instances do not exist, and the AutoCreateNodes setting is set to true, Macro will automatically create the necessary Node instances for the missing commodity types.

Note that these Nodes will not have any exogeneous demand, supply, constraints or other properties defined. They are simply created to allow the Asset to connect to the Location without errors.

Automatically created nodes use the ID <location>_<commodity>, matching the default IDs of nodes explicitly defined in a location's commodities object.

AutoCreateNodes only creates commodity nodes within an existing Location. If an asset endpoint names neither an existing Node nor an existing Location, Macro throws an error identifying the unresolved endpoint. Check the endpoint ID for typos or define the intended Node or Location before loading the asset.

See Also

  • Nodes: Individual commodity balance points that Locations contain and manage
  • Edges: Transmission and distribution infrastructure that connects nodes across locations
  • System: The overall system container that holds locations, assets, and other components
  • Assets: Energy technologies and infrastructure that connect to locations for simplified modeling