Skip to content

Commit

Permalink
r.surf.fractal: Added seed option (#5233)
Browse files Browse the repository at this point in the history
  • Loading branch information
NishantBansal2003 authored Mar 4, 2025
1 parent b4d1b97 commit 2262aaa
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 6 deletions.
30 changes: 27 additions & 3 deletions raster/r.surf.fractal/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ double H; /* Hausdorff-Besickovitch dimension. */
int main(int argc, char *argv[])
{
struct GModule *module;
struct Option *rast_out; /* Structure for output raster */
struct Option *frac_dim; /* Fractal dimension of surface. */
struct Option *num_images; /* Number of images to produce. */
struct Option *rast_out; /* Structure for output raster */
struct Option *frac_dim; /* Fractal dimension of surface. */
struct Option *num_images; /* Number of images to produce. */
struct Option *seed; /* Seed for random number generator. */
long seed_value;

G_gisinit(argv[0]); /* Link with GRASS interface. */

Expand All @@ -54,9 +56,31 @@ int main(int argc, char *argv[])
num_images->required = NO;
num_images->answer = "0";

seed = G_define_option();
seed->key = "seed";
seed->type = TYPE_INTEGER;
seed->required = NO;
seed->label = _("Seed for random number generator");
seed->description = _("The same seed can be used to obtain same results"
" or random seed can be generated by other means.");

if (G_parser(argc, argv)) /* Performs the prompting for */
exit(EXIT_FAILURE); /* keyboard input. */

/****** INITIALISE RANDOM NUMBER GENERATOR ******/
if (seed->answer) {
seed_value = atol(seed->answer);
G_srand48(seed_value);
G_verbose_message(_("Read random seed from %s option: %ld"), seed->key,
seed_value);
}
else {
/* default as it used to be */
seed_value = G_srand48_auto();
G_verbose_message(_("Autogenerated random seed set to: %ld"),
seed_value);
}

rast_out_name = rast_out->answer;
sscanf(frac_dim->answer, "%lf", &H);
H = 3.0 - H;
Expand Down
3 changes: 0 additions & 3 deletions raster/r.surf.fractal/spec_syn.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@ int specsyn(double *data[2], /* Array holding complex data to transform. */
double phase, rad, /* polar coordinates of Fourier coeff. */
*temp[2];

/* You can set GRASS_RANDOM_SEED for repeatability */
G_math_srand_auto(); /* Reset random number generator. */

temp[0] = (double *)G_malloc(nn * nn * sizeof(double));
temp[1] = (double *)G_malloc(nn * nn * sizeof(double));

Expand Down
92 changes: 92 additions & 0 deletions raster/r.surf.fractal/testsuite/test_r_surf_fractal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env python3

"""
MODULE: Test of r.surf.fractal
AUTHOR(S): Nishant Bansal <[email protected]>
PURPOSE: Test fractal surface generation of a given fractal dimension.
COPYRIGHT: (C) 2025 by Nishant Bansal and the GRASS Development Team
This program is free software under the GNU General Public
License (>=v2). Read the file COPYING that comes with GRASS
for details.
"""

import os
from grass.gunittest.case import TestCase
from grass.gunittest.main import test


class TestRSurfFractal(TestCase):
"""Test case for r.surf.fractal"""

# Raster map name to be used as output
output = "test_fractal"

@classmethod
def setUpClass(cls):
"""Set up necessary environment"""
# Set up temporary computational region
cls.use_temp_region()
cls.runModule("g.region", rows=10, cols=10)

@classmethod
def tearDownClass(cls):
"""Clean up temporary environment"""
cls.del_temp_region()

def tearDown(self):
"""Remove the output created from the module after each test"""
self.runModule("g.remove", flags="f", type="raster", name=[self.output])

def test_default_settings(self):
"""Test r.surf.fractal with default settings."""
self.assertModule("r.surf.fractal", output=self.output)
self.assertRasterExists(self.output, msg="Output Raster not created")

def test_dimension_number_params(self):
"""Test r.surf.fractal with the specified dimension and number parameters."""
os.environ["GRASS_RANDOM_SEED"] = "42"
fractal_dim = 2.0005
num_images = 2

self.assertModule(
"r.surf.fractal",
dimension=fractal_dim,
number=num_images,
output=self.output,
)

self.assertRasterExists(self.output, msg="Output Raster not created")
self.assertRasterFitsUnivar(
self.output,
reference={"mean": -3693.824428, "stddev": 12477.543361},
precision=1e-6,
)

def test_random_seed_option(self):
"""Test r.surf.fractal checks whether the random seed option sets a random number."""
fractal_dim = 2.0005
num_images = 2
seed_value = 22

self.assertModule(
"r.surf.fractal",
dimension=fractal_dim,
number=num_images,
seed=seed_value,
output=self.output,
)

self.assertRasterExists(self.output, msg="Output was not created")
self.assertRasterFitsUnivar(
self.output,
reference={"mean": -925.148233, "stddev": 19416.071318},
precision=1e-6,
)


if __name__ == "__main__":
test()

0 comments on commit 2262aaa

Please sign in to comment.