# Optimization of film diffusion - Cadet Process

Hello,

As in this old post, I would like to optimise the value for film diffusion.

I am also following this case study.

I have changed the code as follows:

``````from CADETProcess.optimization import OptimizationProblem
optimization_problem = OptimizationProblem('AxialDispersion_filmDiffusion')

name='axial_dispersion', parameter_path='flow_sheet.column.axial_dispersion',
lb=1e-10, ub=0.1,
transform='auto'
)

name='film_diffusion', parameter_path='flow_sheet.column.film_diffusion',
lb=1e-10, ub=0.1,
transform='auto'
)

comparator,
n_objectives=comparator.n_metrics,
requires=[simulator]
)

def callback(simulation_results, individual, evaluation_object, callbacks_dir='./'):
comparator.plot_comparison(
simulation_results,
file_name=f'{callbacks_dir}/{individual.id}_{evaluation_object}_comparison.png',
show=False
)

``````

Here is the error:

``````Cell In[13], line 14
9     name='axial_dispersion', parameter_path='flow_sheet.column.axial_dispersion',
10     lb=1e-10, ub=0.1,
11     transform='auto'
12 )
15     name='film_diffusion', parameter_path='flow_sheet.column.film_diffusion', indices=0,
16     lb=1e-10, ub=0.1,
17     transform='auto'
18 )
24     comparator,
25     n_objectives=comparator.n_metrics,
26     requires=[simulator]
27 )

342 if parameter_path is not None and len(evaluation_objects) == 0:
343     raise ValueError(
344         "Cannot set parameter_path for variable without evaluation object "
345     )
--> 347 var = OptimizationVariable(
348     name, evaluation_objects, parameter_path,
349     lb=lb, ub=ub, transform=transform,
350     indices=indices,
351 )
353 self._variables.append(var)
355 with warnings.catch_warnings():

File ~\anaconda3\Lib\site-packages\CADETProcess\optimization\optimizationProblem.py:2962, in OptimizationVariable.__init__(self, name, evaluation_objects, parameter_path, lb, ub, transform, indices, precision)
2960     self.evaluation_objects = evaluation_objects
2961     self.parameter_path = parameter_path
-> 2962     self.indices = indices
2963 else:
2964     self.evaluation_objects = None

3167     _ = self.indices
3168 except (ValueError, TypeError) as e:
-> 3169     raise e

3165 # Since indices are constructed on `get`, call the property here:
3166 try:
-> 3167     _ = self.indices
3168 except (ValueError, TypeError) as e:
3169     raise e

3124 parameter_shape = self._get_parameter_shape(eval_obj)
3125 if isinstance(parameter_shape, tuple):
-> 3126     eval_ind = generate_indices(parameter_shape, eval_ind)
3127 else:
3128     if not isinstance(eval_ind, list):

661 size = np.prod(shape)
662 if size == 1:
--> 663     raise ValueError("Scalar parameters cannot have index slices.")
665 if indices is None:
666     indices = np.s_[:]

ValueError: Scalar parameters cannot have index slices.
``````

Could you please explain again what I need to do to solve it? The previous answer is unfortunately unclear to me.

Thanks
Stefanos

Hey Stefanos,

the fix weâ€™ve implemented hasnâ€™t made it into a release version yet.

Youâ€™ll have to use this command:

``````pip install git+https://github.com/fau-advanced-separations/CADET-Process.git@master
``````

to install the current state of CADET-Process.

1 Like

Hey Ronald,

I entered the command and it worked.
However, I still get the same error message.

Is there anything else I need to do? Or do you have another idea what the problem could be?

That is surprising, can you send me the entire script you ran? Thank you.

Here is the experimental data:
Experiment1.csv (69.7 KB)

``````from CADETProcess.processModel import ComponentSystem
from CADETProcess.processModel import Inlet, GeneralRateModel, Outlet,TubularReactor, Cstr,LumpedRateModelWithPores
import numpy as np

Q_ml_min = 0.5  # ml/min
Q_m3_s = Q_ml_min/(60*1e6) #m3/s

V_tracer = 5e-8  #m3

component_system = ComponentSystem()

# Unit Operations
inlet = Inlet(component_system, name='inlet')
inlet.flow_rate = Q_m3_s

column = LumpedRateModelWithPores(component_system, 'column')
column.length = 0.1
column.diameter = 0.0077
column.bed_porosity = 0.3
column.particle_porosity = 0.857
column.axial_dispersion= 2e-7
column.film_diffusion = [2.0e-5]

# Tubing1
tubing1 = TubularReactor(component_system, 'tubing1')
tubing1.length = 0.444
tubing1.cross_section_area = 4.418e-7
tubing1.axial_dispersion = 2e-5

# Tubing2
tubing2 = TubularReactor(component_system, 'tubing2')
tubing2.length = 0.444
tubing2.cross_section_area = 4.418e-7
tubing2.axial_dispersion =  tubing1.axial_dispersion

# UV-detector
uv_detector = Cstr(component_system, name='uv_detector')
uv_detector.V = 2e-9
uv_detector.c = [0]

outlet = Outlet(component_system, name='outlet')

# Flow Sheet
flow_sheet = FlowSheet(component_system)

# Process
process = Process(flow_sheet, 'lwe')
process.cycle_time = 1200

_ = process.add_event('Injection', 'flow_sheet.inlet.c', [0.5], 60)

simulation_results = process_simulator.simulate(process)

fig, ax = simulation_results.solution.outlet.outlet.plot()

import numpy as np

time_uv = data_uv[:, 0] * 60
uv = data_uv[:, 1]

reference = ReferenceIO('Experiment', time_uv, uv) #Vergleich Konzentrationen
_ = reference.plot()

comparator = Comparator()

metrics = comparator.evaluate(simulation_results)
print(metrics)
plot= comparator.plot_comparison(simulation_results)

optimization_problem = OptimizationProblem('AxialDispersion_FilmDiffusion')

name='axial_dispersion', parameter_path='flow_sheet.column.axial_dispersion',
lb=1e-10, ub=0.1,
transform='auto'
)

name='film_diffusion',
parameter_path='flow_sheet.column.film_diffusion',
lb=1e-10, ub=1.0,
transform='auto'
)

comparator,
n_objectives=comparator.n_metrics,
requires=[simulator]
)

def callback(simulation_results, individual, evaluation_object, callbacks_dir='./'):
comparator.plot_comparison(
simulation_results,
file_name=f'{callbacks_dir}/{individual.id}_{evaluation_object}_comparison.png',
show=False
)

``````

Thanks a lot!
Have a nice weekend!

Hey Stefanos,

that code runs fine on my machine with the current master-branch of CADET-Process, so I think itâ€™s probably an installation issue.

Are you using python venvâ€™s or conda environmentâ€™s to manage your python packages? If so, can you please double check that you executed the `pip install` command from above in the correct environment? Thanks!

Hey Ronald,

Thanks for checking!
I am using conda.
I already suspected that I was using the wrong environment.
What would be the right environment?

I think the easiest next step would be to create a new conda env, as described here: A Guide to Reproducible Python Environments and CADET Installations.

Then activate the env, run the `pip install git+http...` command from there and run your code in that env.

2 Likes