Skip to content

Commit 10a2174

Browse files
authored
Merge pull request #45 from solvcon/project/ustmesh
Add the minimal code for unstructured mesh
2 parents 24b51f9 + b3f6bec commit 10a2174

File tree

15 files changed

+2348
-80
lines changed

15 files changed

+2348
-80
lines changed

.clang-tidy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Checks: >
1313
hicpp-avoid-goto,hicpp-exception-baseclass,hicpp-multiway-paths-covered,
1414
hicpp-no-assembler,hicpp-signed-bitwise,
1515
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
16+
-cppcoreguidelines-pro-bounds-constant-array-index,
1617
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
1718
-google-readability-todo,
1819
-google-runtime-references,
@@ -22,6 +23,7 @@ Checks: >
2223
-performance-noexcept-move-constructor,
2324
-readability-named-parameter,
2425
-readability-magic-numbers,
26+
-readability-redundant-access-specifiers,
2527
-misc-non-private-member-variables-in-classes
2628
WarningsAsErrors: ''
2729
HeaderFilterRegex: 'modmesh.*'

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ set(MODMESH_HEADERS
7373
include/modmesh/base.hpp
7474
include/modmesh/profile.hpp
7575
include/modmesh/grid.hpp
76+
include/modmesh/mesh.hpp
77+
include/modmesh/mesh/StaticMesh_decl.hpp
78+
include/modmesh/mesh/boundary.hpp
79+
include/modmesh/mesh/interior.hpp
7680
include/modmesh/small_vector.hpp
7781
include/modmesh/ConcreteBuffer.hpp
7882
include/modmesh/SimpleArray.hpp

include/modmesh/ConcreteBuffer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
#include <stdexcept>
3434
#include <memory>
35+
#include <algorithm>
36+
#include <sstream>
3537

3638
namespace modmesh
3739
{

include/modmesh/SimpleArray.hpp

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,29 @@ class SimpleArray
119119
}
120120
}
121121

122+
// NOLINTNEXTLINE(modernize-pass-by-value)
123+
explicit SimpleArray(small_vector<size_t> const & shape, value_type const & value)
124+
: SimpleArray(shape)
125+
{
126+
std::fill(begin(), end(), value);
127+
}
128+
122129
explicit SimpleArray(std::vector<size_t> const & shape)
123130
: m_shape(shape), m_stride(calc_stride(m_shape))
124131
{
125-
if (!m_shape.empty()) {
132+
if (!m_shape.empty())
133+
{
126134
m_buffer = buffer_type::construct(m_shape[0] * m_stride[0] * ITEMSIZE);
127135
m_body = m_buffer->data<T>();
128136
}
129137
}
130138

139+
explicit SimpleArray(std::vector<size_t> const & shape, value_type const & value)
140+
: SimpleArray(shape)
141+
{
142+
std::fill(begin(), end(), value);
143+
}
144+
131145
explicit SimpleArray(std::shared_ptr<buffer_type> const & buffer)
132146
{
133147
if (buffer)
@@ -169,20 +183,6 @@ class SimpleArray
169183
}
170184
}
171185

172-
static shape_type calc_stride(shape_type const & shape)
173-
{
174-
shape_type stride(shape.size());
175-
if (!shape.empty())
176-
{
177-
stride[shape.size()-1] = 1;
178-
for (size_t it=shape.size()-1; it>0; --it)
179-
{
180-
stride[it-1] = stride[it] * shape[it];
181-
}
182-
}
183-
return stride;
184-
}
185-
186186
SimpleArray(std::initializer_list<T> init)
187187
: SimpleArray(init.size())
188188
{
@@ -199,21 +199,6 @@ class SimpleArray
199199
, m_body(calc_body(m_buffer->data<T>(), m_stride, other.m_nghost))
200200
{}
201201

202-
static T * calc_body(T * data, shape_type const & stride, size_t nghost)
203-
{
204-
if (nullptr == data || stride.empty() || 0 == nghost)
205-
{
206-
// Do nothing.
207-
}
208-
else
209-
{
210-
shape_type shape(stride.size(), 0);
211-
shape[0] = nghost;
212-
data += buffer_offset(stride, shape);
213-
}
214-
return data;
215-
}
216-
217202
SimpleArray(SimpleArray && other) noexcept
218203
: m_buffer(std::move(other.m_buffer))
219204
, m_shape(std::move(other.m_shape))
@@ -248,6 +233,42 @@ class SimpleArray
248233

249234
~SimpleArray() = default;
250235

236+
template < typename ... Args >
237+
SimpleArray & remake(Args && ... args)
238+
{
239+
SimpleArray(args ...).swap(*this);
240+
return *this;
241+
}
242+
243+
static shape_type calc_stride(shape_type const & shape)
244+
{
245+
shape_type stride(shape.size());
246+
if (!shape.empty())
247+
{
248+
stride[shape.size()-1] = 1;
249+
for (size_t it=shape.size()-1; it>0; --it)
250+
{
251+
stride[it-1] = stride[it] * shape[it];
252+
}
253+
}
254+
return stride;
255+
}
256+
257+
static T * calc_body(T * data, shape_type const & stride, size_t nghost)
258+
{
259+
if (nullptr == data || stride.empty() || 0 == nghost)
260+
{
261+
// Do nothing.
262+
}
263+
else
264+
{
265+
shape_type shape(stride.size(), 0);
266+
shape[0] = nghost;
267+
data += buffer_offset(stride, shape);
268+
}
269+
return data;
270+
}
271+
251272
explicit operator bool() const noexcept { return bool(m_buffer) && bool(*m_buffer); }
252273

253274
size_t nbytes() const noexcept { return m_buffer ? m_buffer->nbytes() : 0; }
@@ -359,7 +380,7 @@ class SimpleArray
359380
return SimpleArray(m_shape, m_buffer);
360381
}
361382

362-
void swap(SimpleArray<T> && other)
383+
void swap(SimpleArray & other) noexcept
363384
{
364385
if (this != &other)
365386
{
@@ -372,10 +393,14 @@ class SimpleArray
372393
}
373394

374395
template < typename ... Args >
375-
value_type const & operator()(Args ... args) const { return m_body[buffer_offset(m_stride, args...)]; }
396+
value_type const & operator()(Args ... args) const { return *vptr(args...); }
397+
template < typename ... Args >
398+
value_type & operator()(Args ... args) { return *vptr(args...); }
376399

377400
template < typename ... Args >
378-
value_type & operator()(Args ... args) { return m_body[buffer_offset(m_stride, args...)]; }
401+
value_type const * vptr(Args ... args) const { return m_body + buffer_offset(m_stride, args...); }
402+
template < typename ... Args >
403+
value_type * vptr(Args ... args) { return m_body + buffer_offset(m_stride, args...); }
379404

380405
/* Backdoor */
381406
value_type const & data(size_t it) const { return data()[it]; }

include/modmesh/base.hpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,40 @@
4343
namespace modmesh
4444
{
4545

46+
template <typename I, typename R>
47+
class NumberBase
48+
{
49+
50+
public:
51+
52+
using int_type = I;
53+
using uint_type = std::make_unsigned_t<I>;
54+
using size_type = uint_type;
55+
using real_type = R;
56+
57+
}; /* end class NumberBase */
58+
4659
/**
4760
* Spatial table basic information. Any table-based data store for spatial
4861
* data should inherit this class template.
4962
*/
50-
template <size_t ND>
63+
template <uint8_t ND, typename I, typename R>
5164
class SpaceBase
65+
: public NumberBase<I, R>
5266
{
5367

5468
public:
5569

56-
static constexpr const size_t NDIM = ND;
70+
using dim_type = uint8_t;
71+
static constexpr const dim_type NDIM = ND;
72+
73+
using number_base = NumberBase<I, R>;
5774

58-
using int_type = int32_t;
59-
using uint_type = uint32_t;
60-
using serial_type = uint_type;
61-
using real_type = double;
75+
using int_type = typename number_base::int_type;
76+
using uint_type = typename number_base::uint_type;
77+
using size_type = typename number_base::size_type;
78+
using serial_type = typename number_base::size_type;
79+
using real_type = typename number_base::real_type;
6280

6381
}; /* end class SpaceBase */
6482

include/modmesh/grid.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ namespace modmesh
4242
/**
4343
* Base class template for structured grid.
4444
*/
45-
template <size_t ND>
45+
template <uint8_t ND>
4646
class StaticGridBase
47-
: public SpaceBase<ND>
47+
: public SpaceBase<ND, int32_t, double>
4848
{
4949
}; /* end class StaticGridBase */
5050

include/modmesh/mesh.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#pragma once
2+
3+
/*
4+
* Copyright (c) 2021, Yung-Yu Chen <[email protected]>
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* - Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* - Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* - Neither the name of the copyright holder nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
/**
32+
* Unstructured mesh.
33+
*/
34+
35+
#include <modmesh/mesh/StaticMesh_decl.hpp>
36+
#include <modmesh/mesh/boundary.hpp>
37+
#include <modmesh/mesh/interior.hpp>
38+
39+
// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:

0 commit comments

Comments
 (0)