Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/world_builder/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ namespace WorldBuilder
class Interface;
} // namespace GravityModel

namespace Topography
{
class Interface;
} // namespace GravityModel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Topography


class World;

/**
Expand Down Expand Up @@ -275,6 +280,14 @@ namespace WorldBuilder
*/
std::unique_ptr<WorldBuilder::GravityModel::Interface> gravity_model;

/**
* A pointers to the topography model. This variable is responsible for
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pointers -> pointer

* the topography model and has ownership over it. Therefore a unique
* pointer are used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* pointer are used.
* pointer is used.

Comment on lines +284 to +286
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a random comment as I'm looking at PRs to see what everyone has been working on :)

Suggested change
* A pointers to the topography model. This variable is responsible for
* the topography model and has ownership over it. Therefore a unique
* pointer are used.
* A pointer to the topography model. This variable is responsible for
* the topography model and has ownership over it. Therefore a unique
* pointer is used.

* @see CoordinateSystem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what are you referring to here?

*/
std::unique_ptr<WorldBuilder::Topography::Interface> topography_model;

/**
* This function return the current path as stored in the path variable
* as a string in json pointer format.
Expand Down
157 changes: 157 additions & 0 deletions include/world_builder/topography/interface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
Copyright (C) 2018-2025 by the authors of the World Builder code.
This file is part of the World Builder.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef WORLD_BUILDER_TOPOGRAPHY_INTERFACE_H
#define WORLD_BUILDER_TOPOGRAPHY_INTERFACE_H

#include "world_builder/topography/interface.h"

#include "world_builder/parameters.h"
#include "world_builder/types/string.h"
#include "world_builder/objects/natural_coordinate.h"


namespace WorldBuilder
{
class World;

namespace Topography
{

class ObjectFactory;

/**
* This class is an interface for the topography.
*/
class Interface
{
public:
/**
* Destructor
*/
virtual
~Interface() = default;

/**
* declare and read in the world builder file into the topography class
*/
static
void declare_entries(Parameters &prm,
const std::string &parent_name,
const std::vector<std::string> &required_entries);

/**
* declare and read in the world builder file into the topography class
*/
virtual
void parse_entries(Parameters &prm) = 0;

/**
* A function to register a new type. This is part of the automatic
* registration of the object factory.
*/
static void registerType(const std::string &name,
void ( * /*declare_entries*/)(Parameters &, const std::string &),
ObjectFactory *factory);

/**
* A function to create a new type. This is part of the automatic
* registration of the object factory.
*/
static std::unique_ptr<Interface> create(const std::string &name, WorldBuilder::World *world);

enum SurfaceType
{
reference_surface,
deformed_surface
};

SurfaceType surface_type;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this variable private. The enum type needs to be public, so that calling functions know that this type exists. But the variable itself (surface_type) should still be private as before so that no one accidentally changes this variable and all access has to go through get_surface_type.

Also add documentation what this variable means.


/**
* declare and read in the world builder file into the parameters class
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't seem right?

*/
virtual
SurfaceType
get_surface_type() = 0;

/**
* Returns a temperature based on the given position, depth in the model,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Returns a temperature based on the given position, depth in the model,
* Returns a topography based on the given position, depth in the model,

* gravity and current temperature.
*/
virtual
double get_topography(const Point<3> &position,
const Objects::NaturalCoordinate &position_in_natural_coordinates,
const double depth) const = 0;

protected:
/**
* A pointer to the world class to retrieve variables.
*/
WorldBuilder::World *world;



private:
static std::map<std::string, ObjectFactory *> &get_factory_map()
{
static std::map<std::string, ObjectFactory *> factories;
return factories;
}

static std::map<std::string, void ( *)(Parameters &,const std::string &)> &get_declare_map()
{
static std::map<std::string, void ( *)(Parameters &,const std::string &)> declares;
return declares;
}
};



/**
* A class to create new objects
*/
class ObjectFactory
{
public:
virtual std::unique_ptr<Interface> create(World *world) = 0;
};

/**
* A macro which should be in every derived cpp file to automatically
* register it. Because this is a library, we need some extra measures
* to ensure that the static variable is actually initialized.
*/
#define WB_REGISTER_TOPOGRAPHY(klass,name) \
class klass##Factory : public ObjectFactory { \
public: \
klass##Factory() \
{ \
Interface::registerType(#name, klass::declare_entries, this); \
} \
std::unique_ptr<Interface> create(World *world) override final { \
return std::unique_ptr<Interface>(new klass(world)); \
} \
}; \
static klass##Factory global_##klass##Factory;

} // namespace Topography
} // namespace WorldBuilder

#endif
87 changes: 87 additions & 0 deletions include/world_builder/topography/uniform.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
Copyright (C) 2018-2025 by the authors of the World Builder code.
This file is part of the World Builder.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef WORLD_BUILDER_TOPOGRAPHY_MINIMUM_DEPTH_H
#define WORLD_BUILDER_TOPOGRAPHY_MINIMUM_DEPTH_H

#include "world_builder/topography/interface.h"

#include "world_builder/utilities.h"
#include "world_builder/objects/natural_coordinate.h"


namespace WorldBuilder
{

namespace Topography
{
/**
* This implements a minimum depth topography. The minimum depth topography
* finds the minimum depth of a feature and uses that for the topography.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* finds the minimum depth of a feature and uses that for the topography.
* finds the minimum depth of all active features at a location and uses that depth as the topography.

*/
class Uniform final : public Interface
{
public:
/**
* Constructor
*/
Uniform(WorldBuilder::World *world);

/**
* Destructor
*/
~ Uniform() override final;

/**
* declare and read in the world builder file into the topography class
*/
static
void declare_entries(Parameters &prm, const std::string &parent_name = "");

/**
* declare and read in the world builder file into the topography class
*/
void parse_entries(Parameters &prm) override final;

/**
* declare and read in the world builder file into the topography class
Comment on lines +52 to +63
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These comments are all the same, which makes them less informative. Maybe make them more specific to the functions?

*/
SurfaceType
get_surface_type() override final;

/**
* Returns a temperature based on the given position, depth in the model,
* gravity and current temperature.
*/
double get_topography(const Point<3> &position,
const Objects::NaturalCoordinate &position_in_natural_coordinates,
const double depth) const override final;

private:

/**
* Whether to consider a reference or deformed surface.
*/
SurfaceType surface_type;

};
} // namespace Topography
} // namespace WorldBuilder

#endif
7 changes: 6 additions & 1 deletion source/world_builder/parameters.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "world_builder/features/subducting_plate.h"
#include "world_builder/features/subducting_plate_models/velocity/interface.h"
#include "world_builder/gravity_model/interface.h"
#include "world_builder/topography/interface.h"
#include "world_builder/types/object.h"
#include "world_builder/utilities.h"

Expand Down Expand Up @@ -2089,7 +2090,11 @@ namespace WorldBuilder
*/
template std::unique_ptr<GravityModel::Interface> Parameters::get_unique_pointer<GravityModel::Interface>(const std::string &name);


/**
* Returns a vector of pointers to the Topography Model based on the provided name.
* Note that the variable with this name has to be loaded before this function is called.
*/
template std::unique_ptr<Topography::Interface> Parameters::get_unique_pointer<Topography::Interface>(const std::string &name);

/**
* Todo: Returns a vector of pointers to the Point<3> Type based on the provided name.
Expand Down
92 changes: 92 additions & 0 deletions source/world_builder/topography/interface.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright (C) 2018-2024 by the authors of the World Builder code.

This file is part of the World Builder.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

#include "world_builder/topography/interface.h"

#include "world_builder/types/object.h"

#include <algorithm>

namespace WorldBuilder
{
namespace Topography
{
void
Interface::declare_entries(Parameters &prm, const std::string &parent_name, const std::vector<std::string> &required_entries)
{

unsigned int counter = 0;
for (auto &it : get_declare_map())
{
prm.enter_subsection("oneOf");
{
prm.enter_subsection(std::to_string(counter));
{
prm.enter_subsection("properties");
{
prm.declare_entry("", Types::Object(required_entries), "topography");

prm.declare_entry("model",Types::String("",it.first),
"The name of the model for topography to use.");

it.second(prm, parent_name);
}
prm.leave_subsection();
}
prm.leave_subsection();
}
prm.leave_subsection();

counter++;

}
}

void
Interface::registerType(const std::string &name,
void ( *declare_entries)(Parameters &, const std::string &),
ObjectFactory *factory)
{
get_factory_map()[name] = factory;
get_declare_map()[name] = declare_entries;
}

std::unique_ptr<Interface>
Interface::create(const std::string &name, WorldBuilder::World *world)
{
std::string lower_case_name;
std::transform(name.begin(),
name.end(),
std::back_inserter(lower_case_name),
::tolower);;

// Have a nice assert message to check whether a plugin exists in the case
// of a debug compilation.
WBAssertThrow(get_factory_map().find(lower_case_name) != get_factory_map().end(),
"Internal error: Plugin with name '" << lower_case_name << "' is not found. "
"The size of factories is " << get_factory_map().size() << ".");

// Using at() because the [] will just insert values
// which is undesirable in this case. An exception is
// thrown when the name is not present.
return get_factory_map().at(lower_case_name)->create(world);
}
} // namespace Topography
} // namespace WorldBuilder

Loading
Loading