├── Utilities
├── cosAndSin.m
├── blkmat.m
├── catmat.m
├── split_harmonics.m
├── hbm_excitation_forces.m
├── combine_harmonics.m
├── unpackfreq.m
├── packfreq.m
├── hbm_excitation.m
├── unpackdof.m
├── phasor2ampl.m
├── packdof.m
├── aft_jacobian_fft.m
├── linear_jacobian.m
├── hbm_scaling.m
├── hbm_floquet.m
├── aft_jacobian_ifft.m
├── aft_jacobian.m
├── hbm_derivatives.m
└── hbm_objective.m
├── .gitignore
├── Test
├── test_obj.m
├── test_excite.m
├── test_odefun.m
├── test_params.m
├── test_model.m
├── test_model_allnl.m
├── hbm_solve_test.m
├── fft_test.m
├── hbm_amp_test.m
├── hbm_frf_test.m
└── hbm_bb_test.m
├── CITATION.cff
├── .gitattributes
├── Setup
├── setupLin.m
├── setupNonlin.m
├── setupHarm.m
└── setuphbm.m
├── Standard
├── hbm_output.m
├── hbm_states.m
├── get_time_series.m
├── hbm_balance.m
└── hbm_nonlinear.m
├── Generalised
├── hbm_output3d.m
├── hbm_states3d.m
├── get_time_series3d.m
├── hbm_balance3d.m
└── hbm_nonlinear3d.m
├── Functions
├── hbm_bb_plot.m
├── hbm_solve.m
├── hbm_res.m
├── hbm_amp_plot.m
├── hbm_frf_plot.m
├── hbm_amp.m
├── hbm_bb.m
└── hbm_frf.m
├── README.md
└── LICENSE
/Utilities/cosAndSin.m:
--------------------------------------------------------------------------------
1 | function [cl, sl] = cosAndSin(ph)
2 | cl = cos(ph); sl = sin(ph);
--------------------------------------------------------------------------------
/Utilities/blkmat.m:
--------------------------------------------------------------------------------
1 | function C = blkmat(A)
2 | B = num2cell(A,[1,2]);
3 | C = blkdiag(B{:});
--------------------------------------------------------------------------------
/Utilities/catmat.m:
--------------------------------------------------------------------------------
1 | function C = catmat(A,dim)
2 | B = num2cell(A,[1,2]);
3 | C = cat(dim,B{:});
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.MATLABDriveTag
2 | *.bak
3 | *.asv
4 | *.m~
5 | *.DS_Store
6 | desktop.ini
7 | thumbs.db
--------------------------------------------------------------------------------
/Test/test_obj.m:
--------------------------------------------------------------------------------
1 | function G = test_obj(X,U,F,hbm,problem,w0)
2 | P = problem.P;
3 | H = X(2,P.iDof)./U(2,P.iInput);
4 | % G = -(angle(H) + pi/2).^2;
5 | G = abs(H);
--------------------------------------------------------------------------------
/Utilities/split_harmonics.m:
--------------------------------------------------------------------------------
1 | function Xfft2 = split_harmonics(Xfft,harm)
2 | for i = 1:length(harm.group)
3 | Xfft2{i} = Xfft(harm.group{i}.iFreq,harm.group{i}.iDof);
4 | end
--------------------------------------------------------------------------------
/CITATION.cff:
--------------------------------------------------------------------------------
1 | cff-version: 1.2.0
2 | message: "If you use this library, please cite it as below."
3 | authors:
4 | - family-names: Haslam
5 | given-names: Alexander
6 | title: HarmLAB
7 | url: https://github.com/alxhslm/HarmLAB
8 |
--------------------------------------------------------------------------------
/Utilities/hbm_excitation_forces.m:
--------------------------------------------------------------------------------
1 | function frf = hbm_excitation_forces(problem,frf)
2 | for i = 1:length(frf)
3 | frf(i).Fe = (problem.Ku*frf(i).U.' + problem.Cu*(1i*frf(i).W.*frf(i).U).' + problem.Mu*(frf(i).W.^2.*frf(i).U).').';
4 | end
--------------------------------------------------------------------------------
/Utilities/combine_harmonics.m:
--------------------------------------------------------------------------------
1 | function Xfft2 = combine_harmonics(Xfft,harm)
2 | NDof = sum([harm.group{:}.NDof]);
3 | Xfft2 = zeros(harm.NFreq,NDof);
4 | for i = 1:length(harm.group)
5 | Xfft2(harm.group{i}.iFreq,harm.group{i}.iDof) = Xfft{i};
6 | end
--------------------------------------------------------------------------------
/Utilities/unpackfreq.m:
--------------------------------------------------------------------------------
1 | function Xfull = unpackfreq(X,NHarm,iSub)
2 | Xfull = zeros(2*NHarm(1)+1,2*NHarm(2)+1,size(X,2));
3 | for i = 1:length(iSub)
4 | Xfull(iSub(i,1),iSub(i,2),:) = X(i,:);
5 | end
6 | Xfull = 0.5*(Xfull + conj(flip(flip(Xfull,1),2)));
--------------------------------------------------------------------------------
/Test/test_excite.m:
--------------------------------------------------------------------------------
1 | function U = test_excite(hbm,problem,w0)
2 | P = problem.P;
3 | U = zeros(hbm.harm.NFreq,1);
4 | U(1) = P.f0;
5 | U(2) = P.f;
6 | if hbm.harm.NHarm(2)>0
7 | ii = hbm.harm.kHarm(:,1) == 0 & hbm.harm.kHarm(:,2) == 1;
8 | U(ii) = P.f2;
9 | end
--------------------------------------------------------------------------------
/Utilities/packfreq.m:
--------------------------------------------------------------------------------
1 | function X = packfreq(Xfull,iSub)
2 | X = zeros(length(iSub),size(Xfull,3));
3 |
4 | for i = 1:length(iSub)
5 | X(i,:) = Xfull(iSub(i,1),iSub(i,2),:);
6 | end
7 |
8 | %now double everything as we want single-sided coefficients
9 | X(2:end,:) = 2*X(2:end,:); %except 0Hz
10 |
--------------------------------------------------------------------------------
/Utilities/hbm_excitation.m:
--------------------------------------------------------------------------------
1 | function Fe = hbm_excitation(hbm,problem,w0,U)
2 | w = hbm.harm.kHarm*(repmat(hbm.harm.rFreqBase',1,size(w0,2)) .* w0);
3 | w = permute(w,[1 3 2]);
4 | Wu = repmat(1i*w,1,size(U,2),1);
5 | Udot = U.*Wu;
6 | Uddot = Udot.*Wu;
7 |
8 | Fe = mtransposex(mtimesx(problem.Ku,mtransposex(U)) + mtimesx(problem.Cu,mtransposex(Udot)) + mtimesx(problem.Mu,mtransposex(Uddot)));
--------------------------------------------------------------------------------
/Utilities/unpackdof.m:
--------------------------------------------------------------------------------
1 | function Xc = unpackdof(X,NHarm,NDof,iRetain)
2 | NComp = prod(2*NHarm+1);
3 | if nargin < 4
4 | iRetain = 1:(NDof*NComp);
5 | end
6 | Xfull = zeros(NDof*NComp,size(X,2));
7 | Xfull(iRetain,:) = X;
8 |
9 | if ~isempty(Xfull)
10 | X = permute(reshape(Xfull,NDof,NComp,[]),[2 1 3]);
11 | else
12 | X = zeros(NComp,NDof,size(X,2));
13 | end
14 | Xc = [X(1,:,:); X(2:2:end,:,:) + 1i*X(3:2:end,:,:)];
--------------------------------------------------------------------------------
/Utilities/phasor2ampl.m:
--------------------------------------------------------------------------------
1 | function [amp,x] = phasor2ampl(X,Nfft)
2 | if ~iscell(X)
3 | X = {X};
4 | end
5 |
6 | if nargin < 2
7 | Nfft = 1000;
8 | end
9 |
10 | NHarm = size(X{1},1)-1;
11 |
12 | for i = 1:length(X)
13 | x{i} = permute(freq2time(X{i},NHarm,Nfft),[2 3 1]);
14 | x{i} = cat(3,x{i},x{i}(:,:,1));
15 | amp{i} = max(x{i},[],3) - min(x{i},[],3);
16 | end
17 |
18 | if length(x) == 1
19 | x = x{1};
20 | amp = amp{1};
21 | end
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.mlx -crlf -diff -merge
2 | *.mat -crlf -diff -merge
3 | *.fig -crlf -diff -merge
4 | *.p -crlf -diff -merge
5 | *.slx -crlf -diff -merge
6 | *.mdl -crlf -diff -merge
7 | *.mdlp -crlf -diff -merge
8 | *.slxp -crlf -diff -merge
9 | *.sldd -crlf -diff -merge
10 | *.mexa64 -crlf -diff -merge
11 | *.mexw64 -crlf -diff -merge
12 | *.mexmaci64 -crlf -diff -merge
13 | *.xlsx -crlf -diff -merge
14 | *.docx -crlf -diff -merge
15 | *.pdf -crlf -diff -merge
16 | *.jpg -crlf -diff -merge
17 | *.png -crlf -diff -merge
--------------------------------------------------------------------------------
/Utilities/packdof.m:
--------------------------------------------------------------------------------
1 | function X = packdof(Xc,iRetain)
2 | %order is
3 | % X = [X0; @ 0
4 | % real(X1) @ 1*w0
5 | % imag(X1) @ 1*w0
6 | % real(X2) @ 2*w0
7 | % imag(X2) @ 2*w0
8 | % ...
9 | % real(XN) @ NHarm*w0
10 | % imag(XN)] @ NHarm*w0
11 |
12 | X = mtransposex([real(Xc(2:end,:,:)) imag(Xc(2:end,:,:))]);
13 | X0 = permute(Xc(1,:,:),[2 3 1]);
14 |
15 | X = [X0; reshape(X,[],size(X,3))];
16 |
17 | if nargin < 2
18 | iRetain = 1:size(X,1);
19 | end
20 | X = X(iRetain,:);
21 |
--------------------------------------------------------------------------------
/Setup/setupLin.m:
--------------------------------------------------------------------------------
1 | function lin = setupLin(harm,problem)
2 | %% state-jacobian
3 | [lin.Ak,lin.Ac,lin.Am,lin.Ax] = linear_jacobian(harm,problem.K,problem.C,problem.M,problem.G);
4 |
5 | %% input-jacobian
6 | [lin.Bk,lin.Bc,lin.Bm,lin.Bx] = linear_jacobian(harm,problem.Ku,problem.Cu,problem.Mu);
7 |
8 | %% constant terms
9 | lin.b = [problem.F0;
10 | zeros(problem.NDof*(harm.NComp-1),1)];
11 |
12 | %% floquet multipliers
13 | [floquet.D1xdot,floquet.D1xddot] = linear_jacobian(harm,problem.C,problem.M,0*problem.M);
14 | floquet.D1Gxdot = linear_jacobian(harm,problem.G,0*problem.M,0*problem.M);
15 | floquet.D2 = kron(eye(harm.NComp),problem.M);
16 |
17 | lin.floquet = floquet;
--------------------------------------------------------------------------------
/Utilities/aft_jacobian_fft.m:
--------------------------------------------------------------------------------
1 | function Ju = aft_jacobian_fft(harm)
2 | Nfft = harm.Nfft;
3 | NComp = harm.NComp;
4 |
5 | theta1 = 2*pi/Nfft(1)*(0:(Nfft(1)-1));
6 | theta2 = 2*pi/Nfft(2)*(0:(Nfft(2)-1));
7 | [theta1,theta2] = ndgrid(theta1,theta2);
8 | theta0 = [theta1(:)'; theta2(:)'];
9 |
10 | theta1 = permute(theta0(1,:),[1 3 2]);
11 | theta2 = permute(theta0(2,:),[1 3 2]);
12 |
13 | ju = zeros(NComp,1,Nfft(1)*Nfft(2));
14 | ju(1,1,:) = 1/(Nfft(1)*Nfft(2));
15 | for l = 1:(harm.NFreq-1)
16 | ph = harm.kHarm(l+1,1)*theta1 + harm.kHarm(l+1,2)*theta2;
17 | cl = cos(ph); sl = sin(ph);
18 | ju(2*l,:,:) = 2*cl/(Nfft(1)*Nfft(2));
19 | ju(2*l+1,:,:) = -2*sl/(Nfft(1)*Nfft(2));
20 | end
21 | Ju = ju;
--------------------------------------------------------------------------------
/Standard/hbm_output.m:
--------------------------------------------------------------------------------
1 | function o = hbm_output(hbm,problem,w,u,x)
2 | NInput = problem.NInput;
3 | NDof = problem.NDof;
4 | NHarm = hbm.harm.NHarm;
5 |
6 | ii = find(NHarm ~= 0);
7 | r = hbm.harm.rFreqRatio(ii);
8 | w0 = w * r + hbm.harm.wFreq0(ii);
9 |
10 | if size(x,1) == hbm.harm.NFreq && size(x,2) == problem.NDof
11 | X = x;
12 | U = u;
13 | elseif isvector(x) && size(x,1) == hbm.harm.NComp*problem.NDof
14 | X = unpackdof(x,NFreq-1,NDof);
15 | U = unpackdof(u,NFreq-1,NInput);
16 | end
17 |
18 | %work out the time domain
19 | States = hbm_states(w0,X,U,hbm);
20 |
21 | %push through the nl system
22 | o = feval(problem.model,'output',States,hbm,problem);
23 |
24 | %finally convert into a fourier series
25 | O = hbm.nonlin.FFT*o.';
26 |
27 | if ndims(x) < 2
28 | o = packdof(O);
29 | else
30 | o = O;
31 | end
--------------------------------------------------------------------------------
/Generalised/hbm_output3d.m:
--------------------------------------------------------------------------------
1 | function o = hbm_output3d(hbm,problem,w,u,x)
2 | if hbm.options.bUseStandardHBM
3 | o = hbm_output(hbm,problem,w,u,x);
4 | return;
5 | end
6 |
7 | NInput = problem.NInput;
8 | NDof = problem.NDof;
9 |
10 | r = hbm.harm.rFreqRatio;
11 | w0 = w .* r + hbm.harm.wFreq0;
12 |
13 | if size(x,1) == hbm.harm.NFreq && size(x,2) == problem.NDof
14 | X = x;
15 | U = u;
16 | elseif isvector(x) && size(x,1) == hbm.harm.NComp*problem.NDof
17 | X = unpackdof(x,NFreq-1,NDof);
18 | U = unpackdof(u,NFreq-1,NInput);
19 | end
20 |
21 | %work out the time domain
22 | States = hbm_states3d(w0,X,U,hbm);
23 |
24 | %push through the nl system
25 | o = feval(problem.model,'output',States,hbm,problem);
26 |
27 | %finally convert into a fourier series
28 | O = hbm.nonlin.FFT*o.';
29 |
30 | if size(x,1) == hbm.harm.NFreq && size(x,2) == problem.NDof
31 | o = O;
32 | else
33 | o = packdof(O);
34 | end
--------------------------------------------------------------------------------
/Test/test_odefun.m:
--------------------------------------------------------------------------------
1 | function Mydot = test_odefun(t,y,w0,U,hbm,problem)
2 | NInput = size(U,2);
3 | w = hbm.harm.kHarm*w0(:);
4 | Wu = repmat(1i*w,1,NInput);
5 |
6 | ph = repmat(permute(exp(1i*w*t), [3 2 1]),NInput,1);
7 | amp = repmat(permute(U, [2 3 1]),1,length(t));
8 | ampdot = repmat(permute(U.*Wu, [2 3 1]),1,length(t));
9 | ampddot = repmat(permute(U.*(Wu.^2),[2 3 1]),1,length(t));
10 | u = sum(real(amp .* ph) ,3);
11 | udot = sum(real(ampdot .* ph) ,3);
12 | uddot = sum(real(ampddot .* ph) ,3);
13 |
14 | %extract x and xdot
15 | NDof = problem.NDof;
16 | x = y(1:NDof,:);
17 | xdot = y(NDof+1:end,:);
18 |
19 | Fe = problem.Ku*u + problem.Cu*udot + problem.Mu*uddot;
20 | Fl = -problem.C*xdot - problem.K*x;
21 |
22 | States = struct('t',t,'x',x,'xdot',xdot,'u',u,'udot',udot,'uddot',uddot,'w0',w0);
23 | Fnl = -test_model('nl' ,States,hbm,problem);
24 |
25 | Ftot = Fl + Fnl + Fe;
26 |
27 | %finally assemble the xdot vector
28 | Mydot = [xdot;
29 | Ftot];
--------------------------------------------------------------------------------
/Standard/hbm_states.m:
--------------------------------------------------------------------------------
1 | function States = hbm_states(w0,X,U,hbm)
2 | ii = find(hbm.harm.NHarm ~= 0);
3 |
4 | NFreq = hbm.harm.NFreq;
5 | Nfft = hbm.harm.Nfft(ii);
6 | kHarm = hbm.harm.kHarm(:,ii);
7 | wBase = hbm.harm.rFreqBase(ii)*w0;
8 |
9 | %unpack the inputs
10 | w = kHarm*wBase;
11 | States.t = (0:Nfft-1)/Nfft*2*pi/wBase;
12 |
13 | %compute the fourier coefficients of the derivatives
14 | Wx = repmat(1i*w,1,size(X,2));
15 | Xdot = X.*Wx;
16 | Xddot = Xdot.*Wx;
17 |
18 | %precompute the external inputs
19 | Wu = repmat(1i*w,1,size(U,2));
20 | Udot = U.*Wu;
21 | Uddot = Udot.*Wu;
22 |
23 | States.w0 = w0;
24 | States.wBase = wBase;
25 |
26 | %create the time series from the fourier series
27 | States.x = real(hbm.nonlin.IFFT*X).';
28 | States.xdot = real(hbm.nonlin.IFFT*Xdot).';
29 | States.xddot = real(hbm.nonlin.IFFT*Xddot).';
30 |
31 | %create the vector of inputs
32 | States.u = real(hbm.nonlin.IFFT*U).';
33 | States.udot = real(hbm.nonlin.IFFT*Udot).';
34 | States.uddot = real(hbm.nonlin.IFFT*Uddot).';
35 |
--------------------------------------------------------------------------------
/Generalised/hbm_states3d.m:
--------------------------------------------------------------------------------
1 | function States = hbm_states3d(w0,X,U,hbm)
2 | Nfft = hbm.harm.Nfft;
3 | kHarm = hbm.harm.kHarm;
4 | wBase = hbm.harm.rFreqBase.*w0;
5 |
6 | %unpack the inputs
7 | w = kHarm*wBase';
8 | t1 = (0:Nfft(1)-1)/Nfft(1)*2*pi/(wBase(1)+eps);
9 | t2 = (0:Nfft(2)-1)/Nfft(2)*2*pi/(wBase(2)+eps);
10 | [t1,t2] = ndgrid(t1,t2);
11 | States.t = [t1(:) t2(:)].';
12 |
13 | %compute the fourier coefficients of the derivatives
14 | Wx = repmat(1i*w,1,size(X,2));
15 | Xdot = X.*Wx;
16 | Xddot = Xdot.*Wx;
17 |
18 | %precompute the external inputs
19 | Wu = repmat(1i*w,1,size(U,2));
20 | Udot = U.*Wu;
21 | Uddot = Udot.*Wu;
22 |
23 | States.w0 = w0;
24 | States.wBase = wBase;
25 |
26 | %create the time series from the fourier series
27 | States.x = real(hbm.nonlin.IFFT*X).';
28 | States.xdot = real(hbm.nonlin.IFFT*Xdot).';
29 | States.xddot = real(hbm.nonlin.IFFT*Xddot).';
30 |
31 | %create the vector of inputs
32 | States.u = real(hbm.nonlin.IFFT*U).';
33 | States.udot = real(hbm.nonlin.IFFT*Udot).';
34 | States.uddot = real(hbm.nonlin.IFFT*Uddot).';
35 |
--------------------------------------------------------------------------------
/Test/test_params.m:
--------------------------------------------------------------------------------
1 | function problem = test_params
2 | rng(5);
3 | P.k1 = 10+rand(1)*0;
4 | P.k2 = 10+rand(1)*0;
5 | P.k3 = 5+rand(1)*0;
6 | P.m1 = 1;
7 | P.m2 = 2;
8 | P.c1 = 0.05;
9 | P.c2 = 0.05;
10 | P.c3 = 0.05;
11 |
12 | P.cnl = 0*[0 0 0.005]';
13 | P.knl = [0 0 0.004]';
14 | P.n = 3;
15 |
16 | P.R = [1 0;
17 | -1 1;
18 | 0 1];
19 |
20 | %forcing
21 | P.f0 = 0;
22 | P.f = 2*rand(1)*exp(1i*pi*rand(1));
23 | P.f2 = 0.5*rand(1)*exp(1i*pi*rand(1));
24 |
25 | P.iDof = 1;
26 | P.iInput = 1;
27 |
28 | problem.K = P.k1*[1 0;
29 | 0 0];
30 | problem.K = problem.K + P.k2*[1 -1;
31 | -1 1];
32 | problem.K = problem.K + P.k3*[0 0;
33 | 0 1];
34 | problem.C = P.c1*[1 0;
35 | 0 0];
36 | problem.C = problem.C + P.c2*[1 -1;
37 | -1 1];
38 | problem.C = problem.C + P.c3*[0 0;
39 | 0 1];
40 | problem.M = diag([P.m1 P.m2]);
41 |
42 | problem.F0 = zeros(2,1);
43 |
44 | problem.Mu = zeros(2,1);
45 | problem.Cu = zeros(2,1);
46 | problem.Ku = [0;1];
47 |
48 | problem.model = @test_model;
49 | problem.excite = @test_excite;
50 |
51 | problem.P = P;
--------------------------------------------------------------------------------
/Utilities/linear_jacobian.m:
--------------------------------------------------------------------------------
1 | function [Bk,Bc,Bm,Bx] = linear_jacobian(harm,Ku,Cu,Mu,Gu)
2 | if nargin < 5
3 | Gu = 0*Cu;
4 | end
5 |
6 | %terms dependent on each frequency
7 | Ak{1} = Ku;
8 | for j = 2:harm.NFreq
9 | Ak{j} = blkdiag(Ku,Ku);
10 | end
11 |
12 | for k = 1:2
13 | Ac{k}{1} = 0*Cu;
14 | Am{k}{1} = 0*Mu;
15 | for j = 2:harm.NFreq
16 | Ac{k}{j} = harm.rFreqBase(k)*harm.kHarm(j,k)*antiblkdiag(-Cu,Cu);
17 | Am{k}{j} = -(harm.rFreqBase(k)*harm.kHarm(j,k))^2*blkdiag(Mu,Mu);
18 | end
19 | end
20 |
21 | %cross-terms
22 | Ax{1} = 0*Mu;
23 | for j = 2:harm.NFreq
24 | Ax{j} = -2*prod(harm.kHarm(j,:).*harm.rFreqBase)*blkdiag(Mu,Mu);
25 | end
26 |
27 | %add on gyro terms
28 | for j = 2:harm.NFreq
29 | Am{1}{j} = Am{1}{j} + harm.rFreqBase(1)*harm.kHarm(j,1)*antiblkdiag(-Gu,Gu);
30 | Ax{j} = Ax{j} + harm.rFreqBase(2)*harm.kHarm(j,2)*antiblkdiag(-Gu,Gu);
31 | end
32 |
33 | Bk = blkdiag(Ak{:});
34 | for k = 1:2
35 | Bc{k} = blkdiag(Ac{k}{:});
36 | Bm{k} = blkdiag(Am{k}{:});
37 | end
38 | Bx = blkdiag(Ax{:});
39 |
40 | Bk = sparse(Bk);
41 | for k = 1:2
42 | Bm{k} = sparse(Bm{k});
43 | Bc{k} = sparse(Bc{k});
44 | end
45 | Bx = sparse(Bx);
46 |
--------------------------------------------------------------------------------
/Standard/get_time_series.m:
--------------------------------------------------------------------------------
1 | function varargout = get_time_series(hbm,w0,X,tspan)
2 | NHarm = hbm.harm.NHarm(1);
3 | Nfft = hbm.harm.Nfft(1);
4 |
5 | %unpack the inputs
6 | w = (0:NHarm)'*w0;
7 | t = (0:Nfft-1)'/Nfft*2*pi/w0;
8 |
9 | %compute the fourier coefficients of the derivatives
10 | Wx = repmat(1i*w,1,size(X,2));
11 |
12 | %create the time series from the fourier series
13 | x = real(hbm.nonlin.IFFT*X);
14 | if nargout > 2
15 | Xdot = X.*Wx;
16 | xdot = real(hbm.nonlin.IFFT*Xdot);
17 | if nargout > 3
18 | Xddot = Xdot.*Wx;
19 | xddot = real(hbm.nonlin.IFFT*Xddot);
20 | end
21 | end
22 |
23 | if nargin < 5 || isempty(tspan) %empty
24 | ti = linspace(0,2*pi/min(w0),Nfft);
25 | elseif length(tspan) < 2 %duration
26 | ti = linspace(0,tspan,Nfft);
27 | else
28 | ti = tspan;
29 | end
30 |
31 | for i = 1:size(x,2)
32 | xi(:,i) = interp1(t,x(:,i),ti);
33 | if nargout > 2
34 | xdoti(:,i) = interp1(t,xdot(:,i),ti);
35 | if nargout > 3
36 | xddoti(:,i) = interp1(t,xddot(:,i),ti);
37 | end
38 | end
39 | end
40 |
41 | varargout{1} = ti;
42 | varargout{2} = xi;
43 | if nargout >2
44 | varargout{3} = xdoti;
45 | if nargout > 3
46 | varargout{4} = xddoti;
47 | end
48 | end
--------------------------------------------------------------------------------
/Utilities/hbm_scaling.m:
--------------------------------------------------------------------------------
1 | function problem = hbm_scaling(problem,hbm,results)
2 | X = results.X;
3 | w = results.w;
4 | A = results.A;
5 |
6 | tol = hbm.scaling.tol;
7 | switch hbm.scaling.method
8 | case 'max'
9 | xdc = max(abs(X(1,:)),tol);
10 | xmax = max(max(abs(X(2:end,:)),[],1),tol);
11 | xharm = repmat(xmax,hbm.harm.NFreq-1,1);
12 | case 'abs'
13 | xdc = max(abs(X(1,:)),tol);
14 | xharm = max(abs(X(2:end,:)),tol);
15 | end
16 | Xscale = [xdc; xharm*(1+1i)];
17 | problem.Xscale = packdof(Xscale);
18 | problem.Zscale = problem.Xscale;
19 | problem.Fscale = ones(length(problem.Xscale),1);
20 |
21 | switch problem.type
22 | case 'frf'
23 | problem.wscale = w;
24 | problem.Zscale(end+1) = w;
25 | case 'resonance'
26 | problem.wscale = w;
27 | problem.Zscale(end+1) = w;
28 | case 'bb'
29 | problem.wscale = w;
30 | problem.Zscale(end+1) = w;
31 |
32 | %amplitude is the continuation variable
33 | problem.Ascale = A;
34 | problem.Zscale(end+1) = A;
35 | %need extra constraint
36 | problem.Fscale(end+1) = 1;
37 | case 'amp'
38 | problem.Ascale = A;
39 | problem.Zscale(end+1) = A;
40 | case 'solve'
41 | end
42 |
43 | problem.Jscale = (1./problem.Fscale(:))*problem.Zscale(:)';
--------------------------------------------------------------------------------
/Utilities/hbm_floquet.m:
--------------------------------------------------------------------------------
1 | function sol = hbm_floquet(hbm,problem,sol)
2 | if length(sol) > 1
3 | [sol.L] = deal(0);
4 | for i = 1:length(sol)
5 | sol(i) = hbm_floquet(hbm,problem,sol(i));
6 | end
7 | return;
8 | end
9 |
10 | if size(sol.X,1) == hbm.harm.NFreq && size(sol.X,2) == problem.NDof
11 | x = packdof(sol.X);
12 | u = packdof(sol.U);
13 | else
14 | x = sol.X;
15 | u = sol.U;
16 | end
17 |
18 | w = sol.w;
19 |
20 | if any(isnan(x) | isinf(x))
21 | lambda = NaN(hbm.harm.NComp*problem.NDof,1);
22 | else
23 | [A,B] = floquetMatrices(hbm,problem,w,u,x);
24 | lambda = eig(A,B,'vector');
25 | [~,iSort] = sort(abs(imag(lambda)));
26 | lambda = lambda(iSort);
27 | end
28 |
29 | sol.L = lambda;
30 |
31 | function [A,B] = floquetMatrices(hbm,problem,w,u,x)
32 | hbm.bIncludeNL = 1;
33 | NPts = size(u,2);
34 |
35 | A = zeros(2*size(x,1),2*size(x,1),NPts);
36 | B = zeros(2*size(x,1),2*size(x,1),NPts);
37 | for i = 1:NPts
38 | D0 = hbm_balance3d('floquet0',hbm,problem,w(i),u(:,i),x(:,i));
39 | D1 = hbm_balance3d('floquet1',hbm,problem,w(i),u(:,i),x(:,i));
40 | D2 = hbm_balance3d('floquet2',hbm,problem,w(i),u(:,i),x(:,i));
41 |
42 | I = eye(hbm.harm.NComp*problem.NDof);
43 | Z = zeros(hbm.harm.NComp*problem.NDof);
44 |
45 | B1 = [D1 D0;
46 | -I Z];
47 | B2 = [D2 Z;
48 | Z I];
49 |
50 | A(:,:,i) = B1;
51 | B(:,:,i) = -B2;
52 | end
--------------------------------------------------------------------------------
/Test/test_model.m:
--------------------------------------------------------------------------------
1 | function varargout = test_model(part,States,hbm,problem)
2 | if ~iscell(part)
3 | part = {part};
4 | end
5 |
6 | P = problem.P;
7 | NPts = size(States.t,2);
8 | x = States.x;
9 | xdot = States.xdot;
10 |
11 | varargout = {};
12 |
13 | for i = 1:length(part)
14 | switch part{i}
15 | %% Nonlin
16 | case {'nl','output'}
17 | fel = P.knl .* sgn_power(P.R*x,P.n);
18 | fdamp = P.cnl .* (P.R*xdot).^3;
19 | Fnl = P.R' * (fel + fdamp);
20 |
21 | varargout{end+1} = Fnl;
22 | case 'nl_x'
23 | [~,d] = sgn_power(P.R*x,P.n);
24 | Knl = zeros(problem.NDof);
25 | for j = 1:size(P.R,1)
26 | Knl = Knl + mtimesx(P.knl(j) .* permute(d(j,:),[1 3 2]),P.R(j,:)'*P.R(j,:));
27 | end
28 | varargout{end+1} = Knl;
29 | case 'nl_xdot'
30 | Cnl = zeros(problem.NDof);
31 | d = 3 * P.cnl .* (P.R*xdot).^2;
32 | for j = 1:size(P.R,1)
33 | Cnl = Cnl + mtimesx(P.cnl(j) .* permute(d(j,:),[1 3 2]),P.R(j,:)'*P.R(j,:));
34 | end
35 | varargout{end+1} = Cnl;
36 | case 'nl_u'
37 | varargout{end+1} = zeros(2,1,NPts);
38 | case 'nl_udot'
39 | varargout{end+1} = zeros(2,1,NPts);
40 | otherwise
41 | varargout{end+1} = [];
42 | end
43 | end
--------------------------------------------------------------------------------
/Test/test_model_allnl.m:
--------------------------------------------------------------------------------
1 | function varargout = test_model(part,States,hbm,problem)
2 |
3 | P = problem.P;
4 |
5 | P = problem.P;
6 | NPts = size(States.t,2);
7 | x = States.x;
8 | xdot = States.xdot;
9 | u = States.u;
10 | udot = States.udot;
11 |
12 | varargout = {};
13 |
14 | for i = 1:length(part)
15 | switch part
16 | case {'nonlin','output'}
17 | Fe = problem.Ku*u + problem.Cu*udot + problem.Mu*uddot;
18 | Fl = -problem.C*xdot - problem.K*x - problem.M*xddot;
19 | x1 = x(1,:); x2 = x(2,:);
20 | f = sgn_power(x1-x2,P.n);
21 | Fnl = [-1;1]*P.knl * f - P.cnl * xdot.^3;
22 | varargout{end+1} = Fl + Fnl + Fe;
23 | case 'nl_x'
24 | x1 = permute(x(1,:),[1 3 2]);
25 | x2 = permute(x(2,:),[1 3 2]);
26 | [~,d] = sgn_power(x1-x2,P.n);
27 | k = P.knl * d;
28 |
29 | Knl = -[k -k; -k k];
30 |
31 | varargout{end+1} = Knl;
32 | case 'nl_xdot'
33 | xdot1 = permute(xdot(1,:),[1 3 2]);
34 | xdot2 = permute(xdot(2,:),[1 3 2]);
35 | c1 = P.cnl * 3 * xdot1.^2;
36 | c2 = P.cnl * 3 * xdot2.^2;
37 |
38 | Cnl = -[c1,0*c1;0*c2,c2];
39 | varargout{end+1} = Cnl;
40 | case {'nl_u','nl_udot'}
41 | varargout{end+1} = zeros(2,1,NPts);
42 | case 'freqderiv'
43 | varargout{end+1} = [];
44 | end
45 | end
--------------------------------------------------------------------------------
/Utilities/aft_jacobian_ifft.m:
--------------------------------------------------------------------------------
1 | function [Ju,Judot,Juddot] = aft_jacobian_ifft(harm)
2 | Nfft = harm.Nfft;
3 | NComp = harm.NComp;
4 |
5 | theta1 = 2*pi/Nfft(1)*(0:(Nfft(1)-1));
6 | theta2 = 2*pi/Nfft(2)*(0:(Nfft(2)-1));
7 | [theta1,theta2] = ndgrid(theta1,theta2);
8 | theta0 = [theta1(:)'; theta2(:)'];
9 |
10 | Ju = zeros(1,NComp,Nfft(1)*Nfft(2));
11 | Ju(1,1,:) = 1;
12 | for l = 1:(harm.NFreq-1)
13 | ph = permute(harm.kHarm(l+1,:)*theta0,[1 3 2]);
14 | cl = cos(ph); sl = sin(ph);
15 | Ju(1,2*l,:) = cl;
16 | Ju(1,2*l+1,:) = -sl;
17 | end
18 |
19 | Judot = cell(1,2);
20 | for n = 1:2
21 | Judot{n} = zeros(1,NComp,Nfft(1)*Nfft(2));
22 | for l = 1:(harm.NFreq-1)
23 | ph = permute(harm.kHarm(l+1,:)*theta0,[1 3 2]);
24 | cl = cos(ph); sl = sin(ph);
25 | Judot{n}(1,2*l,:) = -harm.kHarm(l+1,n)*sl;
26 | Judot{n}(1,2*l+1,:) = -harm.kHarm(l+1,n)*cl;
27 | end
28 | end
29 |
30 | Juddot = cell(1,3);
31 | for n = 1:2
32 | Juddot{n} = zeros(1,NComp,Nfft(1)*Nfft(2));
33 | for l = 1:(harm.NFreq-1)
34 | ph = permute(harm.kHarm(l+1,:)*theta0,[1 3 2]);
35 | cl = cos(ph); sl = sin(ph);
36 | Juddot{n}(1,2*l,:) = -harm.kHarm(l+1,n)^2*cl;
37 | Juddot{n}(1,2*l+1,:) = harm.kHarm(l+1,n)^2*sl;
38 | end
39 | end
40 |
41 | n = 3;
42 | Juddot{n} = zeros(1,NComp,Nfft(1)*Nfft(2));
43 | for l = 1:(harm.NFreq-1)
44 | ph = permute(harm.kHarm(l+1,:)*theta0,[1 3 2]);
45 | cl = cos(ph); sl = sin(ph);
46 | Juddot{n}(1,2*l,:) = -prod(harm.kHarm(l+1,:))*cl;
47 | Juddot{n}(1,2*l+1,:) = prod(harm.kHarm(l+1,:))*sl;
48 | end
--------------------------------------------------------------------------------
/Test/hbm_solve_test.m:
--------------------------------------------------------------------------------
1 | function hbm_solve_test(b3d)
2 | problem = test_params;
3 |
4 | hbm.harm.rFreqRatio = 1;
5 | hbm.harm.NHarm = 2;
6 | hbm.harm.Nfft = 32;
7 | hbm.harm.iHarmPlot = 2;
8 |
9 | if nargin < 1
10 | b3d = 1;
11 | end
12 |
13 | if b3d
14 | hbm.harm.rFreqRatio(end+1) = 1.376;
15 | hbm.harm.NHarm(end+1) = 2;
16 | hbm.harm.Nfft(end+1) = 16;
17 | hbm.harm.iHarmPlot(end+1) = 3;
18 | end
19 |
20 | omega = sqrt(eig(problem.K,problem.M));
21 | w0 = 4;
22 | A = 100;
23 |
24 | [hbm,problem] = setuphbm(hbm,problem);
25 | NDof = problem.NDof;
26 |
27 | tic;
28 | sol1 = hbm_solve(hbm,problem,w0,A,[]);
29 | [t1,x1,xdot1] = get_time_series3d(hbm,w0,sol1.X);
30 | tRun(1) = toc;
31 |
32 | tic;
33 | y0 = [x1(1,:) xdot1(1,:)];
34 | fun = @(t,y)test_odefun(t,y,w0*hbm.harm.rFreqRatio,sol1.U,hbm,problem);
35 | M = blkdiag(eye(NDof),problem.M);
36 | options = odeset('Mass',M,'Vectorized','on','MassSingular','yes','RelTol',1E-12,'AbsTol',1E-12);
37 | [t2,y] = ode15s(fun,[t1(1) t1(end)],y0,options);
38 | tRun(2) = toc;
39 |
40 | x2 = y(:,1:NDof);
41 | xdot2 = y(:,NDof+(1:NDof));
42 |
43 | figure
44 | subplot(2,2,1)
45 | plot(t1,x1(:,1:NDof)','-',t2,x2(:,1:NDof)','o-');
46 | ylabel('x');
47 |
48 | subplot(2,2,3)
49 | plot(t1,xdot1(:,1:NDof)','-',t2,xdot2(:,1:NDof)','o-');
50 | ylabel('xdot');
51 | xlabel('Time (s)');
52 |
53 | names = {'HBM','ODE'};
54 |
55 | subplot(1,2,2)
56 | plot(x1(:,1:NDof),xdot1(:,1:NDof));
57 | hold on
58 | plot(x2(:,1:NDof),xdot2(:,1:NDof),'o-');
59 | xlabel('x');
60 | ylabel('xdot');
61 | leg = {};
62 | for i = 1:length(tRun)
63 | leg = [leg cellsprintf([names{i} '(x%d)'],num2cell(1:NDof))];
64 | end
65 | legend(leg)
66 |
67 | for i = 1:length(tRun)
68 | fprintf('%s: %f s\n',names{i},tRun(i));
69 | end
--------------------------------------------------------------------------------
/Test/fft_test.m:
--------------------------------------------------------------------------------
1 | function fft_test
2 |
3 | NHarm = [4 4];
4 | Nfft = [128 128];
5 | iSub = [2 7;
6 | 4 1
7 | 2 4
8 | 3 1];
9 | for i = 1:10000
10 | X = rand(4,1) + rand(4,1)*1i;
11 | tic;
12 | x = freq2time3d(X,NHarm,iSub,Nfft,1);
13 | t(i) = toc;
14 | tic;
15 | x = freq2time3d(X,NHarm,iSub,Nfft,0);
16 | t2(i) = toc;
17 | end
18 | mean(t)
19 | mean(t2)
20 |
21 | function x = freq2time3d(X,NHarm,iSub,Nfft,b)
22 | %NB, scaling so IFFT = Expanding Fourier Coefficients
23 |
24 | %first regenerate the -ve frequency components
25 | Xfft = unpackfreq(X,NHarm,iSub);
26 |
27 | if b
28 | %now augment for Nfft frequencies
29 | iz = floor(Nfft/2)+1;
30 | Xfft2 = zeros(Nfft(1),Nfft(2),size(X,3));
31 | Xfft2(iz(1)+(-NHarm(1):NHarm(1)),iz(2)+(-NHarm(2):NHarm(2)),:) = Xfft;
32 | Xfft = Xfft2;
33 | end
34 |
35 | %finally compute the ifft
36 | Xfft = ifftshift(Xfft,1);
37 | Xfft = ifft(Xfft,Nfft(1),1)*Nfft(1);
38 | if NHarm(2)>0
39 | Xfft = ifftshift(Xfft,2);
40 | x = ifft(Xfft,Nfft(2),2)*Nfft(2);
41 | else
42 | x = Xfft;
43 | end
44 |
45 | %the answer should now be real to machine precision
46 | x = real(x);
47 |
48 | %wrap hypertime
49 | x = reshape(x,prod(Nfft),[]);
50 |
51 |
52 |
53 | function x = freq2time(X,NHarm,Nfft,b)
54 |
55 | %first regenerate the -ve frequency components
56 | X = cat(1,0*X(2:end,:,:), X);
57 | Xfft = 0.5*(X + conj(flip(X,1)));
58 |
59 | if b
60 | %now augment for Nfft frequencies
61 | iz = floor(Nfft/2)+1;
62 | sz = size(X);
63 | Xfft2 = zeros([Nfft,sz(2:end)]);
64 | Xfft2(iz+(-NHarm:NHarm),:,:) = Xfft;
65 | Xfft = Xfft2;
66 | end
67 |
68 | %finally compute the ifft
69 | Xfft = ifftshift(Xfft,1);
70 | x = ifft(Xfft,Nfft,1) * Nfft;
71 |
72 | %the answer should now be real to machine precision
73 | x = real(x);
--------------------------------------------------------------------------------
/Test/hbm_amp_test.m:
--------------------------------------------------------------------------------
1 | function hbm_amp_test(b3d)
2 | problem = test_params;
3 |
4 | hbm.harm.rFreqRatio = 1;
5 | hbm.harm.NHarm = 2;
6 | hbm.harm.Nfft = 32;
7 | hbm.harm.iHarmPlot = 2;
8 |
9 | if nargin < 1
10 | b3d = 1;
11 | end
12 |
13 | if b3d
14 | hbm.harm.rFreqRatio(end+1) = 1.376;
15 | hbm.harm.NHarm(end+1) = 2;
16 | hbm.harm.Nfft(end+1) = 16;
17 | hbm.harm.iHarmPlot(end+1) = 3;
18 | end
19 |
20 | hbm.dependence.x = true;
21 | hbm.dependence.xdot = true;
22 | hbm.dependence.w = false;
23 |
24 | hbm.scaling.tol = 1;
25 |
26 | hbm.cont.step0 = 1E-2;
27 | hbm.cont.max_step = 1E-1;
28 | hbm.cont.min_step = 1E-6;
29 |
30 | hbm.cont.method = 'predcorr';
31 |
32 | [hbm,problem] = setuphbm(hbm,problem);
33 |
34 | omega = sqrt(eig(problem.K,problem.M));
35 | w0 = omega(1);
36 | A0 = 1;
37 | AEnd = 5;
38 |
39 | S = {};
40 |
41 | tic;
42 | hbm.cont.method = 'none';
43 | sol = hbm_amp(hbm,problem,w0,A0,[],AEnd,[]);
44 | S{end+1} = storeResults(sol,toc,'none');
45 |
46 | tic;
47 | hbm.cont.method = 'predcorr';
48 | hbm.cont.predcorr.corrector = 'arclength';
49 | sol = hbm_amp(hbm,problem,w0,A0,[],AEnd,[]);
50 | S{end+1} = storeResults(sol,toc,'arclength');
51 |
52 | tic;
53 | hbm.cont.method = 'predcorr';
54 | hbm.cont.predcorr.corrector = 'pseudo';
55 | sol = hbm_amp(hbm,problem,w0,A0,[],AEnd,[]);
56 | S{end+1} = storeResults(sol,toc,'pseudo');
57 |
58 | figure
59 | for j = 1:problem.NDof
60 | ax_mag(j) = subplot(2,problem.NDof,j);
61 | hold on
62 | for i = 1:length(S)
63 | h(i) = plot(ax_mag(j),S{i}.A,abs(S{i}.X(j,:)));
64 | end
65 | ylabel(ax_mag(j),sprintf('|X_%d| (mag)',j));
66 | end
67 |
68 | for j = 1:problem.NDof
69 | ax_ph(j) = subplot(2,problem.NDof,problem.NDof+j);
70 | hold on
71 | for i = 1:length(S)
72 | hph(i,j) = plot(ax_ph(j),S{i}.A,unwrap(angle(S{i}.X(j,:)),[],2));
73 | end
74 | xlabel(ax_ph(j),'\omega (rads)');
75 | ylabel(ax_ph(j),sprintf('\\angle X_%d (deg)',j));
76 | end
77 | linkaxes([ax_mag ax_ph],'x')
78 |
79 | for i = 1:length(S)
80 | leg{i} = S{i}.name;
81 | end
82 | legend(ax_ph(end),hph(:,end),leg)
83 |
84 | for i = 1:length(S)
85 | fprintf('%10s : %0.2f s\n',S{i}.name,S{i}.t)
86 | end
87 |
88 | function S = storeResults(sol,t,name)
89 | X = cat(3,sol.X);
90 |
91 | S.X = permute(X(2,:,:),[2 3 1]);
92 | S.w = cat(2,sol.w);
93 | S.A = cat(2,sol.A);
94 | S.t = t;
95 | S.name = name;
--------------------------------------------------------------------------------
/Functions/hbm_bb_plot.m:
--------------------------------------------------------------------------------
1 | function varargout = hbm_bb_plot(command,hbm,problem,results)
2 | persistent figBB axBB hBB hWaitbar A H W
3 | if hbm.cont.bUpdate
4 | switch command
5 | case 'init'
6 | if ~isempty(figBB) && ishandle(figBB)
7 | close(figBB)
8 | hBB = [];
9 | end
10 | if ~isempty(hWaitbar) && ishandle(hWaitbar)
11 | close(hWaitbar)
12 | hWaitbar = [];
13 | end
14 |
15 | A = results.A;
16 | W = results.w;
17 | H = abs(results.H);
18 | [figBB,axBB,hBB] = createFig(hbm,problem,A,H,W);
19 | hWaitbar = waitbar(0, 'Amplitude Range');
20 | if nargout > 0
21 | varargout{1} = figBB;
22 | if nargout > 1
23 | varargout{2} = axBB;
24 | end
25 | end
26 |
27 | case {'data','err'}
28 | if ~ishandle(figBB)
29 | [figBB,axBB,hBB] = createFig(hbm,problem,A,H,W);
30 | end
31 | if strcmp(command,'data')
32 | A(end+1) = results.A;
33 | H(end+1) = abs(results.H);
34 | W(end+1) = results.w;
35 | [A,isort] = sort(A);
36 | H = H(isort); W = W(isort);
37 | set(hBB(1),'xdata',A,'ydata',W);
38 | set(hBB(2),'xdata',A,'ydata',H);
39 |
40 | %update our progress
41 | if ~ishandle(hWaitbar)
42 | hWaitbar = waitbar(0, 'Amplitude Range');
43 | end
44 | waitbar((results.A-problem.A0)/(problem.AEnd - problem.A0),hWaitbar);
45 | else
46 | plot(axBB(1),results.A,results.w,'r.')
47 | plot(axBB(2),results.A,abs(results.H),'r.')
48 | end
49 | drawnow
50 | case 'close'
51 | close(hWaitbar)
52 | close(figBB)
53 | hWaitbar = [];
54 | figBB = [];
55 | hBB = [];
56 | axBB = [];
57 | end
58 | end
59 |
60 |
61 | function [fig,axBB,hBB] = createFig(hbm,problem,A,H,w)
62 | fig = figure('Name',['BB: ' problem.name]);
63 |
64 | axBB(1) = subplot(2,1,1);
65 | hBB(1) = plot(axBB(1),A,w,'g.-');
66 | hold on
67 | ylabel('Frequency (rad/s)')
68 | xlabel('Amplitude')
69 | xlim(axBB(1),[problem.AMin problem.AMax]);
70 |
71 | axBB(2) = subplot(2,1,2);
72 | hBB(2) = plot(axBB(2),A,H,'g.-');
73 | hold on
74 | ylabel('Peak amp')
75 | xlabel('Amplitude')
76 | xlim(axBB(2),[problem.AMin problem.AMax]);
--------------------------------------------------------------------------------
/Utilities/aft_jacobian.m:
--------------------------------------------------------------------------------
1 | function [Ju,Judot] = aft_jacobian(harm,NOutput,NInput)
2 | Nfft = harm.Nfft;
3 | NComp = harm.NComp;
4 |
5 | theta1 = 2*pi/Nfft(1)*(0:(Nfft(1)-1));
6 | theta2 = 2*pi/Nfft(2)*(0:(Nfft(2)-1));
7 | [theta1,theta2] = ndgrid(theta1,theta2);
8 | theta0 = [theta1(:)'; theta2(:)'];
9 |
10 | theta1 = permute(theta0(1,:),[1 3 2]);
11 | theta2 = permute(theta0(2,:),[1 3 2]);
12 |
13 | ju = zeros(NComp,NComp,Nfft(1)*Nfft(2));
14 | ju(1,1,:) = 1/(Nfft(1)*Nfft(2));
15 | for l = 1:(harm.NFreq-1)
16 | ph = harm.kHarm(l+1,1)*theta1 + harm.kHarm(l+1,2)*theta2;
17 | cl = cos(ph); sl = sin(ph);
18 | ju(1,2*l,:) = cl/(Nfft(1)*Nfft(2));
19 | ju(1,2*l+1,:) = -sl/(Nfft(1)*Nfft(2));
20 | end
21 | for k = 1:(harm.NFreq-1)
22 | ph = harm.kHarm(k+1,1)*theta1 + harm.kHarm(k+1,2)*theta2;
23 | ck = cos(ph); sk = sin(ph);
24 | ju(2*k,1,:) = 2*ck/(Nfft(1)*Nfft(2));
25 | ju(2*k+1,1,:) = -2*sk/(Nfft(1)*Nfft(2));
26 | for l = 1:(harm.NFreq-1)
27 | ph = harm.kHarm(l+1,1)*theta1 + harm.kHarm(l+1,2)*theta2;
28 | cl = cos(ph); sl = sin(ph);
29 | ju(2*k,2*l,:) = 2*cl.*ck/(Nfft(1)*Nfft(2));
30 | ju(2*k,2*l+1,:) = -2*sl.*ck/(Nfft(1)*Nfft(2));
31 | ju(2*k+1,2*l,:) = -2*cl.*sk/(Nfft(1)*Nfft(2));
32 | ju(2*k+1,2*l+1,:) = 2*sl.*sk/(Nfft(1)*Nfft(2));
33 | end
34 | end
35 | Ju = resize_jacobian(ju,NInput,NOutput);
36 |
37 | Judot = cell(1,2);
38 | for n = 1:2
39 | judot = zeros(NComp,NComp,Nfft(1)*Nfft(2));
40 | for l = 1:(harm.NFreq-1)
41 | ph = harm.kHarm(l+1,1)*theta1 + harm.kHarm(l+1,2)*theta2;
42 | cl = cos(ph); sl = sin(ph);
43 | judot(1,2*l,:) = -harm.kHarm(l+1,n)*sl/(Nfft(1)*Nfft(2));
44 | judot(1,2*l+1,:) = -harm.kHarm(l+1,n)*cl/(Nfft(1)*Nfft(2));
45 | end
46 | for k = 1:(harm.NFreq-1)
47 | ph = harm.kHarm(k+1,1)*theta1 + harm.kHarm(k+1,2)*theta2;
48 | ck = cos(ph); sk = sin(ph);
49 | judot(2*k,1,:) = 0;
50 | judot(2*k+1,1,:) = 0;
51 | for l = 1:(harm.NFreq-1)
52 | ph = harm.kHarm(l+1,1)*theta1 + harm.kHarm(l+1,2)*theta2;
53 | cl = cos(ph); sl = sin(ph);
54 | judot(2*k,2*l,:) = -2*harm.kHarm(l+1,n).*sl.*ck/(Nfft(1)*Nfft(2));
55 | judot(2*k,2*l+1,:) = -2*harm.kHarm(l+1,n).*cl.*ck/(Nfft(1)*Nfft(2));
56 | judot(2*k+1,2*l,:) = 2*harm.kHarm(l+1,n).*sl.*sk/(Nfft(1)*Nfft(2));
57 | judot(2*k+1,2*l+1,:) = 2*harm.kHarm(l+1,n).*cl.*sk/(Nfft(1)*Nfft(2));
58 | end
59 | end
60 | Judot{n} = resize_jacobian(judot,NInput,NOutput);
61 | end
62 |
63 | function Ju = resize_jacobian(ju,NInput,NOutput)
64 | Ju = zeros(NOutput*size(ju,1),NInput*size(ju,2),size(ju,3));
65 | for i = 1:size(ju,3)
66 | Ju(:,:,i) = kron(ju(:,:,i),ones(NOutput,NInput));
67 | end
--------------------------------------------------------------------------------
/Setup/setupNonlin.m:
--------------------------------------------------------------------------------
1 | function nonlin = setupNonlin(harm,problem)
2 | Nfft = harm.Nfft;
3 |
4 | %% fft matrices
5 | theta1 = 2*pi/Nfft(1)*(0:(Nfft(1)-1));
6 | theta2 = 2*pi/Nfft(2)*(0:(Nfft(2)-1));
7 | [theta1,theta2] = ndgrid(theta1,theta2);
8 | theta0 = [theta1(:)'; theta2(:)'];
9 |
10 | nonlin.FFT = zeros(harm.NFreq,Nfft(1)*Nfft(2));
11 | nonlin.FFT(1,:) = 1/(Nfft(1)*Nfft(2));
12 | for k = 2:harm.NFreq
13 | nonlin.FFT(k,:) = 2/(Nfft(1)*Nfft(2))*exp(-1i*harm.kHarm(k,:)*theta0);
14 | end
15 |
16 | nonlin.IFFT = zeros(Nfft(1)*Nfft(2),harm.NFreq);
17 | nonlin.IFFT(:,1) = 1;
18 | for k = 2:harm.NFreq
19 | nonlin.IFFT(:,k) = exp(1i*harm.kHarm(k,:)*theta0);
20 | end
21 |
22 | %% non-linear derivative matrices
23 | %work out the constituent jacobians first
24 | aft = get_aft_jacobians(harm);
25 |
26 | %now make the specifis matrices we will need
27 | nonlin.hbm = get_hbm_matrices(problem,harm,aft);
28 |
29 | function aft = get_aft_jacobians(harm)
30 | aft.fft.J = aft_jacobian_fft(harm);
31 | [aft.ifft.J,aft.ifft.Jdot,aft.ifft.Jddot] = aft_jacobian_ifft(harm);
32 |
33 | %total jacobian
34 | aft.J = mtimesx(aft.fft.J,aft.ifft.J);
35 | for i = 1:2
36 | aft.Jdot{i} = mtimesx(aft.fft.J,harm.rFreqBase(i)*aft.ifft.Jdot{i});
37 | aft.Jddot{i} = mtimesx(aft.fft.J,harm.rFreqBase(i)^2*aft.ifft.Jddot{i});
38 | end
39 | aft.Jddot{3} = mtimesx(aft.fft.J,prod(harm.rFreqBase)*aft.ifft.Jddot{3});
40 |
41 | function hbm = get_hbm_matrices(problem,harm,aft)
42 |
43 | %hbm
44 | hbm.Jfft = resize_jacobian(repmat(aft.fft.J,1,size(aft.fft.J,1)),problem.NDof,problem.NDof);
45 |
46 | hbm.Jifft = resize_jacobian(repmat(aft.ifft.J,size(aft.ifft.J,2),1),problem.NDof,problem.NDof);
47 |
48 | for i = 1:2
49 | hbm.Jdotifft{i} = resize_jacobian(repmat(harm.rFreqBase(i)*aft.ifft.Jdot{i},size(aft.ifft.J,2),1),problem.NDof,problem.NDof);
50 | end
51 |
52 | %% States
53 | hbm.Jx = resize_jacobian(aft.J,problem.NDof,problem.NDof);
54 | hbm.Jxdot = resize_jacobian(aft.Jdot,problem.NDof,problem.NDof);
55 | hbm.Jxddot = resize_jacobian(aft.Jddot,problem.NDof,problem.NDof);
56 |
57 | %% Inputs
58 | hbm.Ju = resize_jacobian(aft.J,problem.NDof,problem.NInput);
59 | hbm.Judot = resize_jacobian(aft.Jdot,problem.NDof,problem.NInput);
60 | hbm.Juddot = resize_jacobian(aft.Jddot,problem.NDof,problem.NInput);
61 |
62 |
63 | %indices for creating the jacobian
64 | hbm.ijacobx = repmat((1:problem.NDof)',harm.NComp,1);
65 | hbm.ijacobu = repmat((1:problem.NInput)',harm.NComp,1);
66 |
67 | function Ju = resize_jacobian(ju,NOutput,NInput)
68 | if ~iscell(ju)
69 | ju = {ju};
70 | end
71 | for k = 1:length(ju)
72 | Ju{k} = zeros(NOutput*size(ju{k},1),NInput*size(ju{k},2),size(ju{k},3));
73 | for i = 1:size(ju{k},3)
74 | Ju{k}(:,:,i) = kron(ju{k}(:,:,i),ones(NOutput,NInput));
75 | end
76 | end
77 | if length(Ju) == 1
78 | Ju = Ju{1};
79 | end
--------------------------------------------------------------------------------
/Setup/setupHarm.m:
--------------------------------------------------------------------------------
1 | function harm = setupHarm(harm)
2 |
3 | harm = setupHarmonics(harm);
4 |
5 | %default missing fields
6 | f = {'Nfft','rFreqRatio','rFreqBase','wFreq0'};
7 | d = {max(8*harm.NHarm,1),0*harm.NHarm+1,0*harm.NHarm+1,0*harm.NHarm};
8 | for i = 1:length(f)
9 | if ~isfield(harm,f{i})
10 | harm.(f{i}) = d{i};
11 | warning('Field %s is missing from the options. Defaulting to %s',f{i},mat2str(d{i}))
12 | end
13 | end
14 |
15 | %check all the sizes match
16 | if length(harm.rFreqRatio) ~= length(harm.NHarm)
17 | error('The number of frequencies should match the number of elements in the NHarm vector')
18 | end
19 |
20 | if length(harm.rFreqRatio) ~= length(harm.Nfft)
21 | error('The number of frequencies should match the number of elements in the Nfft vector')
22 | end
23 |
24 | %now add dummy second harmonic for single harmonic problems
25 | if length(harm.rFreqRatio) == 1
26 | harm.rFreqRatio(2) = 1;
27 | harm.NHarm(2) = 0;
28 | harm.Nfft(2) = 1;
29 | harm.kHarm(:,2) = 0;
30 | end
31 |
32 | %deal with any sub-harmonics, changing base frequencies accordingly
33 | harm = detect_subharmonics(harm);
34 |
35 | harm.kHarm = reorder_harmonics(unique(harm.kHarm,'rows','stable'));
36 |
37 | %find the subscripts for using FFT/IFFT
38 | harm.NFreq = size(harm.kHarm,1);
39 | harm.NComp = (2*(harm.NFreq-1)+1);
40 |
41 | function group = setupHarmonics(group)
42 | if isfield(group,'kHarm')
43 | group.NHarm = max(abs(group.kHarm),[],1);
44 | elseif isfield(group,'NHarm')
45 | if length(group.NHarm) == 1
46 | group.kHarm = (0:group.NHarm)';
47 | elseif group.NHarm(1) == 0
48 | k2 = 0:group.NHarm(2);
49 | k1 = 0*k2;
50 | group.kHarm = [k1(:) k2(:)];
51 | elseif group.NHarm(2) == 0
52 | k1 = 0:group.NHarm(1);
53 | k2 = 0*k1;
54 | group.kHarm = [k1(:) k2(:)];
55 | else
56 | k1 = -group.NHarm(1):group.NHarm(1);
57 | k2 = -group.NHarm(2):group.NHarm(2);
58 | [K1,K2] = ndgrid(k1,k2);
59 | keep = (K1+K2)>0 | ((K1+K2)== 0 & K1>=0);
60 | group.kHarm = [K1(keep) K2(keep)];
61 | end
62 | else
63 | error('Need either kHarm or NHarm');
64 | end
65 |
66 | function harm = detect_subharmonics(harm)
67 | ratio = harm.NHarm*0 + 1;
68 |
69 | [~,fac] = rat(harm.kHarm);
70 |
71 | for k = 1:2
72 | for j = 1:size(harm.kHarm,1)
73 | ratio(k) = lcm(fac(j,k),ratio(k));
74 | end
75 | end
76 |
77 | harm.kHarm = harm.kHarm .* repmat(ratio,size(harm.kHarm,1),1);
78 |
79 | harm.rFreqBase = harm.rFreqBase./ratio;
80 | harm.NHarm = harm.NHarm.*ratio;
81 | harm.Nfft = harm.Nfft.*ratio;
82 |
83 | function kHarm = reorder_harmonics(kHarm)
84 | %find 0th harmonic and put it first
85 |
86 | zero = kHarm(:,1) == 0 & kHarm(:,2) == 0;
87 | first1 = kHarm(:,1) == 1 & kHarm(:,2) == 0;
88 | first2 = kHarm(:,1) == 0 & kHarm(:,2) == 1;
89 | rest = ~zero & ~first1 & ~first2;
90 | ii = [find(zero); find(first1); find(first2); find(rest)];
91 | kHarm = kHarm(ii,:);
--------------------------------------------------------------------------------
/Standard/hbm_balance.m:
--------------------------------------------------------------------------------
1 | function varargout = hbm_balance(command,hbm,problem,w,u,x)
2 | NDofTot = hbm.harm.NComp*problem.NDof;
3 |
4 | ii = find(hbm.harm.NHarm ~= 0);
5 | r = hbm.harm.rFreqRatio(ii);
6 | w0 = w * r + hbm.harm.wFreq0(ii);
7 |
8 | A = (hbm.lin.Ak + w0*hbm.lin.Ac{ii} + w0^2*hbm.lin.Am{ii});
9 | B = (hbm.lin.Bk + w0*hbm.lin.Bc{ii} + w0^2*hbm.lin.Bm{ii});
10 | dAdw = (hbm.lin.Ac{ii} + 2*w0*hbm.lin.Am{ii});
11 | dBdw = (hbm.lin.Bc{ii} + 2*w0*hbm.lin.Bm{ii});
12 |
13 | switch command
14 | case 'func' %F, used by hbm_frf & hbm_bb
15 | cl = B*u - hbm.lin.b - A*x;
16 | if hbm.bIncludeNL
17 | cnl = hbm_nonlinear('func',hbm,problem,w0,x,u);
18 | else
19 | cnl = 0*cl;
20 | end
21 | c = (cl - cnl);
22 | varargout{1} = c;
23 | case 'jacob' %dF_dX, used by hbm_frf & hbm_bb
24 | Jl = -A;
25 | if hbm.bIncludeNL
26 | [Jx,Jxdot,Jxddot] = hbm_nonlinear({'jacobX','jacobXdot','jacobXddot'},hbm,problem,w0,x,u);
27 | Jnl = Jx + w0*Jxdot + w0^2*Jxddot;
28 | else
29 | Jnl = 0*Jl;
30 | end
31 | J = (Jl - Jnl);
32 | varargout{1} = J;
33 | case 'derivW' %dF_dw, used by hbm_frf & hbm_bb
34 | Dl = dBdw*u - dAdw*x;
35 | if hbm.bIncludeNL
36 | if hbm.dependence.xdot || hbm.dependence.w
37 | [Jxdot,Jxddot,Judot,Juddot,Dw] = hbm_nonlinear({'jacobXdot','jacobXddot','jacobUdot','jacobUddot','derivW'},hbm,problem,w0,x,u);
38 | Dxdot = r*Jxdot*x;
39 | Dxddot = 2*r*w0*Jxddot*x;
40 | Dudot = r*Judot*u;
41 | Duddot = 2*r*w0*Juddot*u;
42 | Dnl = Dxdot + Dxddot + Dudot + Duddot + Dw;
43 | else
44 | Dnl = 0*Dl;
45 | end
46 | else
47 | Dnl = 0*Dl;
48 | end
49 | D = (Dl - Dnl);
50 | varargout{1} = D;
51 | case 'derivA' %dF_dA, used in hbm_bb
52 | Dl = B*u;
53 | if hbm.bIncludeNL
54 | [Ju,Judot,Juddot] = hbm_nonlinear({'jacobU','jacobUdot','jacobUddot'},hbm,problem,w0,x,u);
55 | Dnl = (Ju + w0*Judot + w0^2*Juddot)*u;
56 | else
57 | Dnl = 0*Dl;
58 | end
59 | D = (Dl - Dnl);
60 | varargout{1} = D;
61 | case 'floquet0'
62 | D0 = -hbm_balance('jacob',hbm,problem,w0,u,x);
63 | varargout{1} = D0;
64 | case 'floquet1'
65 | D1l = hbm.lin.floquet.D1xdot + 2*hbm.lin.floquet.D1xddot{ii}*w0;
66 | if hbm.bIncludeNL
67 | [D1xdot,D1xddot] = hbm_nonlinear({'floquet1xdot','floquet1xddot'},hbm,problem,w0,x,u);
68 | D1nl = D1xdot + 2*D1xddot*w0;
69 | else
70 | D1nl = 0*D1l;
71 | end
72 | varargout{1} = (D1l - D1nl);
73 | case 'floquet2'
74 | D2l = hbm.lin.floquet.D2;
75 | if hbm.bIncludeNL
76 | D2nl = hbm_nonlinear({'floquet2'},hbm,problem,w0,x,u);
77 | else
78 | D2nl = 0*D2l;
79 | end
80 | varargout{1} = (D2l - D2nl);
81 | end
--------------------------------------------------------------------------------
/Generalised/get_time_series3d.m:
--------------------------------------------------------------------------------
1 | function varargout = get_time_series3d(hbm,w,X,tspan)
2 | if hbm.options.bUseStandardHBM
3 | [varargout{1:nargout}] = get_time_series(hbm,problem,w,u,X);
4 | return;
5 | end
6 |
7 | NHarm = hbm.harm.NHarm;
8 | Nfft = hbm.harm.Nfft;
9 | kHarm = hbm.harm.kHarm;
10 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
11 |
12 | %unpack the inputs
13 | w = kHarm(:,1)*w0(1) + kHarm(:,2)*w0(2);
14 | t1 = (0:Nfft(1)-1)/Nfft(1)*2*pi/w0(1);
15 | t2 = (0:Nfft(2)-1)/Nfft(2)*2*pi/w0(2);
16 | [t1,t2] = ndgrid(t1,t2);
17 | %compute the fourier coefficients of the derivatives
18 | Wx = repmat(1i*w,1,size(X,2));
19 |
20 | %create the time series from the fourier series
21 | x = real(hbm.nonlin.IFFT*X);
22 | if nargout > 2
23 | Xdot = X.*Wx;
24 | xdot = real(hbm.nonlin.IFFT*Xdot);
25 | if nargout > 3
26 | Xddot = Xdot.*Wx;
27 | xddot = real(hbm.nonlin.IFFT*Xddot);
28 | end
29 | end
30 |
31 | x = reshape(x,size(t1,1),size(t1,2),[]);
32 | if nargout > 2
33 | xdot = reshape(xdot,size(t1,1),size(t1,2),[]);
34 | if nargout > 3
35 | xddot = reshape(xddot,size(t1,1),size(t1,2),[]);
36 | end
37 | end
38 |
39 | if nargin < 5 || isempty(tspan) %empty
40 | ti = linspace(0,2*pi/min(w0),max(Nfft));
41 | elseif length(tspan) < 2 %duration
42 | ti = linspace(0,tspan,max(Nfft));
43 | else
44 | ti = tspan;
45 | end
46 |
47 | if ti(end) > 2*pi/w0(1)% && hbm.harm.NHarm(1)>0
48 | N = ceil(ti(end)/t1(end,1));
49 | t0 = t1;
50 | for i = 2:N
51 | t1 = [t1; t0+2*pi*(i-1)/w0(1)];
52 | end
53 | x = repmat(x,N,1);
54 | t2 = repmat(t2,N,1);
55 | if nargout > 2
56 | xdot = repmat(xdot,N,1);
57 | if nargout > 3
58 | xddot = repmat(xddot,N,1);
59 | end
60 | end
61 | end
62 | if ti(end) > 2*pi/w0(2)% && hbm.harm.NHarm(2)>0
63 | N = ceil(ti(end)/t2(1,end));
64 | t0 = t2;
65 | for i = 2:N
66 | t2 = [t2 t0+2*pi*(i-1)/w0(2)];
67 | end
68 | x = repmat(x,1,N);
69 | t1 = repmat(t1,1,N);
70 | if nargout > 2
71 | xdot = repmat(xdot,1,N);
72 | if nargout > 3
73 | xddot = repmat(xddot,1,N);
74 | end
75 | end
76 | end
77 |
78 | if Nfft(2) > 1
79 | for i = 1:size(x,3)
80 | xi(:,i) = interp2(t2,t1,x(:,:,i),ti,ti);
81 | if nargout > 2
82 | xdoti(:,i) = interp2(t2,t1,xdot(:,:,i),ti,ti);
83 | if nargout > 3
84 | xddoti(:,i) = interp2(t2,t1,xddot(:,:,i),ti,ti);
85 | end
86 | end
87 | end
88 | else
89 | for i = 1:size(x,3)
90 | xi(:,i) = interp1(t1,x(:,:,i),ti);
91 | if nargout > 2
92 | xdoti(:,i) = interp1(t1,xdot(:,:,i),ti);
93 | if nargout > 3
94 | xddoti(:,i) = interp1(t1,xddot(:,:,i),ti);
95 | end
96 | end
97 | end
98 | end
99 |
100 | varargout{1} = ti;
101 | varargout{2} = xi;
102 |
103 | if nargout >2
104 | varargout{3} = xdoti;
105 | if nargout > 3
106 | varargout{4} = xddoti;
107 | end
108 | end
109 |
110 |
--------------------------------------------------------------------------------
/Utilities/hbm_derivatives.m:
--------------------------------------------------------------------------------
1 | function varargout = hbm_derivatives(fun,var,States,hbm,problem)
2 | NPts = size(States.t,2);
3 |
4 | if ~iscell(var)
5 | var = {var};
6 | end
7 |
8 | for i = 1:length(var)
9 | switch var{i}(1)
10 | case 'w'
11 | %for frequency derivatives, it's a bit more complicated as we have to rescale the time vector as well
12 | df_dw = feval(problem.model,[fun '_w'],States,hbm,problem);
13 | if isempty(df_dw)
14 | h = 1E-6;
15 | NFreq = length(States.w0);
16 |
17 | if hbm.dependence.w
18 | for k = 1:NFreq
19 | States2 = States;
20 | States2.w0(k) = States.w0(k) + h;
21 | States2.t(k,:) = States.w0(k)*States2.t(k,:)/States2.w0(k);
22 | States2.f = feval(problem.model,fun,States2,hbm,problem);
23 | df_dw{k} = (States2.f-States.f)./h;
24 | end
25 | else
26 | df_dw = repmat({zeros(size(f,1),NPts)},1,NFreq);
27 | end
28 | end
29 | for k = 1:length(States.w0)
30 | df_dw{k} = df_dw{k}.';
31 | end
32 | varargout{i} = df_dw;
33 | case 'x'
34 | %for everything else, we just peturb each element in turn
35 | df_dx = feval(problem.model,[fun '_' var{i}],States,hbm,problem);
36 | if isempty(df_dx)
37 | if hbm.dependence.(var{i})
38 | h = 1E-10;
39 | df_dx = zeros(size(States.f,1),problem.NDof,NPts);
40 | for j = 1:problem.NDof
41 | States2 = States;
42 | States2.(var{i})(j,:) = States.(var{i})(j,:) + h;
43 | States2.f = feval(problem.model,fun,States2,hbm,problem);
44 | df_dx(:,j,:) = permute((States2.f-States.f)./h,[1 3 2]);
45 | end
46 | else
47 | df_dx = zeros(size(States.f,1),problem.NDof,NPts);
48 | end
49 | end
50 | varargout{i} = df_dx;
51 | case 'u'
52 | %for everything else, we just peturb each element in turn
53 | df_du = feval(problem.model,[fun '_' var{i}],States,hbm,problem);
54 | if isempty(df_du)
55 | if hbm.dependence.(var{i})
56 | h = 1E-10;
57 | df_du = zeros(size(States.f,1),problem.NInput,NPts);
58 | for j = 1:problem.NInput
59 | States2 = States;
60 | States2.(var{i})(j,:) = States.(var{i})(j,:) + h;
61 | States2.f = feval(problem.model,fun,States2,hbm,problem);
62 | df_du(:,j,:) = permute((States2.f-States.f)./h,[1 3 2]);
63 | end
64 | else
65 | df_du = zeros(size(States.f,1),problem.NInput,NPts);
66 | end
67 | end
68 | varargout{i} = df_du;
69 | otherwise
70 | error('Unrecognized input')
71 | end
72 | end
--------------------------------------------------------------------------------
/Functions/hbm_solve.m:
--------------------------------------------------------------------------------
1 | function sol = hbm_solve(hbm,problem,w,A,X0)
2 | problem.type = 'solve';
3 |
4 | if nargin < 5 || isempty(X0)
5 | X0 = zeros(hbm.harm.NFreq,problem.NDof);
6 | end
7 | if size(X0,1) == hbm.harm.NFreq && size(X0,2) == problem.NDof
8 | x0 = packdof(X0);
9 | elseif isvector(X0) && length(X0) == hbm.harm.NComp*problem.NDof
10 | x0 = X0;
11 | X0 = unpackdof(x0,hbm.harm.NHarm,problem.NDof);
12 | else
13 | error('Wrong size for X0');
14 | end
15 |
16 | %setup the problem for IPOPT
17 | w0 = w*hbm.harm.rFreqRatio + hbm.harm.wFreq0;
18 | W = hbm.harm.kHarm*(hbm.harm.rFreqBase.*w0)';
19 |
20 | %we have an initial guess vector
21 | U = A*feval(problem.excite,hbm,problem,w0);
22 | u = packdof(U);
23 |
24 | hbm.bIncludeNL = true;
25 |
26 | init.X = X0;
27 | init.w = w;
28 | init.A = A;
29 |
30 | if isfield(problem,'xscale')
31 | xscale = [problem.xscale'; repmat(problem.xscale',hbm.harm.NFreq-1,1)*(1+1i)];
32 | problem.Zscale = packdof(xscale);
33 | problem.Fscale = problem.Zscale*0+1;
34 | problem.Jscale = (1./problem.Fscale(:))*problem.Zscale(:)';
35 | else
36 | problem = hbm_scaling(problem,hbm,init);
37 | end
38 |
39 | %and actually solve it
40 | hbm.max_iter = 3;
41 | bSuccess = false;
42 |
43 | constr_tol = 1E-6;
44 | maxit = 20;
45 |
46 | z0 = x0;
47 | Z0 = z0./problem.Zscale;
48 |
49 | attempts = 0;
50 | while ~bSuccess && attempts < hbm.max_iter
51 | switch hbm.options.solver
52 | case 'fsolve'
53 | fun_constr = @(x)hbm_constraints(x,hbm,problem,w,u);
54 | options = optimoptions('fsolve','Display','iter','SpecifyObjectiveGradient',true,'FunctionTolerance',constr_tol,'MaxIterations',maxit);
55 | [Z,~,EXITFLAG,OUTPUT] = fsolve(fun_constr,Z0,options);
56 | bSuccess = EXITFLAG == 1;
57 | iter = OUTPUT.iterations + 1;
58 | case 'ipopt'
59 | options.jacobian = @hbm_jacobian;
60 | options.jacobianstructure = hbm.sparsity;
61 | options.print_level = 5;
62 | options.max_iter = maxit;
63 | options.constr_viol_tol = constr_tol;
64 | [Z, info] = fipopt('',Z0,@hbm_constraints,options,hbm,problem,w,u);
65 | bSuccess = any(info.status == [0 1]);
66 | iter = info.iter;
67 | end
68 | Z0 = Z + 1E-8*rand(length(Z),1);
69 | attempts = attempts + 1;
70 | end
71 |
72 | if ~bSuccess
73 | Z = Z + NaN;
74 | end
75 | z = Z.*problem.Zscale;
76 |
77 | X = unpackdof(z,hbm.harm.NFreq-1,problem.NDof);
78 |
79 | sol.w = w;
80 | sol.W = W;
81 | sol.A = A;
82 | sol.X = X;
83 | sol.U = U;
84 | sol.F = hbm_output3d(hbm,problem,w,sol.U,sol.X);
85 |
86 | % floquet multipliers
87 | sol = hbm_floquet(hbm,problem,sol);
88 |
89 | %excitation forces
90 | sol = hbm_excitation_forces(problem,sol);
91 |
92 | sol.it = iter;
93 |
94 | function [c,J] = hbm_constraints(Z,hbm,problem,w,u)
95 | %unpack the inputs
96 | x = Z.*problem.Zscale;
97 | c = hbm_balance3d('func',hbm,problem,w,u,x);
98 | c = c ./ problem.Fscale;
99 | if nargout > 1
100 | J = hbm_jacobian(Z,hbm,problem,w,u);
101 | end
102 |
103 | function J = hbm_jacobian(Z,hbm,problem,w,u)
104 | x = Z.*problem.Zscale;
105 | J = hbm_balance3d('jacob',hbm,problem,w,u,x);
106 | J = J .* problem.Jscale;
--------------------------------------------------------------------------------
/Test/hbm_frf_test.m:
--------------------------------------------------------------------------------
1 | function hbm_frf_test(b3d)
2 | problem = test_params;
3 |
4 | hbm.harm.rFreqRatio = 1;
5 | hbm.harm.NHarm = 2;
6 | hbm.harm.Nfft = 32;
7 | hbm.harm.iHarmPlot = 2;
8 |
9 | if nargin < 1
10 | b3d = 1;
11 | end
12 |
13 | if b3d
14 | hbm.harm.rFreqRatio(end+1) = 1.376;
15 | hbm.harm.NHarm(end+1) = 2;
16 | hbm.harm.Nfft(end+1) = 16;
17 | hbm.harm.iHarmPlot(end+1) = 3;
18 | end
19 |
20 | hbm.dependence.x = true;
21 | hbm.dependence.xdot = true;
22 | hbm.dependence.w = false;
23 |
24 | hbm.scaling.tol = 1;
25 |
26 | hbm.cont.step0 = 1E-2;
27 | hbm.cont.max_step = 1E-1;
28 | hbm.cont.min_step = 1E-6;
29 |
30 | hbm.cont.method = 'predcorr';
31 |
32 | [hbm,problem] = setuphbm(hbm,problem);
33 |
34 | omega = sqrt(eig(problem.K,problem.M));
35 | w0 = max(min(omega)-2,0.5);
36 | wEnd = max(omega)+2;
37 | A = 1;
38 |
39 | S = {};
40 |
41 | tic;
42 | hbm.cont.method = 'none';
43 | sol = hbm_frf(hbm,problem,A,w0,[],wEnd,[]);
44 | S{end+1} = storeResults(sol,toc,'none');
45 |
46 | tic;
47 | hbm.cont.method = 'predcorr';
48 | hbm.cont.predcorr.corrector = 'arclength';
49 | sol = hbm_frf(hbm,problem,A,w0,[],wEnd,[]);
50 | S{end+1} = storeResults(sol,toc,'arclength');
51 |
52 | tic;
53 | hbm.cont.method = 'predcorr';
54 | hbm.cont.predcorr.corrector = 'pseudo';
55 | sol = hbm_frf(hbm,problem,A,w0,[],wEnd,[]);
56 | S{end+1} = storeResults(sol,toc,'pseudo');
57 |
58 | tic;
59 | S{end+1} = ode_frf(w0,wEnd,A,hbm,problem);
60 |
61 | figure
62 | hold on
63 | for j = 1:problem.NDof
64 | ax_mag(j) = subplot(2,problem.NDof,j);
65 | hold on
66 | for i = 1:length(S)
67 | hmag(i,j) = plot(ax_mag(j),S{i}.w,abs(S{i}.X(j,:)));
68 | end
69 | ylabel(ax_mag(j),sprintf('|X_%d| (mag)',j));
70 | end
71 |
72 | for j = 1:problem.NDof
73 | ax_ph(j) = subplot(2,problem.NDof,problem.NDof+j);
74 | hold on
75 | for i = 1:length(S)
76 | hph(i,j) = plot(ax_ph(j),S{i}.w,unwrap(angle(S{i}.X(j,:)),[],2));
77 | end
78 | xlabel(ax_ph(j),'\omega (rads)');
79 | ylabel(ax_ph(j),sprintf('\\angle X_%d (deg)',j));
80 | end
81 | linkaxes([ax_mag ax_ph],'x')
82 |
83 | for i = 1:length(S)
84 | leg{i} = S{i}.name;
85 | end
86 | legend(ax_ph(end),hph(:,end),leg)
87 |
88 | for i = 1:length(S)
89 | fprintf('%10s : %0.2f s\n',S{i}.name,S{i}.t)
90 | end
91 |
92 | function S = ode_frf(w0,wEnd,A,hbm,problem)
93 | y = zeros(1,2*problem.NDof);
94 | ws = linspace(w0,wEnd,20);
95 | NCycle = 50;
96 | Nfft = 101;
97 | for i = 1:length(ws)
98 | y0 = y(end,:);
99 | w0 = hbm.harm.rFreqRatio * ws(i);
100 | U = A*test_excite(hbm,problem,w0);
101 | T = 2*pi/ws(i);
102 | [t,y] = ode45(@(t,y)test_odefun(t,y,w0,U,hbm,problem),[0 NCycle*T],y0);
103 |
104 | ii = find(t>(t(end)-T),1);
105 | t = t(ii:end) - t(ii);
106 | y = y(ii:end,:);
107 |
108 | tReg = linspace(0,T,Nfft)';
109 | Fs = Nfft/T;
110 | yReg = interp1(t,y,tReg,'linear','extrap');
111 |
112 | Yfft = 2*fft(yReg,[],1)/Nfft;
113 | Wfft = (0:(Nfft-1))'/Nfft * 2*pi* Fs;
114 |
115 | X(:,i) = Yfft(2,1:problem.NDof);
116 | w(i) = Wfft(2);
117 | end
118 |
119 | S.X = X;
120 | S.w = w;
121 | S.t = toc;
122 | S.name = 'ode';
123 |
124 | function S = storeResults(sol,t,name)
125 | X = cat(3,sol.X);
126 |
127 | S.X = permute(X(2,:,:),[2 3 1]);
128 | S.w = cat(2,sol.w);
129 | S.t = t;
130 | S.name = name;
--------------------------------------------------------------------------------
/Generalised/hbm_balance3d.m:
--------------------------------------------------------------------------------
1 | function varargout = hbm_balance3d(command,hbm,problem,w,u,x)
2 | if hbm.options.bUseStandardHBM
3 | [varargout{1:nargout}] = hbm_balance(command,hbm,problem,w,u,x);
4 | return;
5 | end
6 | NDofTot = hbm.harm.NComp*problem.NDof;
7 |
8 | r = hbm.harm.rFreqRatio;
9 | w0 = w .* r + hbm.harm.wFreq0;
10 |
11 | A = hbm.lin.Ak + prod(w0)*hbm.lin.Ax;
12 | B = hbm.lin.Bk + prod(w0)*hbm.lin.Bx;
13 | dAdw = (r(1)*w0(2) + r(2)*w0(1))*hbm.lin.Ax;
14 | dBdw = (r(1)*w0(2) + r(2)*w0(1))*hbm.lin.Bx;
15 | for k = 1:2
16 | A = A + (w0(k)*hbm.lin.Ac{k} + w0(k)^2*hbm.lin.Am{k});
17 | B = B + (w0(k)*hbm.lin.Bc{k} + w0(k)^2*hbm.lin.Bm{k});
18 |
19 | dAdw = dAdw + r(k)*(hbm.lin.Ac{k} + 2*w0(k)*hbm.lin.Am{k});
20 | dBdw = dBdw + r(k)*(hbm.lin.Bc{k} + 2*w0(k)*hbm.lin.Bm{k});
21 | end
22 |
23 | switch command
24 | case 'func' %F, used by hbm_frf & hbm_bb
25 | cl = B*u - hbm.lin.b - A*x;
26 | if hbm.bIncludeNL
27 | cnl = hbm_nonlinear3d('func',hbm,problem,w0,x,u);
28 | else
29 | cnl = 0*cl;
30 | end
31 | c = (cl - cnl);
32 | varargout{1} = c;
33 | case 'jacob' %dF_dX, used by hbm_frf & hbm_bb
34 | Jl = -A;
35 | if hbm.bIncludeNL
36 | [Jx,Jxdot,Jxddot] = hbm_nonlinear3d({'jacobX','jacobXdot','jacobXddot'},hbm,problem,w0,x,u);
37 | Jnl = Jx + w0(1)*Jxdot{1} + w0(2)*Jxdot{2} + w0(1)^2*Jxddot{1} + w0(2)^2*Jxddot{2} + prod(w0)*Jxddot{3};
38 | else
39 | Jnl = 0*Jl;
40 | end
41 | J = (Jl - Jnl);
42 | varargout{1} = J;
43 | case 'derivW' %dF_dw, used by hbm_frf & hbm_bb
44 | Dl = dBdw*u - dAdw*x;
45 | cl = B*u - hbm.lin.b - A*x;
46 | if hbm.bIncludeNL
47 | if hbm.dependence.xdot || hbm.dependence.w
48 | [Jxdot,Jxddot,Judot,Juddot,Dw] = hbm_nonlinear3d({'jacobXdot','jacobXddot','jacobUdot','jacobUddot','derivW'},hbm,problem,w0,x,u);
49 | Dxdot = (r(1)*Jxdot{1} + r(2)*Jxdot{2})*x;
50 | Dxddot = (2*r(1)*w0(1)*Jxddot{1} + 2*r(2)*w0(2)*Jxddot{2} + (r(1)*w0(2) + r(2)*w0(1))*Jxddot{3})*x;
51 | Dudot = (r(1)*Judot{1} + r(2)*Judot{2})*u;
52 | Duddot = (2*r(1)*w0(1)*Juddot{1} + 2*r(2)*w0(2)*Juddot{2} + (r(1)*w0(2) + r(2)*w0(1))*Juddot{3})*u;
53 | Dw = r(1)*Dw{1} + r(2)*Dw{2};
54 | Dnl = Dxdot + Dxddot + Dudot + Duddot + Dw;
55 | else
56 | Dnl = 0*Dl;
57 | end
58 | else
59 | Dnl = 0*Dl;
60 | end
61 | D = (Dl - Dnl);
62 | varargout{1} = D;
63 | case 'derivA' %dF_dA, used in hbm_bb
64 | Dl = B*u;
65 | if hbm.bIncludeNL
66 | [Ju,Judot,Juddot] = hbm_nonlinear3d({'jacobU','jacobUdot','jacobUddot'},hbm,problem,w0,x,u);
67 | Dnl = (Ju + w0(1)*Judot{1} + w0(2)*Judot{2} +w0(1)^2*Juddot{1} + w0(2)^2*Juddot{2} + prod(w0)*Juddot{3})*u;
68 | else
69 | Dnl = 0*Dl;
70 | end
71 | D = (Dl - Dnl);
72 | varargout{1} = D;
73 | case 'floquet0'
74 | D0 = -hbm_balance3d('jacob',hbm,problem,w,u,x);
75 | varargout{1} = D0;
76 | case 'floquet1'
77 | D1l = hbm.lin.floquet.D1xdot + hbm.lin.floquet.D1Gxdot*w0(1) + 2*(w0(1)*hbm.lin.floquet.D1xddot{1} + w0(2)*hbm.lin.floquet.D1xddot{2});
78 | if hbm.bIncludeNL
79 | [D1xdot,D1xddot] = hbm_nonlinear3d({'floquet1xdot','floquet1xddot'},hbm,problem,w0,x,u);
80 | D1nl = D1xdot + 2*w0(1)*D1xddot{1} + 2*w0(2)*D1xddot{2};
81 | else
82 | D1nl = 0*D1l;
83 | end
84 | varargout{1} = (D1l - D1nl);
85 | case 'floquet2'
86 | D2l = hbm.lin.floquet.D2;
87 | if hbm.bIncludeNL
88 | D2nl = hbm_nonlinear3d({'floquet2'},hbm,problem,w0,x,u);
89 | else
90 | D2nl = 0*D2l;
91 | end
92 | varargout{1} = (D2l - D2nl);
93 | end
--------------------------------------------------------------------------------
/Test/hbm_bb_test.m:
--------------------------------------------------------------------------------
1 | function hbm_bb_test(b3d)
2 | problem = test_params;
3 |
4 | P = problem.P;
5 |
6 | hbm.harm.rFreqRatio = 1;
7 | hbm.harm.NHarm = 2;
8 | hbm.harm.Nfft = 32;
9 | hbm.harm.iHarmPlot = 2;
10 |
11 | hbm.dependence.x = true;
12 | hbm.dependence.xdot = true;
13 | hbm.dependence.w = false;
14 |
15 | hbm.scaling.tol = 1;
16 |
17 | hbm.cont.step0 = 1E-2;
18 | hbm.cont.max_step = 1E-1;
19 | hbm.cont.min_step = 1E-6;
20 |
21 | if nargin < 1
22 | b3d = 1;
23 | end
24 |
25 | if b3d
26 | hbm.harm.rFreqRatio(end+1) = 1.376;
27 | hbm.harm.NHarm(end+1) = 2;
28 | hbm.harm.Nfft(end+1) = 16;
29 | hbm.harm.iHarmPlot(end+1) = 3;
30 | end
31 |
32 | hbm.cont.method = 'predcorr';
33 | hbm.cont.predcorr.corrector = 'arclength';
34 |
35 | problem.res.iOutput = P.iDof;
36 | problem.res.iInput = P.iInput;
37 | problem.res.iHarm = 2;
38 | problem.res.sign = 1;
39 | problem.res.output = 'x';
40 | problem.res.input = 'u';
41 |
42 | [hbm,problem] = setuphbm(hbm,problem);
43 |
44 | iRes = [];
45 | iRes(end+1) = find(hbm.harm.kHarm(:,1) == 1 & hbm.harm.kHarm(:,2) == 0);
46 | iRes(end+1) = find(hbm.harm.kHarm(:,1) == 0 & hbm.harm.kHarm(:,2) == 1);
47 | NRes = length(iRes);
48 |
49 | iMode = 1;
50 | omega = sqrt(eig(problem.K,problem.M));
51 | w0 = max(omega(iMode)-2,0.5);
52 | wEnd = omega(iMode)+2;
53 |
54 | figure
55 | for j = 1:NRes
56 | ax(1,j) = subplot(2,NRes,j);
57 | hold on
58 | if j == 1
59 | ylabel('|X| (mag)');
60 | end
61 | title(sprintf('iHarm %d',iRes(j)))
62 |
63 | ax(2,j) = subplot(2,NRes,NRes+j);
64 | hold on
65 | if j == 1
66 | ylabel('\angle X (deg)');
67 | end
68 |
69 | xlabel('\omega (rads)');
70 | end
71 | linkaxes(ax(:),'x')
72 | xlim([w0,wEnd]);
73 |
74 | As = [1 2 3 4];
75 |
76 | root = fileparts(which(mfilename));
77 | for i = 1:length(As)
78 | file = fullfile(root,sprintf('FRF_A = %0.1f.mat',As(i)));
79 | if ~isfile(file)
80 | sol = hbm_frf(hbm,problem,As(i),w0,[],wEnd,[]);
81 | results.X = cat(3,sol.X);
82 | results.U = cat(3,sol.U);
83 | results.w = cat(2,sol.w);
84 | save(file,'-struct','results');
85 | else
86 | results = load(file);
87 | end
88 |
89 | x = results.X;
90 | u = results.U;
91 | w = results.w;
92 |
93 | for j = 1:NRes
94 | X = permute((x(iRes(j),:,:)),[2 3 1]);
95 | U = permute((u(iRes(j),:,:)),[2 3 1]);
96 | H = X(P.iDof,:)./U(P.iInput,:);
97 | plot(ax(1,j),w,abs(H));
98 | plot(ax(2,j),w,unwrap(angle(H),[],2));
99 |
100 | file = fullfile(root,sprintf('RES_A = %0.1f_%d.mat',As(i),j));
101 | if ~isfile(file)
102 | problem.res.iHarm = iRes(j);
103 | [hbm,problem] = setuphbm(hbm,problem);
104 | [~,ii] = max(H);
105 | res = hbm_res(hbm,problem,w(ii),As(i),x(:,:,ii));
106 | save(file,'-struct','res');
107 | else
108 | res = load(file);
109 | end
110 |
111 | Xres{j}(:,:,i) = res.X;
112 | wres{j}(i) = res.w;
113 | plot(ax(1,j),res.w,abs(res.H),'o')
114 | plot(ax(2,j),res.w,angle(res.H),'o')
115 | end
116 | drawnow
117 | end
118 |
119 | bb = hbm;
120 | bb.cont.method = 'none';
121 |
122 | for j = 1:NRes
123 | A0 = As(1); w0 = wres{j}(1); X0 = Xres{j}(:,:,1);
124 | Aend = As(end); wEnd = wres{j}(end); XEnd = Xres{j}(:,:,end);
125 |
126 | problem.res.iHarm = iRes(j);
127 | [hbm,problem] = setuphbm(hbm,problem);
128 |
129 | file = fullfile(root,sprintf('BB_%d.mat',j));
130 | if ~isfile(file)
131 | sol = hbm_bb(bb,problem,A0,w0,X0,Aend,wEnd,XEnd);
132 | results.X = cat(3,sol.X);
133 | results.U = cat(3,sol.U);
134 | results.w = cat(2,sol.w);
135 | results.H = cat(2,sol.H);
136 | save(file,'-struct','results');
137 | else
138 | results = load(file);
139 | end
140 |
141 | for k = 1:NRes
142 | X = squeeze(results.X(iRes(k),P.iDof,:));
143 | U = squeeze(results.U(iRes(k),P.iInput,:));
144 | H = X./U;
145 | plot(ax(1,k),results.w,abs(H));
146 | plot(ax(2,k),results.w,unwrap(angle(H)));
147 | end
148 | end
--------------------------------------------------------------------------------
/Functions/hbm_res.m:
--------------------------------------------------------------------------------
1 | function sol = hbm_res(hbm,problem,w,A,X0)
2 | problem.type = 'resonance';
3 |
4 | if nargin < 5 || isempty(X0)
5 | X0 = zeros(hbm.harm.NFreq,problem.NDof);
6 | end
7 | if size(X0,1) == hbm.harm.NFreq && size(X0,2) == problem.NDof
8 | x0 = packdof(X0);
9 | elseif isvector(X0) && length(X0) == hbm.harm.NComp*problem.NDof
10 | x0 = X0;
11 | X0 = unpackdof(x0,hbm.harm.NHarm,problem.NDof);
12 | else
13 | error('Wrong size for X0');
14 | end
15 |
16 | %setup the problem for IPOPT
17 | hbm.bIncludeNL = true;
18 |
19 | %first solve @ w0
20 | sol = hbm_solve(hbm,problem,w,A,X0);
21 | X0 = sol.X;
22 | z0 = packdof(X0);
23 |
24 | init.X = X0;
25 | init.w = w;
26 | init.A = A;
27 |
28 | if isfield(problem,'xscale')
29 | xscale = [problem.xscale'; repmat(problem.xscale',hbm.harm.NFreq-1,1)*(1+1i)];
30 | problem.Xscale = packdof(xscale)*sqrt(length(xscale));
31 | problem.wscale = w;
32 | problem.Fscale = problem.Xscale*0+1;
33 | problem.Zscale = [problem.Xscale; problem.wscale];
34 | problem.Jscale = (1./problem.Fscale(:))*problem.Zscale(:)';
35 | else
36 | problem = hbm_scaling(problem,hbm,init);
37 | end
38 |
39 | %and actually solve it
40 | hbm.max_iter = 4;
41 | bSuccess = false;
42 |
43 | constr_tol = 1E-6;
44 | opt_tol = 1E-6;
45 | maxit = 20;
46 |
47 | Z0 = [z0; w]./problem.Zscale;
48 |
49 | zlb = [z0-Inf;problem.res.wMin./problem.wscale];
50 | zub = [z0+Inf;problem.res.wMax./problem.wscale];
51 |
52 | attempts = 0;
53 | while ~bSuccess && attempts < hbm.max_iter
54 | switch hbm.options.solver
55 | case 'fsolve'
56 | fun_obj = @(x)hbm_fsolve_obj(x,hbm,problem,A);
57 | fun_constr = @(x)hbm_fsolve_constr(x,hbm,problem,A);
58 | options = optimoptions('fmincon','SpecifyObjectiveGradient',true,'SpecifyConstraintGradient',true,'Display','iter',...
59 | 'OptimalityTolerance',opt_tol,'ConstraintTolerance',constr_tol,'MaxIterations',maxit);
60 | [Z,~,EXITFLAG,OUTPUT] = fmincon(fun_obj,Z0,[],[],[],[],zlb,zub,fun_constr,options);
61 | bSuccess = EXITFLAG == 1;
62 | iter = OUTPUT.iterations + 1;
63 | case 'ipopt'
64 | options.jacobianstructure = [hbm.sparsity ones(problem.NDof*hbm.harm.NComp,1)];
65 | options.jacobian = @hbm_jacobian;
66 | options.gradient = @hbm_grad;
67 | options.print_level = 5;
68 | options.max_iter = maxit;
69 | options.tol = opt_tol;
70 | options.constr_viol_tol = constr_tol;
71 | options.lb = zlb;
72 | options.ub = zub;
73 | [Z, info] = fipopt(@hbm_obj,Z0,@hbm_constr,options,hbm,problem,A);
74 | bSuccess = any(info.status == [0 1]);
75 | iter = info.iter;
76 | end
77 | Z0 = Z+rand(length(Z),1)*1E-8;
78 | attempts = attempts + 1;
79 | end
80 | if ~bSuccess
81 | Z = Z + NaN;
82 | end
83 | z = Z.*problem.Zscale;
84 |
85 | w = z(end);
86 | w0 = w*hbm.harm.rFreqRatio + hbm.harm.wFreq0;
87 | W = hbm.harm.kHarm*(hbm.harm.rFreqBase.*w0)';
88 |
89 | U = A*feval(problem.excite,hbm,problem,w0);
90 | u = packdof(U);
91 |
92 | x = z(1:end-1);
93 | X = unpackdof(x,hbm.harm.NFreq-1,problem.NDof);
94 |
95 | sol.w = w;
96 | sol.W = W;
97 | sol.A = A;
98 | sol.X = X;
99 | sol.U = U;
100 | sol.F = hbm_output3d(hbm,problem,w,sol.U,sol.X);
101 |
102 | %floquet multipliers & objective
103 | sol.H = hbm_objective('complex',hbm,problem,w,z(1:end-1),u);
104 | sol = hbm_floquet(hbm,problem,sol);
105 | sol = hbm_excitation_forces(problem,sol);
106 |
107 | sol.it = iter;
108 |
109 | function obj = hbm_obj(Z,hbm,problem,A)
110 | x = Z(1:end-1).*problem.Xscale;
111 | w = Z(end).*problem.wscale;
112 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
113 |
114 | U = A*feval(problem.excite,hbm,problem,w0);
115 | u = packdof(U);
116 |
117 | H = hbm_objective('func',hbm,problem,w,x,u);
118 | obj = - problem.res.sign * H;
119 |
120 | function G = hbm_grad(Z,hbm,problem,A)
121 | x = Z(1:end-1).*problem.Xscale;
122 | w = Z(end).*problem.wscale;
123 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
124 |
125 | U = A*feval(problem.excite,hbm,problem,w0);
126 | u = packdof(U);
127 |
128 | [Dx, Dw] = hbm_objective({'jacobX','derivW'},hbm,problem,w,x,u);
129 | G = -problem.res.sign*[Dx Dw];
130 |
131 | G = G.*problem.Zscale(:)';
132 |
133 | function c = hbm_constr(Z,hbm,problem,A)
134 | x = Z(1:end-1).*problem.Xscale;
135 | w = Z(end).*problem.wscale;
136 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
137 |
138 | U = A*feval(problem.excite,hbm,problem,w0);
139 | u = packdof(U);
140 |
141 | c = hbm_balance3d('func',hbm,problem,w,u,x);
142 |
143 | function J = hbm_jacobian(Z,hbm,problem,A)
144 | x = Z(1:end-1).*problem.Xscale;
145 | w = Z(end).*problem.wscale;
146 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
147 |
148 | U = A*feval(problem.excite,hbm,problem,w0);
149 | u = packdof(U);
150 |
151 | Jx_nl = hbm_balance3d('jacob',hbm,problem,w,u,x);
152 | Dw_nl = hbm_balance3d('derivW',hbm,problem,w,u,x);
153 |
154 | J = [Jx_nl Dw_nl];
155 | J = J .* problem.Jscale;
156 |
157 | function [obj,G] = hbm_fsolve_obj(Z,hbm,problem,A)
158 | obj = hbm_obj(Z,hbm,problem,A);
159 | if nargout > 1
160 | G = hbm_grad(Z,hbm,problem,A)';
161 | end
162 |
163 | function [c,ceq,Jc,Jceq] = hbm_fsolve_constr(Z,hbm,problem,A)
164 | ceq = hbm_constr(Z,hbm,problem,A);
165 | c = [];
166 | % c = ceq(end);
167 | % ceq = ceq(1:end-1);
168 | if nargout > 2
169 | Jceq = hbm_jacobian(Z,hbm,problem,A);
170 | Jc = zeros(0,length(Z));
171 |
172 | Jc = Jc';
173 | Jceq = Jceq';
174 | % Jc = Jceq(end,:)';
175 | % Jceq = Jceq(1:end-1,:)';
176 | end
--------------------------------------------------------------------------------
/Utilities/hbm_objective.m:
--------------------------------------------------------------------------------
1 | function varargout = hbm_objective(part,hbm,problem,w,x,u)
2 |
3 | NOutput = problem.res.NOutput;
4 | NInput = problem.res.NInput;
5 |
6 | r = hbm.harm.rFreqRatio;
7 | w0 = w .* r + hbm.harm.wFreq0;
8 |
9 | if problem.res.iHarm > 1
10 | iRe = (1:NOutput)' + 2*(problem.res.iHarm - 2)*NOutput + NOutput;
11 | iIm = (1:NOutput)' + 2*(problem.res.iHarm - 2)*NOutput + 2*NOutput;
12 |
13 | jRe = (1:NInput)' + 2*(problem.res.iHarm - 2)*NInput + NInput;
14 | jIm = (1:NInput)' + 2*(problem.res.iHarm - 2)*NInput + 2*NInput;
15 | else
16 | iRe = (1:NOutput)';
17 | iIm = [];
18 |
19 | jRe = (1:NInput)';
20 | jIm = [];
21 | end
22 |
23 | kk = hbm.harm.kHarm*(hbm.harm.rFreqBase.*hbm.harm.rFreqRatio)';
24 | D = [0 -1;
25 | 1 0];
26 | D = blkdiag(0,kron(diag(kk(2:end)),D));
27 |
28 | %% output
29 | Wx = kron(D,eye(problem.NDof));
30 | switch problem.res.output
31 | case 'none'
32 | Fnl = 0*x;
33 | case 'fnl'
34 | Fnl = hbm_nonlinear3d({'func'},hbm,problem,w0,x,u);
35 | case 'x'
36 | Fnl = x;
37 | case 'xdot'
38 | Fnl = w*Wx*x;
39 | case 'xddot'
40 | Fnl = w^2*Wx*Wx*x;
41 | end
42 |
43 | Fb = Fnl(iRe);
44 | if ~isempty(iIm)
45 | Fb = Fb + 1i*Fnl(iIm);
46 | end
47 | Fb = problem.res.ROutput*Fb;
48 |
49 | %% input
50 | Wu = kron(D,eye(problem.NInput));
51 | switch problem.res.input
52 | case 'unity'
53 | Fex = 0*u+1;
54 | case 'fe'
55 | Ju = prod(w0)*hbm.lin.Bx + hbm.lin.Bk;
56 | for k = 1:2
57 | Ju = Ju + (w0(k)*hbm.lin.Bc{k} + w0(k)^2*hbm.lin.Bm{k});
58 | end
59 | Fex = Ju*u;
60 | case 'u'
61 | Fex = u;
62 | case 'udot'
63 | Fex = w * Wu*u;
64 | case 'uddot'
65 | Fex = w^2 * Wu*Wu*u;
66 | end
67 |
68 | Fe = Fex(jRe);
69 | if ~isempty(jIm)
70 | Fe = Fe + 1i*Fex(jIm);
71 | end
72 | Fe = problem.res.RInput*Fe;
73 |
74 | if ~iscell(part)
75 | part = {part};
76 | end
77 | varargout = cell(1,length(part));
78 |
79 | for i = 1:length(part)
80 | switch part{i}
81 | case 'complex'
82 | H = Fb./Fe;
83 | varargout{i} = H;
84 | case 'func'
85 | H = abs(Fb./Fe);
86 | varargout{i} = H;
87 | case 'jacobX'
88 |
89 | %nl
90 | switch problem.res.output
91 | case 'none'
92 | Jx = 0*Wx;
93 | case 'fnl'
94 | Jx = hbm_nonlinear3d({'jacobX'},hbm,problem,w0,x,u);
95 | case 'x'
96 | Jx = eye(problem.NDof*hbm.harm.NComp);
97 | case 'xdot'
98 | Jx = Wx;
99 | case 'xddot'
100 | Jx = Wx*Wx;
101 | end
102 |
103 | if ~isempty(iIm)
104 | dFbdx = (real(Fb)*Jx(iRe,:) + imag(Fb)*Jx(iIm,:))./(abs(Fb) + eps);
105 | else
106 | dFbdx = (real(Fb)*Jx(iRe,:))./(abs(Fb) + eps);
107 | end
108 | dFbdx = problem.res.ROutput*dFbdx;
109 |
110 | %excitation
111 | dFedx = 0;
112 |
113 | %put it all together
114 | dHdx = (dFbdx.*abs(Fe) - abs(Fb).*dFedx)./abs(Fe).^2;
115 |
116 | varargout{i} = dHdx;
117 | case 'derivW'
118 |
119 | %nl
120 | switch problem.res.output
121 | case 'none'
122 | Dw_nl = 0*x;
123 | case 'fnl'
124 | Dw = hbm_nonlinear3d({'derivW'},hbm,problem,w0,x,u);
125 | if ~iscell(Dw)
126 | Dw = {Dw,0*Dw};
127 | end
128 | Dw_nl = 0*Dw{1};
129 | for k = 1:2
130 | Dw_nl = Dw_nl + r(k)*Dw{k};
131 | end
132 | case 'x'
133 | Dw_nl = zeros(problem.NDof*hbm.harm.NComp,1);
134 | case 'xdot'
135 | Dw_nl = (Wx*x);
136 | case 'xddot'
137 | Dw_nl = 2*w*(Wx*Wx*x);
138 | end
139 | if ~isempty(iIm)
140 | dFbdw = (real(Fb)*Dw_nl(iRe) + imag(Fb)*Dw_nl(iIm))./(abs(Fb) + eps);
141 | else
142 | dFbdw = (real(Fb)*Dw_nl(iRe))./(abs(Fb) + eps);
143 | end
144 | dFbdw = problem.res.ROutput*dFbdw;
145 |
146 | %excitation
147 | switch problem.res.input
148 | case 'unity'
149 | Dw_u = 0*u;
150 | case 'fe'
151 | Dw_u = (r(1)*w0(2) + r(2)*w0(1))*hbm.lin.Bx*u;
152 | for k = 1:2
153 | Dw_u = Dw_u + r(k)*(hbm.lin.Bc{k} + 2*w0(k)*hbm.lin.Bm{k})*u;
154 | end
155 | case 'u'
156 | Dw_u = zeros(problem.NInput*hbm.harm.NComp,1);
157 | case 'udot'
158 | Dw_u = (Wu*u);
159 | case 'uddot'
160 | Dw_u = 2*w*(Wu*Wu*u);
161 | end
162 | if ~isempty(iIm)
163 | dFedw = (real(Fe)*Dw_u(jRe) + imag(Fe)*Dw_u(jIm))./abs(Fe);
164 | else
165 | dFedw = (real(Fe)*Dw_u(jRe))./abs(Fe);
166 | end
167 | dFedw = problem.res.RInput*dFedw;
168 |
169 | dHdw = (dFbdw.*abs(Fe) - abs(Fb).*dFedw)./abs(Fe).^2;
170 |
171 | varargout{i} = dHdw;
172 | end
173 | end
--------------------------------------------------------------------------------
/Standard/hbm_nonlinear.m:
--------------------------------------------------------------------------------
1 | function varargout = hbm_nonlinear(command,hbm,problem,w0,Xp,Up)
2 | NInput = problem.NInput;
3 | NDof = problem.NDof;
4 |
5 | ii = find(hbm.harm.NHarm ~= 0);
6 |
7 | NFreq = hbm.harm.NFreq;
8 | NComp = hbm.harm.NComp;
9 | NDofTot = NComp*NDof;
10 | NInputTot = NComp*NInput;
11 |
12 | %work out the time domain
13 | X = unpackdof(Xp,NFreq-1,NDof);
14 | U = unpackdof(Up,NFreq-1,NInput);
15 |
16 | %get the time series
17 | States = hbm_states(w0,X,U,hbm);
18 |
19 | %push through the nl system
20 | States.f = feval(problem.model,'nl' ,States,hbm,problem);
21 |
22 | %finally convert into the time domain
23 | F = hbm.nonlin.FFT*States.f.';
24 | Fp = packdof(F);
25 |
26 | if ~iscell(command)
27 | command = {command};
28 | end
29 |
30 | varargout = cell(1,length(command));
31 |
32 | ijacobx = hbm.nonlin.hbm.ijacobx;
33 | ijacobu = hbm.nonlin.hbm.ijacobu;
34 |
35 | for o = 1:length(command)
36 | switch command{o}
37 | case 'func'
38 | %all done
39 | varargout{o} = Fp;
40 | case 'derivW' %df_dw
41 | if ~hbm.dependence.w
42 | Dw = zeros(NDofTot,1);
43 | else
44 | dfhbm_dw = hbm_derivatives('nl' ,'w',States,hbm,problem);
45 | if isfield(problem,'derivW')
46 | Dw = feval(problem.derivW,dfhbm_dw,hbm,problem);
47 | else
48 | Dw = packdof(hbm.nonlin.FFT*dfhbm_dw{ii});
49 | end
50 | end
51 | varargout{o} = Dw;
52 | case 'jacobX' %df_dX = Jx*X
53 | if ~hbm.dependence.x
54 | Jx = zeros(NDofTot,NDofTot);
55 | else
56 | States.df_dx = hbm_derivatives('nl','x',States,hbm,problem);
57 |
58 | if isfield(problem,'jacobX')
59 | Jx = feval(problem.jacobX,States,hbm,problem);
60 | else
61 | Jx = sum(hbm.nonlin.hbm.Jx.*States.df_dx(ijacobx,ijacobx,:),3);
62 | end
63 | end
64 | varargout{o} = Jx;
65 | case 'jacobU' %df_dU = Ju*U
66 | if ~hbm.dependence.u
67 | Ju = zeros(NDofTot,NInputTot);
68 | else
69 | States.df_du = hbm_derivatives('nl' ,'u',States,hbm,problem);
70 |
71 | if isfield(problem,'jacobU')
72 | Ju = feval(problem.jacobU,States,problem);
73 | else
74 | Ju = sum(hbm.nonlin.hbm.Ju.*States.df_du(ijacobx,ijacobu,:),3);
75 | end
76 | end
77 | varargout{o} = Ju;
78 | case 'jacobXdot' %df_dX = w*Jxdot*X
79 | if ~hbm.dependence.xdot
80 | Jxdot = zeros(DofTot,NDofTot);
81 | else
82 | States.df_dxdot = hbm_derivatives('nl' ,'xdot',States,hbm,problem);
83 |
84 | if isfield(problem,'jacobXdot')
85 | Jxdot = feval(problem.jacobXdot,States,hbm,problem);
86 | else
87 | Jxdot = sum(hbm.nonlin.hbm.Jxdot{ii}.*States.df_dxdot(ijacobx,ijacobx,:),3);
88 | end
89 | end
90 | varargout{o} = Jxdot;
91 | case 'jacobUdot' %df_dU = w*Judot*U
92 | if ~hbm.dependence.udot
93 | Judot = zeros(NDofTot,NInputTot);
94 | else
95 | States.df_dudot = hbm_derivatives('nl' ,'udot',States,hbm,problem);
96 |
97 | if isfield(problem,'jacobUdot')
98 | Judot = feval(problem.jacobUdot,States,hbm,problem);
99 | else
100 | Judot = sum(hbm.nonlin.hbm.Judot{ii}.*States.df_dudot(ijacobx,ijacobu,:),3);
101 | end
102 | end
103 | varargout{o} = Judot;
104 | case 'jacobXddot' %df_dX = w^2*Jxddot*X
105 | if ~hbm.dependence.xddot
106 | Jxddot = zeros(NDofTot,NDofTot);
107 | else
108 | States.df_dxddot = hbm_derivatives('nl' ,'xddot',States,hbm,problem);
109 |
110 | if isfield(problem,'jacobXddot')
111 | Jxddot = feval(problem.jacobXddot,States,hbm,problem);
112 | else
113 | Jxddot = sum(hbm.nonlin.hbm.Jxddot{ii}.*States.df_dxddot(ijacobx,ijacobx,:),3);
114 | end
115 | end
116 | varargout{o} = Jxddot;
117 | case 'jacobUddot' %df_dU = w^2*Juddot*U
118 | if ~hbm.dependence.uddot
119 | Juddot = zeros(NDofTot,NInputTot);
120 | else
121 | States.df_duddot = hbm_derivatives('nl' ,'uddot',States,hbm,problem);
122 |
123 | if isfield(problem,'jacobUddot')
124 | Juddot = feval(problem.jacobUddot,States,hbm,problem);
125 | else
126 | Juddot = sum(hbm.nonlin.hbm.Juddot{ii}.*States.df_duddot(ijacobx,ijacobu,:),3);
127 | end
128 | end
129 | varargout{o} = Juddot;
130 | case 'floquet1xdot'
131 | if ~hbm.dependence.xdot
132 | D1 = zeros(NDofTot,NDofTot);
133 | else
134 | States.df_dxdot = hbm_derivatives('nl','xdot',States,hbm,problem);
135 |
136 | if isfield(problem,'floquet1xdot')
137 | D1 = feval(problem.floquet1xdot,States,hbm,problem);
138 | else
139 | D1 = sum(hbm.nonlin.hbm.Jx.*States.df_dxdot(ijacobx,ijacobx,:),3);
140 | end
141 | end
142 | varargout{o} = D1;
143 | case 'floquet1xddot'
144 | if ~hbm.dependence.xddot
145 | D1dd = zeros(NDofTot,NDofTot);
146 | else
147 | States.df_dxddot = hbm_derivatives('nl' ,'xddot',States,hbm,problem);
148 |
149 | if isfield(problem,'floquet1xddot')
150 | D1dd = feval(problem.floquet1xddot,States,hbm,problem);
151 | else
152 | D1dd = sum(hbm.nonlin.hbm.Jxdot{ii}.*States.df_dxddot(ijacobx,ijacobx,:),3);
153 | end
154 | end
155 | varargout{o} = D1dd;
156 | case 'floquet2'
157 | if ~hbm.dependence.xddot
158 | D2 = zeros(NDofTot,NDofTot);
159 | else
160 | States.df_dxddot = hbm_derivatives('nl' ,'xddot',States,hbm,problem);
161 |
162 | if isfield(problem,'floquet2')
163 | D2 = feval(problem.floquet2,States,hbm,problem);
164 | else
165 | D2 = sum(hbm.nonlin.hbm.Jx.*States.df_dxddot(ijacobx,ijacobx,:),3);
166 | end
167 | end
168 | varargout{o} = D2;
169 | end
170 | end
--------------------------------------------------------------------------------
/Setup/setuphbm.m:
--------------------------------------------------------------------------------
1 | function [hbm,problem] = setuphbm(hbm,problem)
2 |
3 | %% Harmonics
4 | if ~isfield(hbm,'harm')
5 | hbm.harm = struct();
6 | end
7 | hbm.harm = setupHarm(hbm.harm);
8 |
9 | %% Options
10 | if ~isfield(hbm,'options')
11 | hbm.options = struct();
12 | end
13 | hbm.options = setupOptions(hbm.options);
14 | if hbm.options.bUseStandardHBM && prod(hbm.harm.NHarm) > 0
15 | error('Cannot use standard HBM code in case of more than one fundemental')
16 | end
17 |
18 | %% Dependence
19 | if ~isfield(hbm,'dependence')
20 | hbm.dependence = struct();
21 | end
22 | hbm.dependence = setupDependence(hbm.dependence);
23 |
24 | %% Scaling
25 | if ~isfield(hbm,'scaling')
26 | hbm.scaling = struct();
27 | end
28 | hbm.scaling = setupScaling(hbm.scaling);
29 |
30 | %% Continuation
31 | if ~isfield(hbm,'cont')
32 | hbm.cont = struct();
33 | end
34 | hbm.cont = setupCont(hbm.cont);
35 |
36 | %% Problem definition
37 | problem = setupProblem(problem,hbm);
38 |
39 | if ~isfield(problem,'sparsity')
40 | hbm.sparsity = ones(hbm.harm.NComp*problem.NDof);
41 | else
42 | hbm.sparsity = repmat(problem.sparsity(1:problem.NDof,1:problem.NDof),hbm.harm.NComp);
43 | end
44 |
45 | %% Precompute matrices
46 | hbm.lin = setupLin(hbm.harm,problem);
47 | hbm.nonlin = setupNonlin(hbm.harm,problem);
48 |
49 | hbm.harm = default_missing(hbm.harm,{'iHarmPlot'},{1:hbm.harm.NFreq});
50 |
51 | if ~isfield(problem,'RDofPlot')
52 | if ~isfield(problem,'iDofPlot')
53 | problem.iDofPlot = 1:problem.NDof;
54 | end
55 |
56 | R = zeros(length(problem.iDofPlot),problem.NDof);
57 | for j = 1:length(problem.iDofPlot)
58 | R(j,problem.iDofPlot(j)) = 1;
59 | end
60 |
61 | problem.RDofPlot = R;
62 | elseif size(problem.RDofPlot,2) ~= problem.NDof
63 | error('Wrong size for RDofPlot')
64 | end
65 |
66 | function options = setupOptions(options)
67 | if ~isfield(options,'bUseStandardHBM'), options.bUseStandardHBM = 0; end
68 | if ~isfield(options,'bVerbose'), options.bVerbose = 1; end
69 | if ~isfield(options,'bPlot'), options.bPlot = 1; end
70 |
71 | if ~isfield(options,'solver'), options.solver = 'ipopt'; end
72 |
73 | function dependence = setupDependence(dependence)
74 | dependence = default_missing(dependence,{'x','xdot','xddot','w','u','udot','uddot'},{true,false,false,false,false,false,false});
75 |
76 | function scaling = setupScaling(scaling)
77 | scaling = default_missing(scaling,{'method','tol'},{'max',1E-6});
78 |
79 | function cont = setupCont(cont)
80 | cont = default_missing(cont,{'method','bUpdate','step0','min_step','max_step','ftol','xtol','c', 'C','maxfail','num_iter_increase','num_iter_reduce'},{'predcorr',true,1E-3, 1E-6, 5E-3, 1E-6,1E-6,0.5, 1.05,4,10,3});
81 |
82 | if ~isfield(cont,'predcorr'), cont.predcorr = struct(); end
83 | cont.predcorr = default_missing(cont.predcorr,{'predictor','corrector','bMoorePenrose','solver','maxit'},{'linear','pseudo',true,'ipopt',30});
84 |
85 | function problem = setupProblem(problem,hbm)
86 | if ~isfield(problem,'name')
87 | problem.name = '';
88 | end
89 | problem.NDof = size(problem.K,2);
90 | problem.NInput = size(problem.Ku,2);
91 |
92 | f = {'K','M','C','G'};
93 | for i = 1:length(f)
94 | if ~isfield(problem,f{i})
95 | problem.(f{i}) = zeros(problem.NDof);
96 | elseif size(problem.(f{i}),1) ~= problem.NDof || size(problem.(f{i}),2) ~= problem.NDof
97 | error('Wrong size for %s matrix',f{i})
98 | end
99 | end
100 |
101 | if ~isfield(problem,'F0')
102 | problem.('F0') = zeros(problem.NDof,1);
103 | elseif length(problem.('F0')) ~= problem.NDof
104 | error('Wrong size for %s matrix','F0')
105 | end
106 |
107 |
108 | f = {'Ku','Cu','Mu'};
109 | for i = 1:length(f)
110 | if ~isfield(problem,f{i})
111 | problem.(f{i}) = zeros(problem.NDof,problem.NInput);
112 | elseif size(problem.(f{i}),1) ~= problem.NDof || size(problem.(f{i}),2) ~= problem.NInput
113 | error('Wrong size for %s matrix',f{i})
114 | end
115 | end
116 |
117 | try
118 | States = empty_states(problem);
119 | out = feval(problem.model,'output',States,hbm,problem);
120 | problem.NOutput = length(out);
121 | catch
122 | error('Error detected in non-linear function')
123 | end
124 |
125 | if ~isfield(problem,'update')
126 | problem.update = [];
127 | end
128 |
129 | if isfield(problem,'res')
130 | f = {'input','output','iHarm'};
131 |
132 | if ~isfield(problem.res,f{i})
133 | error('Missing field %s from resonance condition',f{i})
134 | end
135 |
136 | switch problem.res.input
137 | case 'unity'
138 | problem.res.NInput = 1;
139 | case 'fe'
140 | problem.res.NInput = problem.NDof;
141 | case {'u','udot','uddot'}
142 | problem.res.NInput = problem.NInput;
143 | otherwise
144 | error('invalid input')
145 | end
146 |
147 | if ~isfield(problem.res,'RInput')
148 | if ~isfield(problem.res,'iInput')
149 | problem.res.iInput = 1:problem.res.NInput;
150 | end
151 |
152 | R = zeros(length(problem.res.iInput),problem.res.NInput);
153 | for j = 1:length(problem.res.iInput)
154 | R(j,problem.res.iInput(j)) = 1;
155 | end
156 |
157 | problem.res.RInput = R;
158 | elseif size(problem.res.RInput,2) ~= problem.res.NInput
159 | error('Wrong size for RInput')
160 | end
161 |
162 | switch problem.res.output
163 | case 'none'
164 | problem.res.NOutput = 1;
165 | case {'x','xdot','xddot','fnl'}
166 | problem.res.NOutput = problem.NDof;
167 | otherwise
168 | error('invalid output')
169 | end
170 |
171 | if ~isfield(problem.res,'ROutput')
172 | if ~isfield(problem.res,'iOutput')
173 | problem.res.iOutput = 1:problem.res.NOutput;
174 | end
175 |
176 | R = zeros(length(problem.res.iOutput),problem.res.NOutput);
177 | for j = 1:length(problem.res.iOutput)
178 | R(j,problem.res.iOutput(j)) = 1;
179 | end
180 |
181 | problem.res.ROutput = R;
182 | elseif size(problem.res.ROutput,2) ~= problem.res.NOutput
183 | error('Wrong size for ROutput')
184 | end
185 |
186 | if ~isfield(problem.res,'wMin')
187 | problem.res.wMin = -Inf;
188 | end
189 | if ~isfield(problem.res,'wMax')
190 | problem.res.wMax = Inf;
191 | end
192 | end
193 |
194 | function States = empty_states(problem)
195 | States.w0 = NaN;
196 | States.wBase = NaN;
197 | States.t = 0;
198 |
199 | States.x = zeros(problem.NDof,1);
200 | States.xdot = zeros(problem.NDof,1);
201 | States.xddot = zeros(problem.NDof,1);
202 |
203 | States.u = zeros(problem.NInput,1);
204 | States.udot = zeros(problem.NInput,1);
205 | States.uddot = zeros(problem.NInput,1);
206 |
207 | function s = default_missing(s,f,d)
208 | for i = 1:length(f)
209 | if ~isfield(s,f{i})
210 | s.(f{i}) = d{i};
211 | end
212 | end
--------------------------------------------------------------------------------
/Generalised/hbm_nonlinear3d.m:
--------------------------------------------------------------------------------
1 | function varargout = hbm_nonlinear3d(command,hbm,problem,w0,Xp,Up)
2 | if hbm.options.bUseStandardHBM
3 | [varargout{1:nargout}] = hbm_nonlinear(command,hbm,problem,w0(1),Xp,Up);
4 | return;
5 | end
6 |
7 | NInput = problem.NInput;
8 | NDof = problem.NDof;
9 |
10 | NFreq = hbm.harm.NFreq;
11 | NComp = hbm.harm.NComp;
12 | NDofTot = NComp*NDof;
13 | NInputTot = NComp*NInput;
14 |
15 | %work out the time domain
16 | X = unpackdof(Xp,NFreq-1,NDof);
17 | U = unpackdof(Up,NFreq-1,NInput);
18 |
19 | %get the time series
20 | States = hbm_states3d(w0,X,U,hbm);
21 |
22 | %push through the nl system
23 | States.f = feval(problem.model,'nl',States,hbm,problem);
24 |
25 | %finally convert into the frequency domain
26 | F = hbm.nonlin.FFT*States.f.';
27 | Fp = packdof(F);
28 |
29 | if ~iscell(command)
30 | command = {command};
31 | end
32 |
33 | varargout = cell(1,length(command));
34 |
35 | ijacobx = hbm.nonlin.hbm.ijacobx;
36 | ijacobu = hbm.nonlin.hbm.ijacobu;
37 |
38 | for o = 1:length(command)
39 | switch command{o}
40 | case 'func'
41 | %all done
42 | varargout{o} = Fp;
43 | case 'derivW' %df_dw
44 | if ~hbm.dependence.w
45 | Dw = repmat({zeros(NDofTot,1)},1,2);
46 | else
47 | dfhbm_dw = hbm_derivatives('nl','w',States,hbm,problem);
48 | if isfield(problem,'derivW')
49 | Dw = feval(problem.derivW,dfhbm_dw,hbm,problem);
50 | else
51 | for n = 1:2
52 | Dw{n} = packdof(hbm.nonlin.FFT*dfhbm_dw{n});
53 | end
54 | end
55 | end
56 | varargout{o} = Dw;
57 | case 'jacobX' %df_dX = Jx*X
58 | if ~hbm.dependence.x
59 | Jx = zeros(NDofTot,NDofTot);
60 | else
61 | States.df_dx = hbm_derivatives('nl','x',States,hbm,problem);
62 |
63 | if isfield(problem,'jacobX')
64 | Jx = feval(problem.jacobX,States,hbm,problem);
65 | else
66 | Jx = sum(hbm.nonlin.hbm.Jx.*States.df_dx(ijacobx,ijacobx,:),3);
67 | end
68 | end
69 | varargout{o} = Jx;
70 | case 'jacobU' %df_dU = Ju*U
71 | if ~hbm.dependence.u
72 | Ju = zeros(NDofTot,NInputTot);
73 | else
74 | States.df_du = hbm_derivatives('nl','u',States,hbm,problem);
75 |
76 | if isfield(problem,'jacobU')
77 | Ju = feval(problem.jacobU,States.df_du,hbm,problem);
78 | else
79 | Ju = sum(hbm.nonlin.hbm.Ju.*States.df_du(ijacobx,ijacobu,:),3);
80 | end
81 | end
82 | varargout{o} = Ju;
83 | case 'jacobXdot' %df_dX = w*Jxdot*X
84 | if ~hbm.dependence.xdot
85 | Jxdot = repmat({zeros(NDofTot,NDofTot)},1,2);
86 | else
87 | States.df_dxdot = hbm_derivatives('nl','xdot',States,hbm,problem);
88 |
89 | if isfield(problem,'jacobXdot')
90 | Jxdot = feval(problem.jacobXdot,States,hbm,problem);
91 | else
92 | for n = 1:2
93 | Jxdot{n} = sum(hbm.nonlin.hbm.Jxdot{n}.*States.df_dxdot(ijacobx,ijacobx,:),3);
94 | end
95 | end
96 | end
97 | varargout{o} = Jxdot;
98 | case 'jacobUdot' %df_dU = w*Judot*U
99 | if ~hbm.dependence.udot
100 | Judot = repmat({zeros(NDofTot,NInputTot)},1,2);
101 | else
102 | States.df_dudot = hbm_derivatives('nl','udot',States,hbm,problem);
103 |
104 | if isfield(problem,'jacobUdot')
105 | Judot = feval(problem.jacobUdot,States,hbm,problem);
106 | else
107 | for n = 1:2
108 | Judot{n} = sum(hbm.nonlin.hbm.Judot{n}.*States.df_dudot(ijacobx,ijacobu,:),3);
109 | end
110 | end
111 | end
112 | varargout{o} = Judot;
113 | case 'jacobXddot' %df_dX = w^2*Jxddot*X
114 | if ~hbm.dependence.xddot
115 | Jxddot = repmat({zeros(NDofTot,NDofTot)},1,3);
116 | else
117 | States.df_dxddot = hbm_derivatives('nl','xddot',States,hbm,problem);
118 |
119 | if isfield(problem,'jacobXdot')
120 | Jxddot = feval(problem.jacobXdot,States,hbm,problem);
121 | else
122 | for n = 1:3
123 | Jxddot{n} = sum(hbm.nonlin.hbm.Jxddot{n}.*States.df_dxddot(ijacobx,ijacobx,:),3);
124 | end
125 | end
126 | end
127 | varargout{o} = Jxddot;
128 | case 'jacobUddot' %df_dU = w^2*Juddot*U
129 | if ~hbm.dependence.uddot
130 | Juddot = repmat({zeros(NDofTot,NInputTot)},1,3);
131 | else
132 | States.df_duddot = hbm_derivatives('nl','uddot',States,hbm,problem);
133 |
134 | if isfield(problem,'jacobUddot')
135 | Juddot = feval(problem.jacobUddot,States,hbm,problem);
136 | else
137 | for n = 1:3
138 | Juddot{n} = sum(hbm.nonlin.hbm.Juddot{n}.*States.df_duddot(ijacobx,ijacobu,:),3);
139 | end
140 | end
141 | end
142 | varargout{o} = Juddot;
143 | case 'floquet1xdot'
144 | if ~hbm.dependence.xdot
145 | D1 = zeros(NDofTot,NDofTot);
146 | else
147 | States.df_dxdot = hbm_derivatives('nl','xdot',States,hbm,problem);
148 |
149 | if isfield(problem,'floquet1xdot')
150 | D1 = feval(problem.floquet1xdot,States,hbm,problem);
151 | else
152 | D1 = sum(hbm.nonlin.hbm.Jx.*States.df_dxdot(ijacobx,ijacobx,:),3);
153 | end
154 | end
155 | varargout{o} = D1;
156 | case 'floquet1xddot'
157 | if ~hbm.dependence.xddot
158 | D1dd = repmat({zeros(NDofTot,NDofTot)},1,2);
159 | else
160 | States.df_dxddot = hbm_derivatives('nl','xddot',States,hbm,problem);
161 |
162 | if isfield(problem,'floquet1xddot')
163 | D1dd = feval(problem.floquet1xddot,States,hbm,problem);
164 | else
165 | for n = 1:2
166 | D1dd{n} = sum(hbm.nonlin.hbm.Jxdot{n}.*States.df_dxddot(ijacobx,ijacobx,:),3);
167 | end
168 | end
169 | end
170 | varargout{o} = D1dd;
171 | case 'floquet2'
172 | if ~hbm.dependence.xddot
173 | D2 = zeros(NDofTot);
174 | else
175 | States.df_dxddot = hbm_derivatives('nl','xddot',States,hbm,problem);
176 |
177 | if isfield(problem,'floquet2')
178 | D2 = feval(problem.floquet2,States,hbm,problem);
179 | else
180 | D2 = sum(hbm.nonlin.hbm.Jx.*States.df_dxddot(ijacobx,ijacobx,:),3);
181 | end
182 | end
183 | varargout{o} = D2;
184 | end
185 | end
--------------------------------------------------------------------------------
/Functions/hbm_amp_plot.m:
--------------------------------------------------------------------------------
1 | function hbm_amp_plot(command,hbm,problem,results)
2 | persistent fig hSuccess hWarn hErr X A
3 | w0 = problem.w0;
4 |
5 | if hbm.cont.bUpdate
6 | switch command
7 | case 'init'
8 | if ~isempty(fig) && ishandle(fig)
9 | close(fig)
10 | end
11 |
12 | if isempty(results)
13 | X = zeros(hbm.harm.NFreq,problem.NDof);
14 | A = NaN;
15 | else
16 | X = results.X;
17 | A = results.A;
18 | end
19 | [xlin, Alin] = getLinearReponse(hbm,problem,X,w0);
20 |
21 | xlin = mtimesx(xlin,problem.RDofPlot');
22 | Xplot = mtimesx(X,problem.RDofPlot');
23 |
24 | [fig,hSuccess,hWarn,hErr] = createFRF(hbm,problem,Xplot,A,xlin,Alin);
25 |
26 | case {'data','err','warn'}
27 | if ~ishandle(fig(1))
28 | [xlin, Alin] = getLinearReponse(hbm,problem,X(:,:,1),w0);
29 | xlin = mtimesx(xlin,problem.RDofPlot');
30 | Xplot = mtimesx(X,problem.RDofPlot');
31 | [fig,hSuccess,hWarn,hErr] = createFRF(hbm,problem,Xplot,A,xlin,Alin);
32 | end
33 |
34 | X(:,:,end+1) = results.X;
35 | A(:,end+1) = results.A;
36 |
37 | Xplot = mtimesx(X(:,:,end-1:end),problem.RDofPlot');
38 | Xabs = abs(Xplot(:,:,end));
39 | Xph = unwrap(angle(Xplot)); Xph = Xph(:,:,end);
40 | Aplot = A(end);
41 |
42 | if any(strcmpi(command,{'data','warn'}))
43 | update_handles(hSuccess,Xabs,Xph,Aplot,hbm,problem)
44 | %update our progress
45 |
46 | if strcmpi(command,'warn')
47 | %warning, overlay in blue
48 | update_handles(hWarn,Xabs,Xph,Aplot,hbm,problem)
49 | end
50 |
51 | %reset the error points
52 | for i = 1:length(hbm.harm.iHarmPlot)
53 | for j = 1:size(problem.RDofPlot,1)
54 | for k = 1:2
55 | set(hWarn{k}(i,j),'xdata',NaN,'ydata',NaN);
56 | set(hErr{k}(i,j) ,'xdata',NaN,'ydata',NaN);
57 | end
58 | end
59 | end
60 | else
61 | %error
62 | X(:,:,end) = [];
63 | A(:,end) = [];
64 | update_handles(hErr,Xabs,Xph,Aplot,hbm,problem)
65 | end
66 |
67 | drawnow
68 | case 'close'
69 | close(fig)
70 | hSuccess = [];
71 | hWarn = [];
72 | hErr = [];
73 | end
74 | end
75 |
76 | function update_handles(han,Xabs,Xph,A,hbm,problem)
77 | for i = 1:length(hbm.harm.iHarmPlot)
78 | for j = 1:size(problem.RDofPlot,1)
79 | a = [get(han{1}(i,j),'xdata'),A];
80 | mag = [get(han{1}(i,j),'ydata'),permute(Xabs(hbm.harm.iHarmPlot(i),j,:),[1 3 2])];
81 | ph = [get(han{2}(i,j) ,'ydata'),permute(Xph(hbm.harm.iHarmPlot(i),j,:),[1 3 2])];
82 | set(han{1}(i,j),'xdata',a,'ydata',mag);
83 | set(han{2}(i,j) ,'xdata',a,'ydata',ph);
84 | end
85 | end
86 |
87 | function [fMag,hSuccess,hWarn,hErr] = createFRF(hbm,problem,x,A,xlin,Alin)
88 | fMag = figure('Name',['Amp: ' problem.name]);
89 |
90 | for i = 1:length(hbm.harm.iHarmPlot)
91 | for j = 1:size(problem.RDofPlot,1)
92 | tmp = subplot(size(problem.RDofPlot,1),length(hbm.harm.iHarmPlot),(j-1)*length(hbm.harm.iHarmPlot) + i,'Parent',fMag);
93 | Xij = squeeze(xlin(hbm.harm.iHarmPlot(i),j,:));
94 | [tmp2,hLin{1}(i,j),hLin{2}(i,j)] = plotyy(tmp,Alin,abs(Xij),Alin,unwrap(angle(Xij)));
95 | ax{1}(i,j) = tmp2(1); ax{2}(i,j) = tmp2(2);
96 | hold(tmp2(1), 'on');
97 | hold(tmp2(2), 'on');
98 | end
99 | end
100 |
101 | for i = 1:length(hbm.harm.iHarmPlot)
102 | for j = 1:size(problem.RDofPlot,1)
103 | hSuccess{1}(i,j) = plot(ax{1}(i,j),A,abs(squeeze(x(hbm.harm.iHarmPlot(i),j,:))),'g.-');
104 | hSuccess{2}(i,j) = plot(ax{2}(i,j),A,unwrap(angle(squeeze(x(hbm.harm.iHarmPlot(i),j,:)))),'g.-');
105 |
106 | for k = 1:2
107 | hWarn{k}(i,j) = plot(ax{k}(i,j),NaN,NaN,'b.');
108 | hErr{k}(i,j) = plot(ax{k}(i,j),NaN,NaN,'r.');
109 |
110 | %xlim(ax{k}(i,j),wlim(hbm.harm.iHarmPlot(i),:));
111 | set(ax{k}(i,j),'XLimMode','auto')
112 | set(ax{k}(i,j),'YLimMode','auto')
113 | end
114 |
115 | if j==size(problem.RDofPlot,1)
116 | xlabel(ax{1}(i,j),'A (-)')
117 | end
118 | if j==1
119 | title(ax{1}(i,j),harmonicName(hbm,i))
120 | end
121 |
122 | if i == 1
123 | ylabel(ax{1}(i,j),sprintf('|Dof #%d|',j))
124 | end
125 |
126 | if i == length(hbm.harm.iHarmPlot)
127 | ylabel(ax{2}(i,j),sprintf('\\angle Dof #%d',j))
128 | end
129 | end
130 | end
131 |
132 | function s = harmonicName(hbm,i)
133 | k1 = hbm.harm.kHarm(hbm.harm.iHarmPlot(i),1)*hbm.harm.rFreqBase(1);
134 | k2 = hbm.harm.kHarm(hbm.harm.iHarmPlot(i),2)*hbm.harm.rFreqBase(2);
135 | if k1 == 0
136 | s1 = '';
137 | elseif k1 == 1
138 | s1 = '\omega_1';
139 | else
140 | s1 = sprintf('%s\\omega_1',num2frac(k1));
141 | end
142 | if k2 == 0
143 | s2 = '';
144 | elseif k2 == 1
145 | if ~isempty(s1)
146 | s2 = '+\omega_2';
147 | else
148 | s2 = '\omega_2';
149 | end
150 | elseif k2 == -1
151 | s2 = '-\omega_2';
152 | else
153 | if ~isempty(s1)
154 | s2 = sprintf('%+s\\omega_2',num2frac(k2));
155 | else
156 | s2 = sprintf('%s\\omega_2',num2frac(k2));
157 | end
158 | end
159 |
160 | if isempty(s2)
161 | s = s1;
162 | elseif isempty(s1)
163 | s = s2;
164 | else
165 | s = [s1 ' ' s2];
166 | end
167 |
168 | if isempty(s)
169 | s = '0';
170 | end
171 |
172 | function [xlin, Alin] = getLinearReponse(hbm,problem,X,w)
173 | %find the linearised contribution to the stiffness/damping due from the non-linearity
174 | w0 = w*hbm.harm.rFreqRatio + hbm.harm.wFreq0;
175 | wB = w0.*hbm.harm.rFreqBase;
176 | w = hbm.harm.kHarm*wB';
177 |
178 | x0 = X(1,:).';
179 | U = feval(problem.excite,hbm,problem,w0);
180 | u0 = U(1,:).';
181 |
182 | States = hbm_states3d(w0,X,U,hbm);
183 |
184 | Alin = linspace(problem.A0,problem.AEnd,1000);
185 | xlin = zeros(hbm.harm.NFreq,problem.NDof,length(Alin));
186 |
187 | %now loop over all the amplitudes
188 |
189 | for i = 1:length(Alin)
190 | States_i = scale_inputs(States,Alin(i));
191 | States_i.f = feval(problem.model,'nl',States_i,hbm,problem);
192 |
193 | [K_nl, C_nl, M_nl] = hbm_derivatives('nl',{'x','xdot','xddot'},States_i,hbm,problem);
194 | [Ku_nl,Cu_nl,Mu_nl] = hbm_derivatives('nl',{'u','udot','uddot'},States_i,hbm,problem);
195 |
196 | K_nl = mean(K_nl,3); C_nl = mean(C_nl,3); M_nl = mean(M_nl,3);
197 | Ku_nl = mean(Ku_nl,3); Cu_nl = mean(Cu_nl,3); Mu_nl = mean(Mu_nl,3);
198 |
199 | M = problem.M + M_nl;
200 | G = problem.G;
201 | C = problem.C + C_nl;
202 | K = problem.K + K_nl;
203 |
204 | Mu = problem.Mu + Mu_nl;
205 | Cu = problem.Cu + Cu_nl;
206 | Ku = problem.Ku + Ku_nl;
207 |
208 | NFreq = hbm.harm.NFreq;
209 |
210 | for k = 1:NFreq
211 | Fe = (Ku + 1i*w(k)*Cu - w(k)^2*Mu)*Alin(i)*U(k,:).';
212 | H = K + 1i*w(k)*(C + w0(1)*G) - w(k)^2 * M;
213 | xlin(k,:,i) = (H\Fe).';
214 | end
215 | end
216 |
217 | xlin(1,:,:) = xlin(1,:,:) + x0.';
218 |
219 | function States = scale_inputs(States,A)
220 | f = {'u','udot','uddot'};
221 | for i = 1:length(f)
222 | States.(f{i}) = States.(f{i}) * A;
223 | end
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # HarmLAB
2 | This toolbox offers an implementation of the Generalised Harmonic Balance method in MATLAB, supporting up to 2 base frequencies. This library can find periodic and quasi-periodic solutions of non-linear ODEs expressed in the following general form:
3 |
4 | $$M_x \ddot{x} + C_x \dot{x} + K_x x + f_0 + f_{nl}\left({x},\dot{{x}},\ddot{{x}},{u},\dot{{u}},\ddot{{u}},\omega\right)={f}_e (t) = {M}_u\ddot{{u}} + {C}_u\dot{{u}}+{K}_u{u}$$
5 |
6 | with an output given by:
7 |
8 | $$y = g(x, \dot{x}, \ddot{x}, u, \dot{u}, \ddot{u} )$$
9 |
10 | This toolbox has been developed to solve problems in structural dynamics, so much of the notation and terminology used stems from this field. However, it could equally be applied to problems in a variety of other disciplines.
11 |
12 | The input is assumed to be of the following multi-harmonic form:
13 |
14 | $${u} = A \sum_{k} \Re\left({U}_{k} e^{k\omega t}\right)$$
15 |
16 | where the variable $A$ controls the excitation level.
17 |
18 | The output is assumed to contain content at the same frequencies, so can be expressed as:
19 |
20 | $${x} = \sum_{k} \Re\left({X}_{k} e^{k\omega t}\right)$$
21 |
22 | This toolbox supports both Standard HBM with a single base frequency $\omega$ or Generalised HBM with 2 base frequencies which are interpendent:
23 |
24 | $$ \begin{bmatrix}\omega_1 & \omega_2\end{bmatrix} = \begin{bmatrix}\lambda_1 & \lambda_2\end{bmatrix} \Omega $$
25 |
26 | Analytical jacbobians have been implemented throughout to speed up the execution.
27 |
28 | # Problem definition
29 | ## `problem` structure
30 | The `problem` structure defines the system being simulated. This must have the following fields for the linear parts of the system:
31 | - The mass, stiffness and damping matrices `K`, `C`, `M`
32 | - The constant term `F0` (which defaults to `zeros`)
33 | - The matrices for the excitation `Ku`, `Cu`, `Mu`
34 |
35 | The number of DOF and inputs is inferred from the dimensions of the matrices.
36 |
37 | The non-linear parts must be specified via the `model` callback which must have the form:
38 |
39 | ``` MATLAB
40 | function varargout = test_model(part,States,hbm,problem)
41 | switch part
42 | case 'nl'
43 | %f_nl
44 | case 'output'
45 | %y
46 | ...
47 | end
48 | end
49 | ```
50 |
51 | where `part` can be one of:
52 | - `nl` which means this function should return $f_{nl}$
53 | - `nl_x`, `nl_xdot`, `nl_xddot` which means this function should return $\frac{\partial f_{nl}}{\partial x}$ etc
54 | - `nl_u`, `nl_udot`, `nl_uddot` which means this function should return $\frac{\partial f_{nl}}{\partial u}$ etc
55 | - `output` which means this function should return the output $y$
56 |
57 | The excitation must be specified via the `excite` callback which must has the form:
58 |
59 | ``` MATLAB
60 | function U = test_excite(hbm, problem, w0)
61 | ...
62 | end
63 | ```
64 | and returns the $U_k$.
65 |
66 | You can also store other useful information in the `problem` structure which is needed by your model (eg model parameters).
67 |
68 | ### `problem.res` structure
69 | For resonance problems (using `hbm_res` or `hbm_bb`), it is necessary to configure the term to be maximised. This can be of the following form:
70 |
71 | $$ \frac{\|X_k|}{|F_{e,k}|} $$
72 |
73 | The `res` structure should have the following fields to set the numerator and denominator:
74 | - `iHarm` which sets harmonic to use the terms from
75 | - `output` which can be either `x` (or its derivatives),`fnl`, or `none` (to have no output).
76 | - `input` which can be either `u` (or its derivatives), `fe` or `unity` (to have no denominator).
77 | - `NInput` which selects which DOF from `x` to use
78 | - `NOutput` which selects which index from `fe` to use
79 |
80 | ## HBM structure
81 | The other key structure needed to solve problems is the `hbm` structure, which stores information about the harmonics and other options. This has the following fields:
82 | - `harm` contains information about harmonics
83 | - `dependence` contains information about the form of $f_{nl}$
84 | - `cont` contains settings for the continuation algorithm
85 | - `options` contains settings for the continuation algorithm
86 |
87 | ### `harm` structure
88 | This must have the following fields:
89 |
90 | - `rFreqRatio` is the ratio of the base harmonics to the base frequency. ie $\lambda$. For HBM standard problems `rFreqRatio` should be set to 1.0. For GHBM problems this should be vector.
91 | - `NHarm` is the number of harmonics to include for each base frequency. This should have the same length as `rFreqRatio`.
92 | - `Nfft` is the number of FFT points to use for the AFT transformation. This should be set to a power of 2 and have the same length as `rFreqRatio`.
93 | - `iHarmPlot` sets which harmonic to plot on the FRF when using `hbm_frf` or `hbm_bb`. Typically you want to see the first harmonic so this should be set to 1.
94 |
95 | ### `dependence` structure
96 | This should have the following fields:
97 | - `x`, `xdot`, `xddot` if the non-linearity has a dependence on the state and its derivatives
98 | - `u`, `udot`, `uddot` if the non-linearity has an explicit dependence on the state and its derivatives
99 | - `w` if the non-linearity has an explicit frequency dependence
100 |
101 | The value should be set to `1.0` or `True` where there is a dependence, and `0.0` or `False` otherwise
102 | ### `cont` structure
103 | This is used to configure settings of the continuation algorithm. This should have the following fields:
104 | - `method`: this should be one of the following values:
105 | - `predcorr` for a predictor-corrector algorithm (default)
106 | - `none` to simply step in frequency for `hbm_frf` or amplitude for `hbm_amp` and `hbm_bb`
107 | If method is `predcorr`, you can set the following additional settings in the `cont.predcorr` structure:
108 | - `solver` to choose which non-linear solver to use (`fipopt` to use IPOPT or `fsolve` to use the inbuilt solver)
109 | - `step0` which sets the initial step size
110 | - `min_step`/`max_step` which sets the minimum and maximum step size
111 | - `num_iter_increase`/ `num_iter_reduce` which sets the threshold for increasing/decreasing the step size depending on the number of iterations
112 | - `C` and `c` which sets the ratio to increase/decrease stepsize after a successful/unsuccessful step
113 |
114 | ### `options` structure
115 | This sets other more general options and can have the following fields:
116 | - `bUseStandardHBM`: force the solver to use the standard HBM when there is a single base frequency.
117 | - `bVerbose`: toggle whether to suppress output to console
118 | - `bPlot`: toggle whether to suppress plots
119 |
120 | ## Usage
121 | Once `problem` and `hbm` have been defined, you must call the setup code:
122 |
123 | ``` MATLAB
124 | [hbm,problem] = setuphbm(hbm,problem);
125 | ```
126 | You can then call one of the following functions to solve a problem using HBM.
127 |
128 | ### One-off problems
129 | The most simple case is to find a periodic solution for a given excitation. Two different functions are provided to solve such problems, depending on the asumptions made about the base frequency:
130 |
131 | * `hbm_solve` : this assumes the base frequency $\Omega$ is fixed to a known value. This is the simplest and most common case. This can be called as follows:
132 | ``` MATLAB
133 | sol = hbm_solve(hbm,problem,w0,A);
134 | ```
135 | where `sol` contains information about the solution including the components of $x$, $f_{nl}$ and $u$ at each harmonic
136 | * `hbm_res` : this assumes that the base frequency $\Omega$ can vary in order to find the maximum response at resonance. This is configured by the `problem.res` field:
137 | ``` MATLAB
138 | sol = hbm_res(hbm,problem,w0,A,X0);
139 | ```
140 | If you do not have initial guess for `X0`, then this can be omitted or set to `[]`.
141 |
142 | ### Continuation problems
143 | Each of the different types of one-off problem can then be solved over a range of frequencies. If the base frequency is assumed fixed, there are two potential continuation parameters:
144 |
145 | * ```hbm_frf```: The base frequency $\Omega$ is used as the the continuation parameter, keeping the amplitude $A$ fixed. This yields a non-linear frequency response.
146 | ``` MATLAB
147 | sol = hbm_frf(hbm,problem,A,w0,X0,wEnd,XEnd);
148 | ```
149 | * ```hbm_amp```: The base frequency $\Omega$ is kept fixed, and the amplitude $A$ is used as the the continuation parameter.
150 | ``` MATLAB
151 | sol = hbm_frf(hbm,problem,A,w0,X0,wEnd,XEnd);
152 | ```
153 | If the base frequency is allowed to vary, and chosen to satisfy an objective, then there is only one potential continuation parameter:
154 |
155 | * ```hbm_bb```: The excitation amplitude $A$ is the continuation parameter, and the base frequency $\Omega$ is chosen to satisfy the objective. This yields the "backbone" curve.
156 | ``` MATLAB
157 | sol = hbm_bb(bb,problem,A0,w0,X0,Aend,wEnd,XEnd);
158 | ```
159 | If you not have an intial guess for `X0` or `XEnd`, then this can be set to `[]` for any of these functions.
160 |
--------------------------------------------------------------------------------
/Functions/hbm_frf_plot.m:
--------------------------------------------------------------------------------
1 | function hbm_frf_plot(command,hbm,problem,results)
2 | persistent fig hSuccess hWarn hErr X W
3 | if hbm.cont.bUpdate
4 | switch command
5 | case 'init'
6 | if ~isempty(fig) && ishandle(fig)
7 | close(fig)
8 | end
9 |
10 | if isempty(results)
11 | Xi = zeros(hbm.harm.NFreq,problem.NDof);
12 | wi = NaN;
13 | else
14 | Xi = results.X;
15 | wi = results.w;
16 | end
17 | W = getfrequencies(wi,hbm);
18 | X = Xi;
19 |
20 | [xlin, wlin] = getLinearReponse(hbm,problem,Xi,wi,results.A);
21 |
22 | xlin = mtimesx(xlin,problem.RDofPlot');
23 | Xplot = mtimesx(X,problem.RDofPlot');
24 |
25 | [fig,hSuccess,hWarn,hErr] = createFRF(hbm,problem,Xplot,W,xlin,wlin);
26 |
27 | case {'data','err','warn'}
28 | if ~ishandle(fig(1))
29 | [xlin, wlin] = getLinearReponse(hbm,problem,X(:,:,1),W(1),results.A);
30 | xlin = mtimesx(xlin,problem.RDofPlot');
31 | Xplot = mtimesx(X,problem.RDofPlot');
32 | [fig,hSuccess,hWarn,hErr] = createFRF(hbm,problem,Xplot,W,xlin,wlin);
33 | end
34 |
35 | X(:,:,end+1) = results.X;
36 | W(:,end+1) = getfrequencies(results.w,hbm);
37 |
38 | Xplot = mtimesx(X(:,:,end-1:end),problem.RDofPlot');
39 | Xabs = abs(Xplot(:,:,end));
40 | Xph = unwrap(angle(Xplot)); Xph = Xph(:,:,end);
41 | Wfreq = W(:,end);
42 |
43 | if any(strcmpi(command,{'data','warn'}))
44 | update_handles(hSuccess,Xabs,Xph,Wfreq,hbm,problem)
45 | %update our progress
46 |
47 | if strcmpi(command,'warn')
48 | %warning, overlay in blue
49 | update_handles(hWarn,Xabs,Xph,Wfreq,hbm,problem)
50 | end
51 |
52 | %reset the error points
53 | for i = 1:length(hbm.harm.iHarmPlot)
54 | for j = 1:size(problem.RDofPlot,1)
55 | for k = 1:2
56 | set(hWarn{k}(i,j),'xdata',NaN,'ydata',NaN);
57 | set(hErr{k}(i,j) ,'xdata',NaN,'ydata',NaN);
58 | end
59 | end
60 | end
61 | else
62 | %error
63 | X(:,:,end) = [];
64 | W(:,end) = [];
65 | update_handles(hErr,Xabs,Xph,Wfreq,hbm,problem)
66 | end
67 | drawnow
68 | case 'close'
69 | close(fig)
70 | hSuccess = [];
71 | hWarn = [];
72 | hErr = [];
73 | end
74 | end
75 |
76 | function update_handles(han,Xabs,Xph,W,hbm,problem)
77 | for i = 1:length(hbm.harm.iHarmPlot)
78 | for j = 1:size(problem.RDofPlot,1)
79 | w = [get(han{1}(i,j),'xdata'),W(hbm.harm.iHarmPlot(i),:)];
80 | mag = [get(han{1}(i,j),'ydata'),permute(Xabs(hbm.harm.iHarmPlot(i),j,:),[1 3 2])];
81 | ph = [get(han{2}(i,j) ,'ydata'),permute(Xph(hbm.harm.iHarmPlot(i),j,:),[1 3 2])];
82 | ph = unwrap(ph);
83 | set(han{1}(i,j),'xdata',w,'ydata',mag);
84 | set(han{2}(i,j) ,'xdata',w,'ydata',ph);
85 | end
86 | end
87 |
88 | function ws = getfrequencies(w0,hbm)
89 | w = hbm.harm.rFreqBase'.*(hbm.harm.rFreqRatio'*w0 + hbm.harm.wFreq0');
90 | ws = abs(hbm.harm.kHarm(:,1)*w(1,:) + hbm.harm.kHarm(:,2)*w(2,:));
91 | ws(ws == 0) = w0;
92 |
93 | function [fMag,hSuccess,hWarn,hErr] = createFRF(hbm,problem,x,w,xlin,wlin0)
94 | fMag = figure('Name',['FRF: ' problem.name]);
95 |
96 | wlin = getfrequencies(wlin0,hbm);
97 | wlim = getfrequencies([problem.wMin problem.wMax],hbm);
98 |
99 | for i = 1:length(hbm.harm.iHarmPlot)
100 | for j = 1:size(problem.RDofPlot,1)
101 | tmp = subplot(size(problem.RDofPlot,1),length(hbm.harm.iHarmPlot),(j-1)*length(hbm.harm.iHarmPlot) + i,'Parent',fMag);
102 | Xij = squeeze(xlin(hbm.harm.iHarmPlot(i),j,:));
103 | wij = wlin(hbm.harm.iHarmPlot(i),:);
104 | [tmp2,hLin{1}(i,j),hLin{2}(i,j)] = plotyy(tmp,wij,abs(Xij),wij,unwrap(angle(Xij)));
105 | ax{1}(i,j) = tmp2(1); ax{2}(i,j) = tmp2(2);
106 | hold(tmp2(1), 'on');
107 | hold(tmp2(2), 'on');
108 | end
109 | end
110 |
111 | for i = 1:length(hbm.harm.iHarmPlot)
112 | for j = 1:size(problem.RDofPlot,1)
113 | hSuccess{1}(i,j) = plot(ax{1}(i,j),w(hbm.harm.iHarmPlot(i),:),abs(squeeze(x(hbm.harm.iHarmPlot(i),j,:))),'g.-');
114 | hSuccess{2}(i,j) = plot(ax{2}(i,j),w(hbm.harm.iHarmPlot(i),:),unwrap(angle(squeeze(x(hbm.harm.iHarmPlot(i),j,:)))),'m.-');
115 |
116 | for k = 1:2
117 | hWarn{k}(i,j) = plot(ax{k}(i,j),NaN,NaN,'b.');
118 | hErr{k}(i,j) = plot(ax{k}(i,j),NaN,NaN,'r.');
119 |
120 | %xlim(ax{k}(i,j),wlim(hbm.harm.iHarmPlot(i),:));
121 | set(ax{k}(i,j),'XLimMode','auto')
122 | set(ax{k}(i,j),'YLimMode','auto')
123 | end
124 |
125 | if j==size(problem.RDofPlot,1)
126 | xlabel(ax{1}(i,j),'\omega (rads)')
127 | end
128 | if j==1
129 | title(ax{1}(i,j),harmonicName(hbm,i))
130 | end
131 |
132 | if i == 1
133 | ylabel(ax{1}(i,j),sprintf('|Dof #%d|',j))
134 | end
135 |
136 | if i == length(hbm.harm.iHarmPlot)
137 | ylabel(ax{2}(i,j),sprintf('\\angle Dof #%d',j))
138 | end
139 | end
140 | end
141 |
142 | function s = harmonicName(hbm,i)
143 | k1 = hbm.harm.kHarm(hbm.harm.iHarmPlot(i),1)*hbm.harm.rFreqBase(1);
144 | k2 = hbm.harm.kHarm(hbm.harm.iHarmPlot(i),2)*hbm.harm.rFreqBase(2);
145 | if k1 == 0
146 | s1 = '';
147 | elseif k1 == 1
148 | s1 = '\omega_1';
149 | else
150 | s1 = sprintf('%s\\omega_1',num2frac(k1));
151 | end
152 | if k2 == 0
153 | s2 = '';
154 | elseif k2 == 1
155 | if ~isempty(s1)
156 | s2 = '+\omega_2';
157 | else
158 | s2 = '\omega_2';
159 | end
160 | elseif k2 == -1
161 | s2 = '-\omega_2';
162 | else
163 | if ~isempty(s1)
164 | s2 = sprintf('%+s\\omega_2',num2frac(k2));
165 | else
166 | s2 = sprintf('%s\\omega_2',num2frac(k2));
167 | end
168 | end
169 |
170 | if isempty(s2)
171 | s = s1;
172 | elseif isempty(s1)
173 | s = s2;
174 | else
175 | s = [s1 ' ' s2];
176 | end
177 |
178 | if isempty(s)
179 | s = '0';
180 | end
181 |
182 | function [xlin, wlin] = getLinearReponse(hbm,problem,X,w,A)
183 | %find the linearised contribution to the stiffness/damping due from the non-linearity
184 | w0 = w*hbm.harm.rFreqRatio + hbm.harm.wFreq0;
185 |
186 | x0 = X(1,:).';
187 | U = A*feval(problem.excite,hbm,problem,w0);
188 | u0 = U(1,:).';
189 |
190 | %compute a LU table of frequency dependent stiffness etc
191 | wLU = linspace(problem.wMin,problem.wMax,10);
192 | for i = 1:length(wLU)
193 | w0 = wLU(i) * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
194 | States = hbm_states3d(w0,X,U,hbm);
195 | States.f = feval(problem.model,'nl',States,hbm,problem);
196 |
197 | [K_nl, C_nl, M_nl] = hbm_derivatives('nl',{'x','xdot','xddot'},States,hbm,problem);
198 | [Ku_nl,Cu_nl,Mu_nl] = hbm_derivatives('nl',{'u','udot','uddot'},States,hbm,problem);
199 |
200 | %find average stiffness etc over time
201 | K_lu(:,:,i) = mean(K_nl,3); C_lu(:,:,i) = mean(C_nl,3); M_lu(:,:,i) = mean(M_nl,3);
202 | Ku_lu(:,:,i) = mean(Ku_nl,3); Cu_lu(:,:,i) = mean(Cu_nl,3); Mu_lu(:,:,i) = mean(Mu_nl,3);
203 | end
204 |
205 | %now loop over all the frequencies
206 | wlin = linspace(problem.wMin,problem.wMax,1000);
207 | xlin = zeros(hbm.harm.NFreq,problem.NDof,length(wlin));
208 |
209 | NFreq = hbm.harm.NFreq;
210 |
211 | %work out frequencies
212 | w = getfrequencies(wlin,hbm);
213 | for i = 1:length(wlin)
214 | w0 = wlin(i) * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
215 |
216 | %interpolate into LU table
217 | M_nl = interpx(wLU,M_lu,wlin(i));
218 | C_nl = interpx(wLU,C_lu,wlin(i));
219 | K_nl = interpx(wLU,K_lu,wlin(i));
220 |
221 | Mu_nl = interpx(wLU,Mu_lu,wlin(i));
222 | Cu_nl = interpx(wLU,Cu_lu,wlin(i));
223 | Ku_nl = interpx(wLU,Ku_lu,wlin(i));
224 |
225 | M = problem.M + M_nl;
226 | G = problem.G;
227 | C = problem.C + C_nl;
228 | K = problem.K + K_nl;
229 |
230 | Mu = problem.Mu - Mu_nl;
231 | Cu = problem.Cu - Cu_nl;
232 | Ku = problem.Ku - Ku_nl;
233 |
234 | U = A*feval(problem.excite,hbm,problem,w0);
235 |
236 | for k = 1:NFreq
237 | Fe = (Ku + 1i*w(k,i)*Cu - w(k,i)^2*Mu)*U(k,:).';
238 | H = K + 1i*w(k,i)*(C+w0(1)*G) - w(k,i)^2 * M;
239 | xlin(k,:,i) = (H\Fe).';
240 | end
241 | end
242 | xlin(1,:,:) = xlin(1,:,:) + x0.';
--------------------------------------------------------------------------------
/Functions/hbm_amp.m:
--------------------------------------------------------------------------------
1 | function [results,curr] = hbm_amp(hbm,problem,w0,A0,X0,AEnd,XEnd)
2 | problem.type = 'amp';
3 | problem.w0 = w0;
4 |
5 | %first solve @ A0
6 | sol = hbm_solve(hbm,problem,w0,A0,X0);
7 | x0 = packdof(sol.X);
8 | if any(isnan(abs(x0(:))))
9 | error('Failed to solve initial problem')
10 | end
11 | z0 = [x0;A0];
12 |
13 | init.X = sol.X;
14 | init.w = w0;
15 | init.A = A0;
16 |
17 | sol = hbm_solve(hbm,problem,w0,AEnd,XEnd);
18 | xEnd = packdof(sol.X);
19 | if any(isnan(abs(xEnd(:))))
20 | error('Failed to solve final problem')
21 | end
22 | zEnd = [xEnd;AEnd];
23 |
24 | hbm.bIncludeNL = 1;
25 |
26 | if isfield(problem,'xscale')
27 | xscale = [problem.xscale'; repmat(problem.xscale',hbm.harm.NFreq-1,1)*(1+1i)];
28 | problem.Xscale = packdof(xscale)*sqrt(length(xscale));
29 | problem.Ascale = mean([A0 AEnd]);
30 | problem.Fscale = problem.Xscale*0+1;
31 | problem.Zscale = [problem.Xscale; problem.Ascale];
32 | problem.Jscale = (1./problem.Fscale(:))*problem.Zscale(:)';
33 | bUpdateScaling = 0;
34 | else
35 | problem = hbm_scaling(problem,hbm,init);
36 | bUpdateScaling = 1;
37 | end
38 |
39 | AMax = max(AEnd,A0);
40 | AMin = min(AEnd,A0);
41 |
42 | problem.AMax = AMax;
43 | problem.AMin = AMin;
44 | problem.A0 = A0;
45 | problem.AEnd = AEnd;
46 |
47 |
48 | prog.Status = 'success';
49 |
50 | switch hbm.cont.method
51 | case 'none'
52 | hbm_amp_plot('init',hbm,problem,init);
53 | Ascale = mean([A0 AEnd]);
54 |
55 | pred.step = hbm.cont.step0;
56 | direction = sign(AEnd-A0)*Ascale;
57 |
58 | problem.Xscale = 0*problem.Xscale + 1;
59 | problem.Ascale = 1;
60 | problem.Zscale = 0*problem.Zscale + 1;
61 |
62 | z = z0;
63 | J = hbm_amp_jacobian(z,hbm,problem);
64 | t = get_tangent(J);
65 |
66 | corr.step = 0;
67 | corr.it = 0;
68 |
69 | curr = hbm_amp_results(z,t,pred,corr,hbm,problem);
70 | results = curr;
71 | zprev = z0;
72 |
73 | Asol = A0;
74 | zsol = z0;
75 |
76 | while Asol(end) <= AMax && Asol(end) >= AMin
77 | Apred = Asol(end) + pred.step*direction;
78 | if Apred > AMax || Apred < AMin
79 | break;
80 | end
81 |
82 | iPredict = max(length(Asol)-6,1):length(Asol);
83 | if length(iPredict) > 1
84 | zpred = interp1(Asol(iPredict),zsol(:,iPredict)',Apred,'pchip','extrap')';
85 | else
86 | zpred = zsol(:,end);
87 | zpred(end) = Apred;
88 | end
89 |
90 | %now try to solve
91 | xpred = zpred(1:end-1);
92 | Xpred = unpackdof(xpred,hbm.harm.NFreq-1,problem.NDof);
93 | sol = hbm_solve(hbm,problem,w0,Apred,Xpred);
94 | sol.x = packdof(sol.X);
95 |
96 | z = [sol.x; sol.A];
97 | t = z - zprev;
98 | corr.step = norm2(z - zprev);
99 |
100 | curr(end+1) = hbm_amp_results(z,t,pred,corr,hbm,problem);
101 |
102 | if ~any(isnan(curr(end).z))
103 | curr(end).flag = 'Success';
104 | pred.step = min(max(pred.step * hbm.cont.C,hbm.cont.min_step),hbm.cont.max_step);
105 | results(end+1) = curr(end);
106 | Asol(end+1) = results(end).A;
107 | zsol(:,end+1) = results(end).z;
108 | hbm_amp_plot('data',hbm,problem,results(end));
109 | prog.NFail = 0;
110 | if Asol(end) >= AMax || Asol(end) <= AMin
111 | break;
112 | end
113 | zprev = curr(end).z;
114 | else
115 | curr(end).flag = 'Fail';
116 | pred.step = pred.step * hbm.cont.c;
117 | prog.NFail = prog.NFail + 1;
118 | hbm_amp_plot('err',hbm,problem,curr(end));
119 | if prog.NFail > hbm.cont.maxfail
120 | prog.Status = 'Too many failed iterations';
121 | break;
122 | end
123 | end
124 | end
125 |
126 | %add on final point
127 | t = zEnd - zprev;
128 |
129 | pred.step = norm(zEnd - zprev);
130 | corr.step = norm(zEnd - zprev);
131 | corr.it = 0;
132 | curr(end+1) = hbm_amp_results(zEnd,t,pred,corr,hbm,problem);
133 | results(end+1) = curr(end);
134 |
135 | hbm_amp_plot('close',hbm,problem,[]);
136 |
137 | case 'predcorr'
138 | prog.NStep = 1;
139 | prog.NFail = 0;
140 | prog.NIter = 0;
141 |
142 | switch hbm.cont.predcorr.corrector
143 | case 'pseudo'
144 | case 'arclength'
145 | Jstr = [hbm.sparsity 0*x0+1;
146 | 0*x0'+1 1];
147 | switch hbm.cont.predcorr.solver
148 | case 'ipopt'
149 | ipopt_opt.print_level = 0;
150 | ipopt_opt.maxit = hbm.cont.predcorr.maxit;
151 | ipopt_opt.ftol = hbm.cont.ftol;
152 | ipopt_opt.xtol = hbm.cont.xtol;
153 | ipopt_opt.jacob = @hbm_arclength_jacobian;
154 | ipopt_opt.jacobstructure = Jstr;
155 | case 'fsolve'
156 | fsolve_opt = optimoptions('fsolve',...
157 | 'Display','off',...
158 | 'FunctionTolerance',hbm.cont.ftol,...
159 | 'StepTolerance',hbm.cont.xtol,...
160 | 'SpecifyObjectiveGradient',true,...
161 | 'MaxIterations',hbm.cont.predcorr.maxit);
162 | end
163 | end
164 |
165 | Zprev = z0./problem.Zscale;
166 | F = hbm_amp_constraints(Zprev,hbm,problem);
167 | J = hbm_amp_jacobian(Zprev,hbm,problem);
168 | Tprev = get_tangent(J);
169 | Tprev = Tprev * sign(Tprev(end)) * sign(AEnd - A0);
170 |
171 | Zend = zEnd./problem.Zscale;
172 | J = hbm_amp_jacobian(Zend,hbm,problem);
173 | Tend = get_tangent(J);
174 | Tend = Tend * sign(Tend(end)) * sign(AEnd - A0);
175 |
176 | pred.step = hbm.cont.step0;
177 | corr.step = pred.step;
178 | corr.it = 0;
179 |
180 | curr = hbm_amp_results(Zprev,Tprev,pred,corr,hbm,problem);
181 | results = curr;
182 |
183 | hbm_amp_plot('init',hbm,problem,init);
184 | fprintf('STEP PRED CORR STATUS INFO ITER TOT AMP ')
185 | fprintf('Z(%d) ',1:length(z0))
186 | fprintf('\n')
187 |
188 | fprintf('%3d %6.4f %6.4f %s %3s %3d %3d %6.2f ',prog.NStep,pred.step,corr.step,'S','Ini',corr.it,prog.NIter,results(end).A)
189 | fprintf('%+5.2e ',results(end).z)
190 | fprintf('\n')
191 |
192 | zsol = results.z;
193 | tsol = results.t;
194 |
195 | while norm(Zprev - Zend) > hbm.cont.max_step
196 |
197 | %predictor
198 | switch hbm.cont.predcorr.predictor
199 | case 'linear'
200 | Zpred = Zprev + pred.step*Tprev;
201 | case 'quadratic'
202 | if length(results) < 5
203 | Zpred = Zprev + pred.step*Tprev;
204 | else
205 | Zpred = polynomial_predictor(Zsol(:,end-2:end),Tsol(:,end-2:end),pred.step);
206 | end
207 | case 'cubic'
208 | if length(results) < 5
209 | Zpred = Zprev + pred.step*Tprev;
210 | else
211 | Zpred = polynomial_predictor(Zsol(:,end-3:end),Tsol(:,end-3:end),pred.step);
212 | end
213 | end
214 | corr.it = 0;
215 | Zlast = Zpred + Inf;
216 |
217 | %corrector
218 | switch hbm.cont.predcorr.corrector
219 | case 'pseudo'
220 | bConverged = 0;
221 | Z = Zpred;
222 | T = Tprev;
223 |
224 | while corr.it <= hbm.cont.predcorr.maxit
225 | Zlast = Z;
226 | J = hbm_amp_jacobian(Z,hbm,problem);
227 | F = hbm_amp_constraints(Z,hbm,problem);
228 | if hbm.cont.predcorr.bMoorePenrose
229 | Z = Zlast - J\F;
230 | else
231 | B = [J; T'];
232 | R = [J*T; 0];
233 | Q = [F; 0];
234 | W = T - B\R;
235 | T = normalise(W);
236 | Z = Zlast - B\Q;
237 | end
238 | corr.it = corr.it + 1;
239 | if ~(any(abs(Z - Zlast) > hbm.cont.xtol) || any(abs(F) > hbm.cont.ftol))
240 | bConverged = 1;
241 | break;
242 | end
243 | end
244 | if hbm.cont.predcorr.bMoorePenrose
245 | T = get_tangent(J);
246 | T = sign(T'*Tprev)*T;
247 | end
248 | case 'arclength'
249 | corr.Zprev = Zprev;
250 | corr.Tprev = Tprev;
251 | corr.step = pred.step;
252 | switch hbm.cont.predcorr.solver
253 | case 'ipopt'
254 | [Z,info] = fipopt('',Zpred,@hbm_arclength_constraints,ipopt_opt,hbm,problem,corr);
255 | corr.it = info.iter;
256 | bConverged = info.status == 0;
257 | case 'fsolve'
258 | [Z,F,status,out] = fsolve(@hbm_arclength_constraints,Zpred,fsolve_opt,hbm,problem,corr);
259 | corr.it = out.iterations + 1;
260 | bConverged = status == 1;
261 | end
262 | J = hbm_amp_jacobian(Z,hbm,problem);
263 | T = get_tangent(J);
264 | T = sign(T'*Tprev)*T;
265 | end
266 | corr.step = norm(Z - Zprev)*sign((Z-Zprev)'*Tprev);
267 |
268 | prog.NStep = prog.NStep + 1;
269 | prog.NIter = prog.NIter + corr.it;
270 |
271 | %prepare for plots
272 | curr(end+1) = hbm_amp_results(Z,T,pred,corr,hbm,problem);
273 |
274 | if bConverged && curr(end).sCorr >= hbm.cont.min_step && curr(end).sCorr <= hbm.cont.max_step && curr(end).A > 0
275 | %success
276 | status = 'S';
277 | hbm_amp_plot('data',hbm,problem,curr(end));
278 |
279 | if corr.it <= hbm.cont.num_iter_increase
280 | pred.step = min(curr(end).sPred * hbm.cont.C,hbm.cont.max_step/curr(end).sCorr*curr(end).sPred);
281 | curr(end).flag = 'Success: Increasing step size';
282 | info = 'Inc';
283 | elseif corr.it >= hbm.cont.num_iter_reduce
284 | pred.step = max(curr(end).sPred / hbm.cont.C,hbm.cont.min_step/curr(end).sCorr*curr(end).sPred);
285 | curr(end).flag = 'Success: Reducing step size';
286 | info = 'Red';
287 | else
288 | curr(end).flag = 'Success';
289 | info = '';
290 | end
291 |
292 | %store the data
293 | results(end+1) = curr(end);
294 |
295 | if bUpdateScaling
296 | problem = hbm_scaling(problem,hbm,results(end));
297 | end
298 |
299 | zsol(:,end+1) = results(end).z;
300 | tsol(:,end+1) = results(end).t;
301 | Zsol = zsol./(repmat(problem.Zscale,1,size(zsol,2)));
302 | Tsol = normalise(tsol./(repmat(problem.Zscale,1,size(tsol,2))));
303 |
304 | Zend = zEnd./problem.Zscale;
305 |
306 | Zprev = Zsol(:,end);
307 | Tprev = Tsol(:,end);
308 |
309 | prog.NFail = 0;
310 | else
311 | %failed
312 | hbm_amp_plot('err',hbm,problem,curr(end));
313 | status = 'F';
314 |
315 | if curr(end).A < 0
316 | %gone to negative frequencies (somehow)
317 | curr(end).flag = 'Failed: Negative frequency';
318 | info = 'Neg';
319 | elseif corr.it <= hbm.cont.predcorr.maxit
320 | %converge but step size is unacceptable
321 | if curr(end).sCorr < 0
322 | %backwards
323 | pred.step = max(pred.step * hbm.cont.c,hbm.cont.min_step);
324 | curr(end).flag = 'Failed: Wrong direction';
325 | info = 'Bwd';
326 | elseif curr(end).sCorr > hbm.cont.max_step
327 | %too large
328 | pred.step = max(0.9 * pred.step * hbm.cont.max_step / curr(end).sCorr,hbm.cont.min_step);
329 | curr(end).flag = 'Failed: Step too large';
330 | info = 'Lrg';
331 | elseif curr(end).sCorr < hbm.cont.min_step
332 | %too small
333 | pred.step = min(1.1 * pred.step * hbm.cont.min_step / curr(end).sCorr,hbm.cont.max_step);
334 | curr(end).flag = 'Failed: Step too small';
335 | info = 'Sml';
336 | else
337 | pred.step = max(pred.step * hbm.cont.c,hbm.cont.min_step);
338 | curr(end).flag = 'Failed: Other error';
339 | info = 'Oth';
340 | end
341 | else
342 | %failed to converge
343 | if any(abs(Z - Zlast) > hbm.cont.xtol)
344 | curr(end).flag = 'Failed: No convergence';
345 | info = 'xtl';
346 | elseif any(abs(F) > hbm.cont.ftol)
347 | curr(end).flag = 'Failed: Constraints violated';
348 | info = 'ftl';
349 | else
350 | curr(end).flag = 'Failed';
351 | info = '';
352 | end
353 | pred.step = max(pred.step * hbm.cont.c,hbm.cont.min_step);
354 | end
355 |
356 | prog.NFail = prog.NFail + 1;
357 | if prog.NFail > hbm.cont.maxfail
358 | prog.Status = 'Too many failed iterations';
359 | break
360 | elseif curr(end).A < 0
361 | prog.Status = 'Negative frequency';
362 | break
363 | end
364 | end
365 | fprintf('%3d %6.4f %6.4f %s %3s %3d %3d %6.2f ',prog.NStep,curr(end).sPred,curr(end).sCorr,status,info,corr.it,prog.NIter,curr(end).A)
366 | fprintf('%+5.2e ',curr(end).z)
367 | fprintf('\n')
368 | end
369 |
370 | %add on final point
371 | pred.step = norm(Zend - Zprev);
372 | corr.step = norm(Zend - Zprev);
373 | corr.it = 0;
374 | curr(end+1) = hbm_amp_results(Zend,Tend,pred,corr,hbm,problem);
375 | results(end+1) = curr(end);
376 |
377 | hbm_amp_plot('close',hbm,problem,[]);
378 | end
379 |
380 | NPts = length(results);
381 |
382 | if ~isfield(results,'W')
383 | for i = 1:NPts
384 | w0 = results(i).w*hbm.harm.rFreqRatio + hbm.harm.wFreq0;
385 | results(i).W = hbm.harm.kHarm*(hbm.harm.rFreqBase.*w0)';
386 | end
387 | end
388 |
389 | if ~isfield(results,'L')
390 | results = hbm_floquet(hbm,problem,results);
391 | end
392 |
393 | results = hbm_excitation_forces(problem,results);
394 |
395 | %% Predictor
396 | function X_extrap = polynomial_predictor(Z,dZ,s_extrap)
397 | s = norm2(diff(Z,[],2));
398 | s = cumsum([0 s]);
399 | N = length(s);
400 | if ~isempty(dZ)
401 | [A,Ad] = poly_mat(s,N);
402 | p = [A;Ad]\[Z dZ]';
403 | else
404 | A = poly_mat(s,N);
405 | p = A\Z';
406 | end
407 | B = poly_mat(s(end) + s_extrap,N);
408 | X_extrap = (B*p)';
409 |
410 | function [A,Ad] = poly_mat(s,N)
411 | A = zeros(length(s),N);
412 | Ad = zeros(length(s),N);
413 | for i = 1:N
414 | A(:,i) = s.^(i-1);
415 | if nargout > 1
416 | if i > 1
417 | Ad(:,i) = (i-1)*s.^(i-2);
418 | else
419 | Ad(:,i) = 0*s;
420 | end
421 | end
422 | end
423 |
424 | %% Constraints and Jacobian
425 | function c = hbm_amp_constraints(Z,hbm,problem)
426 | %unpack the inputs
427 | x = Z(1:end-1).*problem.Xscale;
428 | A = Z(end).*problem.Ascale;
429 | w = problem.w0;
430 |
431 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
432 |
433 | U = A*feval(problem.excite,hbm,problem,w0);
434 | u = packdof(U);
435 |
436 | c = hbm_balance3d('func',hbm,problem,w,u,x);
437 | c = c ./ problem.Fscale;
438 |
439 | function J = hbm_amp_jacobian(Z,hbm,problem)
440 | %unpack the inputs
441 | x = Z(1:end-1).*problem.Xscale;
442 | A = Z(end).*problem.Ascale;
443 | w = problem.w0;
444 |
445 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
446 |
447 | U = A*feval(problem.excite,hbm,problem,w0);
448 | u = packdof(U);
449 |
450 | Jx = hbm_balance3d('jacob' ,hbm,problem,w,u,x);
451 | Da = hbm_balance3d('derivA',hbm,problem,w,u,x);
452 |
453 | J = [Jx Da];
454 | J = J .* problem.Jscale;
455 |
456 | %% Arclength files
457 | function [c,J] = hbm_arclength_constraints(Z,hbm,problem,corr)
458 | c = hbm_amp_constraints(Z,hbm,problem);
459 | sgn = sign((Z - corr.Zprev)' * corr.Tprev);
460 | s = norm(Z - corr.Zprev) * sgn;
461 | c(end+1) = s - corr.step;
462 | if nargout > 1
463 | J = hbm_arclength_jacobian(Z,hbm,problem,corr);
464 | end
465 |
466 | function J = hbm_arclength_jacobian(Z,hbm,problem,corr)
467 | J = hbm_amp_jacobian(Z,hbm,problem);
468 | sgn = sign((Z - corr.Zprev)' * corr.Tprev);
469 | J(end+1,:) = sgn*((Z - corr.Zprev)'+eps)/(1*(norm(Z - corr.Zprev)+eps));
470 |
471 | %% Utilities
472 | function y = norm2(x)
473 | y = sqrt(sum(x.^2,1));
474 |
475 | function y = normalise(x)
476 | scale = repmat(norm2(x),size(x,1),1);
477 | y = x ./scale;
478 |
479 | function t = get_tangent(J)
480 | [U,S,V] = svd(J,0);
481 | t = V(:,end);
482 | t = t./norm(t);
483 |
484 | function curr = hbm_amp_results(Z,tangent,pred,corr,hbm,problem)
485 | A = Z(end).*problem.Ascale;
486 | x = Z(1:end-1).*problem.Xscale;
487 | w = problem.w0;
488 | t = normalise(tangent.*problem.Zscale);
489 |
490 | curr.z = [x; A];
491 | curr.t = t;
492 |
493 | curr.sCorr = corr.step;
494 | curr.sPred = pred.step;
495 | curr.it = corr.it;
496 | curr.flag = '';
497 |
498 | w0 = w*hbm.harm.rFreqRatio + hbm.harm.wFreq0;
499 |
500 | curr.w = w;
501 | curr.X = unpackdof(x,hbm.harm.NHarm,problem.NDof);
502 | curr.U = A*feval(problem.excite,hbm,problem,w0);
503 | curr.F = hbm_output3d(hbm,problem,curr.w,curr.U,curr.X);
504 | curr.A = A;
--------------------------------------------------------------------------------
/Functions/hbm_bb.m:
--------------------------------------------------------------------------------
1 | function [results,curr] = hbm_bb(hbm,problem,A0,w0,X0,AEnd,wEnd,XEnd)
2 | problem.type = 'bb';
3 |
4 | %first solve @ A0,w0
5 | sol = hbm_res(hbm,problem,w0,A0,X0);
6 | x0 = packdof(sol.X);
7 | if any(isnan(abs(x0(:))))
8 | error('Failed to solve initial problem')
9 | end
10 | z0 = [x0;w0;A0];
11 |
12 | init.X = sol.X;
13 | init.H = sol.H;
14 | init.w = sol.w;
15 | init.A = A0;
16 |
17 | sol = hbm_res(hbm,problem,w0,AEnd,XEnd);
18 | xEnd = packdof(sol.X);
19 | if any(isnan(abs(xEnd(:))))
20 | error('Failed to solve final problem')
21 | end
22 | zEnd = [xEnd;sol.w;AEnd];
23 |
24 | hbm.bIncludeNL = 1;
25 |
26 | if isfield(problem,'xscale')
27 | xscale = [problem.xscale'; repmat(problem.xscale',hbm.harm.NFreq-1,1)*(1+1i)];
28 | problem.Xscale = packdof(xscale)*sqrt(length(xscale));
29 | problem.wscale = mean([w0 wEnd]);
30 | problem.Ascale = mean([A0 AEnd]);
31 | problem.Fscale = [problem.Xscale*0+1;1];
32 | problem.Zscale = [problem.Xscale; problem.wscale; problem.Ascale];
33 | problem.Jscale = (1./problem.Fscale(:))*problem.Zscale(:)';
34 | bUpdateScaling = 0;
35 | else
36 | problem = hbm_scaling(problem,hbm,init);
37 | bUpdateScaling = 1;
38 | end
39 |
40 | AMax = max(AEnd,A0);
41 | AMin = min(AEnd,A0);
42 |
43 | problem.AMax = AMax;
44 | problem.AMin = AMin;
45 | problem.A0 = A0;
46 | problem.AEnd = AEnd;
47 |
48 |
49 | prog.Status = 'success';
50 |
51 | switch hbm.cont.method
52 | case 'none'
53 | hbm_bb_plot('init',hbm,problem,init);
54 | Ascale = mean([A0 AEnd]);
55 |
56 | pred.step = hbm.cont.step0;
57 | direction = sign(AEnd-A0)*Ascale;
58 |
59 | problem.Xscale = 0*problem.Xscale + 1;
60 | problem.wscale = 1;
61 | problem.Ascale = 1;
62 | problem.Zscale = 0*problem.Zscale + 1;
63 |
64 | z = z0;
65 | J = hbm_bb_jacobian(z,hbm,problem);
66 | t = get_tangent(J);
67 |
68 | corr.step = 0;
69 | corr.it = 0;
70 |
71 | curr = hbm_bb_results(z,t,pred,corr,hbm,problem);
72 | results = curr;
73 | zprev = z0;
74 |
75 | Asol = A0;
76 | zsol = z0;
77 |
78 | while Asol(end) <= AMax && Asol(end) >= AMin
79 | Apred = Asol(end) + pred.step*direction;
80 | if Apred > AMax || Apred < AMin
81 | break;
82 | end
83 |
84 | iPredict = max(length(Asol)-6,1):length(Asol);
85 | if length(iPredict) > 1
86 | zpred = interp1(Asol(iPredict),zsol(:,iPredict)',Apred,'pchip','extrap')';
87 | else
88 | zpred = zsol(:,end);
89 | zpred(end) = Apred;
90 | end
91 |
92 | %now try to solve
93 | xpred = zpred(1:end-2);
94 | wpred = zpred(end-1);
95 | Xpred = unpackdof(xpred,hbm.harm.NFreq-1,problem.NDof);
96 | sol = hbm_res(hbm,problem,wpred,Apred,Xpred);
97 | sol.x = packdof(sol.X);
98 |
99 | z = [sol.x; sol.w; sol.A];
100 | t = z - zprev;
101 | corr.step = norm2(z - zprev);
102 |
103 | curr(end+1) = hbm_bb_results(z,t,pred,corr,hbm,problem);
104 |
105 | if ~any(isnan(curr(end).z))
106 | curr(end).flag = 'Success';
107 | pred.step = min(max(pred.step * hbm.cont.C,hbm.cont.min_step),hbm.cont.max_step);
108 | results(end+1) = curr(end);
109 | Asol(end+1) = results(end).A;
110 | zsol(:,end+1) = results(end).z;
111 | hbm_bb_plot('data',hbm,problem,results(end));
112 | prog.NFail = 0;
113 | if Asol(end) >= AMax || Asol(end) <= AMin
114 | break;
115 | end
116 | zprev = curr(end).z;
117 | else
118 | curr(end).flag = 'Fail';
119 | pred.step = pred.step * hbm.cont.c;
120 | prog.NFail = prog.NFail + 1;
121 | hbm_bb_plot('err',hbm,problem,curr(end));
122 | if prog.NFail > hbm.cont.maxfail
123 | prog.Status = 'Too many failed iterations';
124 | break;
125 | end
126 | end
127 | end
128 |
129 | %add on final point
130 | t = zEnd - zprev;
131 |
132 | pred.step = norm(zEnd - zprev);
133 | corr.step = norm(zEnd - zprev);
134 | corr.it = 0;
135 | curr(end+1) = hbm_bb_results(zEnd,t,pred,corr,hbm,problem);
136 | results(end+1) = curr(end);
137 |
138 | hbm_bb_plot('close',hbm,problem,[]);
139 |
140 | case 'predcorr'
141 | prog.NStep = 1;
142 | prog.NFail = 0;
143 | prog.NIter = 0;
144 |
145 | switch hbm.cont.predcorr.corrector
146 | case 'pseudo'
147 | case 'arclength'
148 | Jstr = [hbm.sparsity 0*x0+1;
149 | 0*x0'+1 1];
150 | switch hbm.cont.predcorr.solver
151 | case 'ipopt'
152 | ipopt_opt.print_level = 0;
153 | ipopt_opt.maxit = hbm.cont.predcorr.maxit;
154 | ipopt_opt.ftol = hbm.cont.ftol;
155 | ipopt_opt.xtol = hbm.cont.xtol;
156 | ipopt_opt.jacob = @hbm_arclength_jacobian;
157 | ipopt_opt.jacobstructure = Jstr;
158 | case 'fsolve'
159 | fsolve_opt = optimoptions('fsolve',...
160 | 'Display','off',...
161 | 'FunctionTolerance',hbm.cont.ftol,...
162 | 'StepTolerance',hbm.cont.xtol,...
163 | 'SpecifyObjectiveGradient',true,...
164 | 'MaxIterations',hbm.cont.predcorr.maxit);
165 | end
166 | end
167 |
168 | Zprev = z0./problem.Zscale;
169 | F = hbm_bb_constraints(Zprev,hbm,problem);
170 | J = hbm_bb_jacobian(Zprev,hbm,problem);
171 | Tprev = get_tangent(J);
172 | Tprev = Tprev * sign(Tprev(end)) * sign(AEnd - A0);
173 |
174 | Zend = zEnd./problem.Zscale;
175 | J = hbm_bb_jacobian(Zend,hbm,problem);
176 | Tend = get_tangent(J);
177 | Tend = Tend * sign(Tend(end)) * sign(AEnd - A0);
178 |
179 | pred.step = hbm.cont.step0;
180 | corr.step = pred.step;
181 | corr.it = 0;
182 |
183 | curr = hbm_bb_results(Zprev,Tprev,pred,corr,hbm,problem);
184 | results = curr;
185 |
186 | hbm_bb_plot('init',hbm,problem,init);
187 | fprintf('STEP PRED CORR STATUS INFO ITER TOT FREQ AMP ')
188 | fprintf('Z(%d) ',1:length(z0))
189 | fprintf('\n')
190 |
191 | fprintf('%3d %6.4f %6.4f %s %3s %3d %3d %6.2f %6.2f ',prog.NStep,pred.step,corr.step,'S','Ini',corr.it,prog.NIter,results(end).w,results(end).A)
192 | fprintf('%+5.2e ',results(end).z)
193 | fprintf('\n')
194 |
195 | zsol = results.z;
196 | tsol = results.t;
197 |
198 | while norm(Zprev - Zend) > hbm.cont.max_step
199 |
200 | %predictor
201 | switch hbm.cont.predcorr.predictor
202 | case 'linear'
203 | Zpred = Zprev + pred.step*Tprev;
204 | case 'quadratic'
205 | if length(results) < 5
206 | Zpred = Zprev + pred.step*Tprev;
207 | else
208 | Zpred = polynomial_predictor(Zsol(:,end-2:end),Tsol(:,end-2:end),pred.step);
209 | end
210 | case 'cubic'
211 | if length(results) < 5
212 | Zpred = Zprev + pred.step*Tprev;
213 | else
214 | Zpred = polynomial_predictor(Zsol(:,end-3:end),Tsol(:,end-3:end),pred.step);
215 | end
216 | end
217 | corr.it = 0;
218 | Zlast = Zpred + Inf;
219 |
220 | %corrector
221 | switch hbm.cont.predcorr.corrector
222 | case 'pseudo'
223 | bConverged = 0;
224 | Z = Zpred;
225 | T = Tprev;
226 |
227 | while corr.it <= hbm.cont.predcorr.maxit
228 | Zlast = Z;
229 | J = hbm_bb_jacobian(Z,hbm,problem);
230 | F = hbm_bb_constraints(Z,hbm,problem);
231 | if hbm.cont.predcorr.bMoorePenrose
232 | Z = Zlast - J\F;
233 | else
234 | B = [J; T'];
235 | R = [J*T; 0];
236 | Q = [F; 0];
237 | W = T - B\R;
238 | T = normalise(W);
239 | Z = Zlast - B\Q;
240 | end
241 | corr.it = corr.it + 1;
242 | if ~(any(abs(Z - Zlast) > hbm.cont.xtol) || any(abs(F) > hbm.cont.ftol))
243 | bConverged = 1;
244 | break;
245 | end
246 | end
247 | if hbm.cont.predcorr.bMoorePenrose
248 | T = get_tangent(J);
249 | T = sign(T'*Tprev)*T;
250 | end
251 | case 'arclength'
252 | corr.Zprev = Zprev;
253 | corr.Tprev = Tprev;
254 | corr.step = pred.step;
255 | switch hbm.cont.predcorr.solver
256 | case 'ipopt'
257 | [Z,info] = fipopt('',Zpred,@hbm_arclength_constraints,ipopt_opt,hbm,problem,corr);
258 | corr.it = info.iter;
259 | bConverged = info.status == 0;
260 | case 'fsolve'
261 | [Z,F,status,out] = fsolve(@hbm_arclength_constraints,Zpred,fsolve_opt,hbm,problem,corr);
262 | corr.it = out.iterations + 1;
263 | bConverged = status == 1;
264 | end
265 | J = hbm_bb_jacobian(Z,hbm,problem);
266 | T = get_tangent(J);
267 | T = sign(T'*Tprev)*T;
268 | end
269 | corr.step = norm(Z - Zprev)*sign((Z-Zprev)'*Tprev);
270 |
271 | prog.NStep = prog.NStep + 1;
272 | prog.NIter = prog.NIter + corr.it;
273 |
274 | %prepare for plots
275 | curr(end+1) = hbm_bb_results(Z,T,pred,corr,hbm,problem);
276 |
277 | if bConverged && curr(end).sCorr >= hbm.cont.min_step && curr(end).sCorr <= hbm.cont.max_step && curr(end).A > 0
278 | %success
279 | status = 'S';
280 | hbm_bb_plot('data',hbm,problem,curr(end));
281 |
282 | if corr.it <= hbm.cont.num_iter_increase
283 | pred.step = min(curr(end).sPred * hbm.cont.C,hbm.cont.max_step/curr(end).sCorr*curr(end).sPred);
284 | curr(end).flag = 'Success: Increasing step size';
285 | info = 'Inc';
286 | elseif corr.it >= hbm.cont.num_iter_reduce
287 | pred.step = max(curr(end).sPred / hbm.cont.C,hbm.cont.min_step/curr(end).sCorr*curr(end).sPred);
288 | curr(end).flag = 'Success: Reducing step size';
289 | info = 'Red';
290 | else
291 | curr(end).flag = 'Success';
292 | info = '';
293 | end
294 |
295 | %store the data
296 | results(end+1) = curr(end);
297 |
298 | if bUpdateScaling
299 | problem = hbm_scaling(problem,hbm,results(end));
300 | end
301 |
302 | zsol(:,end+1) = results(end).z;
303 | tsol(:,end+1) = results(end).t;
304 | Zsol = zsol./(repmat(problem.Zscale,1,size(zsol,2)));
305 | Tsol = normalise(tsol./(repmat(problem.Zscale,1,size(tsol,2))));
306 |
307 | Zend = zEnd./problem.Zscale;
308 |
309 | Zprev = Zsol(:,end);
310 | Tprev = Tsol(:,end);
311 |
312 | prog.NFail = 0;
313 | else
314 | %failed
315 | hbm_bb_plot('err',hbm,problem,curr(end));
316 | status = 'F';
317 |
318 | if curr(end).A < 0
319 | %gone to negative frequencies (somehow)
320 | curr(end).flag = 'Failed: Negative frequency';
321 | info = 'Neg';
322 | elseif corr.it <= hbm.cont.predcorr.maxit
323 | %converge but step size is unacceptable
324 | if curr(end).sCorr < 0
325 | %backwards
326 | pred.step = max(pred.step * hbm.cont.c,hbm.cont.min_step);
327 | curr(end).flag = 'Failed: Wrong direction';
328 | info = 'Bwd';
329 | elseif curr(end).sCorr > hbm.cont.max_step
330 | %too large
331 | pred.step = max(0.9 * pred.step * hbm.cont.max_step / curr(end).sCorr,hbm.cont.min_step);
332 | curr(end).flag = 'Failed: Step too large';
333 | info = 'Lrg';
334 | elseif curr(end).sCorr < hbm.cont.min_step
335 | %too small
336 | pred.step = min(1.1 * pred.step * hbm.cont.min_step / curr(end).sCorr,hbm.cont.max_step);
337 | curr(end).flag = 'Failed: Step too small';
338 | info = 'Sml';
339 | else
340 | pred.step = max(pred.step * hbm.cont.c,hbm.cont.min_step);
341 | curr(end).flag = 'Failed: Other error';
342 | info = 'Oth';
343 | end
344 | else
345 | %failed to converge
346 | if any(abs(Z - Zlast) > hbm.cont.xtol)
347 | curr(end).flag = 'Failed: No convergence';
348 | info = 'xtl';
349 | elseif any(abs(F) > hbm.cont.ftol)
350 | curr(end).flag = 'Failed: Constraints violated';
351 | info = 'ftl';
352 | else
353 | curr(end).flag = 'Failed';
354 | info = '';
355 | end
356 | pred.step = max(pred.step * hbm.cont.c,hbm.cont.min_step);
357 | end
358 |
359 | prog.NFail = prog.NFail + 1;
360 | if prog.NFail > hbm.cont.maxfail
361 | prog.Status = 'Too many failed iterations';
362 | break
363 | elseif curr(end).A < 0
364 | prog.Status = 'Negative frequency';
365 | break
366 | end
367 | end
368 | fprintf('%3d %6.4f %6.4f %s %3s %3d %3d %6.2f %6.2f ',prog.NStep,curr(end).sPred,curr(end).sCorr,status,info,corr.it,prog.NIter,curr(end).w,curr(end).A)
369 | fprintf('%+5.2e ',curr(end).z)
370 | fprintf('\n')
371 | end
372 |
373 | %add on final point
374 | pred.step = norm(Zend - Zprev);
375 | corr.step = norm(Zend - Zprev);
376 | corr.it = 0;
377 | curr(end+1) = hbm_bb_results(Zend,Tend,pred,corr,hbm,problem);
378 | results(end+1) = curr(end);
379 |
380 | hbm_bb_plot('close',hbm,problem,[]);
381 | end
382 |
383 | NPts = length(results);
384 |
385 | if ~isfield(results,'W')
386 | for i = 1:NPts
387 | w0 = results(i).w*hbm.harm.rFreqRatio + hbm.harm.wFreq0;
388 | results(i).W = hbm.harm.kHarm*(hbm.harm.rFreqBase.*w0)';
389 | end
390 | end
391 |
392 | if ~isfield(results,'L')
393 | results = hbm_floquet(hbm,problem,results);
394 | end
395 |
396 | results = hbm_excitation_forces(problem,results);
397 |
398 | %% Predictor
399 | function X_extrap = polynomial_predictor(Z,dZ,s_extrap)
400 | s = norm2(diff(Z,[],2));
401 | s = cumsum([0 s]);
402 | N = length(s);
403 | if ~isempty(dZ)
404 | [A,Ad] = poly_mat(s,N);
405 | p = [A;Ad]\[Z dZ]';
406 | else
407 | A = poly_mat(s,N);
408 | p = A\Z';
409 | end
410 | B = poly_mat(s(end) + s_extrap,N);
411 | X_extrap = (B*p)';
412 |
413 | function [A,Ad] = poly_mat(s,N)
414 | A = zeros(length(s),N);
415 | Ad = zeros(length(s),N);
416 | for i = 1:N
417 | A(:,i) = s.^(i-1);
418 | if nargout > 1
419 | if i > 1
420 | Ad(:,i) = (i-1)*s.^(i-2);
421 | else
422 | Ad(:,i) = 0*s;
423 | end
424 | end
425 | end
426 |
427 | %% Constraints and Jacobian
428 | function c = hbm_bb_constraints(Z,hbm,problem)
429 | %unpack the inputs
430 | x = Z(1:end-2).*problem.Xscale;
431 | w = Z(end-1).*problem.wscale;
432 | A = Z(end).*problem.Ascale;
433 |
434 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
435 |
436 | U = A*feval(problem.excite,hbm,problem,w0);
437 | u = packdof(U);
438 |
439 | c = hbm_balance3d('func',hbm,problem,w,u,x);
440 | c(end+1) = resonance_condition(hbm,problem,w,x,A);
441 | c = c ./ problem.Fscale;
442 |
443 | function J = hbm_bb_jacobian(Z,hbm,problem)
444 |
445 | %unpack the inputs
446 | x = Z(1:end-2).*problem.Xscale;
447 | w = Z(end-1).*problem.wscale;
448 | A = Z(end).*problem.Ascale;
449 |
450 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
451 |
452 | U = A*feval(problem.excite,hbm,problem,w0);
453 | u = packdof(U);
454 |
455 | Jx = hbm_balance3d('jacob' ,hbm,problem,w,u,x);
456 | Dw = hbm_balance3d('derivW',hbm,problem,w,u,x);
457 | Da = hbm_balance3d('derivA',hbm,problem,w,u,x);
458 |
459 | [~,drdx,drdw,drdA] = resonance_condition(hbm,problem,w,x,A);
460 |
461 | J = [Jx Dw Da;
462 | drdx drdw drdA];
463 | J = J .* problem.Jscale;
464 |
465 | %% Arclength files
466 | function [c,J] = hbm_arclength_constraints(Z,hbm,problem,corr)
467 | c = hbm_bb_constraints(Z,hbm,problem);
468 |
469 | sgn = sign((Z - corr.Zprev)' * corr.Tprev);
470 | s = norm(Z - corr.Zprev) * sgn;
471 | c(end+1) = s - corr.step;
472 |
473 | if nargout > 1
474 | J = hbm_arclength_jacobian(Z,hbm,problem,corr);
475 | end
476 |
477 | function J = hbm_arclength_jacobian(Z,hbm,problem,corr)
478 | J = hbm_bb_jacobian(Z,hbm,problem);
479 | sgn = sign((Z - corr.Zprev)' * corr.Tprev);
480 | J(end+1,:) = sgn*((Z - corr.Zprev)'+eps)/(1*(norm(Z - corr.Zprev)+eps));
481 |
482 | %% Utilities
483 | function y = norm2(x)
484 | y = sqrt(sum(x.^2,1));
485 |
486 | function y = normalise(x)
487 | scale = repmat(norm2(x),size(x,1),1);
488 | y = x ./scale;
489 |
490 | function t = get_tangent(J)
491 | [U,S,V] = svd(J,0);
492 | t = V(:,end);
493 | t = t./norm(t);
494 |
495 | function curr = hbm_bb_results(Z,tangent,pred,corr,hbm,problem)
496 | A = Z(end).*problem.Ascale;
497 | w = Z(end-1).*problem.wscale;
498 | x = Z(1:end-2).*problem.Xscale;
499 | t = normalise(tangent.*problem.Zscale);
500 |
501 | curr.z = [x;w;A];
502 | curr.t = t;
503 |
504 | curr.sCorr = corr.step;
505 | curr.sPred = pred.step;
506 | curr.it = corr.it;
507 | curr.flag = '';
508 |
509 | w0 = w*hbm.harm.rFreqRatio + hbm.harm.wFreq0;
510 |
511 | curr.w = w;
512 | curr.X = unpackdof(x,hbm.harm.NHarm,problem.NDof);
513 | curr.U = A*feval(problem.excite,hbm,problem,w0);
514 | curr.F = hbm_output3d(hbm,problem,curr.w,curr.U,curr.X);
515 | curr.A = A;
516 |
517 | u = packdof(curr.U);
518 | curr.H = hbm_objective('complex',hbm,problem,w,x,u);
519 |
520 |
521 | function [r,drdx,drdw,drdA] = resonance_condition(hbm,problem,w0,x,A)
522 | U = A*feval(problem.excite,hbm,problem,w0);
523 | u = packdof(U);
524 |
525 | r = hbm_objective({'derivW'},hbm,problem,w0,x,u);
526 | r = -problem.res.sign*r;
527 |
528 | if nargout > 1
529 | h = 1E-10;
530 | x0 = x;
531 | for i = 1:length(x)
532 | x = x0;
533 | x(i) = x(i) + h;
534 | drdx(i) = (resonance_condition(hbm,problem,w0,x,A) - r)/h;
535 | end
536 |
537 | drdw = (resonance_condition(hbm,problem,w0+h,x0,A) - r)/h;
538 | drdA = (resonance_condition(hbm,problem,w0,x0,A+h) - r)/h;
539 | end
--------------------------------------------------------------------------------
/Functions/hbm_frf.m:
--------------------------------------------------------------------------------
1 | function [results,curr] = hbm_frf(hbm,problem,A,w0,X0,wEnd,XEnd)
2 | problem.type = 'frf';
3 | problem.A = A;
4 |
5 | %first solve @ w0
6 | sol = hbm_solve(hbm,problem,w0,A,X0);
7 | x0 = packdof(sol.X);
8 | u0 = packdof(sol.U);
9 | f0 = packdof(sol.F);
10 | if any(isnan(abs(x0(:))))
11 | results = struct('X',x0,...
12 | 'A',A,...
13 | 'U',u0,...
14 | 'F',f0,...
15 | 'w',w0,...
16 | 'err','Failed to solve initial problem');
17 | return;
18 | end
19 | z0 = [x0;w0];
20 |
21 | init.X = sol.X;
22 | init.w = w0;
23 | init.A = A;
24 |
25 | sol = hbm_solve(hbm,problem,wEnd,A,XEnd);
26 | xEnd = packdof(sol.X);
27 | uEnd = packdof(sol.U);
28 | fEnd = packdof(sol.F);
29 | if any(isnan(abs(xEnd(:))))
30 | results = struct('X',xEnd,...
31 | 'A',A,...
32 | 'U',uEnd,...
33 | 'F',fEnd,...
34 | 'w',wEnd,...
35 | 'err','Failed to solve final problem');
36 | return;
37 | end
38 | zEnd = [xEnd;wEnd];
39 |
40 | hbm.bIncludeNL = 1;
41 |
42 | if isfield(problem,'xscale')
43 | xscale = [problem.xscale'; repmat(problem.xscale',hbm.harm.NFreq-1,1)*(1+1i)];
44 | problem.Xscale = packdof(xscale)*sqrt(length(xscale));
45 | problem.wscale = mean([w0 wEnd]);
46 | problem.Fscale = problem.Xscale*0+1;
47 |
48 | problem.Zscale = [problem.Xscale; problem.wscale];
49 | problem.Jscale = (1./problem.Fscale(:))*problem.Zscale(:)';
50 | bUpdateScaling = 0;
51 | else
52 | problem = hbm_scaling(problem,hbm,init);
53 | bUpdateScaling = 1;
54 | end
55 |
56 | wMax = max(wEnd,w0);
57 | wMin = min(wEnd,w0);
58 |
59 | problem.wMax = wMax;
60 | problem.wMin = wMin;
61 | problem.w0 = w0;
62 | problem.wEnd = wEnd;
63 |
64 | prog.Status = 'success';
65 |
66 | switch hbm.cont.method
67 | case 'none'
68 | if hbm.options.bPlot
69 | hbm_frf_plot('init',hbm,problem,init);
70 | end
71 | if ~isempty(problem.update)
72 | feval(problem.update, 0);
73 | end
74 |
75 | wscale = mean([w0 wEnd]);
76 |
77 | pred.step = hbm.cont.step0;
78 | direction = sign(wEnd-w0)*wscale;
79 |
80 | problem.Xscale = 0*problem.Xscale + 1;
81 | problem.wscale = 1;
82 | problem.Zscale = 0*problem.Zscale + 1;
83 |
84 | z = z0;
85 | J = hbm_frf_jacobian(z,hbm,problem);
86 | t = get_tangent(J);
87 |
88 | corr.step = 0;
89 | corr.it = 0;
90 |
91 | curr = hbm_frf_results(z,t,pred,corr,hbm,problem);
92 | results = curr;
93 | zprev = z0;
94 |
95 | wsol = w0;
96 | zsol = z0;
97 | while wsol(end) >= wMin && wsol(end) <= wMax
98 | wpred = wsol(end) + pred.step*direction;
99 | if wpred > wMax || wpred < wMin
100 | break;
101 | end
102 |
103 | iPredict = max(length(wsol)-6,1):length(wsol);
104 | if length(iPredict) > 1
105 | zpred = interp1(wsol(iPredict),zsol(:,iPredict)',wpred,'pchip','extrap')';
106 | else
107 | zpred = zsol(:,end);
108 | zpred(end) = wpred;
109 | end
110 |
111 | %now try to solve
112 | xpred = zpred(1:end-1);
113 | Xpred = unpackdof(xpred,hbm.harm.NFreq-1,problem.NDof);
114 | sol = hbm_solve(hbm,problem,wpred,A,Xpred);
115 | sol.x = packdof(sol.X);
116 |
117 | z = [sol.x; sol.w];
118 | t = z - zprev;
119 | corr.step = norm2(z - zprev);
120 |
121 | curr(end+1) = hbm_frf_results(z,t,pred,corr,hbm,problem);
122 |
123 | %unpack outputs
124 | if ~any(isnan(curr(end).z))
125 | curr(end).flag = 'Success';
126 | pred.step = min(max(pred.step * hbm.cont.C,hbm.cont.min_step),hbm.cont.max_step);
127 | results(end+1) = curr(end);
128 | wsol(end+1) = results(end).w;
129 | zsol(:,end+1) = results(end).z;
130 | if hbm.options.bPlot
131 | hbm_frf_plot('data',hbm,problem,results(end));
132 | end
133 |
134 | prog.NFail = 0;
135 | if wsol(end) >= wMax || wsol(end) <= wMin
136 | break;
137 | end
138 | zprev = curr(end).z;
139 |
140 | if ~isempty(problem.update)
141 | feval(problem.update, (results(end).w - w0)/(wEnd-w0));
142 | end
143 | else
144 | curr(end).flag = 'Failed: No convergence';
145 | pred.step = pred.step * hbm.cont.c;
146 | prog.NFail = prog.NFail + 1;
147 | if hbm.options.bPlot
148 | hbm_frf_plot('err',hbm,problem,curr(end));
149 | end
150 | if prog.NFail > hbm.cont.maxfail
151 | prog.Status = 'Too many failed iterations';
152 | break;
153 | end
154 | end
155 | end
156 |
157 | %add on final point
158 | t = zEnd - zprev;
159 |
160 | pred.step = norm(zEnd - zprev);
161 | corr.step = norm(zEnd - zprev);
162 | corr.it = 0;
163 | curr(end+1) = hbm_frf_results(zEnd,t,pred,corr,hbm,problem);
164 | results(end+1) = curr(end);
165 |
166 | if hbm.options.bPlot
167 | hbm_frf_plot('close',hbm,problem,[]);
168 | end
169 | if ~isempty(problem.update)
170 | feval(problem.update, 1);
171 | end
172 |
173 | case 'predcorr'
174 | prog.NStep = 1;
175 | prog.NFail = 0;
176 | prog.NIter = 0;
177 |
178 | switch hbm.cont.predcorr.corrector
179 | case 'pseudo'
180 | case 'arclength'
181 | Jstr = [hbm.sparsity 0*x0+1;
182 | 0*x0'+1 1];
183 | switch hbm.cont.predcorr.solver
184 | case 'ipopt'
185 | ipopt_opt.print_level = 0;
186 | ipopt_opt.maxit = hbm.cont.predcorr.maxit;
187 | ipopt_opt.ftol = hbm.cont.ftol;
188 | ipopt_opt.xtol = hbm.cont.xtol;
189 | ipopt_opt.jacob = @hbm_arclength_jacobian;
190 | ipopt_opt.jacobstructure = Jstr;
191 | case 'fsolve'
192 | fsolve_opt = optimoptions('fsolve',...
193 | 'Display','off',...
194 | 'FunctionTolerance',hbm.cont.ftol,...
195 | 'StepTolerance',hbm.cont.xtol,...
196 | 'SpecifyObjectiveGradient',true,...
197 | 'MaxIterations',hbm.cont.predcorr.maxit);
198 | end
199 | end
200 |
201 | Zprev = z0./problem.Zscale;
202 | F = hbm_frf_constraints(Zprev,hbm,problem);
203 | J = hbm_frf_jacobian(Zprev,hbm,problem);
204 | Tprev = get_tangent(J);
205 | Tprev = Tprev * sign(Tprev(end)) * sign(wEnd - w0);
206 |
207 | Zend = zEnd./problem.Zscale;
208 | J = hbm_frf_jacobian(Zend,hbm,problem);
209 | Tend = get_tangent(J);
210 | Tend = Tend * sign(Tend(end)) * sign(wEnd - w0);
211 |
212 | pred.step = hbm.cont.step0;
213 | corr.step = pred.step;
214 | corr.it = 0;
215 |
216 | curr = hbm_frf_results(Zprev,Tprev,pred,corr,hbm,problem);
217 | results = curr;
218 |
219 | if hbm.options.bPlot
220 | hbm_frf_plot('init',hbm,problem,init);
221 | end
222 | if ~isempty(problem.update)
223 | feval(problem.update, 0);
224 | end
225 |
226 | if hbm.options.bVerbose
227 | fprintf('STEP PRED CORR STATUS INFO ITER TOT FREQ ')
228 | fprintf('Z(%d) ',1:length(z0))
229 | fprintf('\n')
230 | fprintf('%3d %6.4f %6.4f %s %3s %3d %3d %6.2f ',prog.NStep,pred.step,corr.step,'S','Ini',corr.it,prog.NIter,results(end).w)
231 | fprintf('%+5.2e ',results(end).z)
232 | fprintf('\n')
233 | end
234 |
235 | zsol = results.z;
236 | tsol = results.t;
237 |
238 | while norm(Zprev - Zend) > hbm.cont.max_step
239 |
240 | %predictor
241 | switch hbm.cont.predcorr.predictor
242 | case 'linear'
243 | Zpred = Zprev + pred.step*Tprev;
244 | case 'quadratic'
245 | if length(results) < 5
246 | Zpred = Zprev + pred.step*Tprev;
247 | else
248 | Zpred = polynomial_predictor(Zsol(:,end-2:end),Tsol(:,end-2:end),pred.step);
249 | end
250 | case 'cubic'
251 | if length(results) < 5
252 | Zpred = Zprev + pred.step*Tprev;
253 | else
254 | Zpred = polynomial_predictor(Zsol(:,end-3:end),Tsol(:,end-3:end),pred.step);
255 | end
256 | end
257 | corr.it = 0;
258 | Zlast = Zpred + Inf;
259 |
260 | %corrector
261 | switch hbm.cont.predcorr.corrector
262 | case 'pseudo'
263 | bConverged = 0;
264 | Z = Zpred;
265 | T = Tprev;
266 |
267 | while corr.it <= hbm.cont.predcorr.maxit
268 | Zlast = Z;
269 | J = hbm_frf_jacobian(Z,hbm,problem);
270 | F = hbm_frf_constraints(Z,hbm,problem);
271 | if hbm.cont.predcorr.bMoorePenrose
272 | Z = Zlast - J\F;
273 | else
274 | B = [J; T'];
275 | R = [J*T; 0];
276 | Q = [F; 0];
277 | W = T - B\R;
278 | T = normalise(W);
279 | Z = Zlast - B\Q;
280 | end
281 | corr.it = corr.it + 1;
282 | if ~(any(abs(Z - Zlast) > hbm.cont.xtol) || any(abs(F) > hbm.cont.ftol))
283 | bConverged = 1;
284 | break;
285 | end
286 | end
287 | if hbm.cont.predcorr.bMoorePenrose
288 | T = get_tangent(J);
289 | T = sign(T'*Tprev)*T;
290 | end
291 | case 'arclength'
292 | corr.Zprev = Zprev;
293 | corr.Tprev = Tprev;
294 | corr.step = pred.step;
295 | switch hbm.cont.predcorr.solver
296 | case 'ipopt'
297 | [Z,info] = fipopt('',Zpred,@hbm_arclength_constraints,ipopt_opt,hbm,problem,corr);
298 | corr.it = info.iter;
299 | bConverged = info.status == 0;
300 | case 'fsolve'
301 | [Z,F,status,out] = fsolve(@hbm_arclength_constraints,Zpred,fsolve_opt,hbm,problem,corr);
302 | corr.it = out.iterations + 1;
303 | bConverged = status == 1;
304 | end
305 | J = hbm_frf_jacobian(Z,hbm,problem);
306 | T = get_tangent(J);
307 | T = sign(T'*Tprev)*T;
308 | end
309 | corr.step = norm(Z - Zprev)*sign((Z-Zprev)'*Tprev);
310 |
311 | prog.NStep = prog.NStep + 1;
312 | prog.NIter = prog.NIter + corr.it;
313 |
314 | %prepare for plots
315 | curr(end+1) = hbm_frf_results(Z,T,pred,corr,hbm,problem);
316 |
317 | if bConverged && curr(end).sCorr >= hbm.cont.min_step && curr(end).sCorr <= hbm.cont.max_step && curr(end).w > 0
318 | %success
319 | status = 'S';
320 | if hbm.options.bPlot
321 | hbm_frf_plot('data',hbm,problem,curr(end));
322 | end
323 |
324 | if corr.it <= hbm.cont.num_iter_increase
325 | pred.step = min(curr(end).sPred * hbm.cont.C,hbm.cont.max_step/curr(end).sCorr*curr(end).sPred);
326 | curr(end).flag = 'Success: Increasing step size';
327 | info = 'Inc';
328 | elseif corr.it >= hbm.cont.num_iter_reduce
329 | pred.step = max(curr(end).sPred / hbm.cont.C,hbm.cont.min_step/curr(end).sCorr*curr(end).sPred);
330 | curr(end).flag = 'Success: Reducing step size';
331 | info = 'Red';
332 | else
333 | curr(end).flag = 'Success';
334 | info = '';
335 | end
336 |
337 | %store the data
338 | results(end+1) = curr(end);
339 |
340 | if bUpdateScaling
341 | problem = hbm_scaling(problem,hbm,results(end));
342 | end
343 |
344 | zsol(:,end+1) = results(end).z;
345 | tsol(:,end+1) = results(end).t;
346 | Zsol = zsol./(repmat(problem.Zscale,1,size(zsol,2)));
347 | Tsol = normalise(tsol./(repmat(problem.Zscale,1,size(tsol,2))));
348 |
349 | Zend = zEnd./problem.Zscale;
350 |
351 | Zprev = Zsol(:,end);
352 | Tprev = Tsol(:,end);
353 |
354 | prog.NFail = 0;
355 |
356 | if ~isempty(problem.update)
357 | feval(problem.update, (results(end).w - w0)/(wEnd-w0));
358 | end
359 | else
360 | %failed
361 | if hbm.options.bPlot
362 | hbm_frf_plot('err',hbm,problem,curr(end));
363 | end
364 | status = 'F';
365 |
366 | if curr(end).w < 0
367 | %gone to negative frequencies (somehow)
368 | curr(end).flag = 'Failed: Negative frequency';
369 | info = 'Neg';
370 | elseif corr.it <= hbm.cont.predcorr.maxit
371 | %converge but step size is unacceptable
372 | if curr(end).sCorr < 0
373 | %backwards
374 | pred.step = max(pred.step * hbm.cont.c,hbm.cont.min_step);
375 | curr(end).flag = 'Failed: Wrong direction';
376 | info = 'Bwd';
377 | elseif curr(end).sCorr > hbm.cont.max_step
378 | %too large
379 | pred.step = max(0.9 * pred.step * hbm.cont.max_step / curr(end).sCorr,hbm.cont.min_step);
380 | curr(end).flag = 'Failed: Step too large';
381 | info = 'Lrg';
382 | elseif curr(end).sCorr < hbm.cont.min_step
383 | %too small
384 | pred.step = min(1.1 * pred.step * hbm.cont.min_step / curr(end).sCorr,hbm.cont.max_step);
385 | curr(end).flag = 'Failed: Step too small';
386 | info = 'Sml';
387 | else
388 | pred.step = max(pred.step * hbm.cont.c,hbm.cont.min_step);
389 | curr(end).flag = 'Failed: Other error';
390 | info = 'Oth';
391 | end
392 | else
393 | %failed to converge
394 | if any(abs(Z - Zlast) > hbm.cont.xtol)
395 | curr(end).flag = 'Failed: No convergence';
396 | info = 'xtl';
397 | elseif any(abs(F) > hbm.cont.ftol)
398 | curr(end).flag = 'Failed: Constraints violated';
399 | info = 'ftl';
400 | else
401 | curr(end).flag = 'Failed';
402 | info = '';
403 | end
404 | pred.step = max(pred.step * hbm.cont.c,hbm.cont.min_step);
405 | end
406 |
407 | prog.NFail = prog.NFail + 1;
408 | if prog.NFail > hbm.cont.maxfail
409 | prog.Status = 'Too many failed iterations';
410 | break
411 | elseif curr(end).w < 0
412 | prog.Status = 'Negative frequency';
413 | break
414 | end
415 | end
416 |
417 | if hbm.options.bVerbose
418 | fprintf('%3d %6.4f %6.4f %s %3s %3d %3d %6.2f ',prog.NStep,curr(end).sPred,curr(end).sCorr,status,info,corr.it,prog.NIter,curr(end).w)
419 | fprintf('%+5.2e ',curr(end).z)
420 | fprintf('\n')
421 | end
422 | end
423 |
424 | %add on final point
425 | pred.step = norm(Zend - Zprev);
426 | corr.step = norm(Zend - Zprev);
427 | corr.it = 0;
428 | curr(end+1) = hbm_frf_results(Zend,Tend,pred,corr,hbm,problem);
429 | results(end+1) = curr(end);
430 |
431 | if hbm.options.bPlot
432 | hbm_frf_plot('close',hbm,problem,[]);
433 | end
434 | if ~isempty(problem.update)
435 | feval(problem.update, 1);
436 | end
437 | end
438 |
439 | NPts = length(results);
440 |
441 | if ~isfield(results,'W')
442 | for i = 1:NPts
443 | w0 = results(i).w*hbm.harm.rFreqRatio + hbm.harm.wFreq0;
444 | results(i).W = hbm.harm.kHarm*(hbm.harm.rFreqBase.*w0)';
445 | end
446 | end
447 |
448 | if ~isfield(results,'L')
449 | results = hbm_floquet(hbm,problem,results);
450 | end
451 |
452 | results = hbm_excitation_forces(problem,results);
453 |
454 | %% Predictor
455 | function X_extrap = polynomial_predictor(Z,dZ,s_extrap)
456 | s = norm2(diff(Z,[],2));
457 | s = cumsum([0 s]);
458 | N = length(s);
459 | if ~isempty(dZ)
460 | [A,Ad] = poly_mat(s,N);
461 | p = [A;Ad]\[Z dZ]';
462 | else
463 | A = poly_mat(s,N);
464 | p = A\Z';
465 | end
466 | B = poly_mat(s(end) + s_extrap,N);
467 | X_extrap = (B*p)';
468 |
469 | function [A,Ad] = poly_mat(s,N)
470 | A = zeros(length(s),N);
471 | Ad = zeros(length(s),N);
472 | for i = 1:N
473 | A(:,i) = s.^(i-1);
474 | if nargout > 1
475 | if i > 1
476 | Ad(:,i) = (i-1)*s.^(i-2);
477 | else
478 | Ad(:,i) = 0*s;
479 | end
480 | end
481 | end
482 |
483 | %% Constraints and Jacobian
484 | function c = hbm_frf_constraints(Z,hbm,problem)
485 | %unpack the inputs
486 | x = Z(1:end-1).*problem.Xscale;
487 | w = Z(end).*problem.wscale;
488 | A = problem.A;
489 |
490 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
491 |
492 | U = A*feval(problem.excite,hbm,problem,w0);
493 | u = packdof(U);
494 |
495 | c = hbm_balance3d('func',hbm,problem,w,u,x);
496 | c = c ./ problem.Fscale;
497 |
498 | function J = hbm_frf_jacobian(Z,hbm,problem)
499 | %unpack the inputs
500 | x = Z(1:end-1).*problem.Xscale;
501 | w = Z(end).*problem.wscale;
502 | A = problem.A;
503 |
504 | w0 = w * hbm.harm.rFreqRatio + hbm.harm.wFreq0;
505 |
506 | U = A*feval(problem.excite,hbm,problem,w0);
507 | u = packdof(U);
508 |
509 | Jx = hbm_balance3d('jacob' ,hbm,problem,w,u,x);
510 | Dw = hbm_balance3d('derivW',hbm,problem,w,u,x);
511 |
512 | J = [Jx Dw];
513 | J = J .* problem.Jscale;
514 |
515 | %% Arclength files
516 | function [c,J] = hbm_arclength_constraints(Z,hbm,problem,corr)
517 | c = hbm_frf_constraints(Z,hbm,problem);
518 | sgn = sign((Z - corr.Zprev)' * corr.Tprev);
519 | s = norm(Z - corr.Zprev) * sgn;
520 | c(end+1) = s - corr.step;
521 | if nargout > 1
522 | J = hbm_arclength_jacobian(Z,hbm,problem,corr);
523 | end
524 |
525 | function J = hbm_arclength_jacobian(Z,hbm,problem,corr)
526 | J = hbm_frf_jacobian(Z,hbm,problem);
527 | sgn = sign((Z - corr.Zprev)' * corr.Tprev);
528 | J(end+1,:) = sgn*((Z - corr.Zprev)'+eps)/(1*(norm(Z - corr.Zprev)+eps));
529 |
530 | %% Utilities
531 | function y = norm2(x)
532 | y = sqrt(sum(x.^2,1));
533 |
534 | function y = normalise(x)
535 | scale = repmat(norm2(x),size(x,1),1);
536 | y = x ./scale;
537 |
538 | function t = get_tangent(J)
539 | [U,S,V] = svd(J,0);
540 | t = V(:,end);
541 | t = t./norm(t);
542 |
543 | function curr = hbm_frf_results(Z,tangent,pred,corr,hbm,problem)
544 | w = Z(end).*problem.wscale;
545 | x = Z(1:end-1).*problem.Xscale;
546 | X = unpackdof(x,hbm.harm.NHarm,problem.NDof);
547 | A = problem.A;
548 | t = normalise(tangent.*problem.Zscale);
549 |
550 | curr.z = [x; w];
551 | curr.t = t;
552 |
553 | curr.sCorr = corr.step;
554 | curr.sPred = pred.step;
555 | curr.it = corr.it;
556 | curr.flag = '';
557 |
558 | w0 = w*hbm.harm.rFreqRatio + hbm.harm.wFreq0;
559 |
560 | curr.w = w;
561 | curr.U = A*feval(problem.excite,hbm,problem,w0);
562 | curr.X = X;
563 | curr.F = hbm_output3d(hbm,problem,curr.w,curr.U,curr.X);
564 | curr.A = A;
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------