Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for 'CaseFunction' written in C# #797

Merged
merged 7 commits into from
Jul 13, 2023
116 changes: 116 additions & 0 deletions arcane/src/arcane/core/CaseFunction2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
//-----------------------------------------------------------------------------
// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: Apache-2.0
//-----------------------------------------------------------------------------
/*---------------------------------------------------------------------------*/
/* CaseFunction2.h (C) 2000-2023 */
/* */
/* Fonction du jeu de données avec type de valeur explicite. */
/*---------------------------------------------------------------------------*/
#ifndef ARCANE_CORE_CASEFUNCTION2_H
#define ARCANE_CORE_CASEFUNCTION2_H
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

#include "arcane/utils/Real3.h"
#include "arcane/core/CaseFunction.h"

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

namespace Arcane
{

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*!
* \internal
* \brief Implémentation de CaseFunction permettant de retourner directement
* la valeur associée à un paramètre sans passer par une référence.
*
* Cela est principalement utilisé pour simplifier les extensions C# en évitant
* les différentes surcharges de value().
*
* Pour utiliser cette classe, il faut implémenter les méthodes 'valueAs*'
* pour les deux types d'argument \a Integer et \a Real et pour les
* différents types de retour possibles.
*/
class ARCANE_CORE_EXPORT CaseFunction2

Check warning on line 40 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L40

Added line #L40 was not covered by tests
: public CaseFunction
{
public:

//! Construit une fonction du jeu de données.
explicit CaseFunction2(const CaseFunctionBuildInfo& cfbi)
: CaseFunction(cfbi)
{}

protected:

void value(Real param, Real& v) const override
{
v = valueAsReal(param);
}
void value(Real param, Integer& v) const override
{
v = valueAsInteger(param);
}
void value(Real param, bool& v) const override

Check warning on line 60 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L60

Added line #L60 was not covered by tests
{
v = valueAsBool(param);

Check warning on line 62 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L62

Added line #L62 was not covered by tests
}
void value(Real param, String& v) const override

Check warning on line 64 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L64

Added line #L64 was not covered by tests
{
v = valueAsString(param);

Check warning on line 66 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L66

Added line #L66 was not covered by tests
}
void value(Real param, Real3& v) const override

Check warning on line 68 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L68

Added line #L68 was not covered by tests
{
v = valueAsReal3(param);

Check warning on line 70 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L70

Added line #L70 was not covered by tests
}
void value(Integer param, Real& v) const override

Check warning on line 72 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L72

Added line #L72 was not covered by tests
{
v = valueAsReal(param);

Check warning on line 74 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L74

Added line #L74 was not covered by tests
}
void value(Integer param, Integer& v) const override

Check warning on line 76 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L76

Added line #L76 was not covered by tests
{
v = valueAsInteger(param);

Check warning on line 78 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L78

Added line #L78 was not covered by tests
}
void value(Integer param, bool& v) const override

Check warning on line 80 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L80

Added line #L80 was not covered by tests
{
v = valueAsBool(param);

Check warning on line 82 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L82

Added line #L82 was not covered by tests
}
void value(Integer param, String& v) const override

Check warning on line 84 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L84

Added line #L84 was not covered by tests
{
v = valueAsString(param);

Check warning on line 86 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L86

Added line #L86 was not covered by tests
}
void value(Integer param, Real3& v) const override

Check warning on line 88 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L88

Added line #L88 was not covered by tests
{
v = valueAsReal3(param);

Check warning on line 90 in arcane/src/arcane/core/CaseFunction2.h

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/core/CaseFunction2.h#L90

Added line #L90 was not covered by tests
}

public:

virtual Real valueAsReal(Real param) const = 0;
virtual Integer valueAsInteger(Real param) const = 0;
virtual bool valueAsBool(Real param) const = 0;
virtual String valueAsString(Real param) const = 0;
virtual Real3 valueAsReal3(Real param) const = 0;

virtual Real valueAsReal(Integer param) const = 0;
virtual Integer valueAsInteger(Integer param) const = 0;
virtual bool valueAsBool(Integer param) const = 0;
virtual String valueAsString(Integer param) const = 0;
virtual Real3 valueAsReal3(Integer param) const = 0;
};

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

} // namespace Arcane

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

#endif
1 change: 1 addition & 0 deletions arcane/src/arcane/core/InterfaceImpl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
#include "arcane/core/IPhysicalUnitConverter.h"
#include "arcane/core/IPhysicalUnit.h"
#include "arcane/core/IStandardFunction.h"
#include "arcane/core/CaseFunction2.h"
#include "arcane/core/IServiceAndModuleFactoryMng.h"
#include "arcane/core/IGhostLayerMng.h"
#include "arcane/core/IMeshUniqueIdMng.h"
Expand Down
1 change: 1 addition & 0 deletions arcane/src/arcane/core/srcs.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ set(ARCANE_ORIGINAL_SOURCES
CaseDatasetSource.h
CaseFunction.cc
CaseFunction.h
CaseFunction2.h
CaseOptions.h
CaseOptionServiceImpl.h
CaseOptionTypes.h
Expand Down
1 change: 1 addition & 0 deletions arcane/src/arcane/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,7 @@ if (ARCANE_HAS_DOTNET_TESTS)
if (ARCANE_HAS_TASKS)
arcane_add_csharp_test_sequential(hydro_cs_task testSimpleHydroCS-1.arc -m 15 -K 4)
endif()
arcane_add_csharp_test_sequential(casefunction_cs testCaseFunctionModule-1.arc)
endif()

arcane_add_test_parallel(loadbalance_test1 testLoadBalanceHydro-MeshPartitionerTester.arc 4 -m 30)
Expand Down
19 changes: 19 additions & 0 deletions arcane/src/arcane/tests/CaseFunctionTester.axl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" ?><!-- -*- SGML -*- -->

<module name="CaseFunctionTester" version="1.0" namespace-name="ArcaneTest">
<description>
Module de test des 'ICaseFunction'.
</description>

<options>
<simple name="real-time-multiply-2" type="real">
<description>Fonction f(x) -> x * 2.0</description>
</simple>
<simple name="int-iter-multiply-3" type="int32">
<description>Fonction f(x) -> x * 3.0</description>
</simple>
<simple name="real-norm-l2" type="real">
<description>Fonction f(x,position) -> x * normL2(position)</description>
</simple>
</options>
</module>
196 changes: 196 additions & 0 deletions arcane/src/arcane/tests/CaseFunctionTesterModule.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
//-----------------------------------------------------------------------------
// Copyright 2000-2023 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
// See the top-level COPYRIGHT file for details.
// SPDX-License-Identifier: Apache-2.0
//-----------------------------------------------------------------------------
/*---------------------------------------------------------------------------*/
/* CaseFunctionTesterModule.cc (C) 2000-2023 */
/* */
/* Module de test des 'CaseFunction'. */
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

#include "arcane/core/StandardCaseFunction.h"
#include "arcane/core/ITimeLoopMng.h"
#include "arcane/core/TimeLoopEntryPointInfo.h"
#include "arcane/core/TimeLoop.h"
#include "arcane/core/IMesh.h"

#include "arcane/tests/CaseFunctionTester_axl.h"

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

namespace ArcaneTest
{
using namespace Arcane;
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/*!
* \brief Module de test des 'ICaseFunction'.
*/
class CaseFunctionTesterModule
: public ArcaneCaseFunctionTesterObject
{
public:

explicit CaseFunctionTesterModule(const ModuleBuildInfo& mbi);

public:

static void staticInitialize(ISubDomain* sd);

public:

VersionInfo versionInfo() const override { return Arcane::VersionInfo(0, 1, 0); }

public:

void init();
void loop();

private:
};

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

ARCANE_DEFINE_STANDARD_MODULE(CaseFunctionTesterModule, CaseFunctionTester);

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

namespace
{
class StandardFuncTest
: public StandardCaseFunction
, public IBinaryMathFunctor<Real, Real3, Real>
{
public:

StandardFuncTest(const CaseFunctionBuildInfo& bi)
: StandardCaseFunction(bi)
{}

public:

virtual IBinaryMathFunctor<Real, Real3, Real>* getFunctorRealReal3ToReal()
{
return this;
}
virtual Real apply(Real r, Real3 r3)
{
return r + r3.normL2();
}
};

} // namespace

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

CaseFunctionTesterModule::
CaseFunctionTesterModule(const ModuleBuildInfo& mbi)
: ArcaneCaseFunctionTesterObject(mbi)
{
addEntryPoint(this, "Init",
&CaseFunctionTesterModule::init, IEntryPoint::WInit);
addEntryPoint(this, "Loop",
&CaseFunctionTesterModule::loop);
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

void CaseFunctionTesterModule::
staticInitialize(ISubDomain* sd)
{
ITimeLoopMng* tlm = sd->timeLoopMng();
ITimeLoop* time_loop = tlm->createTimeLoop("CaseFunctionTester");

{
List<TimeLoopEntryPointInfo> clist;
clist.add(TimeLoopEntryPointInfo("CaseFunctionTester.Init"));
time_loop->setEntryPoints(ITimeLoop::WInit, clist);
}

{
List<TimeLoopEntryPointInfo> clist;
clist.add(TimeLoopEntryPointInfo("CaseFunctionTester.Loop"));
time_loop->setEntryPoints(ITimeLoop::WComputeLoop, clist);
}

{
StringList clist;
clist.add("CaseFunctionTester");
time_loop->setRequiredModulesName(clist);
}

tlm->registerTimeLoop(time_loop);
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

void CaseFunctionTesterModule::
loop()
{
if (m_global_iteration() > 10)
subDomain()->timeLoopMng()->stopComputeLoop(true);

// Temps de début d'itération auquel sont calculées les valeurs des fonctions
Real global_time = m_global_old_time();
Int32 global_iter = m_global_iteration();

{
Real v1 = options()->realTimeMultiply2.value();
Real expected_v1 = global_time * 2.0;
info() << "Function: real-time-multiply-2: " << v1;
if (!math::isNearlyEqual(v1, expected_v1))
ARCANE_FATAL("Bad (1) value v={0} expected={1}", v1, expected_v1);

Check warning on line 151 in arcane/src/arcane/tests/CaseFunctionTesterModule.cc

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/tests/CaseFunctionTesterModule.cc#L151

Added line #L151 was not covered by tests
}
{
int v1 = options()->intIterMultiply3.value();
int expected_v1 = global_iter * 3;
info() << "Function: int-iter-multiply-3: " << v1;
if (v1 != expected_v1)
ARCANE_FATAL("Bad (2) value v={0} expected={1}", v1, expected_v1);

Check warning on line 158 in arcane/src/arcane/tests/CaseFunctionTesterModule.cc

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/tests/CaseFunctionTesterModule.cc#L158

Added line #L158 was not covered by tests
}
{
ICaseFunction* opt_function = options()->realNormL2.function();
IStandardFunction* scf = options()->realNormL2.standardFunction();
if (!scf)
ARCANE_FATAL("No standard case function for option 'real-norml2'");

Check warning on line 164 in arcane/src/arcane/tests/CaseFunctionTesterModule.cc

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/tests/CaseFunctionTesterModule.cc#L164

Added line #L164 was not covered by tests
auto* functor = scf->getFunctorRealReal3ToReal();
if (!functor)
ARCANE_FATAL("Standard function '{0}' is not convertible to f(Real,Real3) -> Real", opt_function->name());

Check warning on line 167 in arcane/src/arcane/tests/CaseFunctionTesterModule.cc

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/tests/CaseFunctionTesterModule.cc#L167

Added line #L167 was not covered by tests

VariableNodeReal3& node_coord = defaultMesh()->nodesCoordinates();
ENUMERATE_ (Node, inode, allNodes()) {
Real3 coord = node_coord[inode];
Real v1 = functor->apply(global_time, coord);
Real expected_v1 = global_time * coord.normL2();
info() << "Function: real-norml2: " << v1;
if (!math::isNearlyEqual(v1, expected_v1))
ARCANE_FATAL("Bad (3) value v={0} expected={1}", v1, expected_v1);

Check warning on line 176 in arcane/src/arcane/tests/CaseFunctionTesterModule.cc

View check run for this annotation

Codecov / codecov/patch

arcane/src/arcane/tests/CaseFunctionTesterModule.cc#L176

Added line #L176 was not covered by tests
}
}
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

void CaseFunctionTesterModule::
init()
{
m_global_deltat.assign(1.5);
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

} // End namespace ArcaneTest

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
Loading
Loading