GlobalSeed

model GlobalSeed "Defines global settings for the blocks of sublibrary Noise, especially a global seed value is defined"
    parameter Boolean enableNoise = true "= true, if noise blocks generate noise as output; = false, if they generate a constant output"
        annotation (choices(checkBox = true));
    parameter Boolean useAutomaticSeed = false "= true, choose a seed by system time and process id; = false, use fixedSeed"
        annotation (choices(checkBox = true));
    parameter Integer fixedSeed = 6.7867967e+7 "Fixed global seed for random number generators (if useAutomaticSeed = false)"
        annotation (Dialog(enable = not useAutomaticSeed));
    final parameter Integer seed(fixed = false) "Actually used global seed";
    final parameter Integer id_impure(fixed = false) "ID for impure random number generators Modelica.Math.Random.Utilities.impureXXX"
        annotation (HideResult = true);
initial equation
    id_impure = Modelica.Math.Random.Utilities.initializeImpureRandom(seed);
    seed = if useAutomaticSeed then Modelica.Math.Random.Utilities.automaticGlobalSeed() else fixedSeed;

    annotation (
        defaultComponentName = "globalSeed",
        defaultComponentPrefixes = "inner",
        missingInnerMessage = "\nYour model is using an outer \"globalSeed\" component but\nan inner \"globalSeed\" component is not defined and therefore\na default inner \"globalSeed\" component is introduced by the tool.\nTo change the default setting, drag Noise.GlobalSeed\ninto your model and specify the seed.\n",
        Icon(
            coordinateSystem(
                preserveAspectRatio = false,
                extent = {
                    {-100, -100}, 
                    {100, 100}}),
            graphics = {
                Ellipse(
                    extent = {
                        {-100, 100}, 
                        {100, -100}},
                    lineColor = {0, 0, 127},
                    fillColor = {255, 255, 255},
                    fillPattern = FillPattern.Solid), 
                Text(
                    extent = {
                        {-150, 150}, 
                        {150, 110}},
                    textString = "%name",
                    lineColor = {0, 0, 255}), 
                Line(
                    visible = enableNoise,
                    points = {
                        {-73, -15}, 
                        {-59, -15}, 
                        {-59, 1}, 
                        {-51, 1}, 
                        {-51, -47}, 
                        {-43, -47}, 
                        {-43, -25}, 
                        {-35, -25}, 
                        {-35, 59}, 
                        {-27, 59}, 
                        {-27, 27}, 
                        {-27, 27}, 
                        {-27, -33}, 
                        {-17, -33}, 
                        {-17, -15}, 
                        {-7, -15}, 
                        {-7, -43}, 
                        {3, -43}, 
                        {3, 39}, 
                        {9, 39}, 
                        {9, 53}, 
                        {15, 53}, 
                        {15, -3}, 
                        {25, -3}, 
                        {25, 9}, 
                        {31, 9}, 
                        {31, -21}, 
                        {41, -21}, 
                        {41, 51}, 
                        {51, 51}, 
                        {51, 17}, 
                        {59, 17}, 
                        {59, -49}, 
                        {69, -49}},
                    color = {215, 215, 215}), 
                Text(
                    visible = enableNoise and not useAutomaticSeed,
                    extent = {
                        {-90, -4}, 
                        {88, -30}},
                    lineColor = {255, 0, 0},
                    textString = "%fixedSeed"), 
                Line(
                    visible = not enableNoise,
                    points = {
                        {-80, -4}, 
                        {84, -4}},
                    color = {215, 215, 215}), 
                Text(
                    visible = enableNoise and not useAutomaticSeed,
                    extent = {
                        {-84, 34}, 
                        {94, 8}},
                    lineColor = {255, 0, 0},
                    textString = "fixedSeed =")}),
        Documentation(
            revisions = "<html>\n<table border=1 cellspacing=0 cellpadding=2>\n<tr><th>Date</th> <th align=\"left\">Description</th></tr>\n\n<tr><td> June 22, 2015 </td>\n    <td>\n\n<table border=0>\n<tr><td>\n         <img src=\"modelica://Modelica/Resources/Images/Blocks/Noise/dlr_logo.png\">\n</td><td valign=\"bottom\">\n         Initial version implemented by\n         A. Kl&ouml;ckner, F. v.d. Linden, D. Zimmer, M. Otter.<br>\n         <a href=\"http://www.dlr.de/rmc/sr/en\">DLR Institute of System Dynamics and Control</a>\n</td></tr></table>\n</td></tr>\n\n</table>\n</html>",
            info = "<html>\n<p>\nWhen using one of the blocks of sublibrary <a href=\"modelica://Modelica.Blocks.Noise\">Noise</a>,\non the same or a higher hierarchical level, Noise.GlobalSeed\nmust be dragged resulting in a declaration\n</p>\n\n<pre>\n   <strong>inner</strong> Modelica.Blocks.Noise.GlobalSeed globalSeed;\n</pre>\n\n<p>\nThe GlobalSeed block provides global options for all Noise blocks of the same or a lower\nhierarchical level. The following options can be selected:\n</p>\n\n<blockquote>\n<table border=1 cellspacing=0 cellpadding=2>\n<tr><th>Icon</th>\n    <th>Description</th></tr>\n\n<tr><td> <img src=\"modelica://Modelica/Resources/Images/Blocks/Noise/GlobalSeed_FixedSeed.png\"> </td>\n    <td> <strong>useAutomaticSeed=false</strong> (= default):<br>\n         A fixed global seed is defined with Integer parameter fixedSeed. The value of fixedSeed\n         is displayed in the icon. By default all Noise blocks use fixedSeed for initialization of their\n         pseudo random number generators, in combination with a local seed defined for every instance\n         separately. Therefore, whenever a simulation is performed with the\n         same fixedSeed exactly the same noise is generated in all instances of the Noise\n         blocks (provided the settings of these blocks are not changed as well).<br>\n         This option can be used (a) to design a control system (e.g. by parameter optimization) and keep the same\n         noise for all simulations, or (b) perform Monte Carlo Simulations where\n         fixedSeed is changed from the environment for every simulation, in order to\n         produce different noise at every simulation run.</td></tr>\n\n<tr><td> <img src=\"modelica://Modelica/Resources/Images/Blocks/Noise/GlobalSeed_AutomaticSeed.png\"> </td>\n    <td> <strong>useAutomaticSeed=true</strong>:<br>\n         An automatic global seed is computed by using the ID of the process in which the\n         simulation takes place and the current local time. As a result, the global seed\n         is changed automatically for every new simulation, including parallelized\n         simulation runs. This option can be used to perform Monte Carlo Simulations\n         with minimal effort (just performing many simulation runs) where\n         every simulation run uses a different noise.</td></tr>\n\n\n<tr><td> <img src=\"modelica://Modelica/Resources/Images/Blocks/Noise/GlobalSeed_NoNoise.png\"> </td>\n    <td> <strong>enableNoise=false</strong>:<br>\n         The noise in all Noise instances is switched off and the blocks output a constant\n         signal all the time (usually zero). This option is useful, if a model shall be\n         tested without noise and the noise shall be quickly turned off or on.</td></tr>\n</table>\n</blockquote>\n\n<p>\nAdditionally, the globalSeed instance calls function\n<a href=\"modelica://Modelica.Math.Random.Utilities.initializeImpureRandom\">initializeImpureRandom</a>\nto initialize the impure random number generators\n(<a href=\"modelica://Modelica.Math.Random.Utilities.impureRandom\">impureRandom</a> and\n<a href=\"modelica://Modelica.Math.Random.Utilities.impureRandomInteger\">impureRandomInteger</a>).\nThe return value of this function is stored in parameter <strong>id_impure</strong>. Whenever one of the impure\nrandom number generators need to be called, \"globalSeed.id_impure\" has to be given as input argument.\n</p>\n\n<p>\nNote, the usage of this block is demonstrated with examples\n<a href=\"modelica://Modelica.Blocks.Examples.NoiseExamples.AutomaticSeed\">AutomaticSeed</a> and\n<a href=\"modelica://Modelica.Blocks.Examples.NoiseExamples.ImpureGenerator\">ImpureGenerator</a>.\n</p>\n\n<p>\nPlease note that only one globalSeed instance may be defined in the model due to the initialization\nof the impure random number generators with <a href=\"modelica://Modelica.Math.Random.Utilities.initializeImpureRandom\">initializeImpureRandom</a>!\nSo, the block will usually reside on the top level of the model.\n</p>\n</html>"));
end GlobalSeed;