1function myoutput=pm3_core(myinputs,fpar,nvar,whoiam, ThisMatlab)
2
3% PARALLEL CONTEXT
4% Core functionality for pm3.m function, which can be parallelized.
5
6% INPUTS
7% See the comment in posterior_sampler_core.m funtion.
8
9% OUTPUTS
10% o myoutput  [struc]
11%
12%
13% SPECIAL REQUIREMENTS.
14%   None.
15
16% Copyright (C) 2007-2018 Dynare Team
17%
18% This file is part of Dynare.
19%
20% Dynare is free software: you can redistribute it and/or modify
21% it under the terms of the GNU General Public License as published by
22% the Free Software Foundation, either version 3 of the License, or
23% (at your option) any later version.
24%
25% Dynare is distributed in the hope that it will be useful,
26% but WITHOUT ANY WARRANTY; without even the implied warranty of
27% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28% GNU General Public License for more details.
29%
30% You should have received a copy of the GNU General Public License
31% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
32
33if nargin<4
34    whoiam=0;
35end
36
37% Reshape 'myinputs' for local computation.
38% In order to avoid confusion in the name space, the instruction struct2local(myinputs) is replaced by:
39
40tit1=myinputs.tit1;
41nn=myinputs.nn;
42n2=myinputs.n2;
43Distrib=myinputs.Distrib;
44varlist=myinputs.varlist;
45MaxNumberOfPlotsPerFigure=myinputs.MaxNumberOfPlotsPerFigure;
46name3=myinputs.name3;
47tit3=myinputs.tit3;
48Mean=myinputs.Mean;
49
50if whoiam
51    Parallel=myinputs.Parallel;
52end
53
54
55global options_ M_ oo_
56
57
58if whoiam
59    prct0={0,whoiam,Parallel(ThisMatlab)};
60    h = dyn_waitbar(prct0,'Parallel plots pm3 ...');
61end
62
63
64
65figunumber = 0;
66subplotnum = 0;
67hh = dyn_figure(options_.nodisplay,'Name',[tit1 ' ' int2str(figunumber+1)]);
68RemoteFlag = 0;
69if whoiam
70    if Parallel(ThisMatlab).Local ==0
71        RemoteFlag=1;
72    end
73end
74
75OutputFileName = {};
76
77for i=fpar:nvar
78    if max(abs(Mean(:,i))) > 10^(-6)
79        subplotnum = subplotnum+1;
80        set(0,'CurrentFigure',hh);
81        subplot(nn,nn,subplotnum);
82        if ~(all(all(Distrib(:,:,i)))>0 || ~all(all(Distrib(:,:,i)))<0)
83            plot([1 n2],[0 0],'-r','linewidth',0.5);
84        end
85        hold on
86        for k = 1:9
87            plot(1:n2,squeeze(Distrib(k,:,i)),'-g','linewidth',0.5);
88        end
89        plot(1:n2,Mean(:,i),'-k','linewidth',1);
90        xlim([1 n2]);
91        hold off;
92        name = deblank(varlist(i,:));
93        title(name,'Interpreter','none')
94        yticklabels=get(gca,'yticklabel');
95        if size(char(yticklabels),2)>5 %make sure yticks do not screw up figure
96            yticks=get(gca,'ytick');
97            for ii=1:length(yticks)
98                yticklabels_new{ii,1}=sprintf('%4.3f',yticks(ii));
99            end
100            set(gca,'yticklabel',yticklabels_new)
101        end
102    end
103
104    if whoiam
105        if Parallel(ThisMatlab).Local==0
106            DirectoryName = CheckPath('Output',M_.dname);
107        end
108    end
109
110    if subplotnum == MaxNumberOfPlotsPerFigure || i == nvar
111        dyn_saveas(hh,[M_.dname '/Output/'  M_.fname '_' name3 '_' tit3{i}],options_.nodisplay,options_.graph_format);
112        if RemoteFlag==1
113            OutputFileName = [OutputFileName; {[M_.dname, filesep, 'Output',filesep], [M_.fname '_' name3 '_' deblank(tit3(i,:)) '.*']}];
114        end
115        subplotnum = 0;
116        figunumber = figunumber+1;
117        if (i ~= nvar)
118            hh = dyn_figure(options_.nodisplay,'Name',[name3 ' ' int2str(figunumber+1)]);
119        end
120    end
121
122    if whoiam
123        %         waitbarString = [ 'Variable ' int2str(i) '/' int2str(nvar) ' done.'];
124        %         fMessageStatus((i-fpar+1)/(nvar-fpar+1),whoiam,waitbarString, waitbarTitle, Parallel(ThisMatlab));
125        dyn_waitbar((i-fpar+1)/(nvar-fpar+1),h);
126    end
127
128
129end
130
131if whoiam
132    dyn_waitbar_close(h);
133end
134myoutput.OutputFileName=OutputFileName;
135