FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
FEBio/FEBioOpt/FEObjectiveFunction.cpp at develop · febiosoftware/FEBio · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
febiosoftware
/
FEBio
Public
Notifications
You must be signed in to change notification settings
Fork
89
Star
279
Code
Issues
32
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
FEBio
/
FEBioOpt
/
FEObjectiveFunction.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
474 lines (385 loc) · 10.6 KB
Breadcrumbs
FEBio
/
FEBioOpt
/
FEObjectiveFunction.cpp
Copy path
File metadata and controls
474 lines (385 loc) · 10.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
This file is part of the FEBio source code and is licensed under the MIT license
listed below.
See Copyright-FEBio.txt for details.
Copyright (c) 2021 University of Utah, The Trustees of Columbia University in
the City of New York, and others.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#
include
"
stdafx.h
"
#
include
"
FEObjectiveFunction.h
"
#
include
<
FECore/FEModel.h
>
#
include
<
FECore/log.h
>
//
=============================================================================
FEObjectiveFunction::FEObjectiveFunction
(FEModel* fem) : m_fem(fem)
{
m_verbose =
false
;
}
FEObjectiveFunction::~FEObjectiveFunction
()
{
}
bool
FEObjectiveFunction::Init
()
{
//
make sure we have a model
if
(m_fem ==
0
)
return
false
;
return
true
;
}
void
FEObjectiveFunction::Reset
()
{
}
double
FEObjectiveFunction::Evaluate
()
{
vector<
double
>
dummy
(
Measurements
());
return
Evaluate
(dummy);
}
double
FEObjectiveFunction::Evaluate
(vector<
double
>& y)
{
//
get the number of measurements
int
ndata =
Measurements
();
y.
resize
(ndata);
//
evaluate the functions
EvaluateFunctions
(y);
//
get the measurement vector
vector<
double
>
y0
(ndata);
GetMeasurements
(y0);
vector<
double
>
x
(ndata);
for
(
int
i =
0
; i < ndata; ++i) x[i] = i +
1
;
GetXValues
(x);
//
evaluate regression coefficient R^2
double
rsq =
RegressionCoefficient
(y0, y);
double
chisq =
0.0
;
if
(m_verbose)
feLog
(
"
CURRENT REQUIRED DIFFERENCE
\n
"
);
for
(
int
i =
0
; i<ndata; ++i)
{
double
dy = (y[i] - y0[i]);
chisq += dy*dy;
if
(m_verbose)
feLog
(
"
%15.10lg %15.10lg %15.10lg %15lg
\n
"
, x[i], y[i], y0[i],
fabs
(y[i] - y0[i]));
}
feLog
(
"
objective value: %lg
\n
"
, chisq);
feLog
(
"
regression coef: %lg
\n
"
, rsq);
return
chisq;
}
double
FEObjectiveFunction::RegressionCoefficient
(
const
std::vector<
double
>& y0,
const
std::vector<
double
>& y)
{
int
ndata = (
int
)y0.
size
();
double
xb =
0
, yb =
0
, xyb =
0
, x2b =
0
, y2b =
0
;
for
(
int
i =
0
; i < ndata; ++i)
{
xb += y0[i]; yb += y[i];
xyb += y0[i] * y[i];
x2b +=
pow
(y0[i],
2
); y2b +=
pow
(y[i],
2
);
}
xb /= ndata; yb /= ndata;
xyb /= ndata;
x2b /= ndata; y2b /= ndata;
double
D = (x2b - xb * xb) * (y2b - yb * yb);
double
rsq = (D !=
0
?
pow
(xyb - xb * yb,
2
) / D :
0
);
return
rsq;
}
//
=============================================================================
//
----------------------------------------------------------------------------
FEDataFitObjective::FEDataFitObjective
(FEModel* fem) : FEObjectiveFunction(fem)
{
m_src =
0
;
}
FEDataFitObjective::~FEDataFitObjective
()
{
if
(m_src)
delete
m_src;
m_src =
0
;
}
//
----------------------------------------------------------------------------
bool
FEDataFitObjective::Init
()
{
if
(
FEObjectiveFunction::Init
() ==
false
)
return
false
;
//
get the FE model
FEModel& fem = *
GetFEModel
();
//
initialize data source
if
(m_src ==
0
)
return
false
;
if
(m_src->
Init
() ==
false
)
return
false
;
return
true
;
}
//
----------------------------------------------------------------------------
//
set the data source
void
FEDataFitObjective::SetDataSource
(FEDataSource* src)
{
if
(m_src)
delete
m_src;
m_src = src;
}
//
----------------------------------------------------------------------------
//
set the data measurements
void
FEDataFitObjective::SetMeasurements
(
const
vector<pair<
double
,
double
> >& data)
{
m_lc.
Clear
();
int
n = (
int
)data.
size
();
for
(
int
i=
0
; i<n; ++i)
{
const
pair<
double
,
double
>& pt = data[i];
m_lc.
Add
(pt.
first
, pt.
second
);
}
}
//
----------------------------------------------------------------------------
void
FEDataFitObjective::Reset
()
{
//
call base class first
FEObjectiveFunction::Reset
();
m_src->
Reset
();
}
//
----------------------------------------------------------------------------
//
return the number of measurements. I.e. the size of the measurement vector
int
FEDataFitObjective::Measurements
()
{
return
m_lc.
Points
();
}
//
----------------------------------------------------------------------------
//
Evaluate the measurement vector and return in y0
void
FEDataFitObjective::GetMeasurements
(vector<
double
>& y0)
{
int
ndata = m_lc.
Points
();
y0.
resize
(ndata);
for
(
int
i =
0
; i<ndata; ++i) y0[i] = m_lc.
Point
(i).
y
;
}
void
FEDataFitObjective::GetXValues
(std::vector<
double
>& x)
{
x = m_x;
}
//
----------------------------------------------------------------------------
void
FEDataFitObjective::EvaluateFunctions
(vector<
double
>& f)
{
int
ndata = m_lc.
Points
();
m_x.
resize
(ndata);
for
(
int
i =
0
; i<ndata; ++i)
{
double
xi = m_lc.
Point
(i).
x
;
m_x[i] = xi;
f[i] = m_src->
Evaluate
(xi);
}
}
//
=============================================================================
bool
FEMinimizeObjective::ParamFunction::Init
()
{
if
(!
Function::Init
())
return
false
;
FEParamValue val = m_fem->
GetParameterValue
(
ParamString
(m_name.
c_str
()));
if
(val.
isValid
() ==
false
)
return
false
;
if
(val.
type
() !=
FE_PARAM_DOUBLE
)
return
false
;
m_var = (
double
*)val.
data_ptr
();
if
(m_var ==
nullptr
)
return
false
;
return
true
;
}
FEMinimizeObjective::FilterAvgFunction::FilterAvgFunction
(FEModel* fem, FELogElemSource* pd, FEElementSet* elemSet,
double
trg) : Function(fem)
{
m_pd = pd;
m_elemSet = elemSet;
m_y0 = trg;
}
bool
FEMinimizeObjective::FilterAvgFunction::Init
()
{
if
(!
Function::Init
())
return
false
;
if
(m_pd ==
nullptr
)
return
false
;
if
(m_elemSet ==
nullptr
)
return
false
;
return
true
;
}
double
FEMinimizeObjective::FilterAvgFunction::Value
()
const
{
int
NE
= m_elemSet->
Elements
();
double
sum =
0.0
;
for
(
int
i =
0
; i <
NE
; ++i)
{
sum += m_pd->
value
(m_elemSet->
Element
(i));
}
sum /= (
double
)
NE
;
return
sum;
}
FEMinimizeObjective::FEMinimizeObjective
(FEModel* fem) : FEObjectiveFunction(fem)
{
}
FEMinimizeObjective::~FEMinimizeObjective
()
{
for
(Function* f : m_Func)
delete
f;
m_Func.
clear
();
}
void
FEMinimizeObjective::AddFunction
(FEMinimizeObjective::Function* func)
{
m_Func.
push_back
(func);
}
bool
FEMinimizeObjective::Init
()
{
if
(
FEObjectiveFunction::Init
() ==
false
)
return
false
;
FEModel* fem =
GetFEModel
();
if
(fem ==
nullptr
)
return
false
;
int
N = (
int
) m_Func.
size
();
for
(
int
i=
0
; i<N; ++i)
{
Function& Fi = *m_Func[i];
if
(Fi.
Init
() ==
false
)
return
false
;
}
return
true
;
}
int
FEMinimizeObjective::Measurements
()
{
return
(
int
) m_Func.
size
();
}
void
FEMinimizeObjective::EvaluateFunctions
(vector<
double
>& f)
{
int
N = (
int
)m_Func.
size
();
f.
resize
(N);
for
(
int
i=
0
; i<N; ++i)
{
Function& Fi = *m_Func[i];
f[i] = Fi.
Value
();
}
}
void
FEMinimizeObjective::GetMeasurements
(vector<
double
>& y)
{
int
N = (
int
) m_Func.
size
();
y.
resize
(N);
for
(
int
i=
0
; i<N; ++i)
{
y[i] = m_Func[i]->
Target
();
}
}
//
=============================================================================
FEElementDataTable::FEElementDataTable
(FEModel* fem) : FEObjectiveFunction(fem)
{
m_var =
nullptr
;
}
void
FEElementDataTable::AddValue
(
int
elemID,
double
v)
{
Entry d;
d.
elemId
= elemID;
d.
target
= v;
d.
pe
=
nullptr
;
m_Data.
push_back
(d);
}
void
FEElementDataTable::SetVariable
(FELogElemSource* var)
{
m_var = var;
}
bool
FEElementDataTable::Init
()
{
FEModel& fem = *
GetFEModel
();
FEMesh& mesh = fem.
GetMesh
();
int
N = (
int
)m_Data.
size
();
if
(N ==
0
)
return
false
;
for
(
int
i =
0
; i < N; ++i)
{
Entry& di = m_Data[i];
FEElement* el = mesh.
FindElementFromID
(di.
elemId
);
if
(el ==
nullptr
)
return
false
;
di.
pe
= el;
}
return
true
;
}
//
return number of measurements (i.e. nr of terms in objective function)
int
FEElementDataTable::Measurements
()
{
return
(
int
)m_Data.
size
();
}
//
evaluate the function values (i.e. the f_i above)
void
FEElementDataTable::EvaluateFunctions
(vector<
double
>& f)
{
assert
(m_var);
int
N = (
int
)m_Data.
size
();
f.
resize
(N);
FEModel& fem = *
GetFEModel
();
FEMesh& mesh = fem.
GetMesh
();
for
(
int
i =
0
; i < N; ++i)
{
FEElement* pe = m_Data[i].
pe
;
//
calculate element average measure
double
val = m_var->
value
(*pe);
//
store result
f[i] = val;
}
}
//
get the measurement vector (i.e. the y_i above)
void
FEElementDataTable::GetMeasurements
(vector<
double
>& y)
{
int
N = (
int
)m_Data.
size
();
y.
resize
(N);
for
(
int
i =
0
; i < N; ++i)
{
y[i] = m_Data[i].
target
;
}
}
//
=============================================================================
FENodeDataTable::FENodeDataTable
(FEModel* fem) : FEObjectiveFunction(fem)
{
}
bool
FENodeDataTable::AddValue
(
int
nodeID, vector<
double
>& v)
{
if
(v.
size
() != m_var.
size
())
return
false
;
for
(
int
i =
0
; i < v.
size
(); ++i)
{
Entry d;
d.
nodeId
= nodeID;
d.
target
= v[i];
d.
ivar
= i;
d.
index
= -
1
;
m_Data.
push_back
(d);
}
return
true
;
}
void
FENodeDataTable::AddVariable
(FELogNodeData* var)
{
m_var.
push_back
(var);
}
bool
FENodeDataTable::Init
()
{
FEModel& fem = *
GetFEModel
();
FEMesh& mesh = fem.
GetMesh
();
int
N = (
int
)m_Data.
size
();
if
(N ==
0
)
return
false
;
for
(
int
i =
0
; i < N; ++i)
{
Entry& di = m_Data[i];
FENode* node = mesh.
FindNodeFromID
(di.
nodeId
);
if
(node ==
nullptr
)
return
false
;
di.
index
= di.
nodeId
-
1
;
//
NOTE: This assumes one-based indexing of node IDs.
}
return
true
;
}
//
return number of measurements (i.e. nr of terms in objective function)
int
FENodeDataTable::Measurements
()
{
return
(
int
)m_Data.
size
();
}
//
evaluate the function values (i.e. the f_i above)
void
FENodeDataTable::EvaluateFunctions
(vector<
double
>& f)
{
assert
(m_var.
size
() >
0
);
int
N = (
int
)m_Data.
size
();
f.
resize
(N);
FEModel& fem = *
GetFEModel
();
FEMesh& mesh = fem.
GetMesh
();
for
(
int
i =
0
; i < N; ++i)
{
int
n = m_Data[i].
index
;
int
v = m_Data[i].
ivar
;
//
calculate node value
double
val = m_var[v]->
value
(mesh.
Node
(n));
//
store result
f[i] = val;
}
}
//
get the measurement vector (i.e. the y_i above)
void
FENodeDataTable::GetMeasurements
(vector<
double
>& y)
{
int
N = (
int
)m_Data.
size
();
y.
resize
(N);
for
(
int
i =
0
; i < N; ++i)
{
y[i] = m_Data[i].
target
;
}
}
Back
|
FazBrowse Home
|
New Git URL