Skip to content

Commit 6b7b912

Browse files
CauchyGreenStrain
1 parent 1ad9511 commit 6b7b912

File tree

3 files changed

+89
-4
lines changed

3 files changed

+89
-4
lines changed

src/geometry/functional.c

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3882,6 +3882,7 @@ typedef struct {
38823882
value *fields;
38833883
value *originalfields; // Original fields
38843884
value method; // Method dictionary
3885+
objectmesh *mref; // Reference mesh
38853886
vm *v;
38863887
} integralref;
38873888

@@ -4115,21 +4116,69 @@ static value integral_gradfn(vm *v, int nargs, value *args) {
41154116
return out;
41164117
}
41174118

4119+
/* -------------------
4120+
* Cauchy green strain
4121+
* ------------------- */
4122+
4123+
int cauchygreenhandle; // TL storage handle for CG tensor
4124+
4125+
/** Evaluates the cg strain tensor */
4126+
void integral_evaluatecg(vm *v, value *out) {
4127+
objectintegralelementref *elref = integral_getelementref(v);
4128+
4129+
if (!elref || !elref->iref->mref) {
4130+
morpho_runtimeerror(v, INTEGRAL_SPCLFN, CGTENSOR_FUNCTION); return;
4131+
}
4132+
4133+
int gdim=elref->nv-1; // Dimension of Gram matrix
4134+
4135+
objectmatrix *cg=object_newmatrix(gdim, gdim, true);
4136+
if (!cg) { morpho_runtimeerror(v, ERROR_ALLOCATIONFAILED); return; }
4137+
4138+
double gramrefel[gdim*gdim], gramdefel[gdim*gdim], qel[gdim*gdim], rel[gdim*gdim];
4139+
objectmatrix gramref = MORPHO_STATICMATRIX(gramrefel, gdim, gdim); // Gram matrices
4140+
objectmatrix gramdef = MORPHO_STATICMATRIX(gramdefel, gdim, gdim); //
4141+
objectmatrix q = MORPHO_STATICMATRIX(qel, gdim, gdim); // Inverse of Gram in source domain
4142+
objectmatrix r = MORPHO_STATICMATRIX(rel, gdim, gdim); // Intermediate calculations
4143+
4144+
linearelasticity_calculategram(elref->iref->mref->vert, elref->mesh->dim, elref->nv, elref->vid, &gramref);
4145+
linearelasticity_calculategram(elref->mesh->vert, elref->mesh->dim, elref->nv, elref->vid, &gramdef);
4146+
4147+
if (matrix_inverse(&gramref, &q)!=MATRIX_OK) return false;
4148+
if (matrix_mul(&gramdef, &q, &r)!=MATRIX_OK) return false;
4149+
4150+
matrix_identity(cg);
4151+
matrix_scale(cg, -0.5);
4152+
matrix_accumulate(cg, 0.5, &r);
4153+
4154+
vm_settlvar(v, cauchygreenhandle, MORPHO_OBJECT(cg));
4155+
*out = MORPHO_OBJECT(cg);
4156+
}
4157+
4158+
static value integral_cgfn(vm *v, int nargs, value *args) {
4159+
value out=MORPHO_NIL;
4160+
4161+
vm_gettlvar(v, cauchygreenhandle, &out);
4162+
if (MORPHO_ISNIL(out)) integral_evaluatecg(v, &out);
4163+
4164+
return out;
4165+
}
4166+
41184167
/* ----------------------
41194168
* General initialization
41204169
* ---------------------- */
41214170

41224171
/** Clears threadlocal storage */
41234172
void integral_cleartlvars(vm *v) {
4124-
int handles[] = { elementhandle, normlhandle, tangenthandle, -1 };
4173+
int handles[] = { elementhandle, normlhandle, tangenthandle, cauchygreenhandle, -1 };
41254174

41264175
for (int i=0; handles[i]>=0; i++) {
41274176
vm_settlvar(v, handles[i], MORPHO_NIL);
41284177
}
41294178
}
41304179

41314180
void integral_freetlvars(vm *v) {
4132-
int handles[] = { normlhandle, tangenthandle, -1 };
4181+
int handles[] = { normlhandle, tangenthandle, cauchygreenhandle, -1 };
41334182

41344183
for (int i=0; handles[i]>=0; i++) {
41354184
value val;
@@ -4150,17 +4199,23 @@ value functional_methodproperty;
41504199
bool integral_prepareref(objectinstance *self, objectmesh *mesh, grade g, objectselection *sel, integralref *ref) {
41514200
bool success=false;
41524201
value func=MORPHO_NIL;
4202+
value mref=MORPHO_NIL;
41534203
value field=MORPHO_NIL;
41544204
value method=MORPHO_NIL;
41554205
ref->v=NULL;
41564206
ref->nfields=0;
41574207
ref->method=MORPHO_NIL;
4208+
ref->mref=NULL;
41584209

41594210
if (objectinstance_getpropertyinterned(self, scalarpotential_functionproperty, &func) &&
41604211
MORPHO_ISCALLABLE(func)) {
41614212
ref->integrand=func;
41624213
success=true;
41634214
}
4215+
if (objectinstance_getpropertyinterned(self, linearelasticity_referenceproperty, &mref) &&
4216+
MORPHO_ISMESH(mref)) {
4217+
ref->mref=MORPHO_GETMESH(mref);
4218+
}
41644219
if (objectinstance_getpropertyinterned(self, functional_methodproperty, &method)) {
41654220
ref->method=method;
41664221
}
@@ -4290,10 +4345,13 @@ value LineIntegral_init(vm *v, int nargs, value *args) {
42904345
int nparams = -1;
42914346
int nfixed;
42924347
value method=MORPHO_NIL;
4348+
value mref=MORPHO_NIL;
42934349

4294-
if (builtin_options(v, nargs, args, &nfixed, 1,
4295-
functional_methodproperty, &method)) {
4350+
if (builtin_options(v, nargs, args, &nfixed, 2,
4351+
functional_methodproperty, &method,
4352+
linearelasticity_referenceproperty, &mref)) {
42964353
if (MORPHO_ISDICTIONARY(method)) objectinstance_setproperty(self, functional_methodproperty, method);
4354+
if (MORPHO_ISMESH(mref)) objectinstance_setproperty(self, linearelasticity_referenceproperty, mref);
42974355
} else {
42984356
morpho_runtimeerror(v, LINEINTEGRAL_ARGS);
42994357
return MORPHO_NIL;
@@ -4638,6 +4696,7 @@ void functional_initialize(void) {
46384696
builtin_addfunction(TANGENT_FUNCTION, integral_tangent, BUILTIN_FLAGSEMPTY);
46394697
builtin_addfunction(NORMAL_FUNCTION, integral_normal, BUILTIN_FLAGSEMPTY);
46404698
builtin_addfunction(GRAD_FUNCTION, integral_gradfn, BUILTIN_FLAGSEMPTY);
4699+
builtin_addfunction(CGTENSOR_FUNCTION, integral_cgfn, BUILTIN_FLAGSEMPTY);
46414700

46424701
morpho_defineerror(FUNC_INTEGRAND_MESH, ERROR_HALT, FUNC_INTEGRAND_MESH_MSG);
46434702
morpho_defineerror(FUNC_ELNTFND, ERROR_HALT, FUNC_ELNTFND_MSG);
@@ -4672,6 +4731,7 @@ void functional_initialize(void) {
46724731
elementhandle=vm_addtlvar();
46734732
tangenthandle=vm_addtlvar();
46744733
normlhandle=vm_addtlvar();
4734+
cauchygreenhandle=vm_addtlvar();
46754735

46764736
morpho_addfinalizefn(functional_finalize);
46774737
}

src/geometry/functional.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#define TANGENT_FUNCTION "tangent"
5757
#define NORMAL_FUNCTION "normal"
5858
#define GRAD_FUNCTION "grad"
59+
#define CGTENSOR_FUNCTION "cgtensor"
5960

6061
/* Functional names */
6162
#define LENGTH_CLASSNAME "Length"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import constants
2+
import meshtools
3+
4+
var mb = MeshBuilder()
5+
mb.addvertex([0,0])
6+
mb.addvertex([1,0])
7+
mb.addvertex([0,1])
8+
mb.addface([0,1,2])
9+
10+
var m = mb.build()
11+
12+
var mref = m.clone()
13+
14+
m.setvertexmatrix(2*m.vertexmatrix())
15+
16+
var phi = Field(m, fn (x,y) 1+x)
17+
18+
fn integrand(x, f) {
19+
var cg = cgtensor()
20+
return cg.trace()
21+
}
22+
23+
var a = AreaIntegral(integrand, phi, reference=mref)
24+
print a.total(m) // expect: 6

0 commit comments

Comments
 (0)