Skip to content

Commit ee0eb0b

Browse files
authored
Merge pull request #7 from pcarruscag/develop
update master
2 parents d50461c + fa11fea commit ee0eb0b

18 files changed

+68
-37
lines changed

.github/workflows/codeql.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ name: "CodeQL"
1313

1414
on:
1515
push:
16-
branches: [ "master" ]
16+
branches: [ "master", "develop" ]
1717
pull_request:
1818
# The branches below must be a subset of the branches above
19-
branches: [ "master" ]
19+
branches: [ "master", "develop" ]
2020
schedule:
2121
- cron: '41 14 * * 5'
2222

@@ -52,7 +52,7 @@ jobs:
5252
# Prefix the list here with "+" to use these queries and those in the config file.
5353

5454
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
55-
# queries: security-extended,security-and-quality
55+
queries: security-and-quality
5656

5757

5858
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, or Java).

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# FADO
22
Framework for Aerostructural Design Optimization
33

4-
[![Language grade: Python](https://img.shields.io/lgtm/grade/python/g/pcarruscag/FADO.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/pcarruscag/FADO/context:python)
5-
64
## Motivation
75
Q: "What is my purpose?"
86

__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from tools import GradientScale
1313
from drivers import ExteriorPenaltyDriver
1414
from drivers import ScipyDriver
15+
# Import IpOpt driver if possible.
1516
try: from drivers import IpoptDriver
1617
except: pass
1718
from optimizers import goldenSection

documentation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#

drivers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from drivers.exterior_penalty import *
22
from drivers.scipy_driver import *
3+
# Import IpOpt driver if possible.
34
try: from drivers.ipopt_driver import *
45
except: pass

drivers/base_driver.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#
@@ -46,6 +46,11 @@ def __init__(self,function,scale,bound=-1E20):
4646
self.function = function
4747
#end
4848

49+
class _Monitor:
50+
def __init__(self,function):
51+
self.function = function
52+
#end
53+
4954
def __init__(self):
5055
self._variables = []
5156
self._varScales = None
@@ -61,11 +66,13 @@ def __init__(self):
6166
self._objectives = []
6267
self._constraintsEQ = []
6368
self._constraintsGT = []
69+
self._monitors = []
6470

6571
# function values
6672
self._ofval = None
6773
self._eqval = None
6874
self._gtval = None
75+
self._monval = None
6976

7077
# map the start index of each variable in the design vector
7178
self._variableStartMask = None
@@ -123,6 +130,10 @@ def addUpLowBound(self,function,lower=-1.0,upper=1.0):
123130
self._constraintsGT.append(self._Constraint(function,scale,lower))
124131
self._constraintsGT.append(self._Constraint(function,-1*scale,upper))
125132

133+
def addMonitor(self,function):
134+
"""Add a function to monitor its value, does not participate in the optimization."""
135+
self._monitors.append(self._Monitor(function))
136+
126137
def setWorkingDirectory(self,dir):
127138
"""Set the name of the working directory where each iteration runs, it should not exist."""
128139
self._workDir = dir
@@ -201,6 +212,7 @@ def _preprocessVariables(self):
201212
self._getVarsAndParsFromFun(self._objectives)
202213
self._getVarsAndParsFromFun(self._constraintsEQ)
203214
self._getVarsAndParsFromFun(self._constraintsGT)
215+
self._getVarsAndParsFromFun(self._monitors)
204216

205217
# map the start index of each variable in the design vector
206218
idx = [0]
@@ -262,6 +274,8 @@ def _resetAllValueEvaluations(self):
262274
obj.function.resetValueEvalChain()
263275
for obj in self._constraintsGT:
264276
obj.function.resetValueEvalChain()
277+
for obj in self._monitors:
278+
obj.function.resetValueEvalChain()
265279
#end
266280

267281
def _resetAllGradientEvaluations(self):
@@ -271,6 +285,8 @@ def _resetAllGradientEvaluations(self):
271285
obj.function.resetGradientEvalChain()
272286
for obj in self._constraintsGT:
273287
obj.function.resetGradientEvalChain()
288+
for obj in self._monitors:
289+
obj.function.resetGradientEvalChain()
274290
#end
275291

276292
# Writes a line to the history file.
@@ -283,6 +299,8 @@ def _writeHisLine(self):
283299
hisLine += str(val)+self._hisDelim
284300
for val in self._gtval:
285301
hisLine += str(val)+self._hisDelim
302+
for val in self._monval:
303+
hisLine += str(val)+self._hisDelim
286304
hisLine = hisLine.strip(self._hisDelim)+"\n"
287305
self._hisObj.write(hisLine)
288306
#end

drivers/constrained_optim_driver.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#
@@ -73,6 +73,7 @@ def preprocess(self):
7373
self._ofval = np.zeros((len(self._objectives),))
7474
self._eqval = np.zeros((len(self._constraintsEQ),))
7575
self._gtval = np.zeros((len(self._constraintsGT),))
76+
self._monval = np.zeros((len(self._monitors),))
7677

7778
# write the header for the history file
7879
if self._hisObj is not None:
@@ -83,6 +84,8 @@ def preprocess(self):
8384
header += obj.function.getName()+self._hisDelim
8485
for obj in self._constraintsGT:
8586
header += obj.function.getName()+self._hisDelim
87+
for obj in self._monitors:
88+
header += obj.function.getName()+self._hisDelim
8689
header = header.strip(self._hisDelim)+"\n"
8790
self._hisObj.write(header)
8891
#end

drivers/exterior_penalty.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#
@@ -39,7 +39,7 @@ class ExteriorPenaltyDriver(ParallelEvalDriver):
3939
"""
4040
def __init__(self, tol, freq=40, rini=8, rmax=1024, factorUp=4, factorDown=0.5):
4141
ParallelEvalDriver.__init__(self, True)
42-
42+
4343
# parameters of the method
4444
self._tol = tol
4545
self._freq = freq
@@ -57,8 +57,6 @@ def __init__(self, tol, freq=40, rini=8, rmax=1024, factorUp=4, factorDown=0.5):
5757
self._old_grad = None
5858

5959
# timers, counters, flags
60-
self._funEval = 0
61-
self._jacEval = 0
6260
self._isInit = False
6361
self._isFeasible = False
6462
self._logRowFormat = ""
@@ -79,6 +77,7 @@ def _initialize(self):
7977
self._ofval = np.zeros((len(self._objectives),))
8078
self._eqval = np.zeros((len(self._constraintsEQ),))
8179
self._gtval = np.zeros((len(self._constraintsGT),))
80+
self._monval = np.zeros((len(self._monitors),))
8281

8382
self._eqpen = np.ones((len(self._constraintsEQ),))*self._rini
8483
self._gtpen = np.ones((len(self._constraintsGT),))*self._rini
@@ -102,6 +101,9 @@ def _initialize(self):
102101
headerData.append(obj.function.getName(w-1))
103102
headerData.append("PEN COEFF")
104103
self._logRowFormat += "{:>W.Pg}"*2
104+
for obj in self._monitors:
105+
headerData.append(obj.function.getName(w-1))
106+
self._logRowFormat += "{:>W.Pg}"
105107
# right-align, set width in format and a precision that fits it
106108
self._logRowFormat = self._logRowFormat.replace("W",str(w))+"\n"
107109
self._logRowFormat = self._logRowFormat.replace("P",str(min(8,w-7)))
@@ -120,6 +122,8 @@ def _initialize(self):
120122
header += obj.function.getName()+self._hisDelim
121123
for obj in self._constraintsGT:
122124
header += obj.function.getName()+self._hisDelim
125+
for obj in self._monitors:
126+
header += obj.function.getName()+self._hisDelim
123127
header = header.strip(self._hisDelim)+"\n"
124128
self._hisObj.write(header)
125129
#end
@@ -139,8 +143,10 @@ def _writeLogLine(self):
139143
for (g,r) in zip(self._gtval,self._gtpen):
140144
data.append(g)
141145
data.append(r)
146+
for f in self._monval:
147+
data.append(f)
142148
self._logObj.write(self._logRowFormat.format(*data))
143-
#end
149+
#end
144150

145151
def fun(self,x):
146152
"""Evaluate the penalized function at "x"."""
@@ -199,7 +205,7 @@ def _evaluateGradients(self,x):
199205

200206
# update penalties and params (evaluating the gradient concludes an outer iteration)
201207
if self._freq > 0:
202-
if self._jacEval % self._freq is 0: self.update()
208+
if self._jacEval % self._freq == 0: self.update()
203209

204210
# make copy to use as fallback
205211
self._old_grad[()] = self._grad

drivers/ipopt_driver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#

drivers/parallel_eval_driver.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#
@@ -72,6 +72,7 @@ def _addEvals(flist,vlist,jlist):
7272
_addEvals(self._objectives ,valEvals,jacEvals)
7373
_addEvals(self._constraintsEQ,valEvals,jacEvals)
7474
_addEvals(self._constraintsGT,valEvals,jacEvals)
75+
_addEvals(self._monitors ,valEvals,jacEvals)
7576

7677
# for each unique evaluation list its direct dependencies
7778
self._funEvalGraph = dict(zip(valEvals,[set() for i in range(len(valEvals))]))
@@ -91,6 +92,7 @@ def _addDependencies(flist,funGraph,jacGraph):
9192
_addDependencies(self._objectives ,self._funEvalGraph,self._jacEvalGraph)
9293
_addDependencies(self._constraintsEQ,self._funEvalGraph,self._jacEvalGraph)
9394
_addDependencies(self._constraintsGT,self._funEvalGraph,self._jacEvalGraph)
95+
_addDependencies(self._monitors ,self._funEvalGraph,self._jacEvalGraph)
9496
#end
9597

9698
# run the active evaluations of a dependency graph
@@ -171,6 +173,8 @@ def _evalJacInParallel(self):
171173
for evl in obj.function.getGradientEvalChain():
172174
active[evl] = True
173175

176+
# gradients are not needed for monitor functions
177+
174178
self._evalInParallel(self._jacEvalGraph, active)
175179

176180
self._jacTime += time.time()
@@ -225,6 +229,7 @@ def fetchValues(dst, src):
225229
fetchValues(self._ofval, self._objectives)
226230
fetchValues(self._eqval, self._constraintsEQ)
227231
fetchValues(self._gtval, self._constraintsGT)
232+
fetchValues(self._monval, self._monitors)
228233

229234
self._funTime += time.time()
230235

@@ -253,7 +258,7 @@ def fetchValues(dst, src):
253258
# when the results are read in "function.getGradient".
254259
def _evaluateGradients(self, x):
255260
# we assume that evaluating the gradients requires the functions
256-
self._evaluateFunctions(x)
261+
self._evaluateFunctions(x)
257262

258263
# lazy evaluation
259264
if self._jacReady: return False

drivers/scipy_driver.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#
@@ -27,10 +27,6 @@ class ScipyDriver(ConstrainedOptimizationDriver):
2727
"""
2828
def __init__(self):
2929
ConstrainedOptimizationDriver.__init__(self)
30-
31-
# list of constraints and variable bounds
32-
self._constraints = []
33-
self._bounds = []
3430
#end
3531

3632
def preprocess(self):
@@ -86,7 +82,7 @@ def fun(self, x):
8682
def grad(self, x):
8783
"""Method passed to SciPy to get the objective function gradient."""
8884
# Evaluates gradients and functions if necessary, otherwise it
89-
# simply combines and scales the results.
85+
# simply combines and scales the results.
9086
self._jacTime -= time.time()
9187
try:
9288
self._evaluateGradients(x)

evaluation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#

function.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#
@@ -187,6 +187,7 @@ def getGradient(self,mask=None):
187187
for var,file,parser in zip(self._variables,self._gradFiles,self._gradParse):
188188
grad = parser.read(file)
189189
if var.getSize() == 1:
190+
# Convert the value to a scalar if it is not yet.
190191
try: grad = sum(grad)
191192
except: pass
192193
#end
@@ -200,7 +201,7 @@ def getGradient(self,mask=None):
200201
idx += 1
201202
#end
202203
#end
203-
204+
204205
return gradient
205206
#end
206207

@@ -211,7 +212,7 @@ def _sequentialEval(self,evals):
211212
#end
212213
#end
213214

214-
def resetValueEvalChain(self):
215+
def resetValueEvalChain(self):
215216
self._resetEvals(self._funEval)
216217

217218
def resetGradientEvalChain(self):

optimizers/fletcher_reeves.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#

optimizers/line_searches.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019-2020, FADO Contributors (cf. AUTHORS.md)
1+
# Copyright 2019-2023, FADO Contributors (cf. AUTHORS.md)
22
#
33
# This file is part of FADO.
44
#
@@ -43,7 +43,7 @@ def goldenSection(fun,maxiter,f0=None,lbd0=1,tol=1e-3):
4343
x = [0.0, L2, x[2]-L2, x[2]]
4444
y = [fun(x[1]), fun(x[2])]
4545
feval += 2
46-
46+
4747
# iterate
4848
while feval < maxiter:
4949
if y[0] < y[1]: # keep left interval
@@ -120,7 +120,7 @@ def quadraticInterp(fun,maxiter,f0=None,lbd0=1,tol=1e-3):
120120

121121
# iterate
122122
y_ref = max(max(y),-min(y),tol)
123-
123+
124124
while True:
125125
# compute x_opt'
126126
det = (x[0]-x[1])*(x[1]-x[2])*(x[2]-x[0])

0 commit comments

Comments
 (0)