Browse Source

poly tests

master
gjj 12 months ago
parent
commit
a89ff558b8
  1. 268
      .clang-format
  2. 2
      CMakeLists.txt
  3. 2
      algoim/bernstein.hpp
  4. 83
      algoim/booluarray.hpp
  5. 78
      algoim/polyset.hpp
  6. 1359
      algoim/quadrature_multipoly.hpp
  7. 0
      examples/examples_mytest.cpp
  8. 312
      examples/examples_quad_multipoly.cpp
  9. 179
      gjj/myDebug.hpp
  10. 0
      gjj/myDebugTool.hpp

268
.clang-format

@ -0,0 +1,268 @@
---
BasedOnStyle: Google
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignArrayOfStructures: Left
AlignConsecutiveAssignments:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveBitFields:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveDeclarations:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCompound: true
PadOperators: true
AlignConsecutiveShortCaseStatements:
Enabled: true
AcrossEmptyLines: false
AcrossComments: true
AlignCaseColons: false
AlignEscapedNewlines: Left
AlignOperands: Align
AlignTrailingComments:
Kind: Always
OverEmptyLines: 2
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Always
AllowShortCaseLabelsOnASingleLine: true
AllowShortEnumsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLambdasOnASingleLine: All
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: Yes
AttributeMacros:
- __capability
BinPackArguments: false
BinPackParameters: false
BitFieldColonSpacing: Both
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakAfterAttributes: Never
BreakAfterJavaFieldAnnotations: false
BreakArrays: true
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces: Linux
BreakBeforeConceptDeclarations: Always
BreakBeforeInlineASMColon: OnlyMultiline
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: AfterComma
BreakStringLiterals: true
ColumnLimit: 128
CommentPragmas: "^(!<)? IWYU pragma:"
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Regroup
IncludeCategories:
- Regex: ^<ext/.*\.h>
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: ^<.*\.h>
Priority: 1
SortPriority: 0
CaseSensitive: false
- Regex: ^<.*
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: .*
Priority: 3
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: ([-_](test|unittest))?$
IncludeIsMainSourceRegex: ""
IndentAccessModifiers: false
IndentCaseBlocks: false
IndentCaseLabels: true
IndentExternBlock: AfterExternBlock
IndentGotoLabels: false
IndentPPDirectives: None
IndentRequiresClause: true
IndentWidth: 4
IndentWrappedFunctionNames: true
InsertBraces: false
InsertNewlineAtEOF: false
InsertTrailingCommas: None
IntegerLiteralSeparator:
Binary: 0
BinaryMinDigits: 0
Decimal: 0
DecimalMinDigits: 0
Hex: 0
HexMinDigits: 0
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtEOF: false
KeepEmptyLinesAtTheStartOfBlocks: false
LambdaBodyIndentation: Signature
Language: Cpp
LineEnding: DeriveLF
MacroBlockBegin: ""
MacroBlockEnd: ""
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
ObjCBinPackProtocolList: Never
ObjCBlockIndentWidth: 2
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PPIndentWidth: -1
PackConstructorInitializers: NextLine
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyIndentedWhitespace: 0
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Right
QualifierAlignment: Leave
RawStringFormats:
- Language: Cpp
Delimiters:
- cc
- CC
- cpp
- Cpp
- CPP
- c++
- C++
CanonicalDelimiter: ""
BasedOnStyle: google
- Language: TextProto
Delimiters:
- pb
- PB
- proto
- PROTO
EnclosingFunctions:
- EqualsProto
- EquivToProto
- PARSE_PARTIAL_TEXT_PROTO
- PARSE_TEST_PROTO
- PARSE_TEXT_PROTO
- ParseTextOrDie
- ParseTextProtoOrDie
- ParseTestProto
- ParsePartialTestProto
CanonicalDelimiter: pb
BasedOnStyle: google
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
RemoveParentheses: Leave
RemoveSemicolon: false
RequiresClausePosition: OwnLine
RequiresExpressionIndentation: OuterScope
SeparateDefinitionBlocks: Always
ShortNamespaceLines: 0
SortIncludes: Never
SortJavaStaticImport: Before
SortUsingDeclarations: LexicographicNumeric
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceAroundPointerQualifiers: Default
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeJsonColon: false
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDeclarationName: false
AfterFunctionDefinitionName: false
AfterIfMacros: true
AfterOverloadedOperator: false
AfterRequiresInClause: false
AfterRequiresInExpression: true
BeforeNonEmptyParentheses: false
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInContainerLiterals: true
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParens: Never
SpacesInParensOptions:
InConditionalStatements: false
InCStyleCasts: false
InEmptyParentheses: false
Other: false
SpacesInSquareBrackets: false
Standard: Auto
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 8
UseTab: Never
VerilogBreakBetweenInstancePorts: true
WhitespaceSensitiveMacros:
- BOOST_PP_STRINGIZE
- CF_SWIFT_NAME
- NS_SWIFT_NAME
- PP_STRINGIZE
- STRINGIZE

2
CMakeLists.txt

@ -5,7 +5,7 @@ set(CMAKE_STANDARD 17)
set(CMAKE_EXPORT_COMPILECOMMANDS ON)
include_directories(algoim)
include_directories(algoim gjj)
# find_package(LAPACK REQUIRED)
find_path(LAPACKE_INCLUDE_DIR NAMES lapacke.h PATH_SUFFIXES include)

2
algoim/bernstein.hpp

@ -297,7 +297,7 @@ namespace algoim::bernstein
for (int i = 1; i < P; ++i)
for (int j = P - 1; j >= i; --j)
{
alpha.a(j) *= tau;
alpha.a(j) *= tau; // here the ptr to the array is included in the return
alpha.a(j) += alpha.a(j - 1) * (1.0 - tau);
}
}

83
algoim/booluarray.hpp

@ -8,56 +8,45 @@
namespace algoim
{
namespace booluarray_detail
namespace booluarray_detail
{
constexpr int pow(int base, int exp) { return exp == 0 ? 1 : base * pow(base, exp - 1); }
template <int E, int N>
constexpr int furl(const uvector<int, N>& i) // 为什么这里可以是constexpr,uvector不是变量吗?
{
int ind = i(0);
for (int j = 1; j < N; ++j) ind = ind * E + i(j);
return ind;
}
} // namespace booluarray_detail
// booluarray implements a simple N-dimensional array of booleans, with
// compile-time extent E across all dimensions; it is essentially a basic
// specialisation of uarray<bool,N,E>
template <int N, int E>
class booluarray
{
constexpr static int size = booluarray_detail::pow(E, N);
public:
std::bitset<size> bits;
booluarray() {}
booluarray(bool val)
{
constexpr int pow(int base, int exp)
{
return exp == 0 ? 1 : base * pow(base, exp - 1);
}
template<int E, int N>
constexpr int furl(const uvector<int,N>& i)
{
int ind = i(0);
for (int j = 1; j < N; ++j)
ind = ind * E + i(j);
return ind;
}
if (val) bits.set(); // 全部置为1
}
// booluarray implements a simple N-dimensional array of booleans, with
// compile-time extent E across all dimensions; it is essentially a basic
// specialisation of uarray<bool,N,E>
template<int N, int E>
class booluarray
{
constexpr static int size = booluarray_detail::pow(E, N);
std::bitset<size> bits;
public:
booluarray() {}
booluarray(bool val)
{
if (val)
bits.set();
}
bool operator() (const uvector<int,N>& i) const
{
return bits[booluarray_detail::furl<E>(i)];
}
auto operator() (const uvector<int,N>& i)
{
return bits[booluarray_detail::furl<E>(i)];
}
// returns true iff the entire array is false
bool none() const
{
return bits.none();
}
};
bool operator()(const uvector<int, N>& i) const { return bits[booluarray_detail::furl<E>(i)]; }
auto operator()(const uvector<int, N>& i) { return bits[booluarray_detail::furl<E>(i)]; }
// returns true iff the entire array is false
bool none() const { return bits.none(); }
};
} // namespace algoim
#endif

78
algoim/polyset.hpp

@ -6,50 +6,48 @@
#include <cassert>
#include <vector>
#include "booluarray.hpp"
#include "real.hpp"
#include "xarray.hpp"
namespace algoim
{
// PolySet implements a simple container to hold one or more Bernstein polynomials
// and their associated masks
template<int N, int E>
struct PolySet
{
struct Poly
{
uvector<int,N> ext; // Degree/extent of polynomial
size_t offset; // Offset into buffer, storing the xarray<Real,N> polynomial data
booluarray<N,E> mask; // Mask
};
std::vector<real> buff; // Memory buffer containing polynomial data
std::vector<Poly> items; // Record of contained polynomials
// Access polynomial by index
xarray<real,N> poly(size_t ind)
{
assert(0 <= ind && ind < items.size());
return xarray<real,N>(&buff[items[ind].offset], items[ind].ext);
}
// Access mask by index
booluarray<N,E>& mask(size_t ind)
{
assert(0 <= ind && ind < items.size());
return items[ind].mask;
}
// Add a polynomial/mask pair to the container
void push_back(const xarray<real,N>& p, const booluarray<N,E>& m)
{
items.push_back({p.ext(), buff.size(), m});
buff.resize(buff.size() + p.size());
poly(items.size() - 1) = p;
}
size_t count() const
{
return items.size();
}
// PolySet implements a simple container to hold one or more Bernstein polynomials
// and their associated masks
template <int N, int E>
struct PolySet {
struct Poly {
uvector<int, N> ext; // Degree/extent of polynomial
size_t offset; // Offset into buffer, storing the xarray<Real,N> polynomial data
booluarray<N, E> mask; // Mask
};
std::vector<real> buff; // Memory buffer containing polynomial data
std::vector<Poly> items; // Record of contained polynomials
// Access polynomial by index
xarray<real, N> poly(size_t ind)
{
assert(0 <= ind && ind < items.size());
return xarray<real, N>(&buff[items[ind].offset], items[ind].ext);
}
// Access mask by index
booluarray<N, E>& mask(size_t ind)
{
assert(0 <= ind && ind < items.size());
return items[ind].mask;
}
// Add a polynomial/mask pair to the container
void push_back(const xarray<real, N>& p, const booluarray<N, E>& m)
{
items.push_back({p.ext(), buff.size(), m});
buff.resize(buff.size() + p.size());
poly(items.size() - 1) = p;
}
size_t count() const { return items.size(); }
};
} // namespace algoim
#endif

1359
algoim/quadrature_multipoly.hpp

File diff suppressed because it is too large

0
gjj/myDebugTool.cpp → examples/examples_mytest.cpp

312
examples/examples_quad_multipoly.cpp

@ -1,6 +1,6 @@
// Examples to demonstrate Algoim's methods for computing high-order accurate quadrature schemes
// on multi-component domains implicitly-defined by (one or more) multivariate Bernstein
// polynomials. Additional examples are provided on the GitHub documentation page,
// polynomials. Additional examples are provided on the GitHub documentation page,
// https://algoim.github.io/
#include <cstddef>
@ -15,137 +15,137 @@
#include "vector"
#include "xarray.hpp"
#include "myDebug.hpp"
using namespace algoim;
const static std::vector<std::vector<real>> binomial_table = {
{1, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 0},
{1, 2, 1, 0, 0, 0, 0},
{1, 3, 3, 1, 0, 0, 0},
{1, 4, 6, 4, 1, 0, 0},
{1, 5, 10, 10, 5, 1, 0},
{1, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 0, 0, 0, 0},
{1, 2, 1, 0, 0, 0, 0},
{1, 3, 3, 1, 0, 0, 0},
{1, 4, 6, 4, 1, 0, 0},
{1, 5, 10, 10, 5, 1, 0},
{1, 6, 15, 20, 15, 6, 1}
};
// function模板不允许直接<部分>特化,所以用类模板
template<int N>
template <int N>
struct DebugXArray {
template<typename Phi>
void operator()(const xarray<real, N>& iData, Phi&& phi) {
}
template <typename Phi>
void operator()(const xarray<real, N>& iData, Phi&& phi)
{
}
};
template<>
struct DebugXArray<2>{
template<typename Phi>
void operator()(const xarray<real, 2>& iData, Phi&& phi) {
std::vector<std::vector<real>> data(iData.ext(0), std::vector<real>(iData.ext(1)));
for (int i = 0; i < iData.ext(0); ++i) {
for (int j = 0; j < iData.ext(1); ++j) {
data[i][j] = iData(i, j);
}
}
real inputX1 = 0.2, inputX2 = 0.3;
std::vector<real> b1(iData.ext(0)), b2(iData.ext(1));
int n1 = iData.ext(0) - 1, n2 = iData.ext(1) - 1;
real res = 0;
for (int i = 0; i < iData.ext(0); ++i) {
b1[i] = binomial_table[n1][i] * std::pow(inputX1, i) * std::pow(1 - inputX1, n1-i);
}
for (int i = 0; i < iData.ext(1); ++i) {
b2[i] = binomial_table[n2][i] * std::pow(inputX2, i) * std::pow(1 - inputX2, n2-i);
}
for (int i = 0; i < iData.ext(0); ++i) {
real tmp = 0;
for (int j = 0; j < iData.ext(1); j++) {
tmp += data[i][j] * b2[j];
}
res += tmp * b1[i];
}
template <>
struct DebugXArray<2> {
template <typename Phi>
void operator()(const xarray<real, 2>& iData, Phi&& phi)
{
std::vector<std::vector<real>> data(iData.ext(0), std::vector<real>(iData.ext(1)));
for (int i = 0; i < iData.ext(0); ++i) {
for (int j = 0; j < iData.ext(1); ++j) { data[i][j] = iData(i, j); }
}
real inputX1 = 0.2, inputX2 = 0.3;
std::vector<real> b1(iData.ext(0)), b2(iData.ext(1));
int n1 = iData.ext(0) - 1, n2 = iData.ext(1) - 1;
real res = 0;
for (int i = 0; i < iData.ext(0); ++i) {
b1[i] = binomial_table[n1][i] * std::pow(inputX1, i) * std::pow(1 - inputX1, n1 - i);
}
for (int i = 0; i < iData.ext(1); ++i) {
b2[i] = binomial_table[n2][i] * std::pow(inputX2, i) * std::pow(1 - inputX2, n2 - i);
}
for (int i = 0; i < iData.ext(0); ++i) {
real tmp = 0;
for (int j = 0; j < iData.ext(1); j++) { tmp += data[i][j] * b2[j]; }
res += tmp * b1[i];
}
// original phi function evaluation
const uvector<real,2> x(inputX1, inputX2);
real phiEval = phi(x);
}
// original phi function evaluation
const uvector<real, 2> x(inputX1, inputX2);
real phiEval = phi(x);
}
};
// Driver method which takes a functor phi defining a single polynomial in the reference
// rectangle [xmin, xmax]^N, of Bernstein degree P, along with an integrand function,
// and performances a q-refinement convergence study, comparing the computed integral
// with the given exact answers, for 1 <= q <= qMax.
template<int N, typename Phi, typename F>
void qConv(const Phi& phi, real xmin, real xmax, uvector<int,N> P, const F& integrand, int qMax, real volume_exact, real surf_exact)
template <int N, typename Phi, typename F>
void qConv(const Phi& phi,
real xmin,
real xmax,
uvector<int, N> P,
const F& integrand,
int qMax,
real volume_exact,
real surf_exact)
{
// Construct Bernstein polynomial by mapping [0,1] onto bounding box [xmin,xmax]
xarray<real,N> phipoly(nullptr, P);
xarray<real, N> phipoly(nullptr, P);
algoim_spark_alloc(real, phipoly);
bernstein::bernsteinInterpolate<N>([&](const uvector<real,N>& x) { return phi(xmin + x * (xmax - xmin)); }, phipoly);
DebugXArray<N>()(phipoly, [&](const uvector<real,N>& x) { return phi(xmin + x * (xmax - xmin)); });
bernstein::bernsteinInterpolate<N>([&](const uvector<real, N>& x) { return phi(xmin + x * (xmax - xmin)); }, phipoly);
DebugXArray<N>()(phipoly, [&](const uvector<real, N>& x) { return phi(xmin + x * (xmax - xmin)); });
// Build quadrature hierarchy
ImplicitPolyQuadrature<N> ipquad(phipoly);
// Functional to evaluate volume and surface integrals of given integrand
real volume, surf;
auto compute = [&](int q)
{
auto compute = [&](int q) {
volume = 0.0;
surf = 0.0;
surf = 0.0;
// compute volume integral over {phi < 0} using AutoMixed strategy
ipquad.integrate(AutoMixed, q, [&](const uvector<real,N>& x, real w)
{
if (bernstein::evalBernsteinPoly(phipoly, x) < 0)
volume += w * integrand(xmin + x * (xmax - xmin));
ipquad.integrate(AutoMixed, q, [&](const uvector<real, N>& x, real w) {
if (bernstein::evalBernsteinPoly(phipoly, x) < 0) volume += w * integrand(xmin + x * (xmax - xmin));
});
// compute surface integral over {phi == 0} using AutoMixed strategy
ipquad.integrate_surf(AutoMixed, q, [&](const uvector<real,N>& x, real w, const uvector<real,N>& wn)
{
ipquad.integrate_surf(AutoMixed, q, [&](const uvector<real, N>& x, real w, const uvector<real, N>& wn) {
surf += w * integrand(xmin + x * (xmax - xmin));
});
// scale appropriately
volume *= pow(xmax - xmin, N);
surf *= pow(xmax - xmin, N - 1);
surf *= pow(xmax - xmin, N - 1);
};
// Compute results for all q and output in a convergence table
for (int q = 1; q <= qMax; ++q)
{
for (int q = 1; q <= qMax; ++q) {
compute(q);
std::cout << q << ' ' << volume << ' ' << surf << ' ' << std::abs(volume - volume_exact)/volume_exact << ' ' << std::abs(surf - surf_exact)/surf_exact << std::endl;
std::cout << q << ' ' << volume << ' ' << surf << ' ' << std::abs(volume - volume_exact) / volume_exact << ' '
<< std::abs(surf - surf_exact) / surf_exact << std::endl;
}
}
// Given a set of quadrature points and weights, output them to an VTP XML file for visualisation
// purposes, e.g., using ParaView
template<int N>
void outputQuadratureRuleAsVtpXML(const std::vector<uvector<real,N+1>>& q, std::string fn)
template <int N>
void outputQuadratureRuleAsVtpXML(const std::vector<uvector<real, N + 1>>& q, std::string fn)
{
static_assert(N == 2 || N == 3, "outputQuadratureRuleAsVtpXML only supports 2D and 3D quadrature schemes");
std::ofstream stream(fn);
stream << "<?xml version=\"1.0\"?>\n";
stream << "<VTKFile type=\"PolyData\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
stream << "<PolyData>\n";
stream << "<Piece NumberOfPoints=\"" << q.size() << "\" NumberOfVerts=\"" << q.size() << "\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
stream << "<Piece NumberOfPoints=\"" << q.size() << "\" NumberOfVerts=\"" << q.size()
<< "\" NumberOfLines=\"0\" NumberOfStrips=\"0\" NumberOfPolys=\"0\">\n";
stream << "<Points>\n";
stream << " <DataArray type=\"Float32\" Name=\"Points\" NumberOfComponents=\"3\" format=\"ascii\">";
for (const auto& pt : q)
stream << pt(0) << ' ' << pt(1) << ' ' << (N == 3 ? pt(2) : 0.0) << ' ';
for (const auto& pt : q) stream << pt(0) << ' ' << pt(1) << ' ' << (N == 3 ? pt(2) : 0.0) << ' ';
stream << "</DataArray>\n";
stream << "</Points>\n";
stream << "<Verts>\n";
stream << " <DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">";
for (size_t i = 0; i < q.size(); ++i)
stream << i << ' ';
stream << "</DataArray>\n";
for (size_t i = 0; i < q.size(); ++i) stream << i << ' ';
stream << "</DataArray>\n";
stream << " <DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">";
for (size_t i = 1; i <= q.size(); ++i)
stream << i << ' ';
for (size_t i = 1; i <= q.size(); ++i) stream << i << ' ';
stream << "</DataArray>\n";
stream << "</Verts>\n";
stream << "<PointData Scalars=\"w\">\n";
stream << " <DataArray type=\"Float32\" Name=\"w\" NumberOfComponents=\"1\" format=\"ascii\">";
for (const auto& pt : q)
stream << pt(N) << ' ';
for (const auto& pt : q) stream << pt(N) << ' ';
stream << "</DataArray>\n";
stream << "</PointData>\n";
stream << "</Piece>\n";
@ -156,29 +156,27 @@ void outputQuadratureRuleAsVtpXML(const std::vector<uvector<real,N+1>>& q, std::
// Driver method which takes a functor phi defining a single polynomial in the reference
// rectangle [xmin, xmax]^N, of Bernstein degree P, builds a quadrature scheme with the
// given q, and outputs it for visualisation in a set of VTP XML files
template<int N, typename F>
void outputQuadScheme(const F& fphi, real xmin, real xmax, const uvector<int,N>& P, int q, std::string qfile)
template <int N, typename F>
void outputQuadScheme(const F& fphi, real xmin, real xmax, const uvector<int, N>& P, int q, std::string qfile)
{
// Construct phi by mapping [0,1] onto bounding box [xmin,xmax]
xarray<real,N> phi(nullptr, P);
xarray<real, N> phi(nullptr, P);
algoim_spark_alloc(real, phi);
bernstein::bernsteinInterpolate<N>([&](const uvector<real,N>& x) { return fphi(xmin + x * (xmax - xmin)); }, phi);
bernstein::bernsteinInterpolate<N>([&](const uvector<real, N>& x) { return fphi(xmin + x * (xmax - xmin)); }, phi);
// Build quadrature hierarchy
ImplicitPolyQuadrature<N> ipquad(phi);
// Compute quadrature scheme and record the nodes & weights; phase0 corresponds to
// {phi < 0}, phase1 corresponds to {phi > 0}, and surf corresponds to {phi == 0}.
std::vector<uvector<real,N+1>> phase0, phase1, surf;
ipquad.integrate(AutoMixed, q, [&](const uvector<real,N>& x, real w)
{
std::vector<uvector<real, N + 1>> phase0, phase1, surf;
ipquad.integrate(AutoMixed, q, [&](const uvector<real, N>& x, real w) {
if (bernstein::evalBernsteinPoly(phi, x) < 0)
phase0.push_back(add_component(x, N, w));
else
phase1.push_back(add_component(x, N, w));
});
ipquad.integrate_surf(AutoMixed, q, [&](const uvector<real,N>& x, real w, const uvector<real,N>& wn)
{
ipquad.integrate_surf(AutoMixed, q, [&](const uvector<real, N>& x, real w, const uvector<real, N>& wn) {
surf.push_back(add_component(x, N, w));
});
@ -191,14 +189,20 @@ void outputQuadScheme(const F& fphi, real xmin, real xmax, const uvector<int,N>&
// Driver method which takes two phi functors defining two polynomials in the reference
// rectangle [xmin, xmax]^N, each of of Bernstein degree P, builds a quadrature scheme with the
// given q, and outputs it for visualisation in a set of VTP XML files
template<int N, typename F1, typename F2>
void outputQuadScheme(const F1& fphi1, const F2& fphi2, real xmin, real xmax, const uvector<int,N>& P, int q, std::string qfile)
template <int N, typename F1, typename F2>
void outputQuadScheme(const F1& fphi1,
const F2& fphi2,
real xmin,
real xmax,
const uvector<int, N>& P,
int q,
std::string qfile)
{
// Construct phi by mapping [0,1] onto bounding box [xmin,xmax]
xarray<real,N> phi1(nullptr, P), phi2(nullptr, P);
xarray<real, N> phi1(nullptr, P), phi2(nullptr, P);
algoim_spark_alloc(real, phi1, phi2);
bernstein::bernsteinInterpolate<N>([&](const uvector<real,N>& x) { return fphi1(xmin + x * (xmax - xmin)); }, phi1);
bernstein::bernsteinInterpolate<N>([&](const uvector<real,N>& x) { return fphi2(xmin + x * (xmax - xmin)); }, phi2);
bernstein::bernsteinInterpolate<N>([&](const uvector<real, N>& x) { return fphi1(xmin + x * (xmax - xmin)); }, phi1);
bernstein::bernsteinInterpolate<N>([&](const uvector<real, N>& x) { return fphi2(xmin + x * (xmax - xmin)); }, phi2);
// Build quadrature hierarchy
ImplicitPolyQuadrature<N> ipquad(phi1, phi2);
@ -206,41 +210,36 @@ void outputQuadScheme(const F1& fphi1, const F2& fphi2, real xmin, real xmax, co
// Compute quadrature scheme and record the nodes & weights; one could examine the signs
// of phi1 and phi2 in order to separate the nodes into different components, but for
// simplicity they are agglomerated
std::vector<uvector<real,N+1>> vol, surf;
ipquad.integrate(AutoMixed, q, [&](const uvector<real,N>& x, real w)
{
vol.push_back(add_component(x, N, w));
});
ipquad.integrate_surf(AutoMixed, q, [&](const uvector<real,N>& x, real w, const uvector<real,N>& wn)
{
std::vector<uvector<real, N + 1>> vol, surf;
ipquad.integrate(AutoMixed, q, [&](const uvector<real, N>& x, real w) { vol.push_back(add_component(x, N, w)); });
ipquad.integrate_surf(AutoMixed, q, [&](const uvector<real, N>& x, real w, const uvector<real, N>& wn) {
surf.push_back(add_component(x, N, w));
});
// output to a file
outputQuadratureRuleAsVtpXML<N>(vol, qfile + "-vol.vtp");
outputQuadratureRuleAsVtpXML<N>(surf, qfile + "-surf.vtp");
outputQuadratureRuleAsVtpXML<N>(surf, qfile + "-surf.vtp");
}
void module_test() {
if (false){
void module_test()
{
if (false) {
const uvector<int, 1> P(5);
xarray<real, 1> beta(nullptr, P);
xarray<real, 1> beta(nullptr, P);
algoim_spark_alloc(real, beta);
for(int i = 0; i < 5; i++) beta[i]=i+1;
for (int i = 0; i < 5; i++) beta[i] = i + 1;
const uvector<real, 1> x(0.5);
auto res = bernstein::evalBernsteinPoly(beta, x);
auto res = bernstein::evalBernsteinPoly(beta, x);
std::cout << "res: " << res << std::endl;
}
if (true) {
const uvector<int, 2> P(4, 4);
xarray<real, 2> beta(nullptr, P);
xarray<real, 2> beta(nullptr, P);
algoim_spark_alloc(real, beta);
for(int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) {
beta(i,j)= i + j;
}
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++) { beta(i, j) = i + j; }
const uvector<real, 2> x(0.5, 0.3);
auto res = bernstein::evalBernsteinPoly(beta, x);
auto res = bernstein::evalBernsteinPoly(beta, x);
std::cout << "res: " << res << std::endl;
}
}
@ -253,34 +252,22 @@ int main(int argc, char* argv[])
std::cout << std::scientific << std::setprecision(10);
// q-convergence study for a 2D ellipse
if (false) {
auto ellipse = [](const uvector<real,2>& x)
{
return x(0)*x(0) + x(1)*x(1)*4 - 1;
};
auto integrand = [](const uvector<real,2>& x)
{
return 1.0;
};
if (true) {
auto ellipse = [](const uvector<real, 2>& x) { return x(0) * x(0) + x(1) * x(1) * 4 - 1; };
auto integrand = [](const uvector<real, 2>& x) { return 1.0; };
real volume_exact = algoim::util::pi / 2;
real surf_exact = 4.844224110273838099214251598195914705976959198943300412541558176231060;
real surf_exact = 4.844224110273838099214251598195914705976959198943300412541558176231060;
std::cout << "\n\nEllipse q-convergence test\n";
std::cout << "q area(q) perim(q) area error perim error\n"; // perimeter: 周长
std::cout << "q area(q) perim(q) area error perim error\n"; // perimeter: 周长
qConv<2>(ellipse, -1.1, 1.1, 3, integrand, 50, volume_exact, surf_exact);
}
// q-convergence study for a 3D ellipsoid
if (false) {
auto ellipsoid = [](const uvector<real,3>& x)
{
return x(0)*x(0) + x(1)*x(1)*4 + x(2)*x(2)*9 - 1;
};
auto integrand = [](const uvector<real,3>& x)
{
return 1.0;
};
auto ellipsoid = [](const uvector<real, 3>& x) { return x(0) * x(0) + x(1) * x(1) * 4 + x(2) * x(2) * 9 - 1; };
auto integrand = [](const uvector<real, 3>& x) { return 1.0; };
real volume_exact = (algoim::util::pi * 2) / 9;
real surf_exact = 4.400809564664970341600200389229705943483674323377145800356686868037845;
real surf_exact = 4.400809564664970341600200389229705943483674323377145800356686868037845;
std::cout << "\n\nEllipsoid q-convergence test\n";
std::cout << "q volume(q) surf(q) vol error surf error\n";
qConv<3>(ellipsoid, -1.1, 1.1, 3, integrand, 50, volume_exact, surf_exact);
@ -289,13 +276,12 @@ int main(int argc, char* argv[])
// Visusalisation of a 2D case involving a single polynomial; this example corresponds to
// Figure 3, row 3, left column, https://doi.org/10.1016/j.jcp.2021.110720
if (false) {
auto phi = [](const uvector<real,2>& xx)
{
real x = xx(0)*2 - 1;
real y = xx(1)*2 - 1;
return -0.06225100787918392 + 0.1586472897571363*y + 0.5487135634635731*y*y +
x*(0.3478849533965025 - 0.3321074999999999*y - 0.5595163485848738*y*y) +
x*x*(0.7031095851739786 + 0.29459557349175747*y + 0.030425624999999998*y*y);
auto phi = [](const uvector<real, 2>& xx) {
real x = xx(0) * 2 - 1;
real y = xx(1) * 2 - 1;
return -0.06225100787918392 + 0.1586472897571363 * y + 0.5487135634635731 * y * y
+ x * (0.3478849533965025 - 0.3321074999999999 * y - 0.5595163485848738 * y * y)
+ x * x * (0.7031095851739786 + 0.29459557349175747 * y + 0.030425624999999998 * y * y);
};
outputQuadScheme<2>(phi, 0.0, 1.0, 3, 3, "exampleA");
std::cout << "\n\nQuadrature visualisation of a 2D case involving a single polynomial, corresponding\n";
@ -305,21 +291,22 @@ int main(int argc, char* argv[])
// Visusalisation of a 3D case involving a single polynomial; this example corresponds to
// Figure 3, row 3, right column, https://doi.org/10.1016/j.jcp.2021.110720
if (false){
auto phi = [](const uvector<real,3>& xx)
{
real x = xx(0)*2 - 1;
real y = xx(1)*2 - 1;
real z = xx(2)*2 - 1;
return -0.3003521613375472 - 0.22416584292513722*z + 0.07904600284034838*z*z +
y*(-0.022501556528537706 - 0.16299445153615613*z - 0.10968042065096766*z*z) +
y*y*(0.09321375574517882 - 0.07409794846221623*z + 0.09940785133211516*z*z) +
x*(0.094131400740032 - 0.11906280402685224*z - 0.010060302873268541*z*z +
y*y*(0.01448948481714108 - 0.0262370580373332*z - 0.08632912757566019*z*z) +
y*(0.08171132326327647 - 0.09286444275596013*z - 0.07651000354823911*z*z)) +
x*x*(-0.0914370528387867 + 0.09778971384044874*z - 0.1086777644685091*z*z +
y*y*(-0.04283439400630859 + 0.0750156999192893*z + 0.051754527934553866*z*z) +
y*(-0.052642188754328405 - 0.03538476045586772*z + 0.11117016852276898*z*z));
if (false) {
auto phi = [](const uvector<real, 3>& xx) {
real x = xx(0) * 2 - 1;
real y = xx(1) * 2 - 1;
real z = xx(2) * 2 - 1;
return -0.3003521613375472 - 0.22416584292513722 * z + 0.07904600284034838 * z * z
+ y * (-0.022501556528537706 - 0.16299445153615613 * z - 0.10968042065096766 * z * z)
+ y * y * (0.09321375574517882 - 0.07409794846221623 * z + 0.09940785133211516 * z * z)
+ x
* (0.094131400740032 - 0.11906280402685224 * z - 0.010060302873268541 * z * z
+ y * y * (0.01448948481714108 - 0.0262370580373332 * z - 0.08632912757566019 * z * z)
+ y * (0.08171132326327647 - 0.09286444275596013 * z - 0.07651000354823911 * z * z))
+ x * x
* (-0.0914370528387867 + 0.09778971384044874 * z - 0.1086777644685091 * z * z
+ y * y * (-0.04283439400630859 + 0.0750156999192893 * z + 0.051754527934553866 * z * z)
+ y * (-0.052642188754328405 - 0.03538476045586772 * z + 0.11117016852276898 * z * z));
};
outputQuadScheme<3>(phi, 0.0, 1.0, 3, 3, "exampleB");
std::cout << "\n\nQuadrature visualisation of a 3D case involving a single polynomial, corresponding\n";
@ -329,22 +316,20 @@ int main(int argc, char* argv[])
// Visusalisation of a 2D implicitly-defined domain involving the intersection of two polynomials; this example
// corresponds to the top-left example of Figure 15, https://doi.org/10.1016/j.jcp.2021.110720
if (false){
auto phi0 = [](const uvector<real,2>& xx)
{
real x = xx(0)*2 - 1;
real y = xx(1)*2 - 1;
return 0.014836540349115947 + 0.7022484024095262*y + 0.09974561176434385*y*y +
x*(0.6863910464417281 + 0.03805619999999999*y - 0.09440658332756446*y*y) +
x*x*(0.19266932968830816 - 0.2325190091204104*y + 0.2957473125000001*y*y);
if (false) {
auto phi0 = [](const uvector<real, 2>& xx) {
real x = xx(0) * 2 - 1;
real y = xx(1) * 2 - 1;
return 0.014836540349115947 + 0.7022484024095262 * y + 0.09974561176434385 * y * y
+ x * (0.6863910464417281 + 0.03805619999999999 * y - 0.09440658332756446 * y * y)
+ x * x * (0.19266932968830816 - 0.2325190091204104 * y + 0.2957473125000001 * y * y);
};
auto phi1 = [](const uvector<real,2>& xx)
{
real x = xx(0)*2 - 1;
real y = xx(1)*2 - 1;
return -0.18792528379702625 + 0.6713882473904913*y + 0.3778666084723582*y*y +
x*x*(-0.14480813208127946 + 0.0897755603159206*y - 0.141199875*y*y) +
x*(-0.6169311810674598 - 0.19449299999999994*y - 0.005459163675646665*y*y);
auto phi1 = [](const uvector<real, 2>& xx) {
real x = xx(0) * 2 - 1;
real y = xx(1) * 2 - 1;
return -0.18792528379702625 + 0.6713882473904913 * y + 0.3778666084723582 * y * y
+ x * x * (-0.14480813208127946 + 0.0897755603159206 * y - 0.141199875 * y * y)
+ x * (-0.6169311810674598 - 0.19449299999999994 * y - 0.005459163675646665 * y * y);
};
outputQuadScheme<2>(phi0, phi1, 0.0, 1.0, 3, 3, "exampleC");
std::cout << "\n\nQuadrature visualisation of a 2D implicitly-defined domain involving the\n";
@ -353,7 +338,8 @@ int main(int argc, char* argv[])
std::cout << "(XML VTP file format).\n";
}
module_test();
// module_test();
testMain();
return 0;
}

179
gjj/myDebug.hpp

@ -0,0 +1,179 @@
#include <bitset>
#include <iostream>
#include <booluarray.hpp>
#include <cstddef>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include "bernstein.hpp"
#include "quadrature_multipoly.hpp"
#include "real.hpp"
#include "uvector.hpp"
#include "vector"
#include "xarray.hpp"
using namespace algoim;
// Driver method which takes a functor phi defining a single polynomial in the reference
// rectangle [xmin, xmax]^N, of Bernstein degree P, along with an integrand function,
// and performances a q-refinement convergence study, comparing the computed integral
// with the given exact answers, for 1 <= q <= qMax.
template <int N, typename Phi, typename F>
void qConv1(const Phi& phi,
real xmin,
real xmax,
uvector<int, N> P,
const F& integrand,
int qMax,
real volume_exact,
real surf_exact)
{
// Construct Bernstein polynomial by mapping [0,1] onto bounding box [xmin,xmax]
xarray<real, N> phipoly(nullptr, P);
algoim_spark_alloc(real, phipoly);
bernstein::bernsteinInterpolate<N>([&](const uvector<real, N>& x) { return phi(xmin + x * (xmax - xmin)); }, phipoly);
// Build quadrature hierarchy
ImplicitPolyQuadrature<N> ipquad(phipoly);
// Functional to evaluate volume and surface integrals of given integrand
real volume, surf;
auto compute = [&](int q) {
volume = 0.0;
surf = 0.0;
// compute volume integral over {phi < 0} using AutoMixed strategy
ipquad.integrate(AutoMixed, q, [&](const uvector<real, N>& x, real w) {
if (bernstein::evalBernsteinPoly(phipoly, x) < 0) volume += w * integrand(xmin + x * (xmax - xmin));
});
// compute surface integral over {phi == 0} using AutoMixed strategy
ipquad.integrate_surf(AutoMixed, q, [&](const uvector<real, N>& x, real w, const uvector<real, N>& wn) {
surf += w * integrand(xmin + x * (xmax - xmin));
});
// scale appropriately
volume *= pow(xmax - xmin, N);
surf *= pow(xmax - xmin, N - 1);
};
// Compute results for all q and output in a convergence table
for (int q = 1; q <= qMax; ++q) {
compute(q);
std::cout << q << ' ' << volume << ' ' << surf << ' ' << std::abs(volume - volume_exact) / volume_exact << ' '
<< std::abs(surf - surf_exact) / surf_exact << std::endl;
}
}
// Driver method which takes two phi functors defining two polynomials in the reference
// rectangle [xmin, xmax]^N, each of of Bernstein degree P, builds a quadrature scheme with the
// given q, and outputs it for visualisation in a set of VTP XML files
template <int N, typename F1, typename F2, typename F>
void qConvMultiPloy(const F1& fphi1,
const F2& fphi2,
real xmin,
real xmax,
const uvector<int, N>& P,
const F& integrand,
int q,
std::string qfile)
{
// Construct phi by mapping [0,1] onto bounding box [xmin,xmax]
xarray<real, N> phi1(nullptr, P), phi2(nullptr, P);
algoim_spark_alloc(real, phi1, phi2);
bernstein::bernsteinInterpolate<N>([&](const uvector<real, N>& x) { return fphi1(xmin + x * (xmax - xmin)); }, phi1);
// bernstein::bernsteinInterpolate<N>([&](const uvector<real, N>& x) { return fphi2(xmin + x * (xmax - xmin)); }, phi2);
// Build quadrature hierarchy
// ImplicitPolyQuadrature<N> ipquad(phi1, phi2);
ImplicitPolyQuadrature<N> ipquad(phi1);
// Functional to evaluate volume and surface integrals of given integrand
real volume, surf;
auto compute = [&](int q) {
volume = 0.0;
surf = 0.0;
// compute volume integral over {phi < 0} using AutoMixed strategy
ipquad.integrate(AutoMixed, q, [&](const uvector<real, N>& x, real w) {
// if (bernstein::evalBernsteinPoly(phi1, x) < 0 && bernstein::evalBernsteinPoly(phi2, x) < 0)
if (bernstein::evalBernsteinPoly(phi1, x) < 0) volume += w * integrand(xmin + x * (xmax - xmin));
});
// compute surface integral over {phi == 0} using AutoMixed strategy
ipquad.integrate_surf(AutoMixed, q, [&](const uvector<real, N>& x, real w, const uvector<real, N>& wn) {
surf += w * integrand(xmin + x * (xmax - xmin));
});
// scale appropriately
volume *= pow(xmax - xmin, N);
surf *= pow(xmax - xmin, N - 1);
};
compute(q);
std::cout << "q volume: " << volume << std::endl;
}
void testMultiPloys()
{
// Visusalisation of a 2D implicitly-defined domain involving the intersection of two polynomials; this example
// corresponds to the top-left example of Figure 15, https://doi.org/10.1016/j.jcp.2021.110720
if (true) {
auto phi0 = [](const uvector<real, 3>& xx) {
real x = xx(0);
real y = xx(1);
real z = xx(2);
return x * x + y * y + z * z - 1;
};
auto phi1 = [](const uvector<real, 3>& xx) {
real x = xx(0);
real y = xx(1);
real z = xx(2);
return x * x + y * y + z * z - 1;
// real x = xx(0);
// real y = xx(1);
// real z = xx(2);
// return x * y * z;
};
// auto phi0 = [](const uvector<real, 3>& xx) {
// real x = xx(0) * 2 - 1;
// real y = xx(1) * 2 - 1;
// return 0.014836540349115947 + 0.7022484024095262 * y + 0.09974561176434385 * y * y
// + x * (0.6863910464417281 + 0.03805619999999999 * y - 0.09440658332756446 * y * y)
// + x * x * (0.19266932968830816 - 0.2325190091204104 * y + 0.2957473125000001 * y * y);
// };
// auto phi1 = [](const uvector<real, 3>& xx) {
// real x = xx(0) * 2 - 1;
// real y = xx(1) * 2 - 1;
// return -0.18792528379702625 + 0.6713882473904913 * y + 0.3778666084723582 * y * y
// + x * x * (-0.14480813208127946 + 0.0897755603159206 * y - 0.141199875 * y * y)
// + x * (-0.6169311810674598 - 0.19449299999999994 * y - 0.005459163675646665 * y * y);
// };
auto integrand = [](const uvector<real, 3>& x) { return 1.0; };
qConvMultiPloy<3>(phi0, phi1, -1.0, 1.0, 3, integrand, 3, "exampleC");
std::cout << "\n\nQuadrature visualisation of a 2D implicitly-defined domain involving the\n";
std::cout << "intersection of two polynomials, corresponding to the top-left example of Figure 15,\n";
std::cout << "https://doi.org/10.1016/j.jcp.2021.110720, written to exampleC-xxxx.vtp files\n";
std::cout << "(XML VTP file format).\n";
}
}
void testBitSet()
{
std::bitset<10> set(128);
set.set();
std::cout << set << std::endl;
}
void testBooluarray()
{
algoim::booluarray<2, 3> tmp;
tmp(0) = true;
tmp(1) = true;
tmp(2) = true;
std::cout << tmp.bits << std::endl;
}
void testMain()
{
testBooluarray();
testMultiPloys();
}

0
gjj/myDebugTool.hpp

Loading…
Cancel
Save