How-to: create a samurai domain using boxes#

In this how-to guide, we will show you how to create a samurai domain using boxes. Boxes are a simple way to define a rectangular domain in samurai. You can create complex domains by combining multiple boxes and make differences between them.

First, make sure you have samurai installed. If you haven’t installed it yet, please refer to the installation guide here.

A simple example#

Let’s start with a simple example of creating a domain using one box.

#include <samurai/box.hpp>

int main()
{
    static constexpr std::size_t dim = 3;

    samurai::Box<double, dim> box({0.0, 0.0, 0.0}, {1.0, 1.0, 1.0});

    return 0;
}

In this example, we create a 3D box with the lower corner at \((0.0, 0.0, 0.0)\) and the upper corner at \((1.0, 1.0, 1.0)\).

If you want to create a 2D box, you can do it following the same principle as this:

#include <samurai/box.hpp>

int main()
{
    static constexpr std::size_t dim = 2;

    samurai::Box<double, dim> box({-1.0, -1.0}, {1.0, 1.0});

    return 0;
}

In this example, we create a 2D box with the lower corner at \((-1.0, -1.0)\) and the upper corner at \((1.0, 1.0)\).

That’s it! You have successfully created a samurai domain using boxes. You can now use this domain to create meshes, fields, and perform simulations.

To create a mesh from the box, please refer to the mesh how-to guide here.

Combining multiple boxes#

You can also create more complex domains by combining multiple boxes. For example, you can create a domain that is the union of two boxes:

#include <samurai/domain_builder.hpp>

int main()
{
    static constexpr std::size_t dim = 2;

    samurai::DomainBuilder<dim> domain({-1., -1.}, {1., 1.});
    domain.remove({0.0, 0.0}, {0.4, 0.4});

    return 0;
}

In this example, we first create a box from \((-1.0, -1.0)\) to \((1.0, 1.0)\), and then we remove a smaller box from \((0.0, 0.0)\) to \((0.4, 0.4)\). The DomainBuilder class allows you to easily add and remove boxes to create complex domains.