Optimal management of energy sharing in a community of buildings using a model predictive control

Exporting generated electricity by on-site renewable energy systems from buildings to the grid is only slightly profitable in many countries. Therefore, it is required to investigate the benefits of sharing generated energy in a microgrid within a community of buildings. Exploiting the benefits of peer-to-peer energy exchange between prosumers in a community can make the best use of the on-site generation while reducing their bills. This study elaborates the potential of energy management to minimize the electricity cost of a community consisted of multiple buildings and connected to a microgrid. To implement this, an energy management system is designed based on non-linear economic model predictive control and successive linear programming for sharing the onsite surplus generated electricity between the buildings in the community. Four buildings are simulated and studied as an example of a small community. These buildings are dissimilar in their age, thermal mass, insulation, heating system and on-site renewable energy systems. It is shown that considering the community of buildings as a single entity, the novel model predictive control can be efficiently used for minimizing the energy cost of the community that has various sources of energy generation, conversion and storage, including significant non-linear interactions. Three different scenarios of the energy management system for the studied community are investigated, and the results indicate that the annual electricity energy cost for single buildings can be reduced by 3.0% to 87.9%, depending on the building and its systems, and by 5.4% to 7.7% on the community


Introduction
Buildings are responsible for approximately 40% of energy consumption and 36% of carbon dioxide emissions in the European Union Countries [1,2]. Bearing in mind the increasing demand for energy and the need to diminish the dependence on fossil fuels, there has been increased attention towards renewable sources of energy [3,4]. Among other topics, significant research has been conducted on: improving the solar heating factor of a community through seasonal storage [5]; prosumers that export surplus heat to the district heating grid [6]; the economic competitiveness of microgrids and how they are affected by policies [7]; the energetic and economic potential of heat and electricity prosumers exporting to the distribution grids [8,9] or as part of a community [10] and life-cycle optimizations for embodied and operational emissions of buildings [11,12]. Overall, the results of the studies show that there is massive potential to improve the sustainability of the built environment through several strategies.
The energy management strategies that aim to reduce the demand by the end-user in coordination/collaboration with the utilities are categorized as demand-side management (DSM) [13][14][15]. Among the different DSM methods applied in buildings, demand response pursues optimal matching between the dynamics of energy demand and supply [16,17]. Demand response has sparked vast interest in the field due to its multiple advantages for both the end-user and the grid operator: for the former, demand response allows reducing the expenses on energy by shifting loads to times of the day when prices are lower [18] for the latter, demand response allows reducing the peak loads and distributing the energy demand to times when the grid is less saturated [19,20]. Nevertheless, the implementation of this strategy requires a detailed investigation of its effect on the comfort level inside the building: if this aspect is overseen, the indoor temperature might be undesirable [21,22].
Localized renewable energy generation can contribute to lowering the energy demand in buildings. This can be achieved through energy generation components such as photovoltaics (PV) [23,24] and wind turbines [25] for electricity, solar-thermal panels [26] for heat, microcombined heat and power (micro-CHP) [27] for both electricity and heat, and through energy storage components such as batteries and hot water storage tanks (HWST). Low carbon, efficient and reliable energy (electricity and heat) supply is one of the key obligations for nextgeneration smart cities. The close proximity of multiple energy vectors, for example, electric power and heat, introduces opportunities for energy systems integration and real-time energy management of multiple energy vectors [28].
Nowadays, cities are facing several environmental problems as a result of the population migration to urban areas, which is causing urban sprawl. As a solution, a community of buildings features increased landuse efficiency [29]. In different countries, a different community of buildings, for example, apartments, detached houses, semidetached houses etc., are raising and becoming fashionable. Also, the building types in terms of building physics and technology, for example, may not be the same. Small scale energy cooperatives can be established to share generated electricity from one building owner to another [30]. This orientation may be financially beneficial for each building owner. Moreover, backup capacity requirements and overproduction are reduced having small scale energy cooperatives [31]. Small scale, decentralized projects are being implemented by different performers in the sector. At present, developed countries are making various efforts to promote polycentric and decentralized energy supply concepts to achieve an efficient energy transition [32,33].
Building communities where on-site generation is shared to improve the overall energy balance of the community have been gaining attention in recent years. Energy sharing between prosumers and consumers, sometimes called Peer-to-peer (P2P), has the potential to improve the system energetic and economic performance in several ways, although not without challenges. Long et al. [34] studied the performance of P2P sharing in a microgrid utilizing an energy sharing coordinator. They conclude that, through dynamic pricing based on the supply-demand ratio, every individual consumer and prosumer can be better economically. Pires Klein et al. [35] developed a P2P energy sharing business model for the Portuguese market. While they successfully trialled it in three pilot projects and reached financial benefits for the end-users, the authors stress the need to update the current market structures and regulatory frameworks. Amaral Lopes et al. [36] investigated the enhancement of load matching between net zero energy buildings by creating a Cooperative Net Zero Energy Community. They outline that the key factors for improving the load matching are load heterogeneity, number of controllable devices, and higher amount of available energy to satisfy demand. Hirvonen et al. [37] present a case study where the office and residential buildings share CHP heat and electricity surplus generation, albeit with individually prioritized controls. They found that primary energy consumption can be significantly reduced, but joint coordination might further improve the performance. Genku et al. [27] investigated the sharing possibilities of CHP generation between four different types of non-residential buildings in Japan: an office building, a hotel, a hospital and a shopping centre. They found that the operation strategy of the CHP and the types of buildings being combined have a strong influence on the advantages of energy sharing. Liu et al. [38] argue that PV prosumers can improve their economic performance by sharing among neighbouring prosumers than by operating independently. They formulated a dynamic internal pricing model that is based on the supply and demand ratio of shared energy and showed that all PV prosumers in the study are better off sharing than selling to the grid.
At the local level, increasing distributed energy resources requires that centralized energy systems be re-organized. Koirala et al. [39] presented the principle of integrated community energy systems as a modern development to re-organize local energy systems to integrate distributed energy resources. This concept must be accepted by different effective actors such as local governments, communities, energy suppliers and system operators to achieve sustainability and thus they will have increasingly significant roles in future energy systems. Also, Cai et al. [40] developed an inexact community-scale energy model for planning renewable energy management systems under uncertainty. They obtained interval solutions associated with different risk levels of a constraint violation, which can be used for generating decision alternatives and thus help decision-makers identify desired policies under various economic and system-reliability constraints. Del Río et al. [3] developed an integrated theoretical framework which allows a complete analysis of the impact of renewable energy on local sustainability and which can be empirically applied to identify these benefits in different communities. According to the literature review, there is no technical reports and scientific articles to elaborate energy sharing concept and results through a community of buildings.
There is a great deal of interest in using model predictive control (MPC) for buildings to optimise their performance, for instance, by reducing heating energy consumption [41]. Furthermore, as MPC allows to model future control scenarios and optimise the outcome, there is a great potential for linking and optimising the use of energy storage within buildings using this control strategy. Economic MPC is an established methodology for the management of both demand and supply sides of energy systems [42]. Ruusu et al. [43] presented a new energy management system (EMS) for a variety of energy flexibility conversion, routing and storage options in buildings. They used an efficient non-linear optimization-based MPC method, which requires low computational time by utilizing successive linear programming (SLP) for continuous approximations of discrete (two-level) control problems. As above-mentioned, EMSs have been applied and focused on buildings equipped with or without on-site energy systems. An outcome is that exporting surplus electricity from a building to a grid is not costeffective in some countries [44]. This research elaborates on a novel attitude for the concept of energy sharing in a microgrid in a community of buildings and assets by implementing an advanced energy management system. The novelty and objective of this research work can be highlighted and summarized as: • Developing and implementing an upgraded MPC model from [43] for applications to a community level where the potential and impact are much higher. The community is consisted of four buildings and shaped by various buildings, for example, with different characteristics of age, envelope construction, various sources of energy generation, conversion and storage, as well as interactions with the central electricity grid. • The objective is minimizing the cost of the purchased electricity from the grid by optimizing the energy flow between the buildings in a microgrid in the community. • Investigating and comparing the results of three scenarios for energy management when sharing the generated electricity in the microgrid.
The topic of optimal management of energy flow between buildings in a community and their interaction with the central energy grid when integrating on-site renewable energy has high importance nowadays since communities and cities have ambitious plans to become costeffective and carbon-neutral.
This paper is organized as follows. Section 2 introduces the studied buildings and assets in the community of buildings. Section 3 presents the concept of the developed and upgraded EMSs. Section 4 shows the results and discussion of the EMS simulations in a community of buildings, and conclusions are drawn in Section 5.

Studied buildings in a community of buildings
Beyond individual building energy management systems, expanding the control to a community level is worth studying and analysing. Fig. 1 shows a schematic of three communities of buildings connected to the grid, where it is assumed that a district can be composed of several communities. This study aims to optimize energy performance in one community by minimizing the imported energy cost. The advantage is to mitigate peak energy imports from the grid at a higher level since several buildings in a community can minimize the total cost of energy import.
As a case study, the studied community of buildings consists of the following four buildings, presented in the previous research articles, representing different specifications of single-family houses in Finland: [45][46][47] The EL building is a model of a residential nearly-zero energy building implemented in a semi-virtual emulator platform at Aalto University in Finland. It has a net floor area of 150 m 2 , adheres to the Finnish Building Regulation [48] and is assumed to be occupied by four people. The emulator platform consists of the simulated virtual building, a real energy generation and storage system including PV panel (4.32 kWp), solar-thermal (8.6 m 2 ), roof-mounted wind turbine (4 kW), ground source heat pump (GSHP), (4.5 kW), electric battery (20 kWh) and HWST (500 l), in addition to a computational infrastructure to connect, measure and control the system's operation. Moreover, the airtightness of the EL building adheres to the strictest building code regulation in Finland. The HWST is used for both the space heating demand and domestic hot water (DHW) demand. The heat delivery system in the building is a hydronic radiator heating system. Further detailed information about the EL building can be found in [45][46][47]. [8,9] The VI building is a real single-family house built as a joint pilot project between Fortum Company and ISOVER Company in Finland. Its purpose was to measure the real performance of a highly energyefficient single-family zero-energy house in the Finnish context. The two-storey building is located in Hyvinkää, Southern Finland, and is inhabited by a family of four. The building combines on-site renewable energy generation of electricity, heat and storage including PV panel (9.36 kWp), solar-thermal (6 m 2 ), GSHP (6.3 kW) and HWST (750 l), with high insulation levels. As well, the airtightness of the VI building is similar to the EL building. The HWST is used for both the space heating demand and DHW demand. The heat delivery system in the building is a hydronic radiator heating system. Further detailed information about VI building can be found in [8,9]. [21,44,[49][50][51] The MP is a model building, which represents a highly insulated building with large thermal mass. The building's floor area is 180 m 2 and has four occupants. All the walls of buildings are lightweight concrete, but the roof and the intermediate and base floor are of massive concrete. The thermal insulation level of the MP building is comparable to the VI building. The thermal insulation level and airtightness of the house follow the Finnish guidelines for passive houses [52]. There are no on-site renewable energy systems; instead, the building is equipped with a gas-operated domestic-scale micro-CHP, which has a full capacity of heat and power of 9 kW and 3 kW, respectively. There is a HWST (500 l), which is used to cover both the space heating demand and DHW demand. The heat delivery system in the building is a water-based underfloor heating system. [21,44,[49][50][51] In contrast with the above-mentioned highly energy-efficient buildings, the LW building is a model for an older building with poor insulation. The LW building dates from the 1960s and its structures are wood frame constructions. The floor area of the building is 180 m 2 and occupied by two people. The LW building's thermal insulation is based on a typical level Finnish detached house built in the 1960s following the Finnish energy certificate [53]. Also, the airtightness of the LW building is based on the default value of the Finnish energy certificate [53]. The HWST (300 l) is only used for DHW consumption and is heated using an electric heating element. The heat delivery system in the building is direct electric radiators in the rooms. Table 1 shows the key parameter data of the above-mentioned buildings. Besides, the buildings' envelope specifications are presented in Table 2. These diverse buildings are selected to analyse how each building as an individual and as part of a community will behave under the EMS. Input time series for the electricity, heat and DHW demand of each building have been generated using two building performance simulation programs, while the buildings' energy systems are dynamically simulated in MATLAB.

Energy supply, distribution and utilization
As described above, the four buildings have distinct characteristics regarding heat and electricity generation capacities. Therefore, the implementation of an efficient control system requires identifying the possible ways to supply, exchange, store and utilize the energy within the local microgrid. Fig. 2 shows the community of the four studied buildings organized to buy/sell electricity from/to the centralized grid, and store and share electrical energy in the community. The Figure shows three EMS control strategies (individual building, unplanned sharing and planned sharing between the buildings), which is described later in subsection 3.4.
The on-site generated electricity is assumed to be exchanged between the buildings in the microgrid without any losses. While electricity generation is available in the three highly-efficient buildings (EL, VI and MP), only the EL building has electric storage capacity, and its battery can be charged by electricity provided by any of the generation units in the microgrid that would otherwise be sold to the grid. Based on the previous researches, for example [47,50], the effect of storage heating energy by the thermal mass was small compared with the effect of the energy system components. Thus, the influence of thermal mass on energy cost, for example, is not re-investigated in this research.
All buildings except the LW building use a double-compartment HWST, with an upper compartment that is kept at a minimum temperature of 60 • C to supply DHW and a lower compartment that is kept at a minimum of 40 • C for the low-temperature hydronic heating system.
The LW building has a single-compartment smaller HWST for DHW use only since space heating is done by electric radiators inside the building. An electric heating element is used in all DHW compartments to keep the minimum required temperature.
The EL and VI buildings are equipped with GSHP that provide the bulk of heat for space heating and preheat the city water flowing to the DHW compartment. Furthermore, these two buildings include solarthermal panels to complement the GSHP and reduce the electricity required by the heating element. The bulk of the heat in the MP building is produced by the natural gas-fuelled micro-CHP with a total capacity of 12 kW. The micro-CHP is an internal combustion engine that can increase the water temperature of the HWST up to 90 • C. The MP building also includes an electric heating element to assist in the DHW preparation. The temperature of hot water at the tap provided by the DHW tank is 55 • C [54] for all buildings. More information about the DHW temperature is presented in Section 3. The supply water temperature for the hydronic heat distribution system is controlled according to the outdoor temperature, where the dimensioning of the water supply/return temperatures are 40/30 • C at an outdoor design temperature of −26 • C. The indoor air temperature setpoint for heating is 22 • C for all buildings.
Contrary to electricity, heat is not shared within the microgrid. Thus, depending on the building, each must cover its own heat demand locally by using electricity, solar-thermal or gas. Electricity may come from onsite generation components, from a generation component within the microgrid, or from the centralized grid.

Model predictive control (MPC)
The applied MPC in this research is a development of that in Ruusu et al. [43] expanded for a community of buildings. The economic MPC  method for controlling the devices in these simulated buildings is implemented using a modelling framework built on the MATLAB Optimization Toolbox. The proposed EMS framework consists of ideal linear units, augmented with custom non-linear features to model significant nonlinearities arise from temperature-dependence of solar-thermal collector output and DHW heat exchangers connected on series. In this framework, generated energy demand data by the above-mentioned building simulation tools defined as input data to the framework. Meanwhile, the non-linear MPC optimization utilizes SLP to an optimization problem with a linear objective function and non-linear constraints. The SLP is a method that iteratively solves local linear approximations of a nonlinear optimization problem. To use the SLP method, all decision variables need to be continuously differentiable to form a locally valid linear approximation. This framework was computationally efficient enough for a full-year simulation in a reasonable computational time of about two days in [43].
The optimization solution is a set of time series for the energy nodes, energy devices and boundary flows. The optimization problem solves the system's control inputs and the resulting states during that time window by repeating at regular intervals, considering both the current state of the system under control and revised forecasts for the boundary flows. The control input values for the initial part of the time window are implemented as actual control inputs. The optimization problem can be detailed using decision variables with the states of the storage units. The vector of decisions variables in the optimization problem consists of the following variables: where x(t) represents a vector of the states of each node with non-zero storage capacity. In this way, q(t) represents a vector of average energy flows in the connections of the system between times t and t + 1 presented in: and u(t), presented in Eq. (3), represents a vector of constant control inputs to the energy conversion devices between times t and t + 1.
These vectors combined form the decision variables of the optimization problem. The objective function is a sum of the flow rates into/ out of the energy nodes that are assigned with a cost, such as the grid connections or fuel tanks, multiplied by the associated cost factors. The details of the mathematical method of the model predictive control optimization were precisely presented in our previous paper [43].
Energy storage units in the optimization problem are represented using ideal storage units with a state that is a direct integral of net energy inputs/outputs. For the HWSTs that combine space heating and DHW, each tank is represented by two such units. DHW heating from these tanks uses two series-connected heat exchanges in both tank compartments, as shown in Fig. 3, which creates strong non-linear constraints in the optimization problem. This Figure presents the connections of the HWST for the EL, VI and MP buildings. The LW building has only a single-compartment DHW tank fitted with an electric heater.
Some discrete limits on the problem, such as the limitation of the GSHP operation at times during which the heat tank is below 60 • C, are managed using a continuous sigmoid approximation to an ideal threshold.

Controllable assets and parameters
The MPC has a wide variety of controllable variables that allow it to optimize energy management in the buildings. These variables consist of the operation of the main and auxiliary heat generation components (GSHPs and auxiliary DHW electric heaters), charging and discharging of the heat and electricity storage and operational setpoint temperatures, as well as import and export to the electric grid. These variables provide the MPC with the ability to influence the supply of electricity used for heating in each building. Fig. 4 indicates the available assets that can be used in the studied buildings. Besides, Table 3 summarizes the controllable assets and parameters in each building of the microgrid.

Building simulation tools
The study is entirely simulation-based. The two-building simulation tools (TRNSYS and IDA-ICE) comprehensively tested and validated are used in this research. This section specifies the used building simulation tool for each studied building, the level of accuracy for each tool, and simulation models for the energy systems in all four buildings.
The studied EL and VI buildings were presented and used in [8,9,43,47], and the MP and LW buildings in [21,[49][50][51]55]. The EL and VI buildings were simulated using the TRNSYS software, and the MP and LW were simulated by IDA-ICE software to calculate their energy demand.
TRNSYS and IDA-ICE have been extensively validated in [56-60], and in [50,[61][62][63][64], respectively. For example, Loutzenhiser and Manz [65] validated IDA-ICE and TRNSYS by investigating a model shaped shading, daylighting and load interactions in Annex 43 and presented inaccuracy of air temperature<1%. EQUA Simulation AB [66] studied validation of IDA-ICE by different test cases in terms of building and system characteristics, and range of accuracy was between 90 and 99%. The test case closed to this research had over 98% accuracy. J. Axaopoulos et al. [67] presented the accuracy analysis of TRNSYS by studying a model used photovoltaics, and they found 99.7% accuracy. Mazzeo et al. [57] evaluated the prediction accuracy of IDA-ICE and TRNSYS by means of a comparison of the simulated results and the experimental measurements detected under real operating conditions. They showed that both building performance simulation tools lead to the high overall accuracy in all periods. Therefore, these are befitting tools for simulation of energy consumption, indoor air quality and thermal comfort in buildings. They can be used for a variety of applications, such as integrated energy technologies, thermal models and airflow network. The simulation models for the energy systems in all four buildings are implemented in MATLAB, and the model was validated in [43]. The simulation models use a Shepherd model for the voltage-current relationship. The optimization of the above-mentioned systems is employed every 0.1 h (6 min). This time step is considered to allow for more interactive performance of the battery and HWST. It also proved to follow the operation of the real components of the emulator platform [43]. At each time-step, the performance of the system in the next 24 h is optimized in 0.1 h intervals. The actions found by the optimization for the first time-step in the 24-hour window are derived as control inputs to the simulation model for the next 0.1 h period. More information about the connection between the simulation and optimization models can be found in [43].

Hot water storage tank (HWST)
A stratified HWST model was used in this study that consists of five nodes for each tank. The HWST is modelled as ordinary differential equations and solved using a variable time step solver. Further detailed information about the used HWST can be found in [46,47]. Fig. 3 shows the fully equipped schematic of the HWST, where micro-CHP is only installed in the MP building.
The minimum temperature levels in the lower and the upper parts of the HWST are 40 and 60 • C, respectively, corresponding to lower operating temperature for the water supply to the space heating and the upper limit of GSHP performance. Heat generation by the two GSHPs in the EL and VI buildings is controlled by switching between two temperature setpoints that represent the lower and upper limits of the tank temperatures depending on the power of the heat generation unit. The power is rounded from the continuous approximation (0 to 100%) used in the optimization model into a two-level signal (0 or 100%), using a threshold with a small hysteresis (50%±10%) to avoid rapid flickering around 50%. Temperature setpoints for the heating elements are directly acquired from the heat storage states estimated by the optimization problem solution.

Micro combined heat and power (micro-CHP)
The micro-CHP device in the MP building is an internal combustion engine that operates with natural gas and is based on a product of Vaillant mini-cogeneration systems ecoPOWER [68] with 90% overall efficiency. It is a semi-continuous unit with a heat to power ratio of 3/1 and a maximum generation of 9 kW/3 kW, respectively. The assumed power in this study is adjustable between 50 and 100% of the nominal output rating, due to the minimum rotation speed of its internal combustion engine, while a linear power range from 0 to 100% is approximated in the optimization model. The linear power from optimization (P o ) is converted to power in the simulation (P s ) as follows:  (Tables 1 and 3 indicate the assets' specifications and controllable parameters, respectively). The maximum micro-CHP generation is limited by its full power capacity. It operates according to the heat-tracking strategy and follows the above-mentioned control steps.

Input data
Weather data for the energy generation and consumption calculations are based on a time series for the year 2015 from the Finnish Meteorological Institute for a location in the greater Helsinki region in Finland as shown in Fig. 5. Namely, the weather data required by the system are: outdoor temperature, wind speed, and total solar insolation. This data is fed to the building simulation models in TRNSYS and IDA-ICE to calculate the electricity and heat consumption in the four buildings, and it is used by MATLAB to calculate the generation by the on-site generation components. In addition to this annual weather data, the EMS receives an artificial weather forecast every hour for the next 24 h, also consisting of temperature, wind speed and solar insolation. This data is created by an ad hoc weather forecast generator that adds noise to the actual weather data and is used by the EMS to calculate the forecasted generation by the wind turbine, PV panels and solar-thermal panels, as well as for predicting the heating demands in the buildings.
The electricity market price from NordPool [69] for the next 24-hour window is assumed to be available in the simulation. This information is needed by the EMS to optimize the use of electricity based on its cost dynamics where imports should be avoided when prices are high, and exports should be avoided when prices are low. However, the EMS must move ahead or postpone heat preparation without compromising indoor thermal comfort.
The used electricity price, retail price, purchased from the grid is calculated by the Finnish hourly market price, which includes the transfer price and taxes while the exported electricity price to the grid is equal to the market price. The used electricity price for the year 2015 is shown in Fig. 6 while an energy tax (including VAT) and a transmission fee are 2.793 and 3.980 cent/kWh, respectively. The price for electricity sharing between the buildings is assumed to occur at halfway between the grid import and export prices. The price for gas is assumed to be constant throughout the year at 8.65 cents/kWh.

Energy management scenarios
As exporting generated electricity by on-site renewable energy systems from buildings to the grid is not economically attractive due to the disparity between purchased and exported electricity prices in Finland, new approaches should be proposed for energy management in buildings that have on-site renewable energy systems. This study proposes sharing the on-site surplus generated electricity between the buildings in the community.
This research implements the developed EMS and studies three different energy management scenarios to handle the building electricity demands as outlined in Fig. 2 a, b and c: I) In the first scenario, each building has its own EMS, which interacts separately with the centralized grid, so there is no energy sharing with the other ones; this scenario is named individual building. II) In the second scenario, each building has its own EMS, which interacts separately with the centralized grid, but after that it allows sharing the surplus energy with the other buildings; this scenario is named unplanned sharing. III) In the third scenario, there is one common EMS for the community that allows unrestricted sharing of the electricity between the four buildings; this scenario is named planned sharing. It is obvious that the EMS has the lowest initial costs [43,55] in the community; thus, the initial investment has not taken into account in this study.

Results and discussion
The performance of the EMS is presented and discussed in this section. The assessment is illustrated from a short period (a week) to the annual level. First, the detailed behaviour of the buildings during one representative week is presented. Then, the energetic and economic performances under the three energy management scenarios are shown, followed by the economic behaviour of the buildings in the community.

Individual buildings and community performance during a representative week
This subsection presents the behaviour of each building and the whole community during a representative week in April (days 95 to 101 of the year) under the planned sharing energy management scenario. This week is selected since it includes significant amounts of heating demand, and solar and wind energy generations. In Figs. 7-10, the behaviour is presented in terms of the following parameters: electricity price, which is the main factor to determine the performance of the MPC; the battery state-of-charge, which is a result of the charging and discharging cycles of the battery; electricity surplus, which is the difference between the exported and imported electricity from the building; temperature of the top and bottom layers of the hot water storage tank produced by the stratified tank model; the electricity generated by the photovoltaic and wind; and the heat produced by the electric domestic water heater, heat pump, solar-thermal panels and micro-CHP. Fig. 7 shows the behaviour of the key variables of the EL building. While the battery is only used for covering the local demand of the EL building, its cycles are managed for the common good of all four buildings. The Figure also shows that the heat and electricity storage components are mostly charged at times when solar insolation is abundant, thus via generation by the PV panels and solar-thermal panels. Notably, charging of the battery is delayed to coincide with temporary low points in electricity prices to maximize the cost savings. Another cost-saving action by the MPC can be found in the second half of day 95: despite operating the GSHP on several occasions, the system delays discharging the battery so it can reduce imports during the highprice period on day 96. Such behaviour can also be noted on other days. On a general level, it can be noted that the MPC avoids importing electricity (indicated by the negative values in the surplus electricity graph) in the high-price times during the week.

The EL building
For this building, the electricity surplus does not seem to be utilized by the electric heating element because the solar-thermal panel supplies enough heat to the HWST. Fig. 8 shows the behaviour of the key variables of the VI building. Due to its larger HWST and PV panels compared to the EL building, the VI building has more electricity surplus in the daytime. Compared with the EL building, the behaviour of the VI building is characterized by the performance of its solar-based energy generators (PV and solar-thermal) since it has no wind generator. The surplus generated electricity is used by the GSHP to increase the water temperature in the HWST in days 97-100, but only up to the point of actual demand. The electric heating element is used simultaneously with the solar-thermal generation to use the surplus from PV. This surplus utilization is also timed to maximize cost savings. Fig. 9 shows the behaviour of the key variables of the MP building. This building is not equipped with any renewable energy systems, but it uses a micro-CHP device to produce heat and electricity from natural gas. The micro-CHP device operates in the community besides electricity generation from the PV panels and wind turbine of the other buildings. This provides some extra electricity surplus in the morning when the electricity price is already high, but generation by PV panel is still low. A second short burst occurs in the evening when both local electricity demand and DHW consumption are at their highest. Unlike the morning peak, the evening peak does not raise the temperature in the HWST, due to the high DHW demand. The electric heating element is also applied during the daytime to make use of some of the surplus from the other buildings. In general, the temperature rise in the lower layer of the HWST is due to the micro-CHP operation, while it is mainly due to the electric heater operation in the upper layer, as a result of their connections as shown in Fig. 3. Fig. 10 shows the behaviour of the key variables of the LW building. As the building is old and has poor insulation, it has high electric heating demand, especially during the night. The HWST is mostly heated up based on monitoring electricity prices in the morning, and since the HWST volume of this building is small (300 l), storing heating energy does not appear to assist significantly. The tank is used for covering the DHW demand only and is heated by the electric heater; the two layers of the tank do not show a significant difference in temperature. Fig. 11 shows the behaviour of the key variables of the community of buildings. The consumed and generated electricity are independent terms. The MPC selects the best decision on how to deal with the energy status of the community systems as a whole taking into consideration the forecast of energy demand and generation within the sliding prediction window. The term "Consumed electricity" is consisted of the "Imported electricity", which is purchased electricity from the microgrid, and "Notimported consumed electricity", which includes all electricity consumption not imported by the community. This latter is consisted of all electricity consumption covered by the on-site energy generations and storage systems. The energy flow balance is satisfied meaning that the "Generated electricity" is distributed to the "Not-imported consumed electricity", "Exported electricity" and "Charging battery". For example, at noon of day 97, the Generated electricity is 15.6 kW, Exported electricity is 2.4 kW, Imported electricity is 0 kW, Not-imported consumed electricity is 6.1 kW, and Charging battery is 7.1 kW. The MPC aims to avoid electricity imports at high-price times. Table 4 presents the results of the energetic behaviour of each building under the three energy management scenarios. It shows how each building is involved in each EMS scenario.

Annual energetic and economic performances under the management scenarios
Observations from Table 4 indicate the following: • The EL building purchases less electricity for the operation of its GSHP and electric DHW heater compared with the VI building. This is explained by its higher installed on-site energy generation capacity. • In the individual building and unplanned sharing scenarios, each building has its own EMS. Thus, there is no difference in the GSHP outputs, electric DHW heater demands, electricity stored in the battery and micro-CHP electricity output between these two scenarios. However, in the planned sharing scenario, as the community of buildings is controlled by one EMS, the GSHP outputs are higher, and the electric DHW heater demands drop for the EL, VI and LW buildings. In the MP building, the electric DHW heater demand is higher because the micro-CHP electricity output does not cover the whole electricity demand since it is the only electricity generator in this building. • The battery stores more electricity in the planned sharing scenario since it can be charged by any other generation unit in the other buildings in the community. • The EL building is the only one to import more electricity from the grid in the planned sharing scenario than in the individual building scenario. This is because the priority is to share its own generated electricity with the other buildings, and this leads to temporary energy deficits in the EL building itself.  • The exported electricity to the grid/ microgrid shows the same trend as the imported electricity from the grid/ microgrid. • The EL building imports and exports more in the planned sharing scenario than in the unplanned sharing scenario with both the grid and microgrid. Fig. 12 presents the cumulative curves of the total cost of electricity and natural gas for the community with the three operational scenarios. The slope of the electricity cumulative curves in Fig. 12 shows higher cost in winter (on the two ends of the Figure) and lower cost in summer. This is also valid for the natural gas cost but on a smaller extent, which can be recognised when making a closer look at the Figure, due to the control strategy of the micro-CHP and its limited capacity. The gas consumption is the same for the individual building and unplanned sharing scenarios, which is why only one is shown in Fig. 12. The difference between the individual building and planned sharing scenarios in the cost of natural gas is insignificant as the shared portion of generation by the micro-CHP is small.
The results are also presented in Fig. 13 with separate curves for each building with the individual and planned scenarios. Figs. 12 and 13 show that the planned sharing scenario is more cost beneficial in the annual electricity cost compared specifically with the individual building scenario. The reason is that the generated electricity within the microgrid can be shared with other buildings in the community at lower prices. The gas consumption is the same for the individual building and unplanned sharing scenarios, which is why only one is shown in Fig. 12.
The difference between the individual building and planned sharing scenarios in the cost of natural gas is insignificant as the shared portion of generation by the micro-CHP is small. In the planned sharing scenario, the EL building purchases electricity from the grid occasionally. Fig. 13 shows that the EL building gets the lowest electricity cost saving in the planned sharing scenario compared with the individual scenario, whereas the LW building gets the largest saving. Therefore, a more advanced internal pricing mechanism is needed in the microgrid to manage a fair sharing of the achieved savings when implementing a community level EMS. The above behaviour is indicated in Table 5, which presents the operating energy costs for the three EMS scenarios. The EL building has the lowest energy cost as it has more on-site renewable energy generation components. It has lower costs compared with the VI building because it is equipped with a wind turbine as well, which can generate electricity during more times of the year compared to generations based on solar energy (PV panel and solar-thermal panels). Additionally, storing energy in the battery affects significantly. The EL building takes less benefit from its own on-site energy systems in the planned sharing scenario and thus has a higher building energy cost compared with the unplanned sharing scenario. The VI and MP buildings have similar insulation, but the on-site energy systems of VI building significantly diminish the purchased electricity from the grid. In the MP building, the EMS takes into account the energy generated according to the micro-CHP strategy and decides how to handle any surplus electricity. Due to the non-existence of any on-site energy generation and the low insulation level in the LW building, its energy cost is significantly higher than the other buildings in the community. Annual monetary savings per building is between 58€ to 114€ when optimized from the individual building scenario to the unplanned sharing scenario, and between 52€ and 177€ when optimized from the individual building to the planned sharing scenario. On a single-building level, the energy cost is reduced with respect to that for the individual building scenario by 87.9% and 78.8% in the EL building, 21.1% and 37.6% in the VI building, 4.7% and 6.2% in the MP building, and 3.0% and 4.6% in the LW building by the unplanned and planned sharing scenarios, respectively. On the community level, the energy cost drops by 5.4% and 7.7% by the unplanned and planned sharing scenarios, respectively, with respect to that for the individual building scenario. The use of the advanced MPC in the three scenarios is the reason for the small relative reduction in the energy costs.
The building energy import in the individual building and unplanned sharing scenarios are the same since each building has its own EMS. Using the planned sharing scenario reduces the building energy imports. On a single-building level, the planned sharing scenario decreases the building energy import by 1.5%, 1.9%, 0.3% and 0.4%, for the EL, VI, MP and LW buildings, respectively. Meantime, on a community level, the building energy import is reduced by 0.8%; however, the objective is to reduce the energy cost, not the energy itself.

Conclusions
This study presents the results of the implementation of a developed energy management system in a community of buildings with the aim to minimize the operational energy cost. This is conducted by a non-linear model predictive control based on successive linear programming to optimize the cost savings of a community of buildings. Based on the weather forecast and upcoming electricity market price, the model predictive control determines at each time step the optimal flow of electricity in each component of the system, as well as the optimum exchange of electricity between the buildings within a microgrid in the community and with the centralized grid.
The studied community consists of four different buildings in terms of envelope insulation, thermal mass, on-site renewable energy and heating systems. This research implements the energy management system on the community of buildings using three different energy  management scenarios (individual building, unplanned sharing and planned sharing) that can handle the building's electricity demands by self-consumption, and sharing and selling the generated electricity within the microgrid and with the centralized grid. It is shown that one energy management system can be efficiently used in the planned sharing scenario for optimizing the operation of the whole community of buildings towards the objective of minimizing the energy cost for the community.
The method can provide monetary savings when using the unplanned and planned sharing scenarios compared with the individual building, respectively, in the community of buildings between 5.4% and 7.7%, and for single buildings between 3.0% and 87.9% depending on the building and its systems. The community planned sharing scenario produces more savings than the unplanned sharing scenario. Moreover, the building energy import slightly decreased on the community level.
The results indicate that the monetary efficacy of the successive linear programming solution method is not negatively affected by the number of optimized buildings. It is shown that optimizing the community of buildings as a single entity can result in decreased individual savings for high-efficiency buildings and increased individual savings for low-efficiency buildings. This is noted in the study where the highest savings are perceived by the building with the least investments in energy efficiency (the old building in the community). Sharing locally generated electricity can be economically beneficial when high selfsufficiency of a community is targeted.
Despite that the current study was conducted for cost minimization, the sharing concept and the developed model predictive control are also applicable for other environmental targets on a community level, e.g. minimization of energy consumption or CO 2 emissions.
Insights of the benefits gained from peak load reduction when there is large scale implementations of the concept of energy sharing in districts and cities need to be evaluated from the central grid perspective and not only from the buildings perspective. Furthermore, local/national legal and regulation's obstacles for energy sharing between buildings should be removed if higher benefits are to be reached.

Declaration of Competing Interest
The authors declare that they have no known competing financial interests or personal relationships that could have appeared to influence the work reported in this paper.