<commit_before>module Qernel::Plugins
  module Merit
    # Helper class for determining curves for the demand for electricity due
    # to heating in households on graphs which do not use the full Merit/Fever
    # plugins.
    class SimpleHouseholdHeat
      def initialize(graph, curve_set)
        @graph = graph
        @curve_set = curve_set
        @fever_curves = Qernel::Plugins::Fever::Curves.new(@graph)
      end

      # Public: Returns the total amount of demand for the Fever group matching
      # the given +group_name+.
      #
      # Returns a numeric.
      def demand_value(group_name)
        group_producers(group_name).sum do |producer|
          # We use demand * conversion because output_of_electricity requires a
          # calculated graph, which is not always the case when computing
          # time-resolved loads.
          producer.demand * producer.converter_api.electricity_input_conversion
        end
      end

      def curve(group_name)
        electricity_demand = demand_value(group_name)

        consumers = group_consumers(group_name)
        total_demand = consumers.sum(&:demand)

        # Get demand curves for each consumer.
        individual = consumers.map do |consumer|
          @fever_curves.curve(consumer.fever.curve, consumer) *
            (electricity_demand * (consumer.demand / total_demand))
        end

        TimeResolve::Util.add_curves(individual)
      end

      private

      def group_consumers(group_name)
        group_converters_of_type(group_name, :consumer)
      end

      def group_producers(group_name)
        group_converters_of_type(group_name, :producer)
      end

      def group_converters_of_type(group_name, type)
        Etsource::Fever.group(group_name).keys(type).map do |key|
          @graph.converter(key)
        end
      end
    end
  end # Merit
end # Qernel::Plugins
<commit_msg>Prevent NaN,NaN,... curves when Fever demand is zero<commit_after>module Qernel::Plugins
  module Merit
    # Helper class for determining curves for the demand for electricity due
    # to heating in households on graphs which do not use the full Merit/Fever
    # plugins.
    class SimpleHouseholdHeat
      def initialize(graph, curve_set)
        @graph = graph
        @curve_set = curve_set
        @fever_curves = Qernel::Plugins::Fever::Curves.new(@graph)
      end

      # Public: Returns the total amount of demand for the Fever group matching
      # the given +group_name+.
      #
      # Returns a numeric.
      def demand_value(group_name)
        group_producers(group_name).sum do |producer|
          # We use demand * conversion because output_of_electricity requires a
          # calculated graph, which is not always the case when computing
          # time-resolved loads.
          producer.demand * producer.converter_api.electricity_input_conversion
        end
      end

      def curve(group_name)
        electricity_demand = demand_value(group_name)

        consumers = group_consumers(group_name)
        total_demand = consumers.sum(&:demand)

        # Prevent curve consisting of NaN,NaN,... when total demand is zero.
        return TimeResolve::AggregateCurve.zeroed_profile if total_demand.zero?

        # Get demand curves for each consumer.
        individual = consumers.map do |consumer|
          @fever_curves.curve(consumer.fever.curve, consumer) *
            (electricity_demand * (consumer.demand / total_demand))
        end

        TimeResolve::Util.add_curves(individual)
      end

      private

      def group_consumers(group_name)
        group_converters_of_type(group_name, :consumer)
      end

      def group_producers(group_name)
        group_converters_of_type(group_name, :producer)
      end

      def group_converters_of_type(group_name, type)
        Etsource::Fever.group(group_name).keys(type).map do |key|
          @graph.converter(key)
        end
      end
    end
  end # Merit
end # Qernel::Plugins

{'fraction_non_alphanumeric': 0.06982606503655155, 'fraction_numerical': 0.0, 'mean_word_length': 2.623744292237443, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2765055', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1115, 'n_words': 299}